Skip to content

Commit cc17378

Browse files
committed
Clear ORDER BY for order-insensitive aggregates in AggregateExprBuilder
Instead of passing empty ordering fields from state_fields(), drop the ORDER BY in AggregateExprBuilder::build() when the function is order-insensitive. The ordering fields are then empty from the start, so the aggregate carries no ordering state that later code has to ignore. Also cover the ungrouped case. It fails the same way once the aggregation runs in Partial and Final modes; the filter makes the single-partition `sales_global` input multi-partition so the test reaches that path.
1 parent 90cff05 commit cc17378

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

‎datafusion/physical-expr/src/aggregate.rs‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,15 @@ impl AggregateExprBuilder {
264264
} = self;
265265
assert_or_internal_err!(!args.is_empty(), "args should not be empty");
266266

267+
// An order-insensitive aggregate ignores its ORDER BY, so drop it here.
268+
// Everything derived from `order_bys` below, such as the ordering fields
269+
// in the aggregate's state, then agrees that there is no ordering.
270+
let order_bys = if fun.order_sensitivity().is_insensitive() {
271+
vec![]
272+
} else {
273+
order_bys
274+
};
275+
267276
let ordering_types = order_bys
268277
.iter()
269278
.map(|e| e.expr.data_type(&schema))
@@ -759,18 +768,11 @@ impl AggregateFunctionExpr {
759768

760769
/// the field of the final result of this aggregation.
761770
pub fn state_fields(&self) -> Result<Vec<FieldRef>> {
762-
// An order-insensitive aggregate is never fed its ORDER BY columns (see
763-
// `order_bys`), so its state must not describe ordering fields either.
764-
let ordering_fields = if self.order_sensitivity().is_insensitive() {
765-
&[]
766-
} else {
767-
self.ordering_fields.as_slice()
768-
};
769771
let args = StateFieldsArgs {
770772
name: &self.name,
771773
input_fields: &self.input_fields,
772774
return_field: Arc::clone(&self.return_field),
773-
ordering_fields,
775+
ordering_fields: &self.ordering_fields,
774776
is_distinct: self.is_distinct,
775777
};
776778

‎datafusion/sqllogictest/test_files/group_by.slt‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3076,8 +3076,8 @@ GRC 30 80
30763076
TUR 75 100
30773077

30783078
# An ORDER BY on an order-insensitive aggregator is ignored, so its partial
3079-
# state must not include ordering fields. MIN and MAX use the default state
3080-
# fields; SUM is a control that already defines its own.
3079+
# state must not include ordering fields, with or without GROUP BY. MIN and MAX
3080+
# use the default state fields; SUM is a control that already defines its own.
30813081
query TRRR
30823082
SELECT country, MIN(amount ORDER BY ts DESC) AS min1,
30833083
MAX(amount ORDER BY ts DESC) AS max1,
@@ -3090,6 +3090,18 @@ FRA 50 200 250
30903090
GRC 30 80 110
30913091
TUR 75 100 175
30923092

3093+
# Without GROUP BY too. The filter makes the input multi-partition, so the
3094+
# aggregation runs in Partial and Final modes; over the single partition of
3095+
# `sales_global` alone it would run in Single mode and never build a state.
3096+
query RRR
3097+
SELECT MIN(amount ORDER BY ts DESC) AS min1,
3098+
MAX(amount ORDER BY ts DESC) AS max1,
3099+
SUM(amount ORDER BY ts DESC) AS sum1
3100+
FROM sales_global
3101+
WHERE amount > 0
3102+
----
3103+
30 200 535
3104+
30933105
# Conversion in between FIRST_VALUE and LAST_VALUE to resolve
30943106
# contradictory requirements should work in multi partitions.
30953107
query TT

0 commit comments

Comments
 (0)