Fix DatabricksSqlHook crash on empty result with fetch_one handler#69230
Merged
Conversation
…_one handler DBAPI fetchone() returns None on an empty result set, but the hook's _make_common_data_structure only accepted Row objects or lists of them, so run() with a scalar handler crashed on empty results instead of returning None as the common.sql contract and other providers do.
2 tasks
The base implementation passes the handler result through unchanged, and scalar handlers such as fetch_one_handler legitimately return None for empty result sets, so the annotation excluded a value the method already returns. Mypy's override check rejected the Databricks subclass returning None against the narrower supertype annotation.
eladkal
approved these changes
Jul 2, 2026
…esults These hooks copy the base run() implementation, so their returned result lists can also contain None once the base _make_common_data_structure annotation admits it; mypy rejected the narrower list element type.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes a
TypeErrorinDatabricksSqlHook.run()when a scalar handler is used and the query returns no rows.DBAPI
fetchone()returnsNoneon an empty result set, and common.sql'sfetch_one_handleris typed-> tuple | Noneaccordingly (it also returnsNonefor statements wherecursor.descriptionisNone). The baseDbApiHook._make_common_data_structurepassesNonethrough unchanged, andDbApiHook.run()is annotated... | None. The Databricks override, however, only acceptedRoworSequence[Row]and raisedTypeError: Expected Sequence[Row] or Row, but got <class 'NoneType'>.User-visible impact:
DatabricksSqlHook.get_first(...)— which is justrun(handler=fetch_one_handler)— crashed on any query returning no rows, where every non-overridingDbApiHook(postgres, snowflake, ...) returnsNone. With this change the Databricks hook follows the common.sql contract and passesNonethrough.(The odbc hook's override maps falsy results to
[]instead;Noneis deliberately chosen here to preservefetchonesemantics — "no row" and "empty row set" stay distinguishable — and to match the base contract.)Found while validating #39448 against a live Databricks SQL warehouse; verified there that with this change
run(..., handler=fetch_one_handler)on an empty result returnsNoneand serializes cleanly through XCom.related: #39448
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (Fable 5) following the guidelines