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 @@ -29,9 +29,6 @@
import org.apache.doris.nereids.util.DateUtils;
import org.apache.doris.nereids.util.StandardDateFormat;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.time.LocalDateTime;
import java.time.Year;
import java.time.temporal.ChronoField;
Expand All @@ -44,11 +41,9 @@ public class DateLiteral extends Literal {
public static final String JAVA_DATE_FORMAT = "yyyy-MM-dd";

// for cast datetime type to date type.
private static final LocalDateTime startOfAD = LocalDateTime.of(0, 1, 1, 0, 0, 0);
private static final LocalDateTime endOfAD = LocalDateTime.of(9999, 12, 31, 23, 59, 59);
private static final Logger LOG = LogManager.getLogger(DateLiteral.class);

private static final DateLiteral MIN_DATE = new DateLiteral(0000, 1, 1);
private static final LocalDateTime START_OF_A_DAY = LocalDateTime.of(0, 1, 1, 0, 0, 0);
private static final LocalDateTime END_OF_A_DAY = LocalDateTime.of(9999, 12, 31, 23, 59, 59);
private static final DateLiteral MIN_DATE = new DateLiteral(0, 1, 1);
private static final DateLiteral MAX_DATE = new DateLiteral(9999, 12, 31);
private static final int[] DAYS_IN_MONTH = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Expand Down Expand Up @@ -264,7 +259,7 @@ protected void init(String s) throws AnalysisException {
month = DateUtils.getOrDefault(dateTime, ChronoField.MONTH_OF_YEAR);
day = DateUtils.getOrDefault(dateTime, ChronoField.DAY_OF_MONTH);

if (checkRange() || checkDate()) {
if (checkDatetime(dateTime) || checkRange() || checkDate()) {
throw new AnalysisException("date/datetime literal [" + s + "] is out of range");
}
}
Expand All @@ -284,7 +279,14 @@ protected boolean checkDate() {
}

protected static boolean isDateOutOfRange(LocalDateTime dateTime) {
return dateTime.isBefore(startOfAD) || dateTime.isAfter(endOfAD);
return dateTime.isBefore(START_OF_A_DAY) || dateTime.isAfter(END_OF_A_DAY);
}

private boolean checkDatetime(TemporalAccessor dateTime) {
return DateUtils.getOrDefault(dateTime, ChronoField.HOUR_OF_DAY) != 0
|| DateUtils.getOrDefault(dateTime, ChronoField.MINUTE_OF_HOUR) != 0
|| DateUtils.getOrDefault(dateTime, ChronoField.SECOND_OF_MINUTE) != 0
|| DateUtils.getOrDefault(dateTime, ChronoField.MICRO_OF_SECOND) != 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ public double rangeLength(double high, double low) {
*/
public DateLiteral fromString(String s) {
if (this instanceof DateType) {
return new DateLiteral(s);
DateTimeV2Literal l = new DateTimeV2Literal(DateTimeV2Type.MAX, s);
return new DateLiteral(l.getYear(), l.getMonth(), l.getDay());
} else if (this instanceof DateV2Type) {
return new DateV2Literal(s);
DateTimeV2Literal l = new DateTimeV2Literal(DateTimeV2Type.MAX, s);
return new DateV2Literal(l.getYear(), l.getMonth(), l.getDay());
} else if (this instanceof DateTimeType) {
return new DateTimeLiteral(s);
} else if (this instanceof DateTimeV2Type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,10 @@ void testDateTimeV2TypeDateTimeArithmeticFunctions() {
Assertions.assertEquals(DateTimeExtractAndTransform.dateV2(dateLiteral).toSql(), answer[answerIdx]);

Assertions.assertEquals("'2021 52 2022 01'", DateTimeExtractAndTransform.dateFormat(
new DateLiteral("2022-01-01 00:12:42"),
new DateTimeLiteral("2022-01-01 00:12:42"),
new VarcharLiteral("%x %v %X %V")).toSql());
Assertions.assertEquals("'2023 18 2023 19'", DateTimeExtractAndTransform.dateFormat(
new DateLiteral("2023-05-07 02:41:42"),
new DateTimeLiteral("2023-05-07 02:41:42"),
new VarcharLiteral("%x %v %X %V")).toSql());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ void reject() {
// Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01-1"));
// Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01+01"));
// Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01+1"));
Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01 01:00:00.000000"));
Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01 00:01:00.000000"));
Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01 00:00:01.000000"));
Assertions.assertThrows(AnalysisException.class, () -> new DateLiteral("2022-01-01 00:00:00.000001"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,25 +245,24 @@ void testDateTime() {

@Test
void testIrregularDateTime() {
new DateLiteral("2016-07-02 01:01:00");

new DateLiteral("2016-7-02 01:01:00");
new DateLiteral("2016-07-2 01:01:00");
new DateLiteral("2016-7-2 01:01:00");

new DateLiteral("2016-07-02 1:01:00");
new DateLiteral("2016-07-02 01:1:00");
new DateLiteral("2016-07-02 01:01:0");
new DateLiteral("2016-07-02 1:1:00");
new DateLiteral("2016-07-02 1:01:0");
new DateLiteral("2016-07-02 10:1:0");
new DateLiteral("2016-07-02 1:1:0");

new DateLiteral("2016-7-2 1:1:0");
new DateLiteral("2016-7-02 1:01:0");
new DateLiteral("2016-07-2 1:1:0");
new DateLiteral("2016-7-02 01:01:0");
new DateLiteral("2016-7-2 01:1:0");

new DateTimeV2Literal("2016-7-02 01:01:00");
new DateTimeV2Literal("2016-07-2 01:01:00");
new DateTimeV2Literal("2016-7-2 01:01:00");

new DateTimeV2Literal("2016-07-02 1:01:00");
new DateTimeV2Literal("2016-07-02 01:1:00");
new DateTimeV2Literal("2016-07-02 01:01:0");
new DateTimeV2Literal("2016-07-02 1:1:00");
new DateTimeV2Literal("2016-07-02 1:01:0");
new DateTimeV2Literal("2016-07-02 10:1:0");
new DateTimeV2Literal("2016-07-02 1:1:0");

new DateTimeV2Literal("2016-7-2 1:1:0");
new DateTimeV2Literal("2016-7-02 1:01:0");
new DateTimeV2Literal("2016-07-2 1:1:0");
new DateTimeV2Literal("2016-7-02 01:01:0");
new DateTimeV2Literal("2016-7-2 01:1:0");
}

@Test
Expand Down