Skip to content

fix: classify SQL DML/CTAS queries as eager in _sql_lazy_transformation - #244

Merged
ptch314 merged 3 commits into
mainfrom
fix/sql-dml-and-cancel-matching
Dec 2, 2026
Merged

ptch314 merged 3 commits into
mainfrom
fix/sql-dml-and-cancel-matching

Conversation

@ptch314

@ptch314 ptch314 commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #237 (feat/spark-connect-cell-monitoring). Do not merge until #237 lands.

Fixes a pre-existing main bug in ManagedSparkSession._sql_lazy_transformation(req):

Summary

  • ManagedSparkSession._sql_lazy_transformation(req) previously checked "select" in query.strip().lower().split(), which falsely classified INSERT INTO ... SELECT ..., CREATE TABLE ... AS SELECT ..., MERGE ... USING (SELECT ...), FROM ... INSERT INTO/OVERWRITE, UPDATE, DELETE, CACHE, COPY, etc. as lazy and suppressed Spark UI operation links when cell monitoring is inactive or unsupported (or skipped cell monitoring on feat: integrate ManagedSparkSession with Spark Connect cell monitoring #237).
  • Replaced token-bag lookup with lexical scanning from the start of the query (_is_lazy_sql_query + SQL comment/raw-string/quote/paren/FROM-statement helpers), treating leading SELECT, VALUES, TABLE, non-INSERT FROM queries, and WITH ... CTEs (including optional AS and nested parenthesized WITH) whose main statement is a query as lazy while classifying INSERT, CREATE, REPLACE, MERGE, UPDATE, DELETE, CACHE, COPY, FROM ... INSERT INTO/OVERWRITE, etc. as eager. Supports backtick- and double-quoted CTE identifiers.
  • Added unit tests in tests/unit/test_session.py (test_sql_lazy_transformation) and tests/unit/test_cell_monitoring.py verifying cell monitoring runs and legacy operation links emit for DML/CTAS when unmonitored while skipping pure SELECT queries.

Verification

  • .venv/bin/pyink . --check — 55 files would be left unchanged
  • .venv/bin/vermin --no-tips --violations --eval-annotations --target=3.10- google tests setup.py — Minimum required versions: 3.10
  • .venv/bin/python -m pytest tests/unit/ -v --tb=short -n 2 — 792 passed, 17 skipped, 141 subtests passed (17 Windows IDE capture fixture tests skipped on Linux)

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request enhances the SQL query classification logic in ManagedSparkSession to accurately distinguish between lazy queries (such as SELECT, VALUES, TABLE, FROM, or WITH ...) and eager DML/DDL operations. It introduces a robust SQL lexical parser to skip comments, whitespace, and parenthesized blocks, ensuring correct classification. Additionally, exception outcome classification is updated to be case-insensitive and support SPARK_JOB_CANCELLED. The review feedback suggests improving the SQL parser to support double-quoted CTE identifiers in _is_lazy_sql_query and adding corresponding test cases.

Comment thread tests/unit/test_session.py Resolved
@ptch314
ptch314 force-pushed the fix/sql-dml-and-cancel-matching branch from cb2187b to 80cbede Compare November 30, 2026 22:09
@ptch314 ptch314 changed the title fix: monitor SQL DML/CTAS queries and match cancellation case-insensitively fix: classify SQL DML/CTAS queries as eager in _sql_lazy_transformation Nov 30, 2026
@ptch314
ptch314 force-pushed the fix/sql-dml-and-cancel-matching branch from 80cbede to 1dda5bc Compare November 30, 2026 22:21
Comment thread tests/unit/test_session.py Resolved

@tim-u tim-u left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Removing accidental approval

@ptch314
ptch314 force-pushed the fix/sql-dml-and-cancel-matching branch from 5e93c9a to b35e1b7 Compare December 1, 2026 18:08
Base automatically changed from feat/spark-connect-cell-monitoring to main December 1, 2026 23:43
@ptch314
ptch314 force-pushed the fix/sql-dml-and-cancel-matching branch from b35e1b7 to a9302fd Compare December 1, 2026 23:50
@ptch314
ptch314 merged commit b21a663 into main Dec 2, 2026
34 of 35 checks passed
@ptch314
ptch314 deleted the fix/sql-dml-and-cancel-matching branch December 2, 2026 01:55
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