This is an automated email from the ASF dual-hosted git repository.
jakevin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 484215e1cc [fix](Nereids): datetime - offset is wrong & support
Two-Digital date (#24201)
484215e1cc is described below
commit 484215e1ccc45fcc7137897712f35715930abcd8
Author: jakevin <[email protected]>
AuthorDate: Tue Sep 12 10:17:56 2023 +0800
[fix](Nereids): datetime - offset is wrong & support Two-Digital date
(#24201)
- bug: datetime - offset is wrong
- support Two-Digital date
- remove useless override code
---
.../trees/expressions/literal/DateTimeLiteral.java | 19 -------------------
.../doris/nereids/util/DateTimeFormatterUtils.java | 8 ++++----
.../nereids/util/DateTimeFormatterUtilsTest.java | 21 +++++++++++++++++++++
3 files changed, 25 insertions(+), 23 deletions(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteral.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteral.java
index d55a45d696..eaf1af0cb7 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteral.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeLiteral.java
@@ -32,7 +32,6 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.time.LocalDateTime;
-import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle;
@@ -129,24 +128,16 @@ public class DateTimeLiteral extends DateLiteral {
protected void init(String s) throws AnalysisException {
try {
TemporalAccessor dateTime = null;
- int offset = 0;
// parse timezone
if (haveTimeZoneOffset(s) || haveTimeZoneName(s)) {
- String tzString = new String();
if (haveTimeZoneName(s)) { // GMT, UTC+8, Z[, CN,
Asia/Shanghai]
int split = getTimeZoneSplitPos(s);
Preconditions.checkArgument(split > 0);
- tzString = s.substring(split);
s = s.substring(0, split);
} else { // +04:30
Preconditions.checkArgument(s.charAt(s.length() - 6) ==
'-' || s.charAt(s.length() - 6) == '+');
- tzString = s.substring(s.length() - 6);
s = s.substring(0, s.length() - 6);
}
- ZoneId zone = ZoneId.of(tzString);
- ZoneId dorisZone = DateUtils.getTimeZone();
- offset =
dorisZone.getRules().getOffset(java.time.Instant.now()).getTotalSeconds()
- -
zone.getRules().getOffset(java.time.Instant.now()).getTotalSeconds();
}
if (!s.contains("-") && !s.contains(":")) {
dateTime =
DateTimeFormatterUtils.BASIC_DATE_TIME_FORMATTER.parse(s);
@@ -220,16 +211,6 @@ public class DateTimeLiteral extends DateLiteral {
second = DateUtils.getOrDefault(dateTime,
ChronoField.SECOND_OF_MINUTE);
microSecond = DateUtils.getOrDefault(dateTime,
ChronoField.MICRO_OF_SECOND);
- if (offset != 0) {
- DateTimeLiteral result = (DateTimeLiteral)
this.plusSeconds(offset);
- this.second = result.second;
- this.minute = result.minute;
- this.hour = result.hour;
- this.day = result.day;
- this.month = result.month;
- this.year = result.year;
- }
-
} catch (Exception ex) {
throw new AnalysisException("datetime literal [" + s + "] is
invalid");
}
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateTimeFormatterUtils.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateTimeFormatterUtils.java
index 244d672229..1925e100ac 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateTimeFormatterUtils.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateTimeFormatterUtils.java
@@ -41,12 +41,12 @@ import java.time.temporal.ChronoField;
public class DateTimeFormatterUtils {
// Date: %Y-%m-%d
public static DateTimeFormatter DATE_FORMATTER = new
DateTimeFormatterBuilder()
- .appendOptional(new
DateTimeFormatterBuilder().appendValue(ChronoField.YEAR, 4).toFormatter())
- .appendOptional(new
DateTimeFormatterBuilder().appendValue(ChronoField.YEAR, 2).toFormatter())
+ .appendOptional(
+ new
DateTimeFormatterBuilder().appendValue(ChronoField.YEAR, 4).toFormatter())
+ .appendOptional(
+ new
DateTimeFormatterBuilder().appendValueReduced(ChronoField.YEAR, 2, 2,
1970).toFormatter())
.appendLiteral('-').appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendLiteral('-').appendValue(ChronoField.DAY_OF_MONTH, 2)
- // if year isn't present, use -1 as default, so that it can be
rejected by check
- .parseDefaulting(ChronoField.YEAR, -1)
.toFormatter().withResolverStyle(ResolverStyle.STRICT);
// Date without delimiter: %Y%m%d
public static DateTimeFormatter BASIC_DATE_FORMATTER = new
DateTimeFormatterBuilder()
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/nereids/util/DateTimeFormatterUtilsTest.java
b/fe/fe-core/src/test/java/org/apache/doris/nereids/util/DateTimeFormatterUtilsTest.java
index 3e7ba62fc9..c40f5c4a85 100644
---
a/fe/fe-core/src/test/java/org/apache/doris/nereids/util/DateTimeFormatterUtilsTest.java
+++
b/fe/fe-core/src/test/java/org/apache/doris/nereids/util/DateTimeFormatterUtilsTest.java
@@ -58,4 +58,25 @@ class DateTimeFormatterUtilsTest {
Assertions.assertThrows(DateTimeParseException.class, () ->
formatter.parse("20200219T010101."));
Assertions.assertThrows(DateTimeParseException.class, () ->
formatter.parse("20200219T010101.0000001"));
}
+
+ @Test
+ void testTwoDigitalDate() {
+ DateTimeFormatter formatter = DateTimeFormatterUtils.DATE_FORMATTER;
+ // Year values in the range 00-69 become 2000-2069.
+ // Year values in the range 70-99 become 1970-199
+ for (int i = 0; i < 100; i++) {
+ String str;
+ if (i < 10) {
+ str = "0" + i + "-02-19";
+ } else {
+ str = i + "-02-19";
+ }
+ TemporalAccessor dateTime = formatter.parse(str);
+ if (i < 70) {
+ Assertions.assertEquals(2000 + i,
dateTime.get(ChronoField.YEAR));
+ } else {
+ Assertions.assertEquals(1900 + i,
dateTime.get(ChronoField.YEAR));
+ }
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]