Skip to content

Fix: common_sub_expression_eliminate optimizer rule failed #16066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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 @@ -690,6 +690,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
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