Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] Support more patterns for mv union rewrite by default #55199

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,30 @@ private boolean isPullUpQueryPredicate(ScalarOperator predicate,
});
}

private boolean isPullPredicateRewrite(List<ScalarOperator> queryPredicates,
Set<ColumnRefOperator> mvPredicateUsedColRefs,
MVUnionRewriteMode unionRewriteMode) {
if (unionRewriteMode.isPullPredicateRewrite()) {
return true;
}
// if mv predicates are empty, try to union rewrite.
if (CollectionUtils.isEmpty(mvPredicateUsedColRefs)) {
return true;
}
// if query predicates and mv predicates have the same column, try to union rewrite.
if (queryPredicates.stream()
.anyMatch(pred -> pred.getColumnRefs().stream().anyMatch(col -> mvPredicateUsedColRefs.contains(col)))) {
return true;
}
// TODO: if query predicates are not contained in mv predicates, there maybe a chance to union rewrite
// but the performance may not be good.
// eg:
// query: select dt, k2 from tblA where date_trunc('day', dt) >= '2023-11-01'
// mv : select dt, k2 from tblA where k2 > 10;
// whether to rewrite query by mv should be considered carefully later.
return false;
}

private PredicateSplit getUnionRewriteQueryCompensation(RewriteContext rewriteContext,
ColumnRewriter columnRewriter) {
final PredicateSplit mvCompensationToQuery = getCompensationPredicates(columnRewriter,
Expand All @@ -1674,23 +1698,38 @@ private PredicateSplit getUnionRewriteQueryCompensation(RewriteContext rewriteCo
SessionVariable sessionVariable = optimizerContext.getSessionVariable();
MVUnionRewriteMode unionRewriteMode =
MVUnionRewriteMode.getInstance(sessionVariable.getMaterializedViewUnionRewriteMode());
if (!unionRewriteMode.isPullPredicateRewrite()) {
return null;
}

logMVRewrite(mvRewriteContext, "Try to pull up query's predicates to make possible for union rewrite, " +
"unionRewriteMode:{}", unionRewriteMode);
// try to pull up query's predicates to make possible for rewrite from mv to query.
PredicateSplit mvPredicateSplit = rewriteContext.getMvPredicateSplit();
PredicateSplit queryPredicateSplit = rewriteContext.getQueryPredicateSplit();
Set<ColumnRefOperator> mvPredicateUsedColRefs = Utils.compoundAnd(mvPredicateSplit.getPredicates())
.getColumnRefs().stream()
.map(colRef -> (ColumnRefOperator) columnRewriter.rewriteViewToQuery(colRef))

// filter redundant predicates since they are not used for rewrite.
Set<ColumnRefOperator> mvPredicateUsedColRefs = mvPredicateSplit.getPredicates()
.stream()
.filter(pred -> pred != null && !pred.isRedundant())
.map(pred -> pred.getColumnRefs()
.stream()
.map(colRef -> (ColumnRefOperator) columnRewriter.rewriteViewToQuery(colRef))
.collect(Collectors.toSet()))
.flatMap(Set::stream)
.collect(Collectors.toSet());

List<ScalarOperator> queryPredicates = Lists.newArrayList();
queryPredicates.addAll(Utils.extractConjuncts(queryPredicateSplit.getRangePredicates()));
queryPredicates.addAll(Utils.extractConjuncts(queryPredicateSplit.getResidualPredicates()));
Utils.extractConjuncts(queryPredicateSplit.getRangePredicates())
.stream()
.filter(pred -> pred != null && !pred.isRedundant())
.forEach(queryPredicates::add);
Utils.extractConjuncts(queryPredicateSplit.getResidualPredicates())
.stream()
.filter(pred -> pred != null && !pred.isRedundant())
.forEach(queryPredicates::add);

// if query and mv contains the same column, we can rewrite query by mv.
if (!isPullPredicateRewrite(queryPredicates, mvPredicateUsedColRefs, unionRewriteMode)) {
return null;
}
logMVRewrite(mvRewriteContext, "Try to pull up query's predicates to make possible for union rewrite, " +
"unionRewriteMode:{}", unionRewriteMode);

Set<ScalarOperator> queryOnPredicates = MvUtils.getJoinOnPredicates(rewriteContext.getQueryExpression());
ColumnRefSet queryOnPredicateUsedColRefs = Optional.ofNullable(Utils.compoundAnd(queryOnPredicates))
LiShuMing marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading
Loading