Skip to content
Merged
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 @@ -100,6 +100,7 @@
import com.google.common.base.Preconditions;
import org.apache.commons.collections.CollectionUtils;

import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -222,7 +223,7 @@ private ColumnStatistic castMinMax(ColumnStatistic colStats, DataType targetType
long max = dateMaxLiteral.getValue();
builder.setMaxValue(max);
builder.setMaxExpr(dateMaxLiteral.toLegacyLiteral());
} catch (AnalysisException e) {
} catch (DateTimeException | AnalysisException e) {
convertSuccess = false;
}
}
Expand Down
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 @@ -341,11 +342,14 @@ public static Result<TemporalAccessor, AnalysisException> parseDateTime(String s
// if Year is not present, throw exception
if (!dateTime.isSupported(ChronoField.YEAR)) {
return Result.err(
() -> new AnalysisException("date/datetime literal [" + originalString + "] is invalid")
() -> new DateTimeException("date/datetime literal [" + originalString + "] is invalid")
);
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.time.DateTimeException;
import java.util.function.Consumer;

class DateLiteralTest {
Expand Down Expand Up @@ -67,7 +68,8 @@ void testDate() {
new DateLiteral("2022-1-1");
new DateLiteral("20220101");

Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("-01-01"));
Assertions.assertThrows(DateTimeException.class, () -> new DateLiteral("-01-01"));
Assertions.assertThrows(DateTimeException.class, () -> new DateLiteral("01-01"));
}

@Test
Expand Down Expand Up @@ -128,8 +130,8 @@ void testIrregularDate() {

@Test
void testWrongPunctuationDate() {
Assertions.assertThrows(AnalysisException.class, () -> new DateTimeV2Literal("2020€02€01"));
Assertions.assertThrows(AnalysisException.class, () -> new DateTimeV2Literal("2020【02】01"));
Assertions.assertThrows(DateTimeException.class, () -> new DateTimeV2Literal("2020€02€01"));
Assertions.assertThrows(DateTimeException.class, () -> new DateTimeV2Literal("2020【02】01"));
}

@Test
Expand Down
31 changes: 31 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,34 @@
-- !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

-- !10 --
\N

-- !12 --
\N

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,24 @@ 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)"
qt_10 "select date_add('08-09', 10)"
qt_12 "select date_add('abcd', 10)"
}
Loading