Repository navigation
Commit 224cc56
authored
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.1 parent 517ac6d commit 224cc56
2 files changed
Lines changed: 48 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| |||
396 | 396 | | |
397 | 397 | | |
398 | 398 | | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
399 | 403 | | |
400 | 404 | | |
401 | 405 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9883 | 9883 | | |
9884 | 9884 | | |
9885 | 9885 | | |
| 9886 | + | |
| 9887 | + | |
| 9888 | + | |
| 9889 | + | |
| 9890 | + | |
| 9891 | + | |
| 9892 | + | |
| 9893 | + | |
| 9894 | + | |
| 9895 | + | |
| 9896 | + | |
| 9897 | + | |
| 9898 | + | |
| 9899 | + | |
| 9900 | + | |
| 9901 | + | |
| 9902 | + | |
| 9903 | + | |
| 9904 | + | |
| 9905 | + | |
| 9906 | + | |
| 9907 | + | |
| 9908 | + | |
| 9909 | + | |
| 9910 | + | |
| 9911 | + | |
| 9912 | + | |
| 9913 | + | |
| 9914 | + | |
| 9915 | + | |
| 9916 | + | |
| 9917 | + | |
| 9918 | + | |
| 9919 | + | |
| 9920 | + | |
| 9921 | + | |
| 9922 | + | |
| 9923 | + | |
| 9924 | + | |
| 9925 | + | |
| 9926 | + | |
| 9927 | + | |
| 9928 | + | |
0 commit comments