[opt](mtmv) Support single mv rewrite when query is select star (#48742)

This commit is contained in:
seawinde
2025-03-08 16:25:57 +08:00
committed by GitHub
parent 98c782bedb
commit 856b64e974
6 changed files with 160 additions and 4 deletions

View File

@ -49,6 +49,7 @@ import org.apache.doris.nereids.rules.exploration.mv.MaterializedViewFilterProje
import org.apache.doris.nereids.rules.exploration.mv.MaterializedViewFilterProjectScanRule;
import org.apache.doris.nereids.rules.exploration.mv.MaterializedViewFilterScanRule;
import org.apache.doris.nereids.rules.exploration.mv.MaterializedViewOnlyJoinRule;
import org.apache.doris.nereids.rules.exploration.mv.MaterializedViewOnlyScanRule;
import org.apache.doris.nereids.rules.exploration.mv.MaterializedViewProjectAggregateRule;
import org.apache.doris.nereids.rules.exploration.mv.MaterializedViewProjectFilterAggregateRule;
import org.apache.doris.nereids.rules.exploration.mv.MaterializedViewProjectFilterJoinRule;
@ -270,6 +271,7 @@ public class RuleSet {
.add(MaterializedViewProjectScanRule.INSTANCE)
.add(MaterializedViewProjectFilterScanRule.INSTANCE)
.add(MaterializedViewAggregateOnNoneAggregateRule.INSTANCE)
.add(MaterializedViewOnlyScanRule.INSTANCE)
.build();
public static final List<Rule> DPHYP_REORDER_RULES = ImmutableList.<Rule>builder()

View File

@ -406,6 +406,7 @@ public enum RuleType {
MATERIALIZED_VIEW_PROJECT_SCAN(RuleTypeClass.EXPLORATION),
MATERIALIZED_VIEW_FILTER_PROJECT_SCAN(RuleTypeClass.EXPLORATION),
MATERIALIZED_VIEW_PROJECT_FILTER_SCAN(RuleTypeClass.EXPLORATION),
MATERIALIZED_VIEW_ONLY_SCAN(RuleTypeClass.EXPLORATION),
// implementation rules
LOGICAL_ONE_ROW_RELATION_TO_PHYSICAL_ONE_ROW_RELATION(RuleTypeClass.IMPLEMENTATION),

View File

@ -0,0 +1,84 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.doris.nereids.rules.exploration.mv;
import org.apache.doris.nereids.CascadesContext;
import org.apache.doris.nereids.rules.exploration.mv.StructInfo.PlanCheckContext;
import org.apache.doris.nereids.rules.exploration.mv.mapping.SlotMapping;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
import com.google.common.collect.ImmutableSet;
import java.util.List;
import java.util.stream.Collectors;
/**
* This is responsible for single table rewriting according to different pattern
* */
public abstract class AbstractMaterializedViewScanRule extends AbstractMaterializedViewRule {
@Override
protected Plan rewriteQueryByView(MatchMode matchMode,
StructInfo queryStructInfo,
StructInfo viewStructInfo,
SlotMapping targetToSourceMapping,
Plan tempRewritedPlan,
MaterializationContext materializationContext,
CascadesContext cascadesContext) {
// Rewrite top projects, represent the query projects by view
List<Expression> expressionsRewritten = rewriteExpression(
queryStructInfo.getExpressions(),
queryStructInfo.getTopPlan(),
materializationContext.getShuttledExprToScanExprMapping(),
targetToSourceMapping,
queryStructInfo.getTableBitSet()
);
// Can not rewrite, bail out
if (expressionsRewritten.isEmpty()) {
materializationContext.recordFailReason(queryStructInfo,
"Rewrite expressions by view in scan fail",
() -> String.format("expressionToRewritten is %s,\n mvExprToMvScanExprMapping is %s,\n"
+ "targetToSourceMapping = %s", queryStructInfo.getExpressions(),
materializationContext.getShuttledExprToScanExprMapping(),
targetToSourceMapping));
return null;
}
return new LogicalProject<>(
expressionsRewritten.stream()
.map(expression -> expression instanceof NamedExpression ? expression : new Alias(expression))
.map(NamedExpression.class::cast)
.collect(Collectors.toList()),
tempRewritedPlan);
}
/**
* Check scan is whether valid or not. Support join's input only support project, filter, join,
* logical relation, simple aggregate node. Con not have aggregate above on join.
* Join condition should be slot reference equals currently.
*/
@Override
protected boolean checkQueryPattern(StructInfo structInfo, CascadesContext cascadesContext) {
PlanCheckContext checkContext = PlanCheckContext.of(ImmutableSet.of());
return structInfo.getTopPlan().accept(StructInfo.SCAN_PLAN_PATTERN_CHECKER, checkContext)
&& !checkContext.isContainsTopAggregate();
}
}

View File

@ -0,0 +1,42 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.doris.nereids.rules.exploration.mv;
import org.apache.doris.nereids.rules.Rule;
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation;
import com.google.common.collect.ImmutableList;
import java.util.List;
/**
* MaterializedViewOnlyScanRule
*/
public class MaterializedViewOnlyScanRule extends AbstractMaterializedViewScanRule {
public static final MaterializedViewOnlyScanRule INSTANCE = new MaterializedViewOnlyScanRule();
@Override
public List<Rule> buildRules() {
return ImmutableList.of(
any().when(LogicalCatalogRelation.class::isInstance).thenApplyMultiNoThrow(ctx -> {
return rewrite(ctx.root, ctx.cascadesContext);
}).toRule(RuleType.MATERIALIZED_VIEW_ONLY_SCAN));
}
}

View File

@ -31,3 +31,16 @@
-- !query1_3_after --
6
-- !query1_4_before --
1 2 3 4 5.50 6.50 7.50 8.50 o k 2023-12-08 2023-12-09 2023-12-10 a b yyyyyyyyy
2 4 3 4 5.50 6.50 7.50 8.50 o k 2023-12-09 2023-12-09 2023-12-10 a b yyyyyyyyy
3 2 4 4 5.50 6.50 7.50 8.50 o k 2023-12-10 2023-12-09 2023-12-10 a b yyyyyyyyy
4 3 3 4 5.50 6.50 7.50 8.50 o k 2023-12-11 2023-12-09 2023-12-10 a b yyyyyyyyy
5 2 3 6 7.50 8.50 9.50 10.50 k o 2023-12-12 2023-12-12 2023-12-13 c d xxxxxxxxx
-- !query1_4_after --
1 2 3 4 5.50 6.50 7.50 8.50 o k 2023-12-08 2023-12-09 2023-12-10 a b yyyyyyyyy
2 4 3 4 5.50 6.50 7.50 8.50 o k 2023-12-09 2023-12-09 2023-12-10 a b yyyyyyyyy
3 2 4 4 5.50 6.50 7.50 8.50 o k 2023-12-10 2023-12-09 2023-12-10 a b yyyyyyyyy
4 3 3 4 5.50 6.50 7.50 8.50 o k 2023-12-11 2023-12-09 2023-12-10 a b yyyyyyyyy
5 2 3 6 7.50 8.50 9.50 10.50 k o 2023-12-12 2023-12-12 2023-12-13 c d xxxxxxxxx

View File

@ -116,7 +116,7 @@ suite("mv_scan_table") {
insert into partsupp values
(2, 3, 9, 10.01, 'supply1'),
(2, 3, 10, 11.01, 'supply2');
"""
sql """analyze table orders with sync;"""
@ -124,8 +124,8 @@ suite("mv_scan_table") {
sql """analyze table partsupp with sync;"""
sql """alter table orders modify column o_comment set stats ('row_count'='8');"""
sql """alter table lineitem modify column l_comment set stats ('row_count'='5');"""
sql """alter table partsupp modify column ps_comment set stats ('row_count'='2');"""
sql """alter table lineitem modify column l_comment set stats ('row_count'='5');"""
sql """alter table partsupp modify column ps_comment set stats ('row_count'='2');"""
// with filter
def mv1_0 =
@ -186,4 +186,18 @@ sql """alter table partsupp modify column ps_comment set stats ('row_count'='2')
async_mv_rewrite_success(db, mv1_3, query1_3, "mv1_3")
order_qt_query1_3_after "${query1_3}"
sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_3"""
}
def mv1_4 =
"""
select *
from lineitem
"""
def query1_4 = """
select *
from lineitem
"""
order_qt_query1_4_before "${query1_4}"
async_mv_rewrite_success_without_check_chosen(db, mv1_4, query1_4, "mv1_4")
order_qt_query1_4_after "${query1_4}"
sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_4"""
}