Skip to content

Commit 65803b4

Browse files
committed
[fix](mtmv) Disable sql_limit variable when query rewrite by materialize view (apache#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 2752e77 commit 65803b4

File tree

7 files changed

+518
-0
lines changed

7 files changed

+518
-0
lines changed

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

+5
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

+12
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()) {
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+
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

+5
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,8 @@ class Suite implements GroovyInterceptable {
13471347
logger.info("status is not success")
13481348
}
13491349
Assert.assertEquals("SUCCESS", status)
1350+
logger.info("waitingMTMVTaskFinished analyze mv name is " + result.last().get(5))
1351+
sql "analyze table ${result.last().get(6)}.${mvName} with sync;"
13501352
}
13511353

13521354
void waitingMTMVTaskFinishedByMvNameAllowCancel(String mvName) {
@@ -1420,6 +1422,9 @@ class Suite implements GroovyInterceptable {
14201422
logger.info("status is not success")
14211423
}
14221424
Assert.assertEquals("SUCCESS", status)
1425+
// Need to analyze materialized view for cbo to choose the materialized view accurately
1426+
logger.info("waitingMTMVTaskFinished analyze mv name is " + result.last().get(5))
1427+
sql "analyze table ${result.last().get(6)}.${result.last().get(5)} with sync;"
14231428
}
14241429

14251430
void waitingMTMVTaskFinishedNotNeedSuccess(String jobName) {
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)