fix: prevent panic and incorrect results for COUNT with ORDER BY - #24997
Draft
geoffreyclaude wants to merge 1 commit into
Draft
fix: prevent panic and incorrect results for COUNT with ORDER BY#24997geoffreyclaude wants to merge 1 commit into
geoffreyclaude wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #24997 +/- ##
==========================================
- Coverage 81.67% 81.67% -0.01%
==========================================
Files 1126 1126
Lines 414842 414845 +3
Branches 414842 414845 +3
==========================================
- Hits 338841 338834 -7
- Misses 56070 56076 +6
- Partials 19931 19935 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
geoffreyclaude
force-pushed
the
fix/count-order-by
branch
from
September 6, 2026 20:13
49809cd to
0c5548e
Compare
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?
None; this is a minor bug fix.
Rationale for this change
COUNT(... ORDER BY ...)can panic when used withGROUP BY, or return a count that is too small when an ordering column contains nulls.This query should return
2, because both values being counted are non-null. Previously, it returned1: the null in ordering columnbincorrectly excluded a row from the count. Grouping a single-argument count withORDER BYcould instead trigger an assertion failure.COUNTdoes not depend on input order. However, it inherited the default order-sensitive behavior, which passed ordering keys to its accumulators as additional arguments and could introduce unnecessary sorting.What changes are included in this PR?
Mark
Countas order-insensitive so that ordering keys are excluded from accumulator inputs and no sorting is required solely for the aggregate'sORDER BY.What is the testing strategy for this PR?
Regression tests in
aggregate.sltcover grouped, multi-argument, and non-grouped counts. The data includes both a null ordering key, which must not affect the count, and a null counted argument, which must still be excluded. The non-grouped test usesa + 0to prevent the count from being answered from column statistics.An
EXPLAINassertion verifies that the count does not require a sort.Are there any user-facing changes?
COUNT(... ORDER BY ...)returns the correct count without panicking. Nulls in counted arguments retain their usual behavior; nulls in ordering keys no longer incorrectly exclude rows.