morrySnow commented on code in PR #40803:
URL: https://github.com/apache/doris/pull/40803#discussion_r1764522779


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewAggregateRule.java:
##########
@@ -324,6 +326,46 @@ protected Expression tryRewriteExpression(StructInfo 
queryStructInfo, Expression
         return rewrittenExpression;
     }
 
+    /**
+     * Not all query after rewritten successfully can compensate union all
+     * Such as:
+     * mv def sql is as following, partition column is a
+     * select a, b, count(*) from t1 group by a, b
+     * Query is as following:
+     * select b, count(*) from t1 group by b, after rewritten by materialized 
view successfully
+     * If mv part partition is invalid, can not compensate union all, because 
result is wrong after
+     * compensate union all.
+     */
+    @Override
+    protected boolean canUnionRewrite(Plan queryPlan, MTMV mtmv, 
CascadesContext cascadesContext) {
+        // check query plan is contain the partition column
+        Optional<LogicalAggregate<Plan>> logicalAggregateOptional =
+                queryPlan.collectFirst(planTreeNode -> planTreeNode instanceof 
LogicalAggregate);
+        if (!logicalAggregateOptional.isPresent()) {
+            return true;
+        }
+
+        List<Expression> groupByExpressions = 
logicalAggregateOptional.get().getGroupByExpressions();
+        if (groupByExpressions.isEmpty()) {
+            // Scalar aggregate can not compensate union all
+            return false;
+        }
+        String relatedCol = mtmv.getMvPartitionInfo().getRelatedCol();
+        boolean canUnionRewrite = false;
+        // Check the query plan group by expression contains partition col or 
not
+        List<? extends Expression> groupByShuttledExpressions =
+                
ExpressionUtils.shuttleExpressionWithLineage(groupByExpressions, queryPlan, new 
BitSet());
+        for (Expression expression : groupByShuttledExpressions) {
+            canUnionRewrite = !expression.collectToSet(expr -> expr instanceof 
SlotReference
+                    && ((SlotReference) expr).isColumnFromTable()
+                    && ((SlotReference) 
expr).getColumn().get().getName().equals(relatedCol)).isEmpty();

Review Comment:
   check table name too



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to