Skip to content

fix: prevent panic and incorrect results for COUNT with ORDER BY - #24997

Draft
geoffreyclaude wants to merge 1 commit into
apache:mainfrom
geoffreyclaude:fix/count-order-by
Draft

fix: prevent panic and incorrect results for COUNT with ORDER BY#24997
geoffreyclaude wants to merge 1 commit into
apache:mainfrom
geoffreyclaude:fix/count-order-by

Conversation

@geoffreyclaude

@geoffreyclaude geoffreyclaude commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

None; this is a minor bug fix.

Rationale for this change

COUNT(... ORDER BY ...) can panic when used with GROUP BY, or return a count that is too small when an ordering column contains nulls.

SELECT COUNT(a + 0 ORDER BY b)
FROM (VALUES (1, NULL), (2, 20)) AS t(a, b);

This query should return 2, because both values being counted are non-null. Previously, it returned 1: the null in ordering column b incorrectly excluded a row from the count. Grouping a single-argument count with ORDER BY could instead trigger an assertion failure.

COUNT does 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 Count as order-insensitive so that ordering keys are excluded from accumulator inputs and no sorting is required solely for the aggregate's ORDER BY.

What is the testing strategy for this PR?

Regression tests in aggregate.slt cover 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 uses a + 0 to prevent the count from being answered from column statistics.

An EXPLAIN assertion 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.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Sep 6, 2026
@geoffreyclaude geoffreyclaude changed the title fix: mark Count aggregate function as order-insensitive fix: prevent panic and incorrect results for COUNT with ORDER BY Sep 6, 2026
@codecov-commenter

codecov-commenter commented Sep 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.67%. Comparing base (262936e) to head (0c5548e).

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.
📢 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

functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants