Skip to content

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

Merged
gabotechs merged 1 commit into
apache:mainfrom
geoffreyclaude:fix/count-order-by
Sep 11, 2026
Merged

gabotechs merged 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?

Rationale for this change

COUNT does not depend on input order, but it inherited the default AggregateOrderSensitivity::HardRequirement. As detailed in #25055, this could pass ordering keys to count accumulators as additional arguments, causing incorrect results or a panic, and could introduce unnecessary sorting.

What changes are included in this PR?

Override Count::order_sensitivity to return AggregateOrderSensitivity::Insensitive. This makes aggregate ordering keys ineffective for physical planning: they are excluded from accumulator inputs and no SortExec is required solely for COUNT'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.

A bare global COUNT(a ORDER BY b) over the in-memory test table is folded by AggregateStatistics to num_rows - null_count(a), so CountAccumulator never runs. The test uses a + 0, which preserves a's nullness but prevents that fold, ensuring the regression exercises the accumulator.

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.74%. Comparing base (262936e) to head (af53d29).
⚠️ Report is 79 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24997      +/-   ##
==========================================
+ Coverage   81.67%   81.74%   +0.06%     
==========================================
  Files        1126     1128       +2     
  Lines      414842   416648    +1806     
  Branches   414842   416648    +1806     
==========================================
+ Hits       338841   340578    +1737     
+ Misses      56070    56001      -69     
- Partials    19931    20069     +138     

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

@geoffreyclaude
geoffreyclaude marked this pull request as ready for review September 8, 2026 09:22

@gabotechs gabotechs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 good catch!

@gabotechs
gabotechs added this pull request to the merge queue Sep 11, 2026
Merged via the queue into apache:main with commit 224cc56 Sep 11, 2026
38 checks passed
lukekim pushed a commit to spiceai/datafusion that referenced this pull request Sep 27, 2026
…che#24997)

## Which issue does this PR close?

- Closes apache#25055.

## Rationale for this change

`COUNT` does not depend on input order, but it inherited the default
`AggregateOrderSensitivity::HardRequirement`. As detailed in apache#25055,
this could pass ordering keys to count accumulators as additional
arguments, causing incorrect results or a panic, and could introduce
unnecessary sorting.

## What changes are included in this PR?

Override `Count::order_sensitivity` to return
`AggregateOrderSensitivity::Insensitive`. This makes aggregate ordering
keys ineffective for physical planning: they are excluded from
accumulator inputs and no `SortExec` is required solely for `COUNT`'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.

A bare global `COUNT(a ORDER BY b)` over the in-memory test table is
folded by `AggregateStatistics` to `num_rows - null_count(a)`, so
`CountAccumulator` never runs. The test uses `a + 0`, which preserves
`a`'s nullness but prevents that fold, ensuring the regression exercises
the accumulator.

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.

(cherry picked from commit 224cc56)

Conflict resolved when applying to spiceai-54 (DataFusion 54.1):
- datafusion/sqllogictest/test_files/aggregate.slt: upstream appends this
  commit's COUNT ... ORDER BY block after other blocks that are not on 54.1,
  including the nested-aggregate block. Added only this commit's 43 lines, at
  the end of the 54.1 file.
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) v56.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

COUNT(... ORDER BY ...) can panic or undercount with nullable ordering keys

4 participants