-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix insert to Clickhouse TimestampWithTimeZone #23785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,6 @@ | |
import io.trino.spi.type.Decimals; | ||
import io.trino.spi.type.Int128; | ||
import io.trino.spi.type.StandardTypes; | ||
import io.trino.spi.type.TimeZoneKey; | ||
import io.trino.spi.type.Type; | ||
import io.trino.spi.type.TypeManager; | ||
import io.trino.spi.type.TypeSignature; | ||
|
@@ -104,6 +103,7 @@ | |
import java.util.Map.Entry; | ||
import java.util.Optional; | ||
import java.util.OptionalLong; | ||
import java.util.TimeZone; | ||
import java.util.UUID; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.BiFunction; | ||
|
@@ -167,7 +167,6 @@ | |
import static io.trino.spi.type.BooleanType.BOOLEAN; | ||
import static io.trino.spi.type.DateTimeEncoding.packDateTimeWithZone; | ||
import static io.trino.spi.type.DateTimeEncoding.unpackMillisUtc; | ||
import static io.trino.spi.type.DateTimeEncoding.unpackZoneKey; | ||
import static io.trino.spi.type.DateType.DATE; | ||
import static io.trino.spi.type.DecimalType.createDecimalType; | ||
import static io.trino.spi.type.DoubleType.DOUBLE; | ||
|
@@ -733,7 +732,7 @@ public Optional<ColumnMapping> toColumnMapping(ConnectorSession session, Connect | |
return Optional.of(ColumnMapping.longMapping( | ||
TIMESTAMP_TZ_SECONDS, | ||
shortTimestampWithTimeZoneReadFunction(), | ||
shortTimestampWithTimeZoneWriteFunction())); | ||
shortTimestampWithTimeZoneWriteFunction(column.getTimeZone()))); | ||
} | ||
} | ||
|
||
|
@@ -915,12 +914,12 @@ private static LongReadFunction shortTimestampWithTimeZoneReadFunction() | |
}; | ||
} | ||
|
||
private static LongWriteFunction shortTimestampWithTimeZoneWriteFunction() | ||
private static LongWriteFunction shortTimestampWithTimeZoneWriteFunction(TimeZone columnTimeZone) | ||
{ | ||
return (statement, index, value) -> { | ||
long millisUtc = unpackMillisUtc(value); | ||
TimeZoneKey timeZoneKey = unpackZoneKey(value); | ||
statement.setObject(index, Instant.ofEpochMilli(millisUtc).atZone(timeZoneKey.getZoneId())); | ||
// Clickhouse JDBC driver inserts datetime as string value as yyyy-MM-dd HH:mm:ss and zone from the Column metadata would be used. | ||
statement.setObject(index, Instant.ofEpochMilli(millisUtc).atZone(columnTimeZone.toZoneId())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to perform this translation ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other options I tried didn't work for me. Do you something concrete in mind?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant do we need this translation ? Currently Clickhouse's PreparedStatement implementation performs this translation i.e statement.setObject(index, Instant.ofEpochMilli(millisUtc).atZone(timeZoneKey.getZoneId())); - internally would operate on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does not work There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it fail or ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, tests fail. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stacktrace
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we could try using
We have the Can we add this
As a code comment as it would be a bit confusing on why we need to convert it into a specific timezone as it would be same as |
||
}; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1003,6 +1003,12 @@ public void testClickHouseDateTimeWithTimeZone() | |
Session session = Session.builder(getSession()) | ||
.setTimeZoneKey(TimeZoneKey.getTimeZoneKey(sessionZone.getId())) | ||
.build(); | ||
SqlDataTypeTest.create() | ||
.addRoundTrip("DateTime('Asia/Kathmandu')", "timestamp '2024-01-01 12:34:56'", TIMESTAMP_TZ_SECONDS, "TIMESTAMP '2024-01-01 05:19:56 +05:45'") | ||
.addRoundTrip("DateTime('Asia/Kathmandu')", "timestamp '2024-01-01 12:34:56 Asia/Kathmandu'", TIMESTAMP_TZ_SECONDS, "TIMESTAMP '2024-01-01 12:34:56 +05:45'") | ||
.addRoundTrip("DateTime('Asia/Kathmandu')", "timestamp '2024-01-01 12:34:56 +00:00'", TIMESTAMP_TZ_SECONDS, "TIMESTAMP '2024-01-01 18:19:56 +05:45'") | ||
.addRoundTrip("DateTime('Asia/Kathmandu')", "timestamp '2024-01-01 12:34:56 -01:00'", TIMESTAMP_TZ_SECONDS, "TIMESTAMP '2024-01-01 19:19:56 +05:45'") | ||
.execute(getQueryRunner(), session, clickhouseCreateAndTrinoInsert("tpch.test_timestamp_with_time_zone")); | ||
Comment on lines
+1006
to
+1011
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Praveen2112 without the change tests fail with:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clickhouse does not support Create table with TimestampWithTimeZone yet, thus only insert is tested to columns created by clickhouse. |
||
|
||
dateTimeWithTimeZoneTest(clickhouseDateTimeInputTypeFactory("datetime")) | ||
.execute(getQueryRunner(), session, clickhouseCreateAndInsert("tpch.datetime_tz")); | ||
|
Uh oh!
There was an error while loading. Please reload this page.