Skip to content

Commit c6b924d

Browse files
committed
[improvement](mv) Support to use cast when create sync materialized view (#38008)
Support to use cast when create sync materialized view Such as sync mv def is as following: you can use cast(FLOOR(MINUTE(`time`) / 15) as decimal(9, 0)) in mv def which is not allowed before. CREATE MATERIALIZED VIEW sync_mv AS SELECT decision, code, app_name, event_id, event_type, date_trunc(time, 'minute'), DATE_FORMAT( `time`, '%Y-%m-%d' ), cast(FLOOR(MINUTE(time) / 15) as decimal(9, 0)), count(id) as cnt from test group by code, app_name, event_id, event_type, date_trunc(time, 'minute'), decision, DATE_FORMAT(time, '%Y-%m-%d'), cast(FLOOR(MINUTE(`time`) / 15) as decimal(9, 0));
1 parent 1301ef7 commit c6b924d

File tree

3 files changed

+142
-2
lines changed

3 files changed

+142
-2
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
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !query_before --
3+
cc 2024-07-0300000:00 1
4+
cc 2024-07-0300015:00 1
5+
cd 2024-07-0300000:00 1
6+
7+
-- !query_after --
8+
cc 2024-07-0300000:00 1
9+
cc 2024-07-0300015:00 1
10+
cd 2024-07-0300000:00 1
11+
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
suite("mv_contains_cast") {
19+
String db = context.config.getDbNameByFile(context.file)
20+
sql "use ${db}"
21+
sql "set runtime_filter_mode=OFF";
22+
sql "SET ignore_shape_nodes='PhysicalDistribute,PhysicalProject'"
23+
24+
sql """
25+
drop table if exists test;
26+
"""
27+
28+
sql """
29+
CREATE TABLE IF NOT EXISTS test (
30+
`app_name` VARCHAR(64) NULL COMMENT '标识',
31+
`event_id` VARCHAR(128) NULL COMMENT '标识',
32+
`decision` VARCHAR(32) NULL COMMENT '枚举值',
33+
`time` DATETIME NULL COMMENT '查询时间',
34+
`id` VARCHAR(35) NOT NULL COMMENT 'od',
35+
`code` VARCHAR(64) NULL COMMENT '标识',
36+
`event_type` VARCHAR(32) NULL COMMENT '事件类型'
37+
)
38+
DUPLICATE KEY(app_name, event_id)
39+
PARTITION BY RANGE(time)
40+
(
41+
FROM ("2024-07-01 00:00:00") TO ("2024-07-15 00:00:00") INTERVAL 1 HOUR
42+
)
43+
DISTRIBUTED BY HASH(event_id)
44+
BUCKETS 3 PROPERTIES ("replication_num" = "1");
45+
"""
46+
47+
sql """
48+
insert into test values
49+
('aa', 'bc', 'cc', '2024-07-03 01:15:30', 'dd', 'ee', 'ff'),
50+
('as', 'bd', 'cd', '2024-07-03 06:09:30', 'dd', 'ee', 'ff'),
51+
('ad', 'be', 'cc', '2024-07-03 07:06:30', 'dd', 'ee', 'ff'),
52+
('af', 'bf', 'ce', '2024-07-04 10:01:30', 'dd', 'ee', 'ff'),
53+
('ag', 'bc', 'cc', '2024-07-04 12:55:30', 'dd', 'ee', 'ff'),
54+
('aa', 'bc', 'cc', '2024-07-05 01:15:30', 'dd', 'ee', 'ff'),
55+
('as', 'bd', 'cd', '2024-07-05 06:09:30', 'dd', 'ee', 'ff'),
56+
('ad', 'be', 'cc', '2024-07-06 07:06:30', 'dd', 'ee', 'ff'),
57+
('af', 'bf', 'ce', '2024-07-07 10:01:30', 'dd', 'ee', 'ff'),
58+
('ag', 'bc', 'cc', '2024-07-08 12:55:30', 'dd', 'ee', 'ff');
59+
"""
60+
61+
def query_sql = """
62+
SELECT
63+
decision,
64+
CONCAT(
65+
CONCAT(
66+
DATE_FORMAT(
67+
`time`, '%Y-%m-%d'
68+
),
69+
'',
70+
LPAD(
71+
cast(FLOOR(MINUTE(`time`) / 15) as decimal(9, 0)) * 15,
72+
5,
73+
'00'
74+
),
75+
':00'
76+
)
77+
) as time,
78+
count(id) as cnt
79+
from
80+
test
81+
where
82+
date_trunc(time, 'minute') BETWEEN '2024-07-02 18:00:00'
83+
AND '2024-07-03 20:00:00'
84+
group by
85+
decision,
86+
DATE_FORMAT(
87+
`time`, "%Y-%m-%d"
88+
),
89+
cast(FLOOR(MINUTE(`time`) / 15) as decimal(9, 0));
90+
"""
91+
92+
order_qt_query_before "${query_sql}"
93+
94+
createMV("""
95+
CREATE MATERIALIZED VIEW sync_mv
96+
AS
97+
SELECT
98+
decision,
99+
code,
100+
app_name,
101+
event_id,
102+
event_type,
103+
date_trunc(time, 'minute'),
104+
DATE_FORMAT(
105+
`time`, '%Y-%m-%d'
106+
),
107+
cast(FLOOR(MINUTE(time) / 15) as decimal(9, 0)),
108+
count(id) as cnt
109+
from
110+
test
111+
group by
112+
code,
113+
app_name,
114+
event_id,
115+
event_type,
116+
date_trunc(time, 'minute'),
117+
decision,
118+
DATE_FORMAT(time, '%Y-%m-%d'),
119+
cast(FLOOR(MINUTE(`time`) / 15) as decimal(9, 0));
120+
""")
121+
122+
explain {
123+
sql("""${query_sql}""")
124+
contains "(sync_mv)"
125+
}
126+
127+
order_qt_query_after "${query_sql}"
128+
}

0 commit comments

Comments
 (0)