fix: preserve nullability for non-empty scalar subqueries - #24534
Open
fornwall wants to merge 2 commits into
Open
fix: preserve nullability for non-empty scalar subqueries#24534fornwall wants to merge 2 commits into
fornwall wants to merge 2 commits into
Conversation
Signed-off-by: Fredrik Fornwall <fredrik@fornwall.net>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #24534 +/- ##
========================================
Coverage 81.31% 81.31%
========================================
Files 1117 1117
Lines 395911 396437 +526
Branches 395911 396437 +526
========================================
+ Hits 321918 322373 +455
- Misses 55177 55200 +23
- Partials 18816 18864 +48 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Which issue does this PR close?
Rationale for this change
#24516 conservatively made every scalar subquery nullable because an empty result evaluates to
NULL. Plans guaranteed to return at least one row, such as ungrouped aggregates, cannot produce that empty-resultNULL; marking them nullable loses schema precision and prevents valid expression simplification.What changes are included in this PR?
LogicalPlan::min_rows()lower bound.Expr::nullableinstead of hardcodingnullable = true, so the logical rule is the single source of truth.max_rows()account for the one-row form ofEmptyRelation, keeping both bounds consistent.Are these changes tested?
Yes. Unit tests cover row bounds, logical schema derivation, optimizer simplification, and physical expression nullability for both possibly empty and guaranteed non-empty scalar subqueries. A sqllogictest pins the user-visible effect:
EXPLAINshows that(SELECT count(*) FROM empty_table) IS NULLnow folds tofalse, eliminating the subquery from the plan. (Query results alone cannot show the change: a guaranteed non-empty scalar subquery never evaluates toNULLat runtime.)As an ablation check, restoring the conservative always-nullable behavior makes the logical, optimizer, physical, and sqllogictest tests fail.
The required all-feature Clippy check and extended workspace test suite pass.
Are there any user-facing changes?
Yes. Scalar subqueries proven to return at least one row now preserve the nullability of their projected field, allowing valid simplification. Potentially empty scalar subqueries remain nullable.
LogicalPlan::min_rows()is a new public API.AI usage: Implemented with Codex; reviewed and test coverage extended with Claude Code. I reviewed the code and made modifications where appropriate.