Repository navigation
Commit e72572d
fix: prevent panic and incorrect results for COUNT with ORDER BY (apache#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.1 parent 140c13f commit e72572d
2 files changed
Lines changed: 48 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
378 | 378 | | |
379 | 379 | | |
380 | 380 | | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
381 | 385 | | |
382 | 386 | | |
383 | 387 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9018 | 9018 | | |
9019 | 9019 | | |
9020 | 9020 | | |
| 9021 | + | |
| 9022 | + | |
| 9023 | + | |
| 9024 | + | |
| 9025 | + | |
| 9026 | + | |
| 9027 | + | |
| 9028 | + | |
| 9029 | + | |
| 9030 | + | |
| 9031 | + | |
| 9032 | + | |
| 9033 | + | |
| 9034 | + | |
| 9035 | + | |
| 9036 | + | |
| 9037 | + | |
| 9038 | + | |
| 9039 | + | |
| 9040 | + | |
| 9041 | + | |
| 9042 | + | |
| 9043 | + | |
| 9044 | + | |
| 9045 | + | |
| 9046 | + | |
| 9047 | + | |
| 9048 | + | |
| 9049 | + | |
| 9050 | + | |
| 9051 | + | |
| 9052 | + | |
| 9053 | + | |
| 9054 | + | |
| 9055 | + | |
| 9056 | + | |
| 9057 | + | |
| 9058 | + | |
| 9059 | + | |
| 9060 | + | |
| 9061 | + | |
| 9062 | + | |
| 9063 | + | |
0 commit comments