Repository navigation
feat: rewrite count(non-nullable column) to count(1) - #25990
wudidapaopao wants to merge 7 commits into
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #25990 +/- ##
==========================================
- Coverage 82.90% 82.89% -0.01%
==========================================
Files 1154 1154
Lines 457656 457781 +125
Branches 457656 457781 +125
==========================================
+ Hits 379404 379490 +86
- Misses 55248 55273 +25
- Partials 23004 23018 +14 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@neilconway Thanks for the suggestion! I split the COUNT(non-nullable column) -> COUNT(1) rewrite out of #25849 into this PR. |
92fe1b3 to
2f16a08
Compare
2f16a08 to
df145e4
Compare
@neilconway gentle ping on this one when you have time. Thanks! |
kosiew
left a comment
There was a problem hiding this comment.
Thanks for this optimization. Rewriting COUNT on non-nullable columns to COUNT(1) is a useful improvement, and placing the logic in COUNT's simplifier is appropriate.
The rewrite must preserve logical NULL semantics, not just schema nullability. This distinction matters for dictionary arrays, where non-null dictionary keys can reference NULL values. The existing accumulators handle these correctly, so the safety check belongs in rewrite eligibility rather than execution.
I'm requesting changes because the current guard can incorrectly rewrite COUNT on a dictionary column and change the result. Please address this correctness issue and add regression coverage. The additional filter, outer-join, and alias-metadata tests are non-blocking suggestions.
d24eccf to
b812f92
Compare
There was a problem hiding this comment.
Thanks for the follow-up changes. The additional eligibility checks and regression coverage address the concerns raised in the previous review. In particular, the dictionary logical-null wrong-result issue is resolved, and the new tests cover the relevant correctness boundaries.
The conservative rewrite policy is appropriate. COUNT(1) is only substituted when non-nullability can be established safely, while encoded logical nulls, join-induced nullability, DISTINCT semantics, aggregate filters, and alias metadata remain protected. This preserves SQL behavior while allowing unnecessary column dependencies to be eliminated.
I found no new issues in the follow-up commits, and all previously actionable comments have been addressed. No further changes are requested from my review.
Which issue does this PR close?
Rationale for this change
COUNT(non_nullable_column)counts every input row, but evaluating it still reads and materializes the column unnecessarily.What changes are included in this PR?
DISTINCTcounts over non-nullable columns toCOUNT(1)during expression simplification while preserving output names and schemas.SingleDistinctToGroupByrewrites working when name preservation adds an alias around the rewritten count.What is the testing strategy for this PR?
Covered by unit, optimizer, column-pruning, and sqllogictest cases.
Are there any user-facing changes?
Query results and output schemas are unchanged. Optimized plans can show
count(1) AS count(column), and scans can prune non-nullable columns used only byCOUNT.