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 @@ -376,7 +376,7 @@ protected void init(String s) throws AnalysisException {
}

protected static boolean checkRange(long year, long month, long day) {
return year > MAX_DATE.getYear() || month > MAX_DATE.getMonth() || day > MAX_DATE.getDay();
return year < 0 || year > MAX_DATE.getYear() || month > MAX_DATE.getMonth() || day > MAX_DATE.getDay();
}

protected static boolean checkDate(long year, long month, long day) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
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 @@ -154,23 +152,14 @@ public static Result<DateTimeLiteral, AnalysisException> parseDateTimeLiteral(St

ZoneId zoneId = temporal.query(TemporalQueries.zone());
if (zoneId != null) {
// 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 tempLiteral = new DateTimeLiteral(year, month, day, hour, minute, second);
DateTimeLiteral result = (DateTimeLiteral) tempLiteral.plusSeconds(offset);
second = result.second;
minute = result.minute;
hour = result.hour;
day = result.day;
month = result.month;
year = result.year;
}
LocalDateTime localDateTime = convertTimeZone(year, month, day, hour, minute, second,
zoneId, DateUtils.getTimeZone());
year = localDateTime.getYear();
month = localDateTime.getMonthValue();
day = localDateTime.getDayOfMonth();
hour = localDateTime.getHour();
minute = localDateTime.getMinute();
second = localDateTime.getSecond();
}

long microSecond = DateUtils.getOrDefault(temporal, ChronoField.NANO_OF_SECOND) / 100L;
Expand Down Expand Up @@ -237,28 +226,15 @@ protected void init(String s) throws AnalysisException {

ZoneId zoneId = temporal.query(TemporalQueries.zone());
if (zoneId != null) {
// 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 = 0;
if (this.dataType instanceof TimeStampTzType) {
offset = ZoneId.of("UTC").getRules().getOffset(thatTime).getTotalSeconds()
- zoneId.getRules().getOffset(thatTime).getTotalSeconds();
} else {
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;
this.minute = result.minute;
this.hour = result.hour;
this.day = result.day;
this.month = result.month;
this.year = result.year;
}
ZoneId targetZone = this.dataType instanceof TimeStampTzType ? ZoneId.of("UTC") : DateUtils.getTimeZone();
LocalDateTime localDateTime = convertTimeZone(year, month, day, hour, minute, second,
zoneId, targetZone);
this.year = localDateTime.getYear();
this.month = localDateTime.getMonthValue();
this.day = localDateTime.getDayOfMonth();
this.hour = localDateTime.getHour();
this.minute = localDateTime.getMinute();
this.second = localDateTime.getSecond();
}

microSecond = DateUtils.getOrDefault(temporal, ChronoField.NANO_OF_SECOND) / 100L;
Expand Down Expand Up @@ -293,6 +269,14 @@ protected void init(String s) throws AnalysisException {
}
}

private static LocalDateTime convertTimeZone(long year, long month, long day, long hour, long minute,
long second, ZoneId fromZone, ZoneId toZone) {
return LocalDateTime.of((int) year, (int) month, (int) day, (int) hour, (int) minute, (int) second)
.atZone(fromZone)
.withZoneSameInstant(toZone)
.toLocalDateTime();
}

public boolean checkRange() {
return checkRange(year, month, day) || hour > MAX_DATETIME.getHour() || minute > MAX_DATETIME.getMinute()
|| second > MAX_DATETIME.getSecond() || microSecond > MAX_MICROSECOND;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql --
1 named_gap_ny 2024-03-10 07:30:00.000000+00:00
2 explicit_before_gap 2024-03-10 06:30:00.000000+00:00
3 explicit_after_gap 2024-03-10 07:30:00.000000+00:00
4 implicit_gap_ny 2024-03-10 07:30:00.000000+00:00
5 implicit_after_gap 2024-03-10 07:30:00.000000+00:00

-- !sql --
true

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

suite("test_timestamptz_dst_gap") {
sql """
DROP TABLE IF EXISTS tz_dst_gap;
"""

sql """
CREATE TABLE tz_dst_gap (
id INT,
label VARCHAR(64),
ts_tz TIMESTAMPTZ(6)
)
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 1
PROPERTIES('replication_num' = '1');
"""

sql "SET time_zone = 'Asia/Shanghai';"
sql """
INSERT INTO tz_dst_gap VALUES
(1, 'named_gap_ny', '2024-03-10 02:30:00 America/New_York'),
(2, 'explicit_before_gap', '2024-03-10 01:30:00 -05:00'),
(3, 'explicit_after_gap', '2024-03-10 03:30:00 -04:00');
"""

sql "SET time_zone = 'America/New_York';"
sql """
INSERT INTO tz_dst_gap VALUES
(4, 'implicit_gap_ny', '2024-03-10 02:30:00'),
(5, 'implicit_after_gap', '2024-03-10 03:30:00');
"""

sql "SET time_zone = '+00:00';"
qt_sql """
SELECT id, label, CAST(ts_tz AS VARCHAR(64)) AS rendered_utc
FROM tz_dst_gap
ORDER BY id;
"""

qt_sql """
SELECT t1.ts_tz = t2.ts_tz AS named_and_implicit_gap_match
FROM tz_dst_gap t1, tz_dst_gap t2
WHERE t1.id = 1 AND t2.id = 4;
"""
}
Loading