Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use datafusion_physical_expr_common::sort_expr::{
LexOrdering, LexRequirement, OrderingRequirements, PhysicalSortExpr,
PhysicalSortRequirement,
};
use datafusion_physical_plan::aggregates::AggregateExec;
use datafusion_physical_plan::execution_plan::CardinalityEffect;
use datafusion_physical_plan::filter::FilterExec;
use datafusion_physical_plan::joins::utils::{
Expand Down Expand Up @@ -355,6 +356,10 @@ fn pushdown_requirement_to_children(
}
} else if maintains_input_order.is_empty()
|| !maintains_input_order.iter().any(|o| *o)
// Aggregate output columns can be computed expressions that are not
Copy link
Contributor Author

@alamb alamb Feb 9, 2026

Choose a reason for hiding this comment

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

This is a pretty large hammer (just disables pushing grouping requirements through all AggregateExec`s convinced this is the best solution / correct yet

// order-preserving wrt input columns. The generic index-based rewrite
// in handle_custom_pushdown is not safe for AggregateExec.
|| plan.as_any().is::<AggregateExec>()
|| plan.as_any().is::<RepartitionExec>()
|| plan.as_any().is::<FilterExec>()
// TODO: Add support for Projection push down
Expand Down
69 changes: 69 additions & 0 deletions datafusion/sqllogictest/test_files/sort_pushdown.slt
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,69 @@ LIMIT 3;
5 4
2 -3

# Test 3.7: Aggregate ORDER BY expression should keep SortExec
# Source pattern declared on parquet scan: [x ASC, y ASC].
# Requested pattern in ORDER BY: [x ASC, CAST(y AS BIGINT) % 2 ASC].
# Example for x=1 input y order 1,2,3 gives bucket order 1,0,1, which does not
# match requested bucket ASC order. SortExec is required above AggregateExec.
statement ok
SET datafusion.execution.target_partitions = 1;

statement ok
CREATE TABLE agg_expr_data(x INT, y INT, v INT) AS VALUES
(1, 1, 10),
(1, 2, 20),
(1, 3, 30),
(2, 1, 40),
(2, 2, 50),
(2, 3, 60);

query I
COPY (SELECT * FROM agg_expr_data ORDER BY x, y)
TO 'test_files/scratch/sort_pushdown/agg_expr_sorted.parquet';
----
6

statement ok
CREATE EXTERNAL TABLE agg_expr_parquet(x INT, y INT, v INT)
STORED AS PARQUET
LOCATION 'test_files/scratch/sort_pushdown/agg_expr_sorted.parquet'
WITH ORDER (x ASC, y ASC);

query TT
EXPLAIN SELECT
x,
CAST(y AS BIGINT) % 2,
SUM(v)
FROM agg_expr_parquet
GROUP BY x, CAST(y AS BIGINT) % 2
ORDER BY x, CAST(y AS BIGINT) % 2;
----
logical_plan
01)Sort: agg_expr_parquet.x ASC NULLS LAST, agg_expr_parquet.y % Int64(2) ASC NULLS LAST
02)--Aggregate: groupBy=[[agg_expr_parquet.x, CAST(agg_expr_parquet.y AS Int64) % Int64(2)]], aggr=[[sum(CAST(agg_expr_parquet.v AS Int64))]]
03)----TableScan: agg_expr_parquet projection=[x, y, v]
physical_plan
01)SortExec: expr=[x@0 ASC NULLS LAST, agg_expr_parquet.y % Int64(2)@1 ASC NULLS LAST], preserve_partitioning=[false]
02)--AggregateExec: mode=Single, gby=[x@0 as x, CAST(y@1 AS Int64) % 2 as agg_expr_parquet.y % Int64(2)], aggr=[sum(agg_expr_parquet.v)], ordering_mode=PartiallySorted([0])
03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_pushdown/agg_expr_sorted.parquet]]}, projection=[x, y, v], output_ordering=[x@0 ASC NULLS LAST, y@1 ASC NULLS LAST], file_type=parquet

# Expected output pattern from ORDER BY [x, bucket]:
# rows grouped by x, and within each x bucket appears as 0 then 1.
query III
SELECT
x,
CAST(y AS BIGINT) % 2,
SUM(v)
FROM agg_expr_parquet
GROUP BY x, CAST(y AS BIGINT) % 2
ORDER BY x, CAST(y AS BIGINT) % 2;
----
1 0 20
1 1 40
2 0 50
2 1 100

# Cleanup
statement ok
DROP TABLE timestamp_data;
Expand Down Expand Up @@ -882,5 +945,11 @@ DROP TABLE signed_data;
statement ok
DROP TABLE signed_parquet;

statement ok
DROP TABLE agg_expr_data;

statement ok
DROP TABLE agg_expr_parquet;

statement ok
SET datafusion.optimizer.enable_sort_pushdown = true;
Loading