Skip to content

Commit

Permalink
support case insensitive parsing and bugfix in nereids
Browse files Browse the repository at this point in the history
  • Loading branch information
zclllyybb committed Jul 1, 2024
1 parent 99135b8 commit 7c9c616
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,6 @@ private static boolean isPunctuation(char c) {
return punctuations.contains(c);
}

private static void replacePunctuation(String s, StringBuilder sb, char c, int idx) {
if (idx >= sb.length()) {
return;
}
if (isPunctuation(sb.charAt(idx))) {
sb.setCharAt(idx, c);
} else {
throw new AnalysisException("date/datetime literal [" + s + "] is invalid");
}
}

static String normalize(String s) {
// merge consecutive space
if (s.contains(" ")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQueries;
Expand Down Expand Up @@ -131,6 +132,7 @@ public static int determineScale(String s) {

@Override
protected void init(String s) throws AnalysisException {
// TODO: check and do fast parse like fastParseDate
TemporalAccessor temporal = parse(s);

year = DateUtils.getOrDefault(temporal, ChronoField.YEAR);
Expand All @@ -142,8 +144,13 @@ protected void init(String s) throws AnalysisException {

ZoneId zoneId = temporal.query(TemporalQueries.zone());
if (zoneId != null) {
int offset = DateUtils.getTimeZone().getRules().getOffset(Instant.now()).getTotalSeconds()
- zoneId.getRules().getOffset(Instant.now()).getTotalSeconds();
// get correct DST of that time.
Instant thatTime = ZonedDateTime
.of((int) year, (int) month, (int) day, (int) hour, (int) minute, (int) second, 0, zoneId)
.toInstant();

int offset = DateUtils.getTimeZone().getRules().getOffset(thatTime).getTotalSeconds()
- zoneId.getRules().getOffset(thatTime).getTotalSeconds();
if (offset != 0) {
DateTimeLiteral result = (DateTimeLiteral) this.plusSeconds(offset);
this.second = result.second;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
public class DateTimeFormatterUtils {
public static final DateTimeFormatter ZONE_FORMATTER = new DateTimeFormatterBuilder()
.optionalStart()
.parseCaseInsensitive()
.appendZoneOrOffsetId()
.optionalEnd()
.toFormatter()
Expand Down
11 changes: 10 additions & 1 deletion regression-test/data/datatype_p0/datetimev2/test_timezone.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !analysis --
-- !legacy --
2022-01-01T01:02:55 2022-01-01
2022-02-01T03:02:55 2022-02-01
2022-02-28T19:02:55 2022-03-01
Expand All @@ -16,3 +16,12 @@
2022-05-31T22:32:55 2022-06-01
2022-06-30T20:02:55 2022-07-01

-- !fold1 --
2020-12-12T06:12:12

-- !fold2 --
2020-12-12T22:12:12

-- !fold3 --
2020-12-12T13:12:12

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ suite("test_timezone") {
sql """insert into ${table} values('2022-06-01T01:02:55+04:30', '2022-06-01 01:02:55.123-07:30')"""
sql """insert into ${table} values('20220701010255+07:00', '20220701010255-05:00')"""
sql """insert into ${table} values('20220801+05:00', '20220801America/Argentina/Buenos_Aires')"""
qt_analysis "select * from ${table} order by k1"
qt_legacy "select * from ${table} order by k1"

sql """ truncate table ${table} """

Expand All @@ -56,4 +56,8 @@ suite("test_timezone") {
sql """insert into ${table} values('20220701010255+07:00', '20220701010255-05:00')"""
sql """ set enable_nereids_planner = true """
qt_nereids "select * from ${table} order by k1"

qt_fold1 """ select cast('2020-12-12T12:12:12asia/shanghai' as datetime); """
qt_fold2 """ select cast('2020-12-12T12:12:12america/los_angeLES' as datetime); """
qt_fold3 """ select cast('2020-12-12T12:12:12Europe/pARIS' as datetime); """
}

0 comments on commit 7c9c616

Please sign in to comment.