Skip to content

Commit 9865cc2

Browse files
authored
fix(sql): skip unsafe eager rewrite for avg-style aggregates (#19740)
* fix(sql): correct eager aggregate decimal metadata (#19738) * fix(sql): sync eager aggregate output types (#19738) * fix(sql): repair eager count aggregate rewrite (#19738) * fix(sql): skip unsafe eager rewrite for avg-style aggregates (#19738) * fix(sql): narrow eager avg fix to rule guard (#19738)
1 parent ff11371 commit 9865cc2

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

src/query/sql/src/planner/optimizer/optimizers/rule/agg_rules/rule_eager_aggregation.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ impl<'a> EagerInput<'a> {
258258
.flat_map(|item| item.scalar.used_columns())
259259
.collect::<ColumnSet>();
260260

261+
if self.has_mixed_sum_and_count_aggregates(&eval_scalar_used_columns) {
262+
return Ok(vec![]);
263+
}
264+
261265
let function_factory = AggregateFunctionFactory::instance();
262266
let eager_candidates = EagerCandidates::collect(
263267
&self.final_agg,
@@ -355,6 +359,36 @@ impl<'a> EagerInput<'a> {
355359
.collect())
356360
}
357361

362+
fn has_mixed_sum_and_count_aggregates(&self, eval_scalar_used_columns: &ColumnSet) -> bool {
363+
let mut has_sum = false;
364+
let mut has_count = false;
365+
366+
for aggregate in &self.final_agg.aggregate_functions {
367+
if !eval_scalar_used_columns.contains(&aggregate.index) {
368+
continue;
369+
}
370+
371+
let ScalarExpr::AggregateFunction(aggregate_function) = &aggregate.scalar else {
372+
continue;
373+
};
374+
375+
match aggregate_function.func_name.as_str() {
376+
"sum" => has_sum = true,
377+
"count" => has_count = true,
378+
_ => {}
379+
}
380+
381+
if has_sum && has_count {
382+
// Mixed sum/count outputs are used by rewrites such as AVG = SUM / COUNT.
383+
// The current eager-count rewrite does not preserve this shape, so keep the
384+
// original aggregate plan instead of applying eager aggregation.
385+
return true;
386+
}
387+
}
388+
389+
false
390+
}
391+
358392
fn expand_analyses(
359393
&self,
360394
final_agg: &Aggregate,

tests/sqllogictests/suites/query/aggregate.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ SELECT AVG(1 + a), sum(1 + a), count(1 + a) FROM tc;
105105
----
106106
3.0 9 3
107107

108+
statement ok
109+
create or replace table t0(c0 int);
110+
111+
statement ok
112+
create or replace table t1(c0 int);
113+
114+
statement ok
115+
insert into t0 values (1), (2);
116+
117+
statement ok
118+
insert into t1 values (1), (2);
119+
120+
# https://github.com/databendlabs/databend/issues/19738
121+
query R
122+
select avg(0.5) from t0, t1;
123+
----
124+
0.5000000
125+
108126
## Aggregate with alias and window function
109127
statement ok
110128
select avg(1+a) score, a, percent_rank() over (partition by a % 3 order by score) d, d + 3 from tc group by a;

0 commit comments

Comments
 (0)