[feat](Nereids) Optimize Sum Literal Rewriting by Excluding Single Instances (#35559) (#37047)

pick from master #35559

This PR introduces a change in the method removeOneSumLiteral to enhance
the performance of sum literal rewriting in SQL queries. The
modification ensures that sum literals appearing only once, such as in
expressions like select count(id1 + 1), count(id2 + 1) from t, are not
rewritten.
This commit is contained in:
谢健
2024-07-01 14:57:15 +08:00
committed by GitHub
parent 14c991f09b
commit 24d236b210
2 changed files with 54 additions and 2 deletions

View File

@ -44,6 +44,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
@ -64,13 +65,33 @@ public class SumLiteralRewrite extends OneRewriteRuleFactory {
}
sumLiteralMap.put(pel.first, pel.second);
}
if (sumLiteralMap.isEmpty()) {
Map<NamedExpression, Pair<SumInfo, Literal>> validSumLiteralMap =
removeOneSumLiteral(sumLiteralMap);
if (validSumLiteralMap.isEmpty()) {
return null;
}
return rewriteSumLiteral(agg, sumLiteralMap);
return rewriteSumLiteral(agg, validSumLiteralMap);
}).toRule(RuleType.SUM_LITERAL_REWRITE);
}
// when there only one sum literal like select count(id1 + 1), count(id2 + 1) from t, we don't rewrite them.
private Map<NamedExpression, Pair<SumInfo, Literal>> removeOneSumLiteral(
Map<NamedExpression, Pair<SumInfo, Literal>> sumLiteralMap) {
Map<Expression, Integer> countSum = new HashMap<>();
for (Entry<NamedExpression, Pair<SumInfo, Literal>> e : sumLiteralMap.entrySet()) {
Expression expr = e.getValue().first.expr;
countSum.merge(expr, 1, Integer::sum);
}
Map<NamedExpression, Pair<SumInfo, Literal>> validSumLiteralMap = new HashMap<>();
for (Entry<NamedExpression, Pair<SumInfo, Literal>> e : sumLiteralMap.entrySet()) {
Expression expr = e.getValue().first.expr;
if (countSum.get(expr) > 1) {
validSumLiteralMap.put(e.getKey(), e.getValue());
}
}
return validSumLiteralMap;
}
private Plan rewriteSumLiteral(
LogicalAggregate<?> agg, Map<NamedExpression, Pair<SumInfo, Literal>> sumLiteralMap) {
Set<NamedExpression> newAggOutput = new HashSet<>();