Skip to content

Commit 2b1aa05

Browse files
authored
pick some pr from to branch21 #38115 #38008 #37929 (#38940)
## Proposed changes pr: #38115 commitId: 2b29288 pr: #38008 commitId: c6b924d pr: #37929 commitId: d44fcdc
1 parent 2543b56 commit 2b1aa05

File tree

45 files changed

+2841
-149
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2841
-149
lines changed

fe/fe-core/src/main/java/org/apache/doris/analysis/CreateMaterializedViewStmt.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,9 @@ public void analyzeSelectClause(Analyzer analyzer) throws AnalysisException {
273273

274274
Expr selectListItemExpr = selectListItem.getExpr();
275275
selectListItemExpr.setDisableTableName(true);
276-
if (!(selectListItemExpr instanceof SlotRef) && !(selectListItemExpr instanceof FunctionCallExpr)
277-
&& !(selectListItemExpr instanceof ArithmeticExpr)) {
276+
Expr realItem = selectListItemExpr.unwrapExpr(false);
277+
if (!(realItem instanceof SlotRef) && !(realItem instanceof FunctionCallExpr)
278+
&& !(realItem instanceof ArithmeticExpr)) {
278279
throw new AnalysisException("The materialized view only support the single column or function expr. "
279280
+ "Error column: " + selectListItemExpr.toSql());
280281
}

fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVRelationManager.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.Map;
4848
import java.util.Objects;
4949
import java.util.Set;
50+
import java.util.function.BiPredicate;
5051

5152
/**
5253
* when do some operation, do something about cache
@@ -76,13 +77,17 @@ public Set<BaseTableInfo> getMtmvsByBaseTableOneLevel(BaseTableInfo table) {
7677
* @param ctx
7778
* @return
7879
*/
79-
public Set<MTMV> getAvailableMTMVs(List<BaseTableInfo> tableInfos, ConnectContext ctx) {
80+
public Set<MTMV> getAvailableMTMVs(List<BaseTableInfo> tableInfos, ConnectContext ctx,
81+
boolean forceConsistent, BiPredicate<ConnectContext, MTMV> predicate) {
8082
Set<MTMV> res = Sets.newLinkedHashSet();
8183
Set<BaseTableInfo> mvInfos = getMTMVInfos(tableInfos);
8284
for (BaseTableInfo tableInfo : mvInfos) {
8385
try {
8486
MTMV mtmv = (MTMV) MTMVUtil.getTable(tableInfo);
85-
if (isMVPartitionValid(mtmv, ctx)) {
87+
if (predicate.test(ctx, mtmv)) {
88+
continue;
89+
}
90+
if (isMVPartitionValid(mtmv, ctx, forceConsistent)) {
8691
res.add(mtmv);
8792
}
8893
} catch (AnalysisException e) {
@@ -94,9 +99,10 @@ public Set<MTMV> getAvailableMTMVs(List<BaseTableInfo> tableInfos, ConnectContex
9499
}
95100

96101
@VisibleForTesting
97-
public boolean isMVPartitionValid(MTMV mtmv, ConnectContext ctx) {
102+
public boolean isMVPartitionValid(MTMV mtmv, ConnectContext ctx, boolean forceConsistent) {
103+
long currentTimeMillis = System.currentTimeMillis();
98104
return !CollectionUtils
99-
.isEmpty(MTMVRewriteUtil.getMTMVCanRewritePartitions(mtmv, ctx, System.currentTimeMillis()));
105+
.isEmpty(MTMVRewriteUtil.getMTMVCanRewritePartitions(mtmv, ctx, currentTimeMillis, forceConsistent));
100106
}
101107

102108
private Set<BaseTableInfo> getMTMVInfos(List<BaseTableInfo> tableInfos) {

fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVRewriteUtil.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,9 @@ public class MTMVRewriteUtil {
4545
* @return
4646
*/
4747
public static Collection<Partition> getMTMVCanRewritePartitions(MTMV mtmv, ConnectContext ctx,
48-
long currentTimeMills) {
48+
long currentTimeMills, boolean forceConsistent) {
4949
List<Partition> res = Lists.newArrayList();
5050
Collection<Partition> allPartitions = mtmv.getPartitions();
51-
// check session variable if enable rewrite
52-
if (!ctx.getSessionVariable().isEnableMaterializedViewRewrite()) {
53-
return res;
54-
}
55-
if (MTMVUtil.mtmvContainsExternalTable(mtmv) && !ctx.getSessionVariable()
56-
.isMaterializedViewRewriteEnableContainExternalTable()) {
57-
return res;
58-
}
59-
6051
MTMVRelation mtmvRelation = mtmv.getRelation();
6152
if (mtmvRelation == null) {
6253
return res;
@@ -71,7 +62,7 @@ public static Collection<Partition> getMTMVCanRewritePartitions(MTMV mtmv, Conne
7162
long gracePeriodMills = mtmv.getGracePeriod();
7263
for (Partition partition : allPartitions) {
7364
if (gracePeriodMills > 0 && currentTimeMills <= (partition.getVisibleVersionTime()
74-
+ gracePeriodMills)) {
65+
+ gracePeriodMills) && !forceConsistent) {
7566
res.add(partition);
7667
continue;
7768
}

fe/fe-core/src/main/java/org/apache/doris/nereids/CascadesContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public class CascadesContext implements ScheduleContext {
122122
private final Optional<CTEId> currentTree;
123123
private final Optional<CascadesContext> parent;
124124

125-
private final List<MaterializationContext> materializationContexts;
125+
private final Set<MaterializationContext> materializationContexts;
126126
private boolean isLeadingJoin = false;
127127

128128
private boolean isLeadingDisableJoinReorder = false;
@@ -160,7 +160,7 @@ private CascadesContext(Optional<CascadesContext> parent, Optional<CTEId> curren
160160
this.currentJobContext = new JobContext(this, requireProperties, Double.MAX_VALUE);
161161
this.subqueryExprIsAnalyzed = new HashMap<>();
162162
this.runtimeFilterContext = new RuntimeFilterContext(getConnectContext().getSessionVariable());
163-
this.materializationContexts = new ArrayList<>();
163+
this.materializationContexts = new HashSet<>();
164164
if (statementContext.getConnectContext() != null) {
165165
ConnectContext connectContext = statementContext.getConnectContext();
166166
SessionVariable sessionVariable = connectContext.getSessionVariable();

fe/fe-core/src/main/java/org/apache/doris/nereids/NereidsPlanner.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ public class NereidsPlanner extends Planner {
9292
// The cost of optimized plan
9393
private double cost = 0;
9494
private LogicalPlanAdapter logicalPlanAdapter;
95-
private List<PlannerHook> hooks = new ArrayList<>();
9695

9796
public NereidsPlanner(StatementContext statementContext) {
9897
this.statementContext = statementContext;
@@ -274,7 +273,7 @@ private void analyze(boolean showPlanProcess) {
274273
LOG.debug("Start analyze plan");
275274
}
276275
keepOrShowPlanProcess(showPlanProcess, () -> cascadesContext.newAnalyzer().analyze());
277-
getHooks().forEach(hook -> hook.afterAnalyze(this));
276+
this.statementContext.getPlannerHooks().forEach(hook -> hook.afterAnalyze(this));
278277
NereidsTracer.logImportantTime("EndAnalyzePlan");
279278
if (LOG.isDebugEnabled()) {
280279
LOG.debug("End analyze plan");
@@ -640,14 +639,6 @@ public LogicalPlanAdapter getLogicalPlanAdapter() {
640639
return logicalPlanAdapter;
641640
}
642641

643-
public List<PlannerHook> getHooks() {
644-
return hooks;
645-
}
646-
647-
public void addHook(PlannerHook hook) {
648-
this.hooks.add(hook);
649-
}
650-
651642
private String getTimeMetricString(Function<SummaryProfile, String> profileSupplier) {
652643
return getProfile(summaryProfile -> {
653644
String metricString = profileSupplier.apply(summaryProfile);

fe/fe-core/src/main/java/org/apache/doris/nereids/StatementContext.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ public class StatementContext implements Closeable {
163163

164164
private FormatOptions formatOptions = FormatOptions.getDefault();
165165

166+
private List<PlannerHook> plannerHooks = new ArrayList<>();
167+
166168
public StatementContext() {
167169
this(ConnectContext.get(), null, 0);
168170
}
@@ -488,6 +490,14 @@ public FormatOptions getFormatOptions() {
488490
return formatOptions;
489491
}
490492

493+
public List<PlannerHook> getPlannerHooks() {
494+
return plannerHooks;
495+
}
496+
497+
public void addPlannerHook(PlannerHook plannerHook) {
498+
this.plannerHooks.add(plannerHook);
499+
}
500+
491501
private static class CloseableResource implements Closeable {
492502
public final String resourceName;
493503
public final String threadName;

fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/cascades/OptimizeGroupExpressionJob.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.doris.nereids.jobs.JobType;
2323
import org.apache.doris.nereids.memo.GroupExpression;
2424
import org.apache.doris.nereids.rules.Rule;
25-
import org.apache.doris.qe.ConnectContext;
2625

2726
import com.google.common.collect.ImmutableList;
2827

@@ -102,10 +101,6 @@ private List<Rule> getJoinRules() {
102101
}
103102

104103
private List<Rule> getMvRules() {
105-
ConnectContext connectContext = context.getCascadesContext().getConnectContext();
106-
if (connectContext.getSessionVariable().isEnableMaterializedViewRewrite()) {
107-
return getRuleSet().getMaterializedViewRules();
108-
}
109-
return ImmutableList.of();
104+
return getRuleSet().getMaterializedViewRules();
110105
}
111106
}

fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Analyzer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apache.doris.nereids.CascadesContext;
2121
import org.apache.doris.nereids.jobs.rewrite.RewriteJob;
22+
import org.apache.doris.nereids.rules.analysis.AddInitMaterializationHook;
2223
import org.apache.doris.nereids.rules.analysis.AdjustAggregateNullableForEmptySet;
2324
import org.apache.doris.nereids.rules.analysis.AnalyzeCTE;
2425
import org.apache.doris.nereids.rules.analysis.BindExpression;
@@ -123,6 +124,7 @@ private static List<RewriteJob> buildAnalyzerJobs(Optional<CustomTableResolver>
123124
bottomUp(new BindExpression()),
124125
topDown(new BindSink()),
125126
bottomUp(new CheckAfterBind()),
127+
bottomUp(new AddInitMaterializationHook()),
126128
bottomUp(
127129
new ProjectToGlobalAggregate(),
128130
// this rule check's the logicalProject node's isDistinct property

fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public enum RuleType {
3333
BINDING_INSERT_HIVE_TABLE(RuleTypeClass.REWRITE),
3434
BINDING_INSERT_ICEBERG_TABLE(RuleTypeClass.REWRITE),
3535
BINDING_INSERT_TARGET_TABLE(RuleTypeClass.REWRITE),
36+
INIT_MATERIALIZATION_HOOK_FOR_FILE_SINK(RuleTypeClass.REWRITE),
37+
INIT_MATERIALIZATION_HOOK_FOR_TABLE_SINK(RuleTypeClass.REWRITE),
3638
BINDING_INSERT_FILE(RuleTypeClass.REWRITE),
3739
BINDING_ONE_ROW_RELATION_SLOT(RuleTypeClass.REWRITE),
3840
BINDING_RELATION(RuleTypeClass.REWRITE),
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.rules.analysis;
19+
20+
import org.apache.doris.nereids.rules.Rule;
21+
import org.apache.doris.nereids.rules.RuleType;
22+
import org.apache.doris.nereids.rules.exploration.mv.InitConsistentMaterializationContextHook;
23+
import org.apache.doris.nereids.trees.plans.logical.LogicalTableSink;
24+
25+
import com.google.common.collect.ImmutableList;
26+
27+
import java.util.List;
28+
29+
/**
30+
* Add init materialization hook for table sink and file sink
31+
* */
32+
public class AddInitMaterializationHook implements AnalysisRuleFactory {
33+
34+
@Override
35+
public List<Rule> buildRules() {
36+
return ImmutableList.of(
37+
RuleType.INIT_MATERIALIZATION_HOOK_FOR_FILE_SINK.build(logicalFileSink()
38+
.thenApply(ctx -> {
39+
if (ctx.connectContext.getSessionVariable().isEnableDmlMaterializedViewRewrite()) {
40+
ctx.statementContext.addPlannerHook(InitConsistentMaterializationContextHook.INSTANCE);
41+
}
42+
return ctx.root;
43+
})),
44+
RuleType.INIT_MATERIALIZATION_HOOK_FOR_TABLE_SINK.build(
45+
any().when(LogicalTableSink.class::isInstance)
46+
.thenApply(ctx -> {
47+
if (ctx.connectContext.getSessionVariable().isEnableDmlMaterializedViewRewrite()) {
48+
ctx.statementContext.addPlannerHook(InitConsistentMaterializationContextHook.INSTANCE);
49+
}
50+
return ctx.root;
51+
}))
52+
);
53+
}
54+
}

0 commit comments

Comments
 (0)