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

[#4743]fix(trino-connector): Fix the default precision of time and timestamp in the Iceberg catalog. #4893

Merged
merged 1 commit into from
Sep 13, 2024
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 @@ -2,7 +2,7 @@ CREATE SCHEMA gt_iceberg.gt_db2;

USE gt_iceberg.gt_db2;

-- Unsupported Type: TINYINT, SMALLINT
-- Unsupported Type: CHAR TINYINT, SMALLINT
CREATE TABLE tb01 (
f1 VARCHAR,
f3 VARBINARY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ CREATE TABLE
f11 integer,
f12 bigint,
f13 date,
f14 time(3),
f15 timestamp(3)
f14 time(6),
f15 timestamp(6)
)
COMMENT ''
WITH (
Expand All @@ -28,7 +28,7 @@ INSERT: 1 row

INSERT: 1 row

"Sample text 1","65","123.456","7.89","12.34","true","1000","1000","100000","2024-01-01","08:00:00.000","2024-01-01 08:00:00.000"
"Sample text 1","65","123.456","7.89","12.34","true","1000","1000","100000","2024-01-01","08:00:00.000000","2024-01-01 08:00:00.000000"
"","","","","","","","","","","",""

CREATE TABLE
Expand All @@ -44,8 +44,8 @@ CREATE TABLE
f11 integer NOT NULL,
f12 bigint NOT NULL,
f13 date NOT NULL,
f14 time(3) NOT NULL,
f15 timestamp(3) NOT NULL
f14 time(6) NOT NULL,
f15 timestamp(6) NOT NULL
)
COMMENT ''
WITH (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
package org.apache.gravitino.trino.connector.catalog.iceberg;

import io.trino.spi.TrinoException;
import io.trino.spi.type.TimeType;
import io.trino.spi.type.TimestampType;
import io.trino.spi.type.TimestampWithTimeZoneType;
import io.trino.spi.type.VarbinaryType;
import io.trino.spi.type.VarcharType;
import org.apache.gravitino.rel.types.Type;
Copy link
Contributor

Choose a reason for hiding this comment

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

seems you shoud add corresponding logic to convert MICROS time to Iceberg time, and forbidden other time precision.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@diqiu50 diqiu50 Sep 11, 2024

Choose a reason for hiding this comment

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

Trino iceberg connector can automatically convert other time precision to microsecond

Copy link
Contributor

Choose a reason for hiding this comment

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

what will happen if the user creates an Iceberg table with timestamp(3) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It automatically converts to timestamp(6), The behavior is the same as in the Trino Iceberg catalog.

Copy link
Contributor

Choose a reason for hiding this comment

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

according to the code https://github.com/trinodb/trino/blob/457/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/TypeConverter.java#L148-L177, it will throw an exception in origin Trino Iceberg catalog, Yes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

Copy link
Contributor

Choose a reason for hiding this comment

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

so I don't think it's the right behavior to convert timestamp(3) to timestamp(6) in Gravitino Iceberg connector, it conflict with original behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Trino Iceberg connector can automatically converts timestamp and timestamp(n) to timestamp(6),
The Gravitino connector Iceberg catalog's behavior is the same as Trino Iceberg connector

Expand All @@ -45,7 +48,6 @@ public Type getGravitinoType(io.trino.spi.type.Type type) {
GravitinoErrorCode.GRAVITINO_ILLEGAL_ARGUMENT,
"Iceberg does not support the datatype VARCHAR with length");
}

return Types.StringType.get();
}

Expand All @@ -56,7 +58,17 @@ public Type getGravitinoType(io.trino.spi.type.Type type) {
public io.trino.spi.type.Type getTrinoType(Type type) {
if (Name.FIXED == type.name()) {
return VarbinaryType.VARBINARY;
} else if (Name.TIME == type.name()) {
return TimeType.TIME_MICROS;
} else if (Name.TIMESTAMP == type.name()) {
Types.TimestampType timestampType = (Types.TimestampType) type;
if (timestampType.hasTimeZone()) {
return TimestampWithTimeZoneType.TIMESTAMP_TZ_MICROS;
} else {
return TimestampType.TIMESTAMP_MICROS;
}
}

return super.getTrinoType(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.trino.spi.type.SmallintType;
import io.trino.spi.type.TimeType;
import io.trino.spi.type.TimestampType;
import io.trino.spi.type.TimestampWithTimeZoneType;
import io.trino.spi.type.TinyintType;
import io.trino.spi.type.Type;
import io.trino.spi.type.TypeOperators;
Expand Down Expand Up @@ -103,7 +104,11 @@ public Type getTrinoType(org.apache.gravitino.rel.types.Type type) {
case TIME:
return TimeType.TIME_MILLIS;
case TIMESTAMP:
return TimestampType.TIMESTAMP_MILLIS;
if (((Types.TimestampType) type).hasTimeZone()) {
return TimestampWithTimeZoneType.TIMESTAMP_TZ_MILLIS;
FANNG1 marked this conversation as resolved.
Show resolved Hide resolved
} else {
return TimestampType.TIMESTAMP_MILLIS;
}
case LIST:
return new ArrayType(getTrinoType(((Types.ListType) type).elementType()));
case MAP:
Expand Down Expand Up @@ -173,10 +178,12 @@ public org.apache.gravitino.rel.types.Type getGravitinoType(Type type) {
return Types.BinaryType.get();
} else if (typeClass == io.trino.spi.type.DateType.class) {
return Types.DateType.get();
} else if (typeClass == io.trino.spi.type.TimeType.class) {
} else if (io.trino.spi.type.TimeType.class.isAssignableFrom(typeClass)) {
return Types.TimeType.get();
} else if (io.trino.spi.type.TimestampType.class.isAssignableFrom(typeClass)) {
return Types.TimestampType.withoutTimeZone();
} else if (io.trino.spi.type.TimestampWithTimeZoneType.class.isAssignableFrom(typeClass)) {
return Types.TimestampType.withTimeZone();
} else if (typeClass == io.trino.spi.type.ArrayType.class) {
// Ignore nullability for the type, we could only get nullability from column metadata
return Types.ListType.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
package org.apache.gravitino.trino.connector.catalog.iceberg;

import io.trino.spi.TrinoException;
import io.trino.spi.type.TimeType;
import io.trino.spi.type.TimestampType;
import io.trino.spi.type.TimestampWithTimeZoneType;
import io.trino.spi.type.VarcharType;
import org.apache.gravitino.rel.types.Types;
import org.apache.gravitino.trino.connector.util.GeneralDataTypeTransformer;
Expand Down Expand Up @@ -51,5 +54,16 @@ public void testTrinoTypeToGravitinoType() {
Assertions.assertEquals(
generalDataTypeTransformer.getGravitinoType(varcharTypeWithoutLength),
Types.StringType.get());

Assertions.assertEquals(
generalDataTypeTransformer.getTrinoType(Types.TimeType.get()), TimeType.TIME_MICROS);

Assertions.assertEquals(
generalDataTypeTransformer.getTrinoType(Types.TimestampType.withoutTimeZone()),
TimestampType.TIMESTAMP_MICROS);

Assertions.assertEquals(
generalDataTypeTransformer.getTrinoType(Types.TimestampType.withTimeZone()),
TimestampWithTimeZoneType.TIMESTAMP_TZ_MICROS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.trino.spi.type.SmallintType;
import io.trino.spi.type.TimeType;
import io.trino.spi.type.TimestampType;
import io.trino.spi.type.TimestampWithTimeZoneType;
import io.trino.spi.type.TinyintType;
import io.trino.spi.type.TypeOperators;
import io.trino.spi.type.UuidType;
Expand Down Expand Up @@ -91,6 +92,9 @@ public void testGetGravitinoType() {
Assertions.assertEquals(
dataTypeTransformer.getGravitinoType(TimestampType.TIMESTAMP_MILLIS),
Types.TimestampType.withoutTimeZone());
Assertions.assertEquals(
dataTypeTransformer.getGravitinoType(TimestampWithTimeZoneType.TIMESTAMP_TZ_MILLIS),
Types.TimestampType.withTimeZone());

Assertions.assertEquals(
dataTypeTransformer.getGravitinoType(new ArrayType(IntegerType.INTEGER)),
Expand Down Expand Up @@ -168,6 +172,9 @@ public void testGetTrinoType() {
Assertions.assertEquals(
dataTypeTransformer.getTrinoType(Types.TimestampType.withoutTimeZone()),
TimestampType.TIMESTAMP_MILLIS);
Assertions.assertEquals(
dataTypeTransformer.getTrinoType(Types.TimestampType.withTimeZone()),
TimestampWithTimeZoneType.TIMESTAMP_TZ_MILLIS);

Assertions.assertEquals(
dataTypeTransformer.getTrinoType(Types.ListType.nullable(Types.IntegerType.get())),
Expand Down
Loading