starocean999 commented on code in PR #38387:
URL: https://github.com/apache/doris/pull/38387#discussion_r1697985131
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewAggregateRule.java:
##########
@@ -355,22 +360,116 @@ private boolean isGroupByEquals(Pair<Plan,
LogicalAggregate<Plan>> queryTopPlanA
viewGroupExpressionQueryBased
);
}
- if
(queryGroupShuttledExpression.equals(viewShuttledExpressionQueryBasedToGroupByExpressionMap.values()))
{
+ if
(queryGroupShuttledExpression.equals(viewShuttledExpressionQueryBasedToGroupByExpressionMap.keySet()))
{
// return true, if equals directly
return true;
}
+
+ boolean isGroupByEquals = false;
+ // check is equals by group by eliminate
+ isGroupByEquals |=
isGroupByEqualsAfterGroupByEliminate(queryGroupShuttledExpression,
+ viewShuttledExpressionQueryBasedToGroupByExpressionMap,
+ groupByExpressionToViewShuttledExpressionQueryBasedMap,
+ viewAggregate,
+ cascadesContext);
+ // check is equals by equal filter eliminate
+ Optional<LogicalFilter<Plan>> filterOptional =
tempRewrittenPlan.collectFirst(LogicalFilter.class::isInstance);
+ if (!filterOptional.isPresent()) {
+ return isGroupByEquals;
+ }
+ isGroupByEquals |= isGroupByEqualsAfterEqualFilterEliminate(
+ (LogicalPlan) tempRewrittenPlan,
+ queryGroupShuttledExpression,
+ viewShuttledExpressionQueryBasedToGroupByExpressionMap,
+ materializationContext);
+ return isGroupByEquals;
+ }
+
+ /**
+ * Check group by is equals by equal filter eliminate
+ * For example query is select a, b, c from t1 where a = 1 and d = 'xx'
group by a, b, c;
+ * mv is select a, b, c, d from t1 group by a, b, c, d;
+ * the group by expression between query and view is equals after equal
filter eliminate
+ * should not aggregate roll up
+ * */
+ private static boolean isGroupByEqualsAfterEqualFilterEliminate(
+ LogicalPlan tempRewrittenPlan,
+ Set<Expression> queryGroupShuttledExpression,
+ Map<Expression, Expression>
viewShuttledExprQueryBasedToViewGroupByExprMap,
+ MaterializationContext materializationContext) {
+
+ Map<Expression, Expression> viewShuttledExprToScanExprMapping =
+
materializationContext.getShuttledExprToScanExprMapping().flattenMap().get(0);
+ Set<Expression> viewShuttledExprQueryBasedSet =
viewShuttledExprQueryBasedToViewGroupByExprMap.keySet();
+ // view group by expr can not cover query group by expr
+ if
(!viewShuttledExprQueryBasedSet.containsAll(queryGroupShuttledExpression)) {
+ return false;
+ }
+ Set<Expression> viewShouldUniformExpressionSet = new HashSet<>();
+ // calc the group by expr which is needed to roll up and should be
uniform
+ for (Map.Entry<Expression, Expression> expressionEntry :
+ viewShuttledExprQueryBasedToViewGroupByExprMap.entrySet()) {
+ if
(queryGroupShuttledExpression.contains(expressionEntry.getKey())) {
+ // the group expr which query has, do not require uniform
+ continue;
+ }
+ viewShouldUniformExpressionSet.add(expressionEntry.getValue());
+ }
+
+ DataTrait dataTrait = tempRewrittenPlan.computeDataTrait();
+ for (Expression shouldUniformExpr : viewShouldUniformExpressionSet) {
+ Expression viewScanExpression =
viewShuttledExprToScanExprMapping.get(shouldUniformExpr);
+ if (viewScanExpression == null) {
+ return false;
+ }
+ if (!(viewScanExpression instanceof Slot)) {
+ return false;
+ }
+ if (!dataTrait.isUniform((Slot) viewScanExpression)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Check group by is equal or not after group by eliminate
Review Comment:
the comment of isGroupByEqualsAfterEqualFilterEliminate is easy to
understand, but not isGroupByEqualsAfterGroupByEliminate, can we improve that?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]