Skip to content

Commit ddba981

Browse files
authored
[feature](Nereids) support datev1 and datetimev1 (apache#28581)
1 parent 8da9bb6 commit ddba981

7 files changed

Lines changed: 198 additions & 4 deletions

File tree

‎fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ specifiedPartition
753753

754754
constant
755755
: NULL #nullLiteral
756-
| type=(DATE | DATEV1 | DATEV2 | TIMESTAMP) STRING_LITERAL #typeConstructor
756+
| type=(DATE | DATEV1 | DATEV2 | TIMESTAMP) STRING_LITERAL #typeConstructor
757757
| number #numericLiteral
758758
| booleanValue #booleanLiteral
759759
| STRING_LITERAL #stringLiteral

‎fe/fe-core/src/main/java/org/apache/doris/nereids/types/DataType.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ public static DataType convertPrimitiveFromStrings(List<String> types, boolean u
216216
case "date":
217217
dataType = DateType.INSTANCE;
218218
break;
219+
case "datev1":
220+
dataType = DateType.NOT_CONVERSION;
221+
break;
219222
case "datev2":
220223
dataType = DateV2Type.INSTANCE;
221224
break;
@@ -234,6 +237,17 @@ public static DataType convertPrimitiveFromStrings(List<String> types, boolean u
234237
throw new AnalysisException("Nereids do not support type: " + type);
235238
}
236239
break;
240+
case "datetimev1":
241+
switch (types.size()) {
242+
case 1:
243+
dataType = DateTimeType.NOT_CONVERSION;
244+
break;
245+
case 2:
246+
throw new AnalysisException("Nereids do not support datetimev1 type with precision");
247+
default:
248+
throw new AnalysisException("Nereids do not support type: " + type);
249+
}
250+
break;
237251
case "datetimev2":
238252
switch (types.size()) {
239253
case 1:

‎fe/fe-core/src/main/java/org/apache/doris/nereids/types/DateTimeType.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,23 @@
3131
public class DateTimeType extends DateLikeType {
3232

3333
public static final DateTimeType INSTANCE = new DateTimeType();
34+
public static final DateTimeType NOT_CONVERSION = new DateTimeType(false);
3435

3536
private static final int WIDTH = 16;
3637

38+
private final boolean shouldConversion;
39+
3740
private DateTimeType() {
41+
this.shouldConversion = true;
42+
}
43+
44+
private DateTimeType(boolean shouldConversion) {
45+
this.shouldConversion = shouldConversion;
3846
}
3947

4048
@Override
4149
public DataType conversion() {
42-
if (Config.enable_date_conversion) {
50+
if (Config.enable_date_conversion && shouldConversion) {
4351
return DateTimeV2Type.SYSTEM_DEFAULT;
4452
}
4553
return this;

‎fe/fe-core/src/main/java/org/apache/doris/nereids/types/DateType.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,23 @@
3131
public class DateType extends DateLikeType {
3232

3333
public static final DateType INSTANCE = new DateType();
34+
public static final DateType NOT_CONVERSION = new DateType(false);
3435

3536
private static final int WIDTH = 16;
3637

38+
private final boolean shouldConversion;
39+
3740
private DateType() {
41+
this.shouldConversion = true;
42+
}
43+
44+
private DateType(boolean shouldConversion) {
45+
this.shouldConversion = shouldConversion;
3846
}
3947

4048
@Override
4149
public DataType conversion() {
42-
if (Config.enable_date_conversion) {
50+
if (Config.enable_date_conversion && shouldConversion) {
4351
return DateV2Type.INSTANCE;
4452
}
4553
return this;

‎fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.doris.common.Pair;
2323
import org.apache.doris.nereids.StatementContext;
2424
import org.apache.doris.nereids.analyzer.UnboundResultSink;
25+
import org.apache.doris.nereids.exceptions.AnalysisException;
2526
import org.apache.doris.nereids.exceptions.ParseException;
2627
import org.apache.doris.nereids.glue.LogicalPlanAdapter;
2728
import org.apache.doris.nereids.trees.expressions.Cast;
@@ -37,6 +38,8 @@
3738
import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
3839
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
3940
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
41+
import org.apache.doris.nereids.types.DateTimeType;
42+
import org.apache.doris.nereids.types.DateType;
4043
import org.apache.doris.nereids.types.DecimalV2Type;
4144
import org.apache.doris.nereids.types.DecimalV3Type;
4245
import org.apache.doris.qe.SessionVariable;
@@ -305,7 +308,27 @@ public void testParseDecimal() {
305308
.stream()
306309
.mapToLong(e -> e.<Set<DecimalLiteral>>collect(DecimalLiteral.class::isInstance).size())
307310
.sum();
308-
Assertions.assertEquals(doubleCount, Config.enable_decimal_conversion ? 0 : 1);
311+
Assertions.assertEquals(Config.enable_decimal_conversion ? 0 : 1, doubleCount);
312+
}
313+
314+
@Test
315+
public void testDatev1() {
316+
String dv1 = "SELECT CAST('2023-12-18' AS DATEV1)";
317+
NereidsParser nereidsParser = new NereidsParser();
318+
LogicalPlan logicalPlan = (LogicalPlan) nereidsParser.parseSingle(dv1).child(0);
319+
Assertions.assertEquals(DateType.INSTANCE, logicalPlan.getExpressions().get(0).getDataType());
320+
}
321+
322+
@Test
323+
public void testDatetimev1() {
324+
String dtv1 = "SELECT CAST('2023-12-18' AS DATETIMEV1)";
325+
NereidsParser nereidsParser = new NereidsParser();
326+
LogicalPlan logicalPlan = (LogicalPlan) nereidsParser.parseSingle(dtv1).child(0);
327+
Assertions.assertEquals(DateTimeType.INSTANCE, logicalPlan.getExpressions().get(0).getDataType());
328+
329+
String wrongDtv1 = "SELECT CAST('2023-12-18' AS DATETIMEV1(2))";
330+
Assertions.assertThrows(AnalysisException.class, () -> nereidsParser.parseSingle(wrongDtv1).child(0));
331+
309332
}
310333

311334
@Test
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !sql1 --
3+
2016-11-04
4+
5+
-- !sql2 --
6+
2016-11-04
7+
8+
-- !sql1 --
9+
1 2000-01-01 2000-01-01T11:11:11
10+
2 2000-02-02 2000-02-02T11:11:11
11+
3 2000-03-02 2000-03-02T11:11:11
12+
13+
-- !sql2 --
14+
1 2000-01-01 2000-01-01T11:11:11 1 2000-01-01 2000-01-01T11:11:11
15+
2 2000-02-02 2000-02-02T11:11:11 2 2000-02-02 2000-02-02T11:11:11
16+
3 2000-03-02 2000-03-02T11:11:11 3 2000-03-02 2000-03-02T11:11:11
17+
18+
-- !sql2 --
19+
1 2000-01-01 2000-01-01T11:11:11 1 2000-01-01 2000-01-01T11:11:11
20+
2 2000-02-02 2000-02-02T11:11:11 2 2000-02-02 2000-02-02T11:11:11
21+
3 2000-03-02 2000-03-02T11:11:11 3 2000-03-02 2000-03-02T11:11:11
22+
23+
-- !sql2 --
24+
1 2000-01-01 2000-01-01T11:11:11 1 2000-01-01 2000-01-01T11:11:11
25+
2 2000-02-02 2000-02-02T11:11:11 2 2000-02-02 2000-02-02T11:11:11
26+
3 2000-03-02 2000-03-02T11:11:11 3 2000-03-02 2000-03-02T11:11:11
27+
28+
-- !sql2 --
29+
1 2000-01-01 2000-01-01T11:11:11 1 2000-01-01 2000-01-01T11:11:11
30+
2 2000-02-02 2000-02-02T11:11:11 2 2000-02-02 2000-02-02T11:11:11
31+
3 2000-03-02 2000-03-02T11:11:11 3 2000-03-02 2000-03-02T11:11:11
32+
33+
-- !sql2 --
34+
1 2000-01-01 2000-01-01T11:11:11 1 2000-01-01 2000-01-01T11:11:11
35+
2 2000-02-02 2000-02-02T11:11:11 2 2000-02-02 2000-02-02T11:11:11
36+
3 2000-03-02 2000-03-02T11:11:11 3 2000-03-02 2000-03-02T11:11:11
37+
38+
-- !sql2 --
39+
1 2000-01-01 2000-01-01T11:11:11 1 2000-01-01 2000-01-01T11:11:11
40+
2 2000-02-02 2000-02-02T11:11:11 2 2000-02-02 2000-02-02T11:11:11
41+
3 2000-03-02 2000-03-02T11:11:11 3 2000-03-02 2000-03-02T11:11:11
42+
43+
-- !sql2 --
44+
1 2000-01-01 2000-01-01T11:11:11 1 2000-01-01 2000-01-01T11:11:11
45+
2 2000-02-02 2000-02-02T11:11:11 2 2000-02-02 2000-02-02T11:11:11
46+
3 2000-03-02 2000-03-02T11:11:11 3 2000-03-02 2000-03-02T11:11:11
47+
48+
-- !sql2 --
49+
1 2000-01-01 2000-01-01T11:11:11 1 2000-01-01 2000-01-01T11:11:11
50+
2 2000-02-02 2000-02-02T11:11:11 2 2000-02-02 2000-02-02T11:11:11
51+
3 2000-03-02 2000-03-02T11:11:11 3 2000-03-02 2000-03-02T11:11:11
52+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
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("test_datev1") {
20+
def tbName = "test_datev1_exprs"
21+
22+
sql """set enable_nereids_planner=true"""
23+
sql """set enable_fallback_to_original_planner=false"""
24+
sql """set enable_nereids_dml=true"""
25+
26+
sql "DROP TABLE IF EXISTS ${tbName}"
27+
sql """
28+
create table ${tbName}(k1 datetimev1, k2 int) distributed by hash(k1) buckets 1 properties("replication_num" = "1");
29+
"""
30+
sql """ insert into ${tbName} values("2016-11-04 00:00:01", 1); """
31+
32+
qt_sql1 """ select dt
33+
from
34+
(
35+
select cast(k1 as datev1) as dt
36+
from ${tbName}
37+
) r; """
38+
qt_sql2 """ select dt
39+
from
40+
(
41+
select cast(k1 as date) as dt
42+
from ${tbName}
43+
) r; """
44+
sql "DROP TABLE ${tbName}"
45+
46+
tbName = "test_datev1_runtime_filter"
47+
sql "DROP TABLE IF EXISTS ${tbName}"
48+
sql """
49+
CREATE TABLE IF NOT EXISTS ${tbName} (
50+
c0 int,
51+
c2 datev1,
52+
c3 datetimev1
53+
)
54+
DISTRIBUTED BY HASH(c0) BUCKETS 5 properties("replication_num" = "1");
55+
"""
56+
sql "insert into ${tbName} values(1, '2000-01-01', '2000-01-01 11:11:11')"
57+
sql "insert into ${tbName} values(2, '2000-02-02', '2000-02-02 11:11:11')"
58+
sql "insert into ${tbName} values(3, '2000-03-02', '2000-03-02 11:11:11')"
59+
60+
qt_sql1 "select * from ${tbName} ORDER BY c2"
61+
62+
sql " set runtime_filter_type = 1; "
63+
qt_sql2 "select * from ${tbName} a, ${tbName} b WHERE a.c3 = b.c3 ORDER BY a.c2"
64+
65+
sql " set runtime_filter_type = 2; "
66+
qt_sql2 "select * from ${tbName} a, ${tbName} b WHERE a.c3 = b.c3 ORDER BY a.c2"
67+
68+
sql " set runtime_filter_type = 4; "
69+
qt_sql2 "select * from ${tbName} a, ${tbName} b WHERE a.c3 = b.c3 ORDER BY a.c2"
70+
71+
sql " set runtime_filter_type = 8; "
72+
qt_sql2 "select * from ${tbName} a, ${tbName} b WHERE a.c3 = b.c3 ORDER BY a.c2"
73+
74+
sql " set runtime_filter_wait_time_ms = 0; "
75+
76+
sql " set runtime_filter_type = 1; "
77+
qt_sql2 "select * from ${tbName} a, ${tbName} b WHERE a.c3 = b.c3 ORDER BY a.c2"
78+
79+
sql " set runtime_filter_type = 2; "
80+
qt_sql2 "select * from ${tbName} a, ${tbName} b WHERE a.c3 = b.c3 ORDER BY a.c2"
81+
82+
sql " set runtime_filter_type = 4; "
83+
qt_sql2 "select * from ${tbName} a, ${tbName} b WHERE a.c3 = b.c3 ORDER BY a.c2"
84+
85+
sql " set runtime_filter_type = 8; "
86+
qt_sql2 "select * from ${tbName} a, ${tbName} b WHERE a.c3 = b.c3 ORDER BY a.c2"
87+
88+
sql "DROP TABLE ${tbName}"
89+
}

0 commit comments

Comments
 (0)