[Fix](nereids) fix rule SimplifyWindowExpression (#34099)

Co-authored-by: feiniaofeiafei <moailing@selectdb.com>
This commit is contained in:
feiniaofeiafei
2024-04-25 15:01:55 +08:00
committed by yiguolei
parent f34fe46bfa
commit 987f755206
3 changed files with 53 additions and 29 deletions

View File

@ -27,10 +27,12 @@ import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.WindowExpression;
import org.apache.doris.nereids.trees.expressions.functions.BoundFunction;
import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
import org.apache.doris.nereids.trees.plans.logical.LogicalWindow;
import org.apache.doris.nereids.util.TypeCoercionUtils;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
@ -87,11 +89,13 @@ public class SimplifyWindowExpression extends OneRewriteRuleFactory {
if (function instanceof BoundFunction) {
BoundFunction boundFunction = (BoundFunction) function;
String name = ((BoundFunction) function).getName();
if ((name.equals(COUNT) && boundFunction.child(0).notNullable())
if ((name.equals(COUNT) && checkCount((Count) boundFunction))
|| REWRRITE_TO_CONST_WINDOW_FUNCTIONS.contains(name)) {
projectionsBuilder.add(new Alias(alias.getExprId(), new TinyIntLiteral((byte) 1), alias.getName()));
} else if (REWRRITE_TO_SLOT_WINDOW_FUNCTIONS.contains(name)) {
projectionsBuilder.add(new Alias(alias.getExprId(), boundFunction.child(0), alias.getName()));
projectionsBuilder.add(new Alias(alias.getExprId(),
TypeCoercionUtils.castIfNotSameType(boundFunction.child(0), boundFunction.getDataType()),
alias.getName()));
} else {
remainWindowExpression.add(expr);
}
@ -120,4 +124,8 @@ public class SimplifyWindowExpression extends OneRewriteRuleFactory {
window.child(0)));
}
}
private boolean checkCount(Count count) {
return count.isCountStar() || count.child(0).notNullable();
}
}