Skip to content
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

Support writing timestamp with timezone in BigQuery #17793

Merged
merged 1 commit into from
Jul 10, 2023
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 @@ -94,7 +94,7 @@ private BigQueryType() {}
1, // 9 digits after the dot
};
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("''HH:mm:ss.SSSSSS''");
private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("''yyyy-MM-dd HH:mm:ss.SSSSSS''");
private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSS").withZone(UTC);

private static RowType.Field toRawTypeField(String name, Field field)
{
Expand Down Expand Up @@ -158,7 +158,6 @@ public static String timeToStringConverter(Object value)
return TIME_FORMATTER.format(toZonedDateTime(epochSeconds, nanoAdjustment, UTC));
}

@VisibleForTesting
public static String timestampToStringConverter(Object value)
{
LongTimestampWithTimeZone timestamp = (LongTimestampWithTimeZone) value;
Expand Down Expand Up @@ -289,7 +288,7 @@ public static Optional<String> convertToString(Type type, StandardSQLTypeName bi
case DATE:
return Optional.of(dateToStringConverter(value));
case DATETIME:
return Optional.of(datetimeToStringConverter(value));
return Optional.of(datetimeToStringConverter(value)).map("'%s'"::formatted);
case FLOAT64:
return Optional.of(floatToStringConverter(value));
case INT64:
Expand All @@ -312,7 +311,7 @@ public static Optional<String> convertToString(Type type, StandardSQLTypeName bi
case TIME:
return Optional.of(timeToStringConverter(value));
case TIMESTAMP:
return Optional.of(timestampToStringConverter(value));
return Optional.of(timestampToStringConverter(value)).map("'%s'"::formatted);
default:
throw new IllegalArgumentException("Unsupported type: " + bigqueryType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.trino.spi.block.Block;
import io.trino.spi.type.ArrayType;
import io.trino.spi.type.DecimalType;
import io.trino.spi.type.LongTimestampWithTimeZone;
import io.trino.spi.type.RowType;
import io.trino.spi.type.Type;
import io.trino.spi.type.VarcharType;
Expand All @@ -31,6 +32,7 @@
import java.util.List;
import java.util.Map;

import static io.trino.plugin.bigquery.BigQueryType.timestampToStringConverter;
import static io.trino.plugin.bigquery.BigQueryType.toZonedDateTime;
import static io.trino.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
Expand All @@ -42,6 +44,7 @@
import static io.trino.spi.type.IntegerType.INTEGER;
import static io.trino.spi.type.SmallintType.SMALLINT;
import static io.trino.spi.type.TimestampType.TIMESTAMP_MICROS;
import static io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_MICROS;
import static io.trino.spi.type.Timestamps.MICROSECONDS_PER_SECOND;
import static io.trino.spi.type.Timestamps.NANOSECONDS_PER_MICROSECOND;
import static io.trino.spi.type.TinyintType.TINYINT;
Expand All @@ -65,7 +68,7 @@ public static Object readNativeValue(Type type, Block block, int position)
return null;
}

// TODO https://github.com/trinodb/trino/issues/13741 Add support for time, timestamp with time zone, geography, map type
// TODO https://github.com/trinodb/trino/issues/13741 Add support for time, geography, map type
if (type.equals(BOOLEAN)) {
return BOOLEAN.getBoolean(block, position);
}
Expand Down Expand Up @@ -103,6 +106,10 @@ public static Object readNativeValue(Type type, Block block, int position)
int nanoAdjustment = floorMod(epochMicros, MICROSECONDS_PER_SECOND) * NANOSECONDS_PER_MICROSECOND;
return DATETIME_FORMATTER.format(toZonedDateTime(epochSeconds, nanoAdjustment, UTC));
}
if (type.equals(TIMESTAMP_TZ_MICROS)) {
LongTimestampWithTimeZone timestamp = (LongTimestampWithTimeZone) TIMESTAMP_TZ_MICROS.getObject(block, position);
return timestampToStringConverter(timestamp);
}
if (type instanceof ArrayType arrayType) {
Block arrayBlock = block.getObject(position, Block.class);
ImmutableList.Builder<Object> list = ImmutableList.builderWithExpectedSize(arrayBlock.getPositionCount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ protected Optional<DataMappingTestSetup> filterDataMappingSmokeTestData(DataMapp
case "timestamp":
case "timestamp(3)":
case "timestamp(3) with time zone":
ebyhr marked this conversation as resolved.
Show resolved Hide resolved
case "timestamp(6) with time zone":
regadas marked this conversation as resolved.
Show resolved Hide resolved
return Optional.of(dataMappingTestSetup.asUnsupported());
}
return Optional.of(dataMappingTestSetup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import io.trino.spi.type.ArrayType;
import io.trino.spi.type.RowType;
import io.trino.spi.type.RowType.Field;
import io.trino.spi.type.TimeZoneKey;
import io.trino.testing.AbstractTestQueryFramework;
import io.trino.testing.TestingSession;
import io.trino.testing.datatype.CreateAndInsertDataSetup;
import io.trino.testing.datatype.CreateAndTrinoInsertDataSetup;
import io.trino.testing.datatype.CreateAsSelectDataSetup;
Expand All @@ -31,6 +33,7 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.time.ZoneId;
import java.util.Optional;

import static io.trino.spi.type.BigintType.BIGINT;
Expand All @@ -45,6 +48,7 @@
import static io.trino.spi.type.VarcharType.VARCHAR;
import static io.trino.testing.TestingNames.randomNameSuffix;
import static java.lang.String.format;
import static java.time.ZoneOffset.UTC;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
Expand All @@ -54,6 +58,9 @@ public abstract class BaseBigQueryTypeMapping
extends AbstractTestQueryFramework
{
private BigQueryQueryRunner.BigQuerySqlExecutor bigQuerySqlExecutor;
private final ZoneId jvmZone = ZoneId.systemDefault();
private final ZoneId vilnius = ZoneId.of("Europe/Vilnius");
private final ZoneId kathmandu = ZoneId.of("Asia/Kathmandu");

@BeforeClass(alwaysRun = true)
public void initBigQueryExecutor()
Expand Down Expand Up @@ -526,44 +533,129 @@ public void testTime()
.execute(getQueryRunner(), bigqueryViewCreateAndInsert("test.time"));
}

@Test
public void testTimestampWithTimeZone()
@Test(dataProvider = "sessionZonesDataProvider")
public void testTimestampWithTimeZone(ZoneId zoneId)
{
SqlDataTypeTest.create()
Session session = Session.builder(getSession())
.setTimeZoneKey(TimeZoneKey.getTimeZoneKey(zoneId.getId()))
.build();

testTimestampWithTimeZone("TIMESTAMP(6) WITH TIME ZONE")
.execute(getQueryRunner(), trinoCreateAsSelect("test.timestamp_tz"))
.execute(getQueryRunner(), trinoCreateAsSelect(session, "test.timestamp_tz"))
.execute(getQueryRunner(), session, trinoCreateAsSelect("test.timestamp_tz"));

testTimestampWithTimeZone("TIMESTAMP")
.execute(getQueryRunner(), bigqueryCreateAndInsert("test.timestamp_tz"));
Comment on lines +548 to +549
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

private SqlDataTypeTest testTimestampWithTimeZone(String inputType)
{
return SqlDataTypeTest.create()
// min value in BigQuery
.addRoundTrip("TIMESTAMP", "TIMESTAMP '0001-01-01 00:00:00.000000 UTC'",
.addRoundTrip(inputType, "TIMESTAMP '0001-01-01 00:00:00.000 UTC'",
ebyhr marked this conversation as resolved.
Show resolved Hide resolved
regadas marked this conversation as resolved.
Show resolved Hide resolved
TIMESTAMP_TZ_MICROS, "TIMESTAMP '0001-01-01 00:00:00.000000 UTC'")
.addRoundTrip("TIMESTAMP", "TIMESTAMP '1970-01-01 00:00:00.000000 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1970-01-01 00:00:00.000000 UTC'")
.addRoundTrip("TIMESTAMP", "TIMESTAMP '1970-01-01 00:00:00.000000 Asia/Kathmandu'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1969-12-31 18:30:00.000000 UTC'")
.addRoundTrip("TIMESTAMP", "TIMESTAMP '1970-01-01 00:00:00.000000+02:17'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1969-12-31 21:43:00.000000 UTC'")
.addRoundTrip("TIMESTAMP", "TIMESTAMP '1970-01-01 00:00:00.000000-07:31'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1970-01-01 07:31:00.000000 UTC'")
.addRoundTrip("TIMESTAMP", "TIMESTAMP '1958-01-01 13:18:03.123456 UTC'",
// before epoch
.addRoundTrip(inputType, "TIMESTAMP '1958-01-01 13:18:03.123 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1958-01-01 13:18:03.123000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1958-01-01 13:18:03.123456 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1958-01-01 13:18:03.123456 UTC'")
.addRoundTrip("TIMESTAMP", "TIMESTAMP '1958-01-01 13:18:03.123000 Asia/Kathmandu'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1958-01-01 07:48:03.123000 UTC'")
.addRoundTrip("TIMESTAMP", "TIMESTAMP '1958-01-01 13:18:03.123000+02:17'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1958-01-01 11:01:03.123000 UTC'")
.addRoundTrip("TIMESTAMP", "TIMESTAMP '1958-01-01 13:18:03.123000-07:31'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1958-01-01 20:49:03.123000 UTC'")
.addRoundTrip("TIMESTAMP", "TIMESTAMP '2019-03-18 10:01:17.987654 UTC'",
.addRoundTrip(inputType, "TIMESTAMP '1958-01-01 13:18:03.123000 Asia/Kathmandu'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1958-01-01 07:48:03.123000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1969-12-31 23:59:59.999995 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1969-12-31 23:59:59.999995 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1969-12-31 23:59:59.999949 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1969-12-31 23:59:59.999949 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1969-12-31 23:59:59.999994 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1969-12-31 23:59:59.999994 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1970-01-01 00:00:00.000000 Asia/Kathmandu'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1969-12-31 18:30:00.000000 UTC'")
// epoch
.addRoundTrip(inputType, "TIMESTAMP '1970-01-01 00:00:00.000 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1970-01-01 00:00:00.000000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1970-01-01 00:00:00.000000 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1970-01-01 00:00:00.000000 UTC'")
// after epoch
.addRoundTrip(inputType, "TIMESTAMP '1970-01-01 00:00:01 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1970-01-01 00:00:01.000000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1970-01-01 00:00:01.1 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1970-01-01 00:00:01.100000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1970-01-01 00:00:01.12 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1970-01-01 00:00:01.120000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1970-01-01 00:00:01.123 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1970-01-01 00:00:01.123000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1970-01-01 00:00:01.1234 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1970-01-01 00:00:01.123400 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1970-01-01 00:00:01.12345 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1970-01-01 00:00:01.123450 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1970-01-01 00:00:01.123456 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1970-01-01 00:00:01.123456 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1970-01-01 00:13:42.000 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1970-01-01 00:13:42.000000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1970-01-01 00:13:42.123456 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1970-01-01 00:13:42.123456 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1986-01-01 00:13:07.000 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1986-01-01 00:13:07.000000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '1986-01-01 00:13:07.456789 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '1986-01-01 00:13:07.456789 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '2018-03-25 03:17:17.000 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2018-03-25 03:17:17.000000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '2018-03-25 03:17:17.456789 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2018-03-25 03:17:17.456789 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '2018-04-01 02:13:55.123 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2018-04-01 02:13:55.123000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '2018-04-01 02:13:55.123456 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2018-04-01 02:13:55.123456 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '2018-10-28 01:33:17.456 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2018-10-28 01:33:17.456000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '2018-10-28 01:33:17.123456 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2018-10-28 01:33:17.123456 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '2018-10-28 03:33:33.333 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2018-10-28 03:33:33.333000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '2018-10-28 03:33:33.333333 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2018-10-28 03:33:33.333333 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '2019-03-18 10:01:17.987 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2019-03-18 10:01:17.987000 UTC'")
.addRoundTrip(inputType, "TIMESTAMP '2019-03-18 10:01:17.987654 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2019-03-18 10:01:17.987654 UTC'")
.addRoundTrip("TIMESTAMP", "TIMESTAMP '2019-03-18 10:01:17.987000 Asia/Kathmandu'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2019-03-18 04:16:17.987000 UTC'")
.addRoundTrip("TIMESTAMP", "TIMESTAMP '2019-03-18 10:01:17.987000+02:17'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2019-03-18 07:44:17.987000 UTC'")
.addRoundTrip("TIMESTAMP", "TIMESTAMP '2019-03-18 10:01:17.987000-07:31'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2019-03-18 17:32:17.987000 UTC'")
.addRoundTrip("TIMESTAMP", "TIMESTAMP '2021-09-07 23:59:59.999999-00:00'",
.addRoundTrip(inputType, "TIMESTAMP '2021-09-07 23:59:59.999999 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '2021-09-07 23:59:59.999999 UTC'")
// max value in BigQuery
ebyhr marked this conversation as resolved.
Show resolved Hide resolved
.addRoundTrip("TIMESTAMP", "TIMESTAMP '9999-12-31 23:59:59.999999-00:00'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '9999-12-31 23:59:59.999999 UTC'")
.execute(getQueryRunner(), bigqueryCreateAndInsert("test.timestamp_tz"));
// TODO (https://github.com/trinodb/trino/pull/12210) Add support for timestamp with time zone type in views
.addRoundTrip(inputType, "TIMESTAMP '9999-12-31 23:59:59.999999 UTC'",
TIMESTAMP_TZ_MICROS, "TIMESTAMP '9999-12-31 23:59:59.999999 UTC'");
}

@DataProvider
public Object[][] sessionZonesDataProvider()
{
return new Object[][] {
{UTC},
{jvmZone},
// using two non-JVM zones so that we don't need to worry what BigQuery system zone is
{vilnius},
{kathmandu},
{TestingSession.DEFAULT_TIME_ZONE_KEY.getZoneId()},
};
}

@Test
public void testUnsupportedTimestampWithTimeZone()
{
try (TestTable table = new TestTable(getBigQuerySqlExecutor(), "test.unsupported_tz", "(col timestamp)")) {
assertQueryFails("INSERT INTO " + table.getName() + " VALUES (timestamp '-2021-09-07 23:59:59.999999 UTC')", "Failed to insert rows.*");
assertQueryFails("INSERT INTO " + table.getName() + " VALUES (timestamp '-0001-01-01 00:00:00.000000 UTC')", "Failed to insert rows.*");
assertQueryFails("INSERT INTO " + table.getName() + " VALUES (timestamp '0000-12-31 23:59:59.999999 UTC')", "Failed to insert rows.*");
assertQueryFails("INSERT INTO " + table.getName() + " VALUES (timestamp '10000-01-01 00:00:00.000000 UTC')", "Failed to insert rows.*");
regadas marked this conversation as resolved.
Show resolved Hide resolved

assertThatThrownBy(() -> getBigQuerySqlExecutor().execute("INSERT INTO " + table.getName() + " VALUES (timestamp '-2021-09-07 23:59:59.999999 UTC')"))
.hasMessageContaining("Invalid TIMESTAMP literal");
assertThatThrownBy(() -> getBigQuerySqlExecutor().execute("INSERT INTO " + table.getName() + " VALUES (timestamp '-0001-01-01 00:00:00.000000 UTC')"))
.hasMessageContaining("Invalid TIMESTAMP literal");
assertThatThrownBy(() -> getBigQuerySqlExecutor().execute("INSERT INTO " + table.getName() + " VALUES (timestamp '0000-12-31 23:59:59.999999 UTC')"))
.hasMessageContaining("Invalid TIMESTAMP literal");
assertThatThrownBy(() -> getBigQuerySqlExecutor().execute("INSERT INTO " + table.getName() + " VALUES (timestamp '10000-01-01 00:00:00.000000 UTC')"))
.hasMessageContaining("Invalid TIMESTAMP literal");
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.trino.plugin.bigquery;

import io.trino.spi.type.TimeZoneKey;
import org.testng.annotations.Test;

import static io.airlift.slice.Slices.utf8Slice;
Expand Down Expand Up @@ -42,7 +43,10 @@ public void testTimestampToStringConverter()
{
assertThat(BigQueryType.timestampToStringConverter(
fromEpochSecondsAndFraction(1585658096, 123_456_000_000L, UTC_KEY)))
.isEqualTo("'2020-03-31 12:34:56.123456'");
.isEqualTo("2020-03-31 12:34:56.123456");
assertThat(BigQueryType.timestampToStringConverter(
fromEpochSecondsAndFraction(1585658096, 123_456_000_000L, TimeZoneKey.getTimeZoneKey("Asia/Kathmandu"))))
.isEqualTo("2020-03-31 12:34:56.123456");
}

@Test
Expand Down