[fix](nereids) ExtractAndNormalizeWindowExpression rule should push down correct exprs to child (#21827)

consider the window function:
```sql
substr(
ref_1.cp_type,
sum(CASE WHEN ref_1.cp_type = 0 THEN 3 ELSE 2 END) OVER (),
1)
```
Before the pr, only "CASE WHEN ref_1.cp_type  = 0 THEN 3 ELSE 2 END" is pushed down.
But both "ref_1.cp_type" and "CASE WHEN ref_1.cp_type  = 0 THEN 3 ELSE 2 END"
should be pushed down.
This pr fix it
This commit is contained in:
starocean999
2023-07-20 11:47:55 +08:00
committed by GitHub
parent 0f116ce148
commit 86d7233b06
3 changed files with 19 additions and 12 deletions

View File

@ -34,7 +34,6 @@ import com.google.common.collect.Sets;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
@ -98,21 +97,16 @@ public class ExtractAndNormalizeWindowExpression extends OneRewriteRuleFactory i
if (expression.anyMatch(WindowExpression.class::isInstance)) {
Set<Slot> inputSlots = Sets.newHashSet(expression.getInputSlots());
Set<WindowExpression> collects = expression.collect(WindowExpression.class::isInstance);
Set<Slot> windowInputSlots = collects.stream()
.flatMap(win -> win.getInputSlots().stream())
.collect(Collectors.toSet());
// substr(
// ref_1.cp_type,
// max(
// cast(ref_1.`cp_catalog_page_number` as int)) over (...)
// ),
// 1)
// sum(CASE WHEN ref_1.cp_type = 0 THEN 3 ELSE 2 END) OVER (),
// 1),
//
// in above case, ref_1.cp_type should be pushed down. ref_1.cp_type is in
// substr.inputSlots, but not in windowExpression.inputSlots
// in above case,
// ref_1.cp_type and CASE WHEN ref_1.cp_type = 0 THEN 3 ELSE 2 END
// should be pushed down.
//
// inputSlots= {ref_1.cp_type}
inputSlots.removeAll(windowInputSlots);
return Stream.concat(
collects.stream().flatMap(windowExpression ->
windowExpression.getExpressionsInWindowSpec().stream()
@ -121,7 +115,7 @@ public class ExtractAndNormalizeWindowExpression extends OneRewriteRuleFactory i
.filter(expr -> !expr.isConstant())
),
inputSlots.stream()
);
).distinct();
}
return ImmutableList.of(expression).stream();
})

View File

@ -5,3 +5,6 @@
-- !select2 --
10.00000000000000
-- !select3 --
1

View File

@ -64,5 +64,15 @@ suite("agg_window_project") {
order_qt_select2 """select b / 1 * 10 from test_window_table;"""
order_qt_select3 """SELECT
CASE
WHEN b != 0
AND sum(CASE WHEN b = 0 THEN 3 ELSE 2 END) OVER () < 50000
THEN 1
ELSE 0
END AS val
FROM
test_window_table;"""
sql "DROP TABLE IF EXISTS test_window_table;"
}