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

[improvement](mtmv) Support to partition prune when query rewrite by sync materialized view #38527

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 @@ -233,7 +233,7 @@ protected List<Plan> doRewrite(StructInfo queryStructInfo, CascadesContext casca
continue;
}
Plan rewrittenPlan;
Plan mvScan = materializationContext.getScanPlan();
Plan mvScan = materializationContext.getScanPlan(queryStructInfo);
Plan queryPlan = queryStructInfo.getTopPlan();
if (compensatePredicates.isAlwaysTrue()) {
rewrittenPlan = mvScan;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.doris.nereids.rules.exploration.mv.mapping.ExpressionMapping;
import org.apache.doris.nereids.trees.plans.ObjectId;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.PreAggStatus;
import org.apache.doris.nereids.trees.plans.RelationId;
import org.apache.doris.nereids.trees.plans.algebra.Relation;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
Expand Down Expand Up @@ -56,7 +57,8 @@ public class AsyncMaterializationContext extends MaterializationContext {
*/
public AsyncMaterializationContext(MTMV mtmv, Plan mvPlan, Plan mvOriginalPlan, List<Table> baseTables,
List<Table> baseViews, CascadesContext cascadesContext, StructInfo structInfo) {
super(mvPlan, mvOriginalPlan, MaterializedViewUtils.generateMvScanPlan(mtmv, cascadesContext),
super(mvPlan, mvOriginalPlan, MaterializedViewUtils.generateMvScanPlan(mtmv, mtmv.getBaseIndexId(),
mtmv.getPartitionIds(), PreAggStatus.on(), cascadesContext),
cascadesContext, structInfo);
this.mtmv = mtmv;
}
Expand All @@ -67,7 +69,8 @@ public MTMV getMtmv() {

@Override
Plan doGenerateScanPlan(CascadesContext cascadesContext) {
return MaterializedViewUtils.generateMvScanPlan(this.mtmv, cascadesContext);
return MaterializedViewUtils.generateMvScanPlan(this.mtmv, this.mtmv.getBaseIndexId(),
this.mtmv.getPartitionIds(), PreAggStatus.on(), cascadesContext);
}

@Override
Expand Down Expand Up @@ -107,7 +110,8 @@ public Optional<Pair<Id, Statistics>> getPlanStatistics(CascadesContext cascades
return Optional.empty();
}
RelationId relationId = null;
Optional<LogicalOlapScan> logicalOlapScan = this.getScanPlan().collectFirst(LogicalOlapScan.class::isInstance);
Optional<LogicalOlapScan> logicalOlapScan = this.getScanPlan(null)
.collectFirst(LogicalOlapScan.class::isInstance);
if (logicalOlapScan.isPresent()) {
relationId = logicalOlapScan.get().getRelationId();
}
Expand All @@ -127,7 +131,8 @@ boolean isFinalChosen(Relation relation) {
);
}

public Plan getScanPlan() {
@Override
public Plan getScanPlan(StructInfo queryInfo) {
return scanPlan;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public Plan getOriginalPlan() {
return originalPlan;
}

public Plan getScanPlan() {
public Plan getScanPlan(StructInfo queryStructInfo) {
return scanPlan;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.doris.analysis.Expr;
import org.apache.doris.analysis.SlotRef;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.MTMV;
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.catalog.PartitionType;
import org.apache.doris.catalog.TableIf;
Expand Down Expand Up @@ -224,35 +223,20 @@ public static List<StructInfo> extractStructInfo(Plan plan, Plan originalPlan, C
* when query rewrite, because one plan may hit the materialized view repeatedly and the mv scan output
* should be different
*/
public static Plan generateMvScanPlan(MTMV materializedView, CascadesContext cascadesContext) {
public static Plan generateMvScanPlan(OlapTable table, long indexId,
List<Long> partitionIds,
PreAggStatus preAggStatus,
CascadesContext cascadesContext) {
return new LogicalOlapScan(
cascadesContext.getStatementContext().getNextRelationId(),
materializedView,
materializedView.getFullQualifiers(),
table,
ImmutableList.of(table.getQualifiedDbName()),
ImmutableList.of(),
materializedView.getPartitionIds(),
materializedView.getBaseIndexId(),
PreAggStatus.on(),
ImmutableList.of(),
// this must be empty, or it will be used to sample
ImmutableList.of(),
Optional.empty());
}

/**
* LIke above but generate scan plan for sync materialized view
*/
public static Plan generateMvScanPlan(OlapTable olapTable, long indexId, CascadesContext cascadesContext) {
return new LogicalOlapScan(
cascadesContext.getStatementContext().getNextRelationId(),
olapTable,
ImmutableList.of(olapTable.getQualifiedDbName()),
// this must be empty, or it will be used to sample
ImmutableList.of(),
olapTable.getPartitionIds(),
partitionIds,
indexId,
PreAggStatus.unset(),
preAggStatus,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be unset

Copy link
Contributor Author

@seawinde seawinde Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the preAggStatus is variable.
it's is unset when sync materialization context and is on when async materialization context

ImmutableList.of(),
// this must be empty, or it will be used to sample
ImmutableList.of(),
Optional.empty());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.doris.nereids.CascadesContext;
import org.apache.doris.nereids.trees.plans.ObjectId;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.PreAggStatus;
import org.apache.doris.nereids.trees.plans.RelationId;
import org.apache.doris.nereids.trees.plans.algebra.Relation;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
Expand Down Expand Up @@ -54,7 +55,8 @@ public class SyncMaterializationContext extends MaterializationContext {
public SyncMaterializationContext(Plan mvPlan, Plan mvOriginalPlan, OlapTable olapTable,
long indexId, String indexName, CascadesContext cascadesContext, Statistics statistics) {
super(mvPlan, mvOriginalPlan,
MaterializedViewUtils.generateMvScanPlan(olapTable, indexId, cascadesContext), cascadesContext, null);
MaterializedViewUtils.generateMvScanPlan(olapTable, indexId, olapTable.getPartitionIds(),
PreAggStatus.unset(), cascadesContext), cascadesContext, null);
this.olapTable = olapTable;
this.indexId = indexId;
this.indexName = indexName;
Expand All @@ -63,7 +65,8 @@ public SyncMaterializationContext(Plan mvPlan, Plan mvOriginalPlan, OlapTable ol

@Override
Plan doGenerateScanPlan(CascadesContext cascadesContext) {
return MaterializedViewUtils.generateMvScanPlan(olapTable, indexId, cascadesContext);
return MaterializedViewUtils.generateMvScanPlan(olapTable, indexId, olapTable.getPartitionIds(),
PreAggStatus.unset(), cascadesContext);
}

@Override
Expand Down Expand Up @@ -96,13 +99,29 @@ String getStringInfo() {
@Override
Optional<Pair<Id, Statistics>> getPlanStatistics(CascadesContext cascadesContext) {
RelationId relationId = null;
Optional<LogicalOlapScan> scanObj = this.getScanPlan().collectFirst(LogicalOlapScan.class::isInstance);
Optional<LogicalOlapScan> scanObj = this.getScanPlan(null)
.collectFirst(LogicalOlapScan.class::isInstance);
if (scanObj.isPresent()) {
relationId = scanObj.get().getRelationId();
}
return Optional.of(Pair.of(relationId, normalizeStatisticsColumnExpression(statistics)));
}

@Override
public Plan getScanPlan(StructInfo queryStructInfo) {
if (queryStructInfo == null) {
return scanPlan;
}
if (queryStructInfo.getRelations().size() == 1
&& queryStructInfo.getRelations().get(0) instanceof LogicalOlapScan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refer to org.apache.doris.nereids.rules.rewrite.DeferMaterializeTopNResult, maybe relation could be LogicalDeferMaterializeOlapScan? can we collect correct struct info?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we can collect the LogicalDeferMaterializeOlapScan when collect the struct info

private static class RelationCollector extends DefaultPlanVisitor<Void, List<CatalogRelation>> {
        @Override
        public Void visit(Plan plan, List<CatalogRelation> collectedRelations) {
            if (plan instanceof CatalogRelation) {
                collectedRelations.add((CatalogRelation) plan);
            }
            return super.visit(plan, collectedRelations);
        }
    }

&& !((LogicalOlapScan) queryStructInfo.getRelations().get(0)).getSelectedPartitionIds().isEmpty()
&& scanPlan instanceof LogicalOlapScan) {
return ((LogicalOlapScan) scanPlan).withSelectedPartitionIds(
((LogicalOlapScan) queryStructInfo.getRelations().get(0)).getSelectedPartitionIds());
}
return scanPlan;
}

/**
* Calc the relation is chosen finally or not
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public boolean isMVPartitionValid(MTMV mtmv, ConnectContext ctx, boolean isMVPar
.analyze()
.rewrite();
// scan plan output will be refreshed after mv rewrite successfully, so need tmp store
Set<Slot> materializationScanOutput = c1.getMaterializationContexts().get(0).getScanPlan().getOutputSet();
Set<Slot> materializationScanOutput = c1.getMaterializationContexts().get(0)
.getScanPlan(null).getOutputSet();
tmpPlanChecker
.optimize()
.printlnBestPlanTree();
Expand Down
43 changes: 43 additions & 0 deletions regression-test/data/mv_p0/partition_prune/partition_prune.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !query1_before --
aa bc 2024-07-03T01:15:30 1
ad be 2024-07-03T07:06:30 1
af bf 2024-07-04T10:01:30 1
ag bc 2024-07-04T10:01:35 1
as bd 2024-07-03T01:15:30 1

-- !query1_after --
aa bc 2024-07-03T01:15:30 1
ad be 2024-07-03T07:06:30 1
af bf 2024-07-04T10:01:30 1
ag bc 2024-07-04T10:01:35 1
as bd 2024-07-03T01:15:30 1

-- !query2_before --
2024-07-03T01:00 aa bc
2024-07-03T06:00 as bd
2024-07-03T07:00 ad be
2024-07-04T10:00 af bf
2024-07-04T12:00 ag bc

-- !query2_after --
2024-07-03T01:00 aa bc
2024-07-03T06:00 as bd
2024-07-03T07:00 ad be
2024-07-04T10:00 af bf
2024-07-04T12:00 ag bc

-- !query3_before --
aa bc 2024-07-03T01:00 2.1
ad be 2024-07-03T07:00 3.1
af bf 2024-07-04T10:00 4.1
ag bc 2024-07-04T12:00 5.1
as bd 2024-07-03T06:00 1.1

-- !query3_after --
aa bc 2024-07-03T01:00 2.1
ad be 2024-07-03T07:00 3.1
af bf 2024-07-04T10:00 4.1
ag bc 2024-07-04T12:00 5.1
as bd 2024-07-03T06:00 1.1

4 changes: 2 additions & 2 deletions regression-test/suites/auth_p0/test_select_column_auth.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ suite("test_select_column_auth","p0,auth") {
def validCluster = clusters[0][0]
sql """GRANT USAGE_PRIV ON CLUSTER ${validCluster} TO ${user}""";
}

sql """create database ${dbName}"""
sql("""use ${dbName}""")
sql """
CREATE TABLE IF NOT EXISTS ${dbName}.`${tableName}` (
id BIGINT,
Expand All @@ -55,7 +55,7 @@ suite("test_select_column_auth","p0,auth") {
sql """create view ${dbName}.${mv_name} as select * from ${dbName}.${tableName};"""
sql """alter table ${dbName}.${tableName} add rollup ${rollup_name}(username)"""
sleep(5 * 1000)
sql """create materialized view ${mtmv_name} as select username from ${dbName}.${tableName}"""
createMV("""create materialized view ${mtmv_name} as select username from ${dbName}.${tableName}""")
sleep(5 * 1000)
sql """CREATE MATERIALIZED VIEW ${dbName}.${mtmv_name}
BUILD IMMEDIATE REFRESH AUTO ON MANUAL
Expand Down
Loading
Loading