Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

branch-2.1: [fix] fix cast literal to date like throw exception #48780

Draft
wants to merge 4 commits into
base: branch-2.1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import com.google.common.collect.ImmutableSet;

import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
Expand Down Expand Up @@ -273,8 +274,8 @@ static Result<String, AnalysisException> normalize(String s) {
}

/** parseDateLiteral */
public static Result<DateLiteral, AnalysisException> parseDateLiteral(String s) {
Result<TemporalAccessor, AnalysisException> parseResult = parseDateTime(s);
public static Result<DateLiteral, ? extends Exception> parseDateLiteral(String s) {
Result<TemporalAccessor, ? extends Exception> parseResult = parseDateTime(s);
if (parseResult.isError()) {
return parseResult.cast();
}
Expand All @@ -290,7 +291,7 @@ public static Result<DateLiteral, AnalysisException> parseDateLiteral(String s)
}

/** parseDateTime */
public static Result<TemporalAccessor, AnalysisException> parseDateTime(String s) {
public static Result<TemporalAccessor, ? extends Exception> parseDateTime(String s) {
// fast parse '2022-01-01'
if (s.length() == 10 && s.charAt(4) == '-' && s.charAt(7) == '-') {
TemporalAccessor date = fastParseDate(s);
Expand Down Expand Up @@ -346,6 +347,9 @@ public static Result<TemporalAccessor, AnalysisException> parseDateTime(String s
}

return Result.ok(dateTime);
} catch (DateTimeException e) {
return Result.err(() ->
new DateTimeException("date/datetime literal [" + originalString + "] is invalid", e));
} catch (Exception ex) {
return Result.err(() -> new AnalysisException("date/datetime literal [" + originalString + "] is invalid"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ public static int determineScale(String s) {
}

/** parseDateTimeLiteral */
public static Result<DateTimeLiteral, AnalysisException> parseDateTimeLiteral(String s, boolean isV2) {
Result<TemporalAccessor, AnalysisException> parseResult = parseDateTime(s);
public static Result<DateTimeLiteral, ? extends Exception> parseDateTimeLiteral(String s, boolean isV2) {
Result<TemporalAccessor, ? extends Exception> parseResult = parseDateTime(s);
if (parseResult.isError()) {
return parseResult.cast();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,12 @@ public static Optional<Expression> characterLiteralTypeCoercion(String value, Da
} else if (dataType.isDateTimeType() && DateTimeChecker.isValidDateTime(value)) {
ret = DateTimeLiteral.parseDateTimeLiteral(value, false).orElse(null);
} else if (dataType.isDateV2Type() && DateTimeChecker.isValidDateTime(value)) {
Result<DateLiteral, AnalysisException> parseResult
Result<DateLiteral, ? extends Exception> parseResult
= DateV2Literal.parseDateLiteral(value);
if (parseResult.isOk()) {
ret = parseResult.get();
} else {
Result<DateTimeLiteral, AnalysisException> parseResult2
Result<DateTimeLiteral, ? extends Exception> parseResult2
= DateTimeV2Literal.parseDateTimeLiteral(value, true);
if (parseResult2.isOk()) {
ret = parseResult2.get();
Expand Down
25 changes: 25 additions & 0 deletions regression-test/data/nereids_syntax_p0/test_cast_datetime.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,28 @@
-- !1 --
1

-- !2 --
1 2000-01-01 2000-01-01 2000-01-01T00:00 2000-01-01T12:12:12
2 \N \N \N \N
3 \N \N \N \N

-- !3 --
1 \N \N \N \N
2 \N \N \N \N
3 \N \N \N \N

-- !4 --
\N

-- !5 --
\N

-- !7 --
\N

-- !8 --
\N

-- !9 --
\N

19 changes: 15 additions & 4 deletions regression-test/suites/nereids_syntax_p0/test_cast_datetime.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ suite("test_cast_datetime") {

sql "drop table if exists casttbl"
sql """CREATE TABLE casttbl (
a int null,
mydate date NULL,
mydatev2 DATEV2 null,
mydatetime datetime null,
mydatetimev2 datetimev2 null
) ENGINE=OLAP
DUPLICATE KEY(`mydate`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`mydate`) BUCKETS 1
DISTRIBUTED BY HASH(`a`) BUCKETS 1
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
Expand All @@ -36,11 +36,22 @@ suite("test_cast_datetime") {
);
"""

sql "insert into casttbl values ('2000-01-01', '2000-01-01 12:12:12', '2000-01-01', '2000-01-01 12:12:12');"
sql "insert into casttbl values (1, '2000-01-01', '2000-01-01 12:12:12', '2000-01-01', '2000-01-01 12:12:12');"

sql "set enable_nereids_planner=true;"
sql "set enable_fallback_to_original_planner=false"

//when BE storage support 'Date < Date', we should remove this case
//currently, if we rewrite expr to CAST(mydatetime AS DATE) < date '2019-06-01', BE returns 0 tuple.
qt_1 "select count(1) from casttbl where CAST(CAST(mydatetime AS DATE) AS DATETIME) < date '2019-06-01';"
}

sql "insert into casttbl values(2, '', '', '', ''), (3, '2020', '2020', '2020', '2020')"
qt_2 "select * from casttbl"
qt_3 "select a, '' = mydate, '' = mydatev2, '' = mydatetime, '' = mydatetimev2 from casttbl"

qt_4 "select '' > date '2019-06-01'"
qt_5 "select '' > date_sub('2019-06-01', -10)"
qt_7 "select '' > cast('2019-06-01 00:00:00' as datetime)"
qt_8 "select date_add('', 10)"
qt_9 "select date_add('2020', 10)"
}
Loading