Skip to content

Commit ebee26d

Browse files
lincoln-lilxiedeyantu
authored andcommitted
[CALCITE-7192] AggregateReduceFunctionsRule lost FILTER condition in STDDEV/VAR function decomposition
1 parent 6211a95 commit ebee26d

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

core/src/main/java/org/apache/calcite/rel/rules/AggregateReduceFunctionsRule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ private static RexNode reduceStddev(
552552

553553
final AggregateCall sumArgSquaredAggCall =
554554
createAggregateCallWithBinding(typeFactory, SqlStdOperatorTable.SUM,
555-
argSquared.getType(), oldAggRel, oldCall, argSquaredOrdinal, -1);
555+
argSquared.getType(), oldAggRel, oldCall, argSquaredOrdinal, oldCall.filterArg);
556556

557557
final RexNode sumArgSquared =
558558
rexBuilder.addAggCall(sumArgSquaredAggCall,

core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,6 +2147,30 @@ private void checkSemiOrAntiJoinProjectTranspose(JoinRelType type) {
21472147
.check();
21482148
}
21492149

2150+
/** Test case for
2151+
* <a href="https://issues.apache.org/jira/browse/CALCITE-7192">[CALCITE-7192]
2152+
* AggregateReduceFunctionsRule lost FILTER condition in STDDEV/VAR function decomposition</a>. */
2153+
@Test void testVarianceStddevWithFilter() {
2154+
// Test to ensure FILTER conditions are correctly propagated to decomposed aggregates
2155+
// for all functions that use the `reduceStddev` method
2156+
final RelOptRule rule = AggregateReduceFunctionsRule.Config.DEFAULT
2157+
.withOperandFor(LogicalAggregate.class)
2158+
.withFunctionsToReduce(
2159+
EnumSet.of(SqlKind.STDDEV_POP, SqlKind.STDDEV_SAMP,
2160+
SqlKind.VAR_POP, SqlKind.VAR_SAMP, SqlKind.AVG))
2161+
.toRule();
2162+
final String sql = "select name, "
2163+
+ "stddev_pop(deptno) filter (where deptno > 10), "
2164+
+ "stddev_samp(deptno) filter (where deptno > 20), "
2165+
+ "var_pop(deptno) filter (where deptno > 30), "
2166+
+ "var_samp(deptno) filter (where deptno > 40), "
2167+
+ "avg(deptno) filter (where deptno > 50)\n"
2168+
+ "from sales.dept group by name";
2169+
sql(sql)
2170+
.withRule(rule)
2171+
.check();
2172+
}
2173+
21502174
@Test void testDistinctCountWithoutGroupBy() {
21512175
final String sql = "select max(deptno), count(distinct ename)\n"
21522176
+ "from sales.emp";

core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20068,6 +20068,28 @@ LogicalProject(DEPTNO=[$7])
2006820068
<![CDATA[
2006920069
LogicalProject(DEPTNO=[$0], NAME=[$1])
2007020070
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
20071+
]]>
20072+
</Resource>
20073+
</TestCase>
20074+
<TestCase name="testVarianceStddevWithFilter">
20075+
<Resource name="sql">
20076+
<![CDATA[select name, stddev_pop(deptno) filter (where deptno > 10), stddev_samp(deptno) filter (where deptno > 20), var_pop(deptno) filter (where deptno > 30), var_samp(deptno) filter (where deptno > 40), avg(deptno) filter (where deptno > 50)
20077+
from sales.dept group by name]]>
20078+
</Resource>
20079+
<Resource name="planBefore">
20080+
<![CDATA[
20081+
LogicalAggregate(group=[{0}], EXPR$1=[STDDEV_POP($1) FILTER $2], EXPR$2=[STDDEV_SAMP($1) FILTER $3], EXPR$3=[VAR_POP($1) FILTER $4], EXPR$4=[VAR_SAMP($1) FILTER $5], EXPR$5=[AVG($1) FILTER $6])
20082+
LogicalProject(NAME=[$1], DEPTNO=[$0], $f2=[>($0, 10)], $f3=[>($0, 20)], $f4=[>($0, 30)], $f5=[>($0, 40)], $f6=[>($0, 50)])
20083+
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
20084+
]]>
20085+
</Resource>
20086+
<Resource name="planAfter">
20087+
<![CDATA[
20088+
LogicalProject(NAME=[$0], EXPR$1=[CAST(POWER(/(-($1, /(*($2, $2), $3)), $3), 0.5:DECIMAL(2, 1))):INTEGER], EXPR$2=[CAST(POWER(/(-($4, /(*($5, $5), $6)), CASE(=($6, 1), null:BIGINT, -($6, 1))), 0.5:DECIMAL(2, 1))):INTEGER], EXPR$3=[CAST(/(-($7, /(*($8, $8), $9)), $9)):INTEGER], EXPR$4=[CAST(/(-($10, /(*($11, $11), $12)), CASE(=($12, 1), null:BIGINT, -($12, 1)))):INTEGER], EXPR$5=[CAST(/($13, $14)):INTEGER])
20089+
LogicalAggregate(group=[{0}], agg#0=[SUM($7) FILTER $2], agg#1=[SUM($1) FILTER $2], agg#2=[COUNT() FILTER $2], agg#3=[SUM($7) FILTER $3], agg#4=[SUM($1) FILTER $3], agg#5=[COUNT() FILTER $3], agg#6=[SUM($7) FILTER $4], agg#7=[SUM($1) FILTER $4], agg#8=[COUNT() FILTER $4], agg#9=[SUM($7) FILTER $5], agg#10=[SUM($1) FILTER $5], agg#11=[COUNT() FILTER $5], agg#12=[SUM($1) FILTER $6], agg#13=[COUNT() FILTER $6])
20090+
LogicalProject(NAME=[$0], DEPTNO=[$1], $f2=[$2], $f3=[$3], $f4=[$4], $f5=[$5], $f6=[$6], $f7=[*($1, $1)])
20091+
LogicalProject(NAME=[$1], DEPTNO=[$0], $f2=[>($0, 10)], $f3=[>($0, 20)], $f4=[>($0, 30)], $f5=[>($0, 40)], $f6=[>($0, 50)])
20092+
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
2007120093
]]>
2007220094
</Resource>
2007320095
</TestCase>

core/src/test/resources/sql/agg.iq

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,30 @@ group by gender;
239239

240240
!ok
241241

242+
# [CALCITE-7192] lost FILTER condition when decomposition
243+
select gender,
244+
stddev_pop(deptno) as p,
245+
stddev_pop(deptno) filter (where deptno > 50) as pf,
246+
stddev_samp(deptno) as s,
247+
stddev_samp(deptno) filter (where deptno > 40) as sf,
248+
var_pop(deptno) as vp,
249+
var_pop(deptno) filter (where deptno > 30) as vpf,
250+
var_samp(deptno) as vs,
251+
var_samp(deptno) filter (where deptno > 20) as vsf,
252+
avg(deptno) as a,
253+
avg(deptno) filter (where deptno > 10) as af
254+
from emp
255+
group by gender;
256+
+--------+----+----+----+----+-----+-----+-----+-----+----+----+
257+
| GENDER | P | PF | S | SF | VP | VPF | VS | VSF | A | AF |
258+
+--------+----+----+----+----+-----+-----+-----+-----+----+----+
259+
| F | 17 | 0 | 19 | 7 | 304 | 25 | 380 | 225 | 36 | 42 |
260+
| M | 17 | | 20 | | 289 | 0 | 433 | | 26 | 35 |
261+
+--------+----+----+----+----+-----+-----+-----+-----+----+----+
262+
(2 rows)
263+
264+
!ok
265+
242266
select city, gender as c from emps;
243267
+---------------+---+
244268
| CITY | C |

0 commit comments

Comments
 (0)