Skip to content

fix(databricks): support cancelling SQL Lab queries on SEA connections - #42900

Open
eschutho wants to merge 1 commit into
masterfrom
databricks-stop-query
Open

fix(databricks): support cancelling SQL Lab queries on SEA connections#42900
eschutho wants to merge 1 commit into
masterfrom
databricks-stop-query

Conversation

@eschutho

@eschutho eschutho commented Aug 7, 2026

Copy link
Copy Markdown
Member

SUMMARY

SQL Lab's "stop query" action has no effect on Databricks connections using the Python Connector driver (databricks-sql-connector), the officially recommended driver for new deployments. The engine spec never implemented get_cancel_query_id/cancel_query, so a stopped query keeps running on the warehouse until it finishes or times out on its own.

FIX

  • Added get_cancel_query_id/cancel_query to DatabricksPythonConnectorEngineSpec, gated on the connection using Databricks' Statement Execution API (SEA) backend (use_sea=True in connection params).
    • The driver's only cancellation primitive is Cursor.cancel(), which needs a full command identifier tied to the session that ran the query. Superset always issues cancellation from a brand-new connection (see sql_lab.cancel_query), so the identifier has to be serializable and reusable elsewhere.
    • On a SEA connection, that identifier is a plain statement id, which the driver can turn back into a valid cancel target on any cursor via CommandId.from_sea_statement_id(...). That's what's captured and replayed here.
    • On the default Thrift backend, the equivalent identifier requires an operation secret that the driver only holds on the live CommandId object of the executing cursor — it's never exposed through cursor.query_id or any other public/documented accessor. Rather than reconstructing that secret from private driver internals, get_cancel_query_id returns None for Thrift connections, so "stop query" fails explicitly instead of silently doing nothing.
  • Added has_query_id_before_execute = False so the cancel id is captured after the statement executes — the driver only populates cursor.active_command_id once a command has actually run.
  • cancel_query lets any error from the fresh cancel cursor propagate instead of swallowing it into a generic False/"could not cancel" result, since silently reporting a failed cancel as if it were handled would be misleading.

NOT CHANGED

  • Databricks Interactive Cluster (Hive-based) connections — already work via has_implicit_cancel (the live cursor is closed to cancel server-side); out of scope here.
  • ODBC (SQL Endpoints) — pyodbc cursors expose .cancel() (SQLCancel), but like the Thrift case above, Superset's cancel flow uses a fresh cursor/connection with no reference to the one running the query, so it's unclear this would actually cancel anything. Left unimplemented pending real-world verification.
  • Legacy databricks-dbapi connector — already flagged in this file as legacy ("Use Python Connector for new deployments"); not worth building cancel support for.
  • Thrift backend (the default) on the Python Connector — see above; this PR only covers SEA-enabled connections. Connections using the default configuration will see no behavior change.

TESTING INSTRUCTIONS

Added unit tests in tests/unit_tests/db_engine_specs/test_databricks.py covering:

  • get_cancel_query_id returns the SEA statement id when use_sea=True
  • get_cancel_query_id returns None when no command has executed yet
  • get_cancel_query_id returns None on the default (non-SEA) backend — this is intended behavior, not a gap
  • cancel_query reconstructs the SEA command id and delegates to the driver's cursor.cancel()
  • cancel_query rejects a malformed cancel id without touching the cursor
  • cancel_query propagates errors from the cancel attempt instead of swallowing them

Ran pytest tests/unit_tests/db_engine_specs/test_databricks.py tests/unit_tests/db_engine_specs/test_base.py (138 passed) and the full tests/unit_tests/db_engine_specs/ suite (1265 passed, 9 pre-existing/unrelated failures in test_bigquery.py and test_mysql.py reproduced identically on master, caused by local environment dependency versions, not this change).

Ran pre-commit (ruff, ruff-format, mypy, pylint, db-engine-spec-metadata) on the changed files — all passed.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

🤖 Generated with Claude Code

Databricks queries run through the Python Connector previously had no
"stop query" support. The connector's only cancellation mechanism is
Cursor.cancel(), which needs a full command identifier from the same
backend session; the default Thrift backend's identifier includes a
secret that's never exposed via any public/documented accessor, so it
can't be reconstructed on the fresh cursor Superset uses to issue
cancellation. The newer, opt-in SEA (Statement Execution API) backend
uses a plain statement id instead, which can be captured and reused
safely, so cancellation is implemented for that case only.

Co-Authored-By: Claude <noreply@anthropic.com>
@dosubot dosubot Bot added data:connect:databricks Databricks DB Experience sqllab Namespace | Anything related to the SQL Lab labels Aug 7, 2026
@bito-code-review

bito-code-review Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #670807

Actionable Suggestions - 0
Filtered by Review Rules

Bito filtered these suggestions based on rules created automatically for your feedback. Manage rules.

  • superset/db_engine_specs/databricks.py - 1
Review Details
  • Files reviewed - 2 · Commit Range: e878fe7..e878fe7
    • superset/db_engine_specs/databricks.py
    • tests/unit_tests/db_engine_specs/test_databricks.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@netlify

netlify Bot commented Aug 7, 2026

Copy link
Copy Markdown

Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit e878fe7
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6a76558dc571e700088aea2a
😎 Deploy Preview https://deploy-preview-42900--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Aug 7, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 31.57895% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.23%. Comparing base (1de35d1) to head (e878fe7).
⚠️ Report is 50 commits behind head on master.

Files with missing lines Patch % Lines
superset/db_engine_specs/databricks.py 31.57% 13 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #42900      +/-   ##
==========================================
- Coverage   65.73%   65.23%   -0.50%     
==========================================
  Files        2843     2842       -1     
  Lines      162672   161006    -1666     
  Branches    37255    36958     -297     
==========================================
- Hits       106935   105036    -1899     
- Misses      53645    53947     +302     
+ Partials     2092     2023      -69     
Flag Coverage Δ
hive 38.24% <31.57%> (+0.28%) ⬆️
mysql 57.78% <31.57%> (-0.02%) ⬇️
postgres 57.82% <31.57%> (-0.02%) ⬇️
presto 40.19% <31.57%> (+0.32%) ⬆️
python 58.05% <31.57%> (-1.16%) ⬇️
sqlite 57.45% <31.57%> (-0.03%) ⬇️
unit ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data:connect:databricks Databricks DB Experience preset-io size/L sqllab Namespace | Anything related to the SQL Lab

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant