Skip to content

Commit f1b6e16

Browse files
authored
[test](mtmv) Add ut test for session variable guard when generate mtmv cache (#58863)
mtmvCache is globally reused, and each query shares the same instance of mtmvCache. When generating the cache, it should not include any attributes that are specific to an individual query. current code has no problem, just add some ut Related PR: #58031
1 parent 4c215b1 commit f1b6e16

File tree

8 files changed

+173
-10
lines changed

8 files changed

+173
-10
lines changed

fe/fe-core/src/test/java/org/apache/doris/nereids/mv/IdStatisticsMapTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ public boolean canBeCandidate() {
6767
return true;
6868
}
6969
};
70+
71+
new MockUp<MTMV>() {
72+
@Mock
73+
public boolean canBeCandidate() {
74+
return true;
75+
}
76+
};
77+
connectContext.getState().setIsQuery(true);
78+
7079
connectContext.getSessionVariable().enableMaterializedViewRewrite = true;
7180
connectContext.getSessionVariable().enableMaterializedViewNestRewrite = true;
7281
connectContext.getSessionVariable().setPreMaterializedViewRewriteStrategy(PreRewriteStrategy.NOT_IN_RBO.name());
@@ -124,6 +133,8 @@ public boolean canBeCandidate() {
124133
return true;
125134
}
126135
};
136+
connectContext.getState().setIsQuery(true);
137+
127138
connectContext.getSessionVariable().enableMaterializedViewRewrite = true;
128139
connectContext.getSessionVariable().enableMaterializedViewNestRewrite = true;
129140
connectContext.getSessionVariable().setPreMaterializedViewRewriteStrategy(
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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.mv;
19+
20+
import org.apache.doris.catalog.MTMV;
21+
import org.apache.doris.mtmv.MTMVCache;
22+
import org.apache.doris.mtmv.MTMVRelationManager;
23+
import org.apache.doris.nereids.CascadesContext;
24+
import org.apache.doris.nereids.rules.exploration.mv.AsyncMaterializationContext;
25+
import org.apache.doris.nereids.rules.exploration.mv.MaterializationContext;
26+
import org.apache.doris.nereids.sqltest.SqlTestBase;
27+
import org.apache.doris.nereids.trees.expressions.SessionVarGuardExpr;
28+
import org.apache.doris.nereids.trees.plans.Plan;
29+
import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
30+
import org.apache.doris.nereids.util.PlanChecker;
31+
import org.apache.doris.qe.ConnectContext;
32+
import org.apache.doris.qe.SessionVariable;
33+
import org.apache.doris.qe.SqlModeHelper;
34+
35+
import mockit.Mock;
36+
import mockit.MockUp;
37+
import org.junit.jupiter.api.Assertions;
38+
import org.junit.jupiter.api.Test;
39+
40+
import java.util.BitSet;
41+
import java.util.List;
42+
import java.util.Map;
43+
import java.util.Optional;
44+
import java.util.Set;
45+
46+
/**
47+
* Relevant test case about mtmv cache.
48+
*/
49+
public class MTMVCacheTest extends SqlTestBase {
50+
51+
@Test
52+
void testMTMVCacheIsCorrect() throws Exception {
53+
connectContext.getSessionVariable().setDisableNereidsRules("PRUNE_EMPTY_PARTITION");
54+
BitSet disableNereidsRules = connectContext.getSessionVariable().getDisableNereidsRules();
55+
new MockUp<SessionVariable>() {
56+
@Mock
57+
public BitSet getDisableNereidsRules() {
58+
return disableNereidsRules;
59+
}
60+
};
61+
new MockUp<MTMVRelationManager>() {
62+
@Mock
63+
public boolean isMVPartitionValid(MTMV mtmv, ConnectContext ctx, boolean forceConsistent,
64+
Map<List<String>, Set<String>> queryUsedPartitions) {
65+
return true;
66+
}
67+
};
68+
69+
new MockUp<MTMV>() {
70+
@Mock
71+
public boolean canBeCandidate() {
72+
return true;
73+
}
74+
};
75+
connectContext.getState().setIsQuery(true);
76+
77+
connectContext.getSessionVariable().enableMaterializedViewRewrite = true;
78+
connectContext.getSessionVariable().enableMaterializedViewNestRewrite = true;
79+
createMvByNereids("create materialized view mv1 BUILD IMMEDIATE REFRESH COMPLETE ON MANUAL\n"
80+
+ " DISTRIBUTED BY RANDOM BUCKETS 1\n"
81+
+ " PROPERTIES ('replication_num' = '1') \n"
82+
+ " as select T1.id, sum(score) from T1 group by T1.id;");
83+
CascadesContext c1 = createCascadesContext(
84+
"select T1.id, sum(score) from T1 group by T1.id;",
85+
connectContext
86+
);
87+
PlanChecker.from(c1)
88+
.analyze()
89+
.rewrite()
90+
.optimize()
91+
.printlnBestPlanTree();
92+
List<MaterializationContext> normalMaterializationContexts = c1.getMaterializationContexts();
93+
Assertions.assertEquals(1, normalMaterializationContexts.size());
94+
95+
MTMV mtmv = ((AsyncMaterializationContext) normalMaterializationContexts.get(0)).getMtmv();
96+
MTMVCache cacheWithoutGuard = mtmv.getOrGenerateCache(connectContext);
97+
98+
Optional<LogicalAggregate<? extends Plan>> aggregate = cacheWithoutGuard.getAllRulesRewrittenPlanAndStructInfo().key()
99+
.collectFirst(LogicalAggregate.class::isInstance);
100+
Assertions.assertTrue(aggregate.isPresent());
101+
// should not contain SessionVarGuardExpr
102+
Assertions.assertTrue(aggregate.get().getOutputExpressions().stream()
103+
.noneMatch(expr -> expr.containsType(SessionVarGuardExpr.class)));
104+
105+
// set guard check session var
106+
connectContext.getSessionVariable().setSqlMode(SqlModeHelper.MODE_NO_UNSIGNED_SUBTRACTION);
107+
CascadesContext c2 = createCascadesContext(
108+
"select T1.id, sum(score) from T1 group by T1.id;",
109+
connectContext
110+
);
111+
connectContext.getState().setIsQuery(true);
112+
PlanChecker.from(c2)
113+
.analyze()
114+
.rewrite()
115+
.optimize()
116+
.printlnBestPlanTree();
117+
118+
List<MaterializationContext> sessionChangedMaterializationContexts = c2.getMaterializationContexts();
119+
Assertions.assertEquals(1, sessionChangedMaterializationContexts.size());
120+
121+
MTMV mvWithGuard = ((AsyncMaterializationContext) sessionChangedMaterializationContexts.get(0)).getMtmv();
122+
MTMVCache cacheWithGuard = mvWithGuard.getOrGenerateCache(connectContext);
123+
124+
aggregate = cacheWithGuard.getAllRulesRewrittenPlanAndStructInfo().key()
125+
.collectFirst(LogicalAggregate.class::isInstance);
126+
Assertions.assertTrue(aggregate.isPresent());
127+
// should contain SessionVarGuardExpr
128+
Assertions.assertTrue(aggregate.get().getOutputExpressions().stream()
129+
.anyMatch(expr -> expr.containsType(SessionVarGuardExpr.class)));
130+
dropMvByNereids("drop materialized view mv1");
131+
}
132+
}

fe/fe-core/src/test/java/org/apache/doris/nereids/mv/MtmvCacheNewConnectContextTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.apache.doris.nereids.mv;
1919

2020
import org.apache.doris.catalog.MTMV;
21-
import org.apache.doris.mtmv.BaseTableInfo;
2221
import org.apache.doris.mtmv.MTMVRelationManager;
2322
import org.apache.doris.nereids.CascadesContext;
2423
import org.apache.doris.nereids.sqltest.SqlTestBase;
@@ -32,6 +31,7 @@
3231
import org.junit.jupiter.api.Test;
3332

3433
import java.util.BitSet;
34+
import java.util.List;
3535
import java.util.Map;
3636
import java.util.Set;
3737

@@ -55,11 +55,18 @@ public BitSet getDisableNereidsRules() {
5555
};
5656
new MockUp<MTMVRelationManager>() {
5757
@Mock
58-
public boolean isMVPartitionValid(MTMV mtmv, ConnectContext ctx, boolean isMVPartitionValid,
59-
Map<BaseTableInfo, Set<String>> queryUsedRelatedTablePartitionsMap) {
58+
public boolean isMVPartitionValid(MTMV mtmv, ConnectContext ctx, boolean forceConsistent,
59+
Map<List<String>, Set<String>> queryUsedPartitions) {
6060
return true;
6161
}
6262
};
63+
new MockUp<MTMV>() {
64+
@Mock
65+
public boolean canBeCandidate() {
66+
return true;
67+
}
68+
};
69+
connectContext.getState().setIsQuery(true);
6370
connectContext.getSessionVariable().enableMaterializedViewRewrite = true;
6471
connectContext.getSessionVariable().enableMaterializedViewNestRewrite = true;
6572

fe/fe-core/src/test/java/org/apache/doris/nereids/mv/MvTableIdIsLongTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public boolean canBeCandidate() {
6363
return true;
6464
}
6565
};
66+
connectContext.getState().setIsQuery(true);
6667
connectContext.getSessionVariable().enableMaterializedViewRewrite = true;
6768
connectContext.getSessionVariable().enableMaterializedViewNestRewrite = true;
6869
createMvByNereids("create materialized view mv1 BUILD IMMEDIATE REFRESH COMPLETE ON MANUAL\n"

fe/fe-core/src/test/java/org/apache/doris/nereids/mv/OptimizeGetAvailableMvsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.doris.nereids.mv;
1919

2020
import org.apache.doris.catalog.DistributionInfo;
21+
import org.apache.doris.catalog.MTMV;
2122
import org.apache.doris.catalog.MaterializedIndex;
2223
import org.apache.doris.catalog.MaterializedIndex.IndexState;
2324
import org.apache.doris.catalog.OlapTable;
@@ -112,6 +113,14 @@ public List<Long> getSelectedPartitionIds() {
112113
}
113114
};
114115

116+
new MockUp<MTMV>() {
117+
@Mock
118+
public boolean canBeCandidate() {
119+
return true;
120+
}
121+
};
122+
connectContext.getState().setIsQuery(true);
123+
115124
connectContext.getSessionVariable().enableMaterializedViewRewrite = true;
116125
connectContext.getSessionVariable().enableMaterializedViewNestRewrite = true;
117126
createMvByNereids("create materialized view mv1 "

fe/fe-core/src/test/java/org/apache/doris/nereids/mv/PointQueryShouldNotMvRewriteTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
import java.util.BitSet;
3535
import java.util.HashSet;
36+
import java.util.List;
37+
import java.util.Map;
3638
import java.util.Set;
3739

3840
/**
@@ -53,7 +55,7 @@ public BitSet getDisableNereidsRules() {
5355
new MockUp<MTMVRelationManager>() {
5456
@Mock
5557
public boolean isMVPartitionValid(MTMV mtmv, ConnectContext ctx, boolean forceConsistent,
56-
Set<String> relatedPartitions) {
58+
Map<List<String>, Set<String>> queryUsedPartitions) {
5759
return true;
5860
}
5961
};
@@ -63,6 +65,7 @@ public boolean canBeCandidate() {
6365
return true;
6466
}
6567
};
68+
connectContext.getState().setIsQuery(true);
6669
connectContext.getSessionVariable().enableMaterializedViewRewrite = true;
6770
connectContext.getSessionVariable().enableMaterializedViewNestRewrite = true;
6871

regression-test/suites/nereids_rules_p0/mv/union_all_compensate/union_all_compensate.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ suite("union_all_compensate") {
125125
sql """analyze table test_table1 with sync"""
126126
sql """analyze table test_table2 with sync"""
127127

128+
sql """alter table test_table1 modify column num set stats ('row_count'='20');"""
129+
sql """alter table test_table2 modify column num set stats ('row_count'='16');"""
130+
128131
// Aggregate, scalar aggregate, should not compensate union all
129132
sql """ DROP MATERIALIZED VIEW IF EXISTS test_agg_mv"""
130133
sql """
@@ -194,9 +197,6 @@ suite("union_all_compensate") {
194197
sql "set enable_sql_cache=true"
195198
order_qt_query1_1_after_use_sql_cache "${query1_0}"
196199

197-
sql """alter table test_table1 modify column num set stats ('row_count'='20');"""
198-
sql """alter table test_table2 modify column num set stats ('row_count'='16');"""
199-
200200

201201
// Aggregate, if query group by expression doesn't use the partition column, but the invalid partition is in the
202202
// grace_period, should not compensate union all, but should rewritten successfully

regression-test/suites/nereids_rules_p0/mv/union_rewrite/partition_curd_union_rewrite.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ suite ("partition_curd_union_rewrite") {
7878
);
7979
"""
8080

81-
sql """alter table orders modify column o_comment set stats ('row_count'='3');"""
82-
sql """alter table lineitem modify column l_comment set stats ('row_count'='3');"""
83-
8481
sql"""
8582
insert into orders values
8683
(1, 1, 'ok', 99.5, '2023-10-17', 'a', 'b', 1, 'yy'),
@@ -107,6 +104,9 @@ suite ("partition_curd_union_rewrite") {
107104
(3, 2, 3, 6, 7.5, 8.5, 9.5, 10.5, 'k', 'o', '2023-10-19', '2023-10-19', '2023-10-19', 'c', 'd', 'xxxxxxxxx');
108105
"""
109106

107+
sql """alter table orders modify column o_comment set stats ('row_count'='9');"""
108+
sql """alter table lineitem modify column l_comment set stats ('row_count'='9');"""
109+
110110
sql """analyze table orders with sync;"""
111111
sql """analyze table lineitem with sync;"""
112112

0 commit comments

Comments
 (0)