Skip to content
Merged
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
26 changes: 24 additions & 2 deletions datafusion/optimizer/src/common_subexpr_eliminate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,19 @@ impl CommonSubexprEliminate {
} => {
let rewritten_aggr_expr = new_exprs_list.pop().unwrap();
let new_aggr_expr = original_exprs_list.pop().unwrap();
let saved_names = if let Some(aggr_expr) = aggr_expr {
let name_preserver = NamePreserver::new_for_projection();
aggr_expr
.iter()
.map(|expr| Some(name_preserver.save(expr)))
.collect::<Vec<_>>()
} else {
new_aggr_expr
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the same as a vec of all None?

vec![None; new_agg_expr.len()]`

Copy link
Contributor Author

@Col-Waltz Col-Waltz May 16, 2025

Choose a reason for hiding this comment

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

Yes it is, thanks for the comment. But this will not work, because vec! requires value to implement Clone trait but the Option<SavedName> doesn't. It seems to me easier to do this by map instead of adding trait to SavedName.

.clone()
.into_iter()
.map(|_| None)
.collect::<Vec<_>>()
};

let mut agg_exprs = common_exprs
.into_iter()
Expand All @@ -326,10 +339,19 @@ impl CommonSubexprEliminate {
for expr in &new_group_expr {
extract_expressions(expr, &mut proj_exprs)
}
for (expr_rewritten, expr_orig) in
rewritten_aggr_expr.into_iter().zip(new_aggr_expr)
for ((expr_rewritten, expr_orig), saved_name) in
rewritten_aggr_expr
.into_iter()
.zip(new_aggr_expr)
.zip(saved_names)
{
if expr_rewritten == expr_orig {
let expr_rewritten = if let Some(saved_name) = saved_name
{
saved_name.restore(expr_rewritten)
} else {
expr_rewritten
};
if let Expr::Alias(Alias { expr, name, .. }) =
expr_rewritten
{
Expand Down
25 changes: 25 additions & 0 deletions datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,31 @@ SELECT c2, var_samp(c12) FILTER (WHERE c12 > 0.95) FROM aggregate_test_100 GROUP
4 NULL
5 NULL

statement ok
CREATE TABLE t (
a DOUBLE,
b BIGINT,
c INT
) AS VALUES
(1.0, 10, -5),
(2.0, 20, -5),
(3.0, 20, 4);

# https://github.com/apache/datafusion/issues/15291
Copy link
Contributor

Choose a reason for hiding this comment

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

for some reason this query is failing for me

> WITH s AS (
    SELECT
        COUNT(a) FILTER (WHERE (b * b) - 3600 <= b),
	COUNT(a) FILTER (WHERE (b * b) - 3000 <= b AND (c >= 0)),
	COUNT(a) FILTER (WHERE (b * b) - 3000 <= b AND (c >= 0) AND (c >= 0))
    FROM t
) SELECT * FROM s;  🤔 Invalid statement: SQL error: ParserError("Expected: ), found: ( at Line: 3, Column: 25")

Copy link
Contributor

Choose a reason for hiding this comment

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

Nm, I can get it to reproduce like this

> set datafusion.sql_parser.dialect = 'postgres';
0 row(s) fetched.
Elapsed 0.000 seconds.

> CREATE TABLE t (
  a DOUBLE,
  b BIGINT,
  c INT
) AS VALUES
(1.0, 10, -5),
(2.0, 20, -5),
(3.0, 20, 4);
0 row(s) fetched.
Elapsed 0.002 seconds.

> WITH s AS (
    SELECT
        COUNT(a) FILTER (WHERE (b * b) - 3600 <= b),
	COUNT(a) FILTER (WHERE (b * b) - 3000 <= b AND (c >= 0)),
	COUNT(a) FILTER (WHERE (b * b) - 3000 <= b AND (c >= 0) AND (c >= 0))
    FROM t
) SELECT * FROM s
;
Optimizer rule 'common_sub_expression_eliminate' failed
caused by
Schema error: No field named "count(t.a) FILTER (WHERE t.b * t.b - Int64(3600) <= t.b)". Did you mean 'count(t.a) FILTER (WHERE __common_expr_1 AS t.b * t.b - Int64(3600) <= t.b)'?.

query III
WITH s AS (
SELECT
COUNT(a) FILTER (WHERE (b * b) - 3600 <= b),
COUNT(a) FILTER (WHERE (b * b) - 3000 <= b AND (c >= 0)),
COUNT(a) FILTER (WHERE (b * b) - 3000 <= b AND (c >= 0) AND (c >= 0))
FROM t
) SELECT * FROM s
----
3 1 1

statement ok
DROP TABLE t

# Restore the default dialect
statement ok
set datafusion.sql_parser.dialect = 'Generic';
Expand Down