Skip to content

Commit 4ab4f71

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 d720563 commit 4ab4f71

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;
@@ -57,6 +59,9 @@ public static ConnectContext createMTMVContext(MTMV mtmv) {
5759
ctx.getSessionVariable().enableFallbackToOriginalPlanner = false;
5860
ctx.getSessionVariable().enableNereidsDML = true;
5961
ctx.getSessionVariable().allowModifyMaterializedViewData = true;
62+
// Disable add default limit rule to avoid refresh data wrong
63+
ctx.getSessionVariable().setDisableNereidsRules(
64+
String.join(",", ImmutableSet.of(RuleType.ADD_DEFAULT_LIMIT.name())));
6065
Optional<String> workloadGroup = mtmv.getWorkloadGroup();
6166
if (workloadGroup.isPresent()) {
6267
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
@@ -58,6 +58,7 @@
5858
import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanVisitor;
5959
import org.apache.doris.nereids.trees.plans.visitor.NondeterministicFunctionCollector;
6060
import org.apache.doris.nereids.util.ExpressionUtils;
61+
import org.apache.doris.qe.SessionVariable;
6162

6263
import com.google.common.collect.HashMultimap;
6364
import com.google.common.collect.ImmutableList;
@@ -247,11 +248,22 @@ public static Plan rewriteByRules(
247248
CascadesContext rewrittenPlanContext = CascadesContext.initContext(
248249
cascadesContext.getStatementContext(), rewrittenPlan,
249250
cascadesContext.getCurrentJobContext().getRequiredProperties());
251+
// Tmp old disable rule variable
252+
Set<String> oldDisableRuleNames = rewrittenPlanContext.getStatementContext().getConnectContext()
253+
.getSessionVariable()
254+
.getDisableNereidsRuleNames();
255+
rewrittenPlanContext.getStatementContext().getConnectContext().getSessionVariable()
256+
.setDisableNereidsRules(String.join(",", ImmutableSet.of(RuleType.ADD_DEFAULT_LIMIT.name())));
257+
rewrittenPlanContext.getStatementContext().invalidCache(SessionVariable.DISABLE_NEREIDS_RULES);
250258
try {
251259
rewrittenPlanContext.getConnectContext().setSkipAuth(true);
252260
rewrittenPlan = planRewriter.apply(rewrittenPlanContext);
253261
} finally {
254262
rewrittenPlanContext.getConnectContext().setSkipAuth(false);
263+
// Recover old disable rules variable
264+
rewrittenPlanContext.getStatementContext().getConnectContext().getSessionVariable()
265+
.setDisableNereidsRules(String.join(",", oldDisableRuleNames));
266+
rewrittenPlanContext.getStatementContext().invalidCache(SessionVariable.DISABLE_NEREIDS_RULES);
255267
}
256268
Map<ExprId, Slot> exprIdToNewRewrittenSlot = Maps.newLinkedHashMap();
257269
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
@@ -1176,6 +1176,8 @@ class Suite implements GroovyInterceptable {
11761176
logger.info("status is not success")
11771177
}
11781178
Assert.assertEquals("SUCCESS", status)
1179+
logger.info("waitingMTMVTaskFinished analyze mv name is " + result.last().get(5))
1180+
sql "analyze table ${result.last().get(6)}.${mvName} with sync;"
11791181
}
11801182

11811183
void waitingMTMVTaskFinishedByMvNameAllowCancel(String mvName) {
@@ -1246,6 +1248,9 @@ class Suite implements GroovyInterceptable {
12461248
logger.info("status is not success")
12471249
}
12481250
Assert.assertEquals("SUCCESS", status)
1251+
// Need to analyze materialized view for cbo to choose the materialized view accurately
1252+
logger.info("waitingMTMVTaskFinished analyze mv name is " + result.last().get(5))
1253+
sql "analyze table ${result.last().get(6)}.${result.last().get(5)} with sync;"
12491254
}
12501255

12511256
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)