[fix](mtmv) check groupby in agg-bottom-plan when rewrite agg query by mv (#34274)

check groupby in agg-bottom-plan when rewrite and rollup agg query by mv
This commit is contained in:
SWEI
2024-05-15 12:00:29 +08:00
committed by yiguolei
parent f9c42f34dd
commit baf9a45e57
3 changed files with 65 additions and 0 deletions

View File

@ -230,6 +230,7 @@ public abstract class AbstractMaterializedViewAggregateRule extends AbstractMate
// split the query top plan expressions to group expressions and functions, if can not, bail out.
Pair<Set<? extends Expression>, Set<? extends Expression>> queryGroupAndFunctionPair
= topPlanSplitToGroupAndFunction(queryTopPlanAndAggPair, queryStructInfo);
Set<? extends Expression> queryTopPlanGroupBySet = queryGroupAndFunctionPair.key();
Set<? extends Expression> queryTopPlanFunctionSet = queryGroupAndFunctionPair.value();
// try to rewrite, contains both roll up aggregate functions and aggregate group expression
List<NamedExpression> finalOutputExpressions = new ArrayList<>();
@ -284,6 +285,31 @@ public abstract class AbstractMaterializedViewAggregateRule extends AbstractMate
}
// add project to guarantee group by column ref is slot reference,
// this is necessary because physical createHash will need slotReference later
if (queryGroupByExpressions.size() != queryTopPlanGroupBySet.size()) {
for (Expression expression : queryGroupByExpressions) {
if (queryTopPlanGroupBySet.contains(expression)) {
continue;
}
Expression queryGroupShuttledExpr = ExpressionUtils.shuttleExpressionWithLineage(
expression, queryTopPlan, queryStructInfo.getTableBitSet());
AggregateExpressionRewriteContext context = new AggregateExpressionRewriteContext(true,
mvExprToMvScanExprQueryBased, queryTopPlan, queryStructInfo.getTableBitSet());
// group by expression maybe group by a + b, so we need expression rewriter
Expression rewrittenGroupByExpression = queryGroupShuttledExpr.accept(AGGREGATE_EXPRESSION_REWRITER,
context);
if (!context.isValid()) {
// group expr can not rewrite by view
materializationContext.recordFailReason(queryStructInfo,
"View dimensions doesn't not cover the query dimensions in bottom agg ",
() -> String.format("mvExprToMvScanExprQueryBased is %s,\n queryGroupShuttledExpr is %s",
mvExprToMvScanExprQueryBased, queryGroupShuttledExpr));
return null;
}
NamedExpression groupByExpression = rewrittenGroupByExpression instanceof NamedExpression
? (NamedExpression) rewrittenGroupByExpression : new Alias(rewrittenGroupByExpression);
finalGroupExpressions.add(groupByExpression);
}
}
List<Expression> copiedFinalGroupExpressions = new ArrayList<>(finalGroupExpressions);
List<NamedExpression> projectsUnderAggregate = copiedFinalGroupExpressions.stream()
.map(NamedExpression.class::cast)