Skip to content

Commit 0fdb1ee

Browse files
authored
[fix](mtmv) Disable sql_limit variable when query rewrite by materialize view (#40106)
In BI or some other SQL client,it will set `sql_select_limit` or `default_order_by_limit` session variables automatically. This will make mistake when query rewrite by materialized view, Because after query rewrite by materialized view, the rewritten plan would be optimized by rules again which should add limit on SQL. This pr fix this.
1 parent 2745e04 commit 0fdb1ee

8 files changed

Lines changed: 540 additions & 7 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.doris.nereids.glue.LogicalPlanAdapter;
3131
import org.apache.doris.nereids.parser.NereidsParser;
3232
import org.apache.doris.nereids.properties.PhysicalProperties;
33+
import org.apache.doris.nereids.rules.RuleType;
3334
import org.apache.doris.nereids.trees.plans.Plan;
3435
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
3536
import org.apache.doris.nereids.trees.plans.commands.info.CreateMTMVInfo;
@@ -39,6 +40,7 @@
3940
import org.apache.doris.qe.ConnectContext;
4041
import org.apache.doris.qe.SessionVariable;
4142

43+
import com.google.common.collect.ImmutableSet;
4244
import com.google.common.collect.Sets;
4345

4446
import java.util.List;
@@ -55,6 +57,9 @@ public static ConnectContext createMTMVContext(MTMV mtmv) {
5557
ctx.getState().reset();
5658
ctx.setThreadLocalInfo();
5759
ctx.getSessionVariable().allowModifyMaterializedViewData = true;
60+
// Disable add default limit rule to avoid refresh data wrong
61+
ctx.getSessionVariable().setDisableNereidsRules(
62+
String.join(",", ImmutableSet.of(RuleType.ADD_DEFAULT_LIMIT.name())));
5863
Optional<String> workloadGroup = mtmv.getWorkloadGroup();
5964
if (workloadGroup.isPresent()) {
6065
ctx.getSessionVariable().setWorkloadGroup(workloadGroup.get());

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializedViewUtils.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.apache.doris.nereids.util.ExpressionUtils;
7474
import org.apache.doris.qe.ConnectContext;
7575
import org.apache.doris.qe.OriginStatement;
76+
import org.apache.doris.qe.SessionVariable;
7677

7778
import com.google.common.collect.HashMultimap;
7879
import com.google.common.collect.ImmutableList;
@@ -267,11 +268,22 @@ public static Plan rewriteByRules(
267268
CascadesContext rewrittenPlanContext = CascadesContext.initContext(
268269
cascadesContext.getStatementContext(), rewrittenPlan,
269270
cascadesContext.getCurrentJobContext().getRequiredProperties());
271+
// Tmp old disable rule variable
272+
Set<String> oldDisableRuleNames = rewrittenPlanContext.getStatementContext().getConnectContext()
273+
.getSessionVariable()
274+
.getDisableNereidsRuleNames();
275+
rewrittenPlanContext.getStatementContext().getConnectContext().getSessionVariable()
276+
.setDisableNereidsRules(String.join(",", ImmutableSet.of(RuleType.ADD_DEFAULT_LIMIT.name())));
277+
rewrittenPlanContext.getStatementContext().invalidCache(SessionVariable.DISABLE_NEREIDS_RULES);
270278
try {
271279
rewrittenPlanContext.getConnectContext().setSkipAuth(true);
272280
rewrittenPlan = planRewriter.apply(rewrittenPlanContext);
273281
} finally {
274282
rewrittenPlanContext.getConnectContext().setSkipAuth(false);
283+
// Recover old disable rules variable
284+
rewrittenPlanContext.getStatementContext().getConnectContext().getSessionVariable()
285+
.setDisableNereidsRules(String.join(",", oldDisableRuleNames));
286+
rewrittenPlanContext.getStatementContext().invalidCache(SessionVariable.DISABLE_NEREIDS_RULES);
275287
}
276288
Map<ExprId, Slot> exprIdToNewRewrittenSlot = Maps.newLinkedHashMap();
277289
for (Slot slot : rewrittenPlan.getOutput()) {

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalOlapScan.java‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.apache.commons.lang3.tuple.Pair;
4949
import org.json.JSONObject;
5050

51+
import java.util.ArrayList;
5152
import java.util.Arrays;
5253
import java.util.HashMap;
5354
import java.util.List;
@@ -502,7 +503,7 @@ AGGREGATE KEY (siteid,citycode,username)
502503
if (getTable() instanceof MTMV) {
503504
MTMV mtmv = (MTMV) getTable();
504505
MTMVCache cache = mtmv.getCache();
505-
if (cache == null) {
506+
if (cache == null || this.getSelectedIndexId() != this.getTable().getBaseIndexId()) {
506507
return;
507508
}
508509
Plan originalPlan = cache.getOriginalPlan();
@@ -528,7 +529,7 @@ public void computeUniform(DataTrait.Builder builder) {
528529
if (getTable() instanceof MTMV) {
529530
MTMV mtmv = (MTMV) getTable();
530531
MTMVCache cache = mtmv.getCache();
531-
if (cache == null) {
532+
if (cache == null || this.getSelectedIndexId() != this.getTable().getBaseIndexId()) {
532533
return;
533534
}
534535
Plan originalPlan = cache.getOriginalPlan();
@@ -542,7 +543,7 @@ public void computeEqualSet(DataTrait.Builder builder) {
542543
if (getTable() instanceof MTMV) {
543544
MTMV mtmv = (MTMV) getTable();
544545
MTMVCache cache = mtmv.getCache();
545-
if (cache == null) {
546+
if (cache == null || this.getSelectedIndexId() != this.getTable().getBaseIndexId()) {
546547
return;
547548
}
548549
Plan originalPlan = cache.getOriginalPlan();
@@ -556,7 +557,7 @@ public void computeFd(DataTrait.Builder builder) {
556557
if (getTable() instanceof MTMV) {
557558
MTMV mtmv = (MTMV) getTable();
558559
MTMVCache cache = mtmv.getCache();
559-
if (cache == null) {
560+
if (cache == null || this.getSelectedIndexId() != this.getTable().getBaseIndexId()) {
560561
return;
561562
}
562563
Plan originalPlan = cache.getOriginalPlan();
@@ -567,9 +568,23 @@ public void computeFd(DataTrait.Builder builder) {
567568

568569
Map<Slot, Slot> constructReplaceMap(MTMV mtmv) {
569570
Map<Slot, Slot> replaceMap = new HashMap<>();
570-
List<Slot> originOutputs = mtmv.getCache().getOriginalPlan().getOutput();
571-
for (int i = 0; i < getOutput().size(); i++) {
572-
replaceMap.put(originOutputs.get(i), getOutput().get(i));
571+
// Need remove invisible column, and then mapping them
572+
List<Slot> originOutputs = new ArrayList<>();
573+
for (Slot originSlot : mtmv.getCache().getOriginalPlan().getOutput()) {
574+
if (!(originSlot instanceof SlotReference) || (((SlotReference) originSlot).isVisible())) {
575+
originOutputs.add(originSlot);
576+
}
577+
}
578+
List<Slot> targetOutputs = new ArrayList<>();
579+
for (Slot targeSlot : getOutput()) {
580+
if (!(targeSlot instanceof SlotReference) || (((SlotReference) targeSlot).isVisible())) {
581+
targetOutputs.add(targeSlot);
582+
}
583+
}
584+
Preconditions.checkArgument(originOutputs.size() == targetOutputs.size(),
585+
"constructReplaceMap, the size of originOutputs and targetOutputs should be same");
586+
for (int i = 0; i < targetOutputs.size(); i++) {
587+
replaceMap.put(originOutputs.get(i), targetOutputs.get(i));
573588
}
574589
return replaceMap;
575590
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !query_mv_1 --
3+
1 1 o 10.50 2023-12-08 a b 1 yy
4+
1 1 o 10.50 2023-12-08 a b 1 yy
5+
1 1 o 10.50 2023-12-08 a b 1 yy
6+
1 1 o 9.50 2023-12-08 a b 1 yy
7+
2 1 o 11.50 2023-12-09 a b 1 yy
8+
2 1 o 11.50 2023-12-09 a b 1 yy
9+
2 1 o 11.50 2023-12-09 a b 1 yy
10+
3 1 o 12.50 2023-12-10 a b 1 yy
11+
3 1 o 12.50 2023-12-10 a b 1 yy
12+
3 1 o 12.50 2023-12-10 a b 1 yy
13+
3 1 o 33.50 2023-12-10 a b 1 yy
14+
4 2 o 43.20 2023-12-11 c d 2 mm
15+
4 2 o 43.20 2023-12-11 c d 2 mm
16+
4 2 o 43.20 2023-12-11 c d 2 mm
17+
5 2 o 1.20 2023-12-12 c d 2 mi
18+
5 2 o 56.20 2023-12-12 c d 2 mi
19+
5 2 o 56.20 2023-12-12 c d 2 mi
20+
5 2 o 56.20 2023-12-12 c d 2 mi
21+
22+
-- !query_mv_2 --
23+
1 1 o 10.50 2023-12-08 a b 1 yy
24+
1 1 o 10.50 2023-12-08 a b 1 yy
25+
1 1 o 10.50 2023-12-08 a b 1 yy
26+
1 1 o 9.50 2023-12-08 a b 1 yy
27+
2 1 o 11.50 2023-12-09 a b 1 yy
28+
2 1 o 11.50 2023-12-09 a b 1 yy
29+
2 1 o 11.50 2023-12-09 a b 1 yy
30+
3 1 o 12.50 2023-12-10 a b 1 yy
31+
3 1 o 12.50 2023-12-10 a b 1 yy
32+
3 1 o 12.50 2023-12-10 a b 1 yy
33+
3 1 o 33.50 2023-12-10 a b 1 yy
34+
4 2 o 43.20 2023-12-11 c d 2 mm
35+
4 2 o 43.20 2023-12-11 c d 2 mm
36+
4 2 o 43.20 2023-12-11 c d 2 mm
37+
5 2 o 1.20 2023-12-12 c d 2 mi
38+
5 2 o 56.20 2023-12-12 c d 2 mi
39+
5 2 o 56.20 2023-12-12 c d 2 mi
40+
5 2 o 56.20 2023-12-12 c d 2 mi
41+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !query1_0_before --
3+
4
4+
5+
-- !query1_0_after --
6+
4
7+
8+
-- !query2_0_before --
9+
4
10+
11+
-- !query2_0_after --
12+
4
13+
14+
-- !query3_0_before --
15+
4
16+
17+
-- !query3_0_after --
18+
4
19+

‎regression-test/framework/src/main/groovy/org/apache/doris/regression/suite/Suite.groovy‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,8 @@ class Suite implements GroovyInterceptable {
13291329
logger.info("status is not success")
13301330
}
13311331
Assert.assertEquals("SUCCESS", status)
1332+
logger.info("waitingMTMVTaskFinished analyze mv name is " + result.last().get(5))
1333+
sql "analyze table ${result.last().get(6)}.${mvName} with sync;"
13321334
}
13331335

13341336
void waitingMTMVTaskFinishedByMvNameAllowCancel(String mvName) {
@@ -1399,6 +1401,9 @@ class Suite implements GroovyInterceptable {
13991401
logger.info("status is not success")
14001402
}
14011403
Assert.assertEquals("SUCCESS", status)
1404+
// Need to analyze materialized view for cbo to choose the materialized view accurately
1405+
logger.info("waitingMTMVTaskFinished analyze mv name is " + result.last().get(5))
1406+
sql "analyze table ${result.last().get(6)}.${result.last().get(5)} with sync;"
14021407
}
14031408

14041409
void waitingMTMVTaskFinishedNotNeedSuccess(String jobName) {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package limit
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
19+
suite("refresh_with_sql_limit") {
20+
String db = context.config.getDbNameByFile(context.file)
21+
sql "use ${db}"
22+
sql "set runtime_filter_mode=OFF";
23+
sql "SET ignore_shape_nodes='PhysicalDistribute,PhysicalProject'"
24+
25+
sql """
26+
drop table if exists orders;
27+
"""
28+
29+
sql """
30+
CREATE TABLE IF NOT EXISTS orders (
31+
o_orderkey INTEGER NOT NULL,
32+
o_custkey INTEGER NOT NULL,
33+
o_orderstatus CHAR(1) NOT NULL,
34+
o_totalprice DECIMALV3(15,2) NOT NULL,
35+
o_orderdate DATE NOT NULL,
36+
o_orderpriority CHAR(15) NOT NULL,
37+
o_clerk CHAR(15) NOT NULL,
38+
o_shippriority INTEGER NOT NULL,
39+
O_COMMENT VARCHAR(79) NOT NULL
40+
)
41+
DUPLICATE KEY(o_orderkey, o_custkey)
42+
PARTITION BY RANGE(o_orderdate) (
43+
PARTITION `day_2` VALUES LESS THAN ('2023-12-9'),
44+
PARTITION `day_3` VALUES LESS THAN ("2023-12-11"),
45+
PARTITION `day_4` VALUES LESS THAN ("2023-12-30")
46+
)
47+
DISTRIBUTED BY HASH(o_orderkey) BUCKETS 3
48+
PROPERTIES (
49+
"replication_num" = "1"
50+
);
51+
"""
52+
53+
sql """
54+
insert into orders values
55+
(1, 1, 'o', 9.5, '2023-12-08', 'a', 'b', 1, 'yy'),
56+
(1, 1, 'o', 10.5, '2023-12-08', 'a', 'b', 1, 'yy'),
57+
(1, 1, 'o', 10.5, '2023-12-08', 'a', 'b', 1, 'yy'),
58+
(1, 1, 'o', 10.5, '2023-12-08', 'a', 'b', 1, 'yy'),
59+
(2, 1, 'o', 11.5, '2023-12-09', 'a', 'b', 1, 'yy'),
60+
(2, 1, 'o', 11.5, '2023-12-09', 'a', 'b', 1, 'yy'),
61+
(2, 1, 'o', 11.5, '2023-12-09', 'a', 'b', 1, 'yy'),
62+
(3, 1, 'o', 12.5, '2023-12-10', 'a', 'b', 1, 'yy'),
63+
(3, 1, 'o', 12.5, '2023-12-10', 'a', 'b', 1, 'yy'),
64+
(3, 1, 'o', 12.5, '2023-12-10', 'a', 'b', 1, 'yy'),
65+
(3, 1, 'o', 33.5, '2023-12-10', 'a', 'b', 1, 'yy'),
66+
(4, 2, 'o', 43.2, '2023-12-11', 'c','d',2, 'mm'),
67+
(4, 2, 'o', 43.2, '2023-12-11', 'c','d',2, 'mm'),
68+
(4, 2, 'o', 43.2, '2023-12-11', 'c','d',2, 'mm'),
69+
(5, 2, 'o', 56.2, '2023-12-12', 'c','d',2, 'mi'),
70+
(5, 2, 'o', 56.2, '2023-12-12', 'c','d',2, 'mi'),
71+
(5, 2, 'o', 56.2, '2023-12-12', 'c','d',2, 'mi'),
72+
(5, 2, 'o', 1.2, '2023-12-12', 'c','d',2, 'mi');
73+
"""
74+
sql """analyze table orders with sync"""
75+
76+
77+
sql """DROP MATERIALIZED VIEW IF EXISTS mv_1"""
78+
sql """set default_order_by_limit = 2;"""
79+
sql """set sql_select_limit = 2;"""
80+
sql"""
81+
CREATE MATERIALIZED VIEW mv_1
82+
BUILD DEFERRED REFRESH COMPLETE ON MANUAL
83+
DISTRIBUTED BY RANDOM BUCKETS 2
84+
PROPERTIES ('replication_num' = '1')
85+
AS select * from orders;
86+
"""
87+
sql """refresh materialized view mv_1 auto;"""
88+
def job_name = getJobName(db, "mv_1");
89+
waitingMTMVTaskFinished(job_name)
90+
91+
// Reset and test mv data is right or not
92+
sql """set default_order_by_limit = -1;"""
93+
sql """set sql_select_limit = -1;"""
94+
order_qt_query_mv_1 "select * from mv_1"
95+
sql """ DROP MATERIALIZED VIEW IF EXISTS mv_1"""
96+
97+
98+
sql """DROP MATERIALIZED VIEW IF EXISTS mv_2"""
99+
sql """set default_order_by_limit = 2"""
100+
sql """set sql_select_limit = 2"""
101+
sql"""
102+
CREATE MATERIALIZED VIEW mv_2
103+
BUILD IMMEDIATE REFRESH COMPLETE ON MANUAL
104+
DISTRIBUTED BY RANDOM BUCKETS 2
105+
PROPERTIES ('replication_num' = '1')
106+
AS select * from orders;
107+
"""
108+
waitingMTMVTaskFinished(getJobName(db, "mv_2"))
109+
110+
// Reset and test mv data is right or not
111+
sql """set default_order_by_limit = -1;"""
112+
sql """set sql_select_limit = -1;"""
113+
order_qt_query_mv_2 "select * from mv_2"
114+
sql """ DROP MATERIALIZED VIEW IF EXISTS mv_2"""
115+
}

0 commit comments

Comments
 (0)