seawinde commented on code in PR #62607:
URL: https://github.com/apache/doris/pull/62607#discussion_r3263216532
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewWindowRule.java:
##########
@@ -104,20 +117,141 @@ protected Plan rewriteQueryByView(MatchMode matchMode,
StructInfo queryStructInf
viewToQuerySlotMapping,
ImmutableMap.of(), cascadesContext
);
- // Can not rewrite, bail out
+ // If generic rewrite fails, try roll up from query expressions.
if (expressionsRewritten.isEmpty()) {
- materializationContext.recordFailReason(queryStructInfo,
- "Rewrite expressions by view in window scan fail",
- () -> String.format("expressionToRewritten is %s,\n
mvExprToMvScanExprMapping is %s,\n"
- + "targetToSourceMapping = %s",
queryStructInfo.getExpressions(),
-
materializationContext.getShuttledExprToScanExprMapping(),
- viewToQuerySlotMapping));
- return null;
+ expressionsRewritten =
rollupWindowAggregateFunctions(queryStructInfo.getExpressions(),
+ queryStructInfo.getTopPlan(),
mvExprToMvScanExprQueryBased, true, false);
+ if (expressionsRewritten.isEmpty()) {
+ materializationContext.recordFailReason(queryStructInfo,
+ "Rewrite expressions by view in window scan fail",
+ () -> String.format("expressionToRewritten is %s,\n
mvExprToMvScanExprMapping is %s,\n"
+ + "targetToSourceMapping = %s",
queryStructInfo.getExpressions(),
+
materializationContext.getShuttledExprToScanExprMapping(),
+ viewToQuerySlotMapping));
+ return null;
+ }
}
return new LogicalProject<>(
expressionsRewritten.stream()
.map(expression -> expression instanceof
NamedExpression ? expression : new Alias(expression))
.map(NamedExpression.class::cast)
.collect(Collectors.toList()), tempRewrittenPlan);
}
+
+ private static List<Expression> rollupWindowAggregateFunctions(List<?
extends Expression> expressions,
+ Plan queryTopPlan, Map<Expression, Expression>
mvExprToMvScanExprQueryBased,
+ boolean needShuttle, boolean strictSlotRewrite) {
+ WindowAggregateRollupContext context = new
WindowAggregateRollupContext(queryTopPlan,
+ mvExprToMvScanExprQueryBased, strictSlotRewrite);
+ List<? extends Expression> inputExpressions = needShuttle
+ ? ExpressionUtils.shuttleExpressionWithLineage(expressions,
queryTopPlan)
+ : expressions;
+ List<Expression> rewrittenExpressions = inputExpressions.stream()
+ .map(expression ->
expression.accept(WindowAggregateRollupRewriter.INSTANCE, context))
+ .collect(Collectors.toList());
+ return context.isValid() ? rewrittenExpressions : ImmutableList.of();
+ }
+
+ private static Function rollupWindowAggregateFunction(AggregateFunction
queryAggregateFunction,
+ Expression queryAggregateFunctionShuttled, Map<Expression,
Expression> mvExprToMvScanExprQueryBased) {
+ for (Map.Entry<Expression, Expression> expressionEntry :
mvExprToMvScanExprQueryBased.entrySet()) {
+ Expression viewExpression = expressionEntry.getKey();
+ // Window mapping keys may be full WindowExpression while rollup
handlers match aggregate functions.
+ if (viewExpression instanceof WindowExpression) {
Review Comment:
Dropping the WindowExpression wrapper here removes the
partition/order/frame information from the rollup check. The aggregate rollup
handlers only validate function compatibility, so a view expression
with the same aggregate function but a different window spec could be
selected incorrectly if graph comparison is relaxed for this feature. Please
explicitly verify the query and MV window specs match
before using the MV window column.
--
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]