Skip to content

fix(statement): distinguish SELECT aliases from DML row counts - #433

Closed
aakash-saravanan-db wants to merge 9 commits into
mainfrom
aakash-saravanan_data/fix-select-alias-rowcount
Closed

aakash-saravanan-db wants to merge 9 commits into
mainfrom
aakash-saravanan_data/fix-select-alias-rowcount

Conversation

@aakash-saravanan-db

@aakash-saravanan-db aakash-saravanan-db commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

SEA returns affected-row counts as result columns such as
num_affected_rows. The kernel previously identified these results from
their column names alone, so an ordinary query such as:

SELECT 7 AS num_affected_rows

was incorrectly exposed as having modified seven rows.

This change also checks the submitted SQL statement before inferring
modified-row metadata. The shared classifier recognizes direct, CTE-prefixed,
and FROM ... INSERT multi-insert DML, as well as COPY INTO, CTAS/RTAS, and
literal EXECUTE IMMEDIATE write statements. It ignores keywords inside
comments, quoted values, identifiers, and nested expressions. Dynamic
EXECUTE IMMEDIATE statements whose SQL is unavailable remain unknown.

ID-only attached statements do not have their submitted SQL, so they do not
infer counts from result-column names. Any count supplied directly in status
metadata remains available. Async result fetches retain the terminal wire
status across successful, failed, and reader-error paths before returning a
result or mapped error.

Validation

  • Full root workspace unit, integration, and doc-test suite passed.
  • Default-feature and Rustls-only all-target Clippy passed with warnings denied.
  • Focused classifier regressions cover direct and CTE-prefixed DML,
    multi-insert, COPY INTO, CTAS/RTAS, literal EXECUTE IMMEDIATE, dynamic SQL,
    and SELECT controls.
  • Focused async fetch-result regressions cover successful, failed, and
    reader-error terminal paths.
  • Rust formatting and whitespace checks passed.
  • Rebuilt Python binding against both tested SQL backends: SELECT 7 AS num_affected_rows
    returns row 7 with rowcount=-1.
  • DBSQL direct, CTE-prefixed, and zero-row DML retain counts 2, 1, and 0.
  • Live DBSQL COPY INTO returns the three-column counter row [2, 2, 0]; the rebuilt Python kernel reports rowcount=2 before and after fetching it.

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 2 Low

Looks good: requiring DML SQL before exposing row counts fixes the SELECT-alias misreport, and every path is covered (sync execute, execute-direct, submit, attached/reader() handles), with wiremock and C ABI tests. Two low-severity edge cases where the new keyword scanner misclassifies real DML.

Comment thread src/statement/dml_count.rs Resolved
Comment thread src/statement/dml_count.rs Resolved
@aakash-saravanan-db
aakash-saravanan-db force-pushed the aakash-saravanan_data/fix-select-alias-rowcount branch from ebd4f26 to f62ad30 Compare November 10, 2026 22:11

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Medium · 1 Low

The fix is sound: SQL-gated inference closes the alias false-positive across sync, async and C ABI, and it has good test coverage. Two concerns: attached handles (used by the Python bindings) lose DML counts, and FROM-first multi-insert DML is no longer counted.

Comment thread src/session.rs Resolved
Comment thread src/statement/dml_count.rs Resolved

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good overall, with one low-severity note: the new SQL check stops CTAS/RTAS and EXECUTE IMMEDIATE DML from reporting affected-row counts. The infer_num_modified_rows setting is passed through the sync, async, execute_direct and attach paths correctly, and the new FetchResultOutcome caches the terminal status on every error path. The new wiremock tests cover all of this well.

Comment thread src/statement/dml_count.rs Resolved
Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Medium

Overall the change looks good, with one medium gap: the new SQL classifier doesn't recognize CTAS/RTAS when the query after AS is in parentheses, so those statements stop reporting row counts. Also worth noting: ID-only attach_async_statement handles (which the PyO3 bindings expose) deliberately no longer infer DML counts from result columns. Python callers who re-attach to DML by ID will now get rowcount=-1 unless the server sends the count in status metadata.

Comment thread src/statement/dml_count.rs Resolved
Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good overall, with one low concern. Gating column-name count inference on the submitted SQL is the right fix, and the classifier is careful about comments, quoting and CTEs. The classifier claims to handle COPY INTO, but the strict all-counter-columns check may still reject COPY INTO's real result, so it may never get a count. Nit: the get_statement_status doc comment in src/client/sea.rs now ends with a stray empty /// line where the removed sentence used to be.

Comment thread src/statement/dml_count.rs Resolved
Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Medium

Looks good overall. One medium concern: handles reattached by statement ID no longer report modified-row counts for DML, and the wrappers can observe that. The SQL classifier, the async terminal-status caching and the new regression tests look correct; I traced the comment, quote, CTE and multi-insert paths and found no incorrect results.

Comment thread src/session.rs Resolved
Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good overall. One low-severity note: attached handles no longer infer DML counts, which is intentional and documented but visible to users. The SQL classifier and the handling of the terminal status in async fetch both look correct, with good test coverage at the unit, wiremock and C ABI levels.

Comment thread src/session.rs Resolved
Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

The fix looks sound, with one low-severity concern. The SQL classifier handles comments, quoted text, CTEs, multi-insert, COPY INTO, CTAS/RTAS and EXECUTE IMMEDIATE, and has thorough unit tests. The async terminal-status caching paths have tests that would fail without the change. The concern is that execute_direct's Completed path still never infers DML counts, unlike the other entry points.

Comment thread src/statement/mutable.rs Resolved

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 2 Low

Looks good, with two low-severity notes. Row-count inference now requires the submitted SQL to be a modifying statement, so a SELECT using counter names like num_affected_rows no longer reports modified rows; the async path now records the terminal status on every fetch outcome, and the tests cover these paths. The notes are a CTAS classifier gap (it stops at the first AS, so forms like STORED AS PARQUET AS SELECT are missed) and a missing changelog entry for the main fix.

Comment thread src/statement/dml_count.rs Resolved
Comment thread NEXT_CHANGELOG.md Resolved
Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good overall, with one low-severity gap: the SQL classifier and the plumbing that passes the inference flag look sound. I checked UTF-8 slicing, nested comments, CTE forms and raw strings, and both terminal-error paths now record the status. The one gap is an existing inconsistency in the execute_direct Completed arm that this PR could close cheaply.

Comment thread src/statement/mutable.rs Resolved

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good overall. The SQL-gated DML-count inference and the new FetchResultOutcome status retention are sound and have tests at the unit and wiremock levels. One low finding: the allow-list classifier now drops counts for statements it doesn't recognize, such as SQL scripting blocks, and the changelog doesn't mention that.

Comment thread src/statement/dml_count.rs Resolved
Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

@vuanhphung

Copy link
Copy Markdown
Collaborator

Will all of this complexity go away if we modify SEA API to expose numModifiedRows in the result manifest? I slightly prefer that tbh to simplify client side

This branch was previously deployed

1 inactive deployment
azure-prod — 1d6faa7f Deployed Nov 11, 2026 by aakash-saravanan-db via followup #2402
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants