wombatu-kun commented on code in PR #20680: URL: https://github.com/apache/hudi/pull/20680#discussion_r4596070379
########## hudi-trino/src/main/java/io/trino/plugin/hudi/util/TrinoTypeMapper.java: ########## @@ -0,0 +1,134 @@ +/* + * Licensed 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. + */ +package io.trino.plugin.hudi.util; + +import io.trino.spi.TrinoException; +import io.trino.spi.type.ArrayType; +import io.trino.spi.type.MapType; +import io.trino.spi.type.RowType; +import io.trino.spi.type.Type; +import io.trino.spi.type.TypeOperators; +import org.apache.hudi.common.schema.HoodieSchema; +import org.apache.hudi.common.schema.HoodieSchema.TimePrecision; +import org.apache.hudi.common.schema.HoodieSchemaType; + +import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED; +import static io.trino.spi.type.BigintType.BIGINT; +import static io.trino.spi.type.BooleanType.BOOLEAN; +import static io.trino.spi.type.DateType.DATE; +import static io.trino.spi.type.DecimalType.createDecimalType; +import static io.trino.spi.type.Decimals.MAX_PRECISION; +import static io.trino.spi.type.DoubleType.DOUBLE; +import static io.trino.spi.type.IntegerType.INTEGER; +import static io.trino.spi.type.RealType.REAL; +import static io.trino.spi.type.TimeType.createTimeType; +import static io.trino.spi.type.TimestampType.createTimestampType; +import static io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType; +import static io.trino.spi.type.TinyintType.TINYINT; +import static io.trino.spi.type.UuidType.UUID; +import static io.trino.spi.type.VarbinaryType.VARBINARY; +import static io.trino.spi.type.VarcharType.VARCHAR; +import static java.util.Objects.requireNonNull; + +/** + * Maps {@link HoodieSchema} to Trino types directly, one Trino type per {@link HoodieSchemaType}. + * <p> + * The connector does not use this mapping yet: its column types still come from the Hive type strings in the + * metastore. That path gives different types for the rows marked below, because it goes through what Hive can + * declare. Moving the connector onto this mapping changes those column types and is a separate, gated step. + * <table> + * <caption>HoodieSchema to Trino</caption> + * <tr><th>HoodieSchema</th><th>Trino</th><th>Type identity</th></tr> + * <tr><td>BOOLEAN, INT, LONG, FLOAT, DOUBLE</td><td>boolean, integer, bigint, real, double</td><td>kept</td></tr> + * <tr><td>STRING, BYTES</td><td>varchar, varbinary</td><td>kept</td></tr> + * <tr><td>DECIMAL(p, s)</td><td>decimal(p, s), up to precision 38</td><td>kept</td></tr> + * <tr><td>DATE</td><td>date</td><td>kept</td></tr> + * <tr><td>TIME millis, micros</td><td>time(3), time(6)</td><td>kept; integer or bigint through the metastore</td></tr> + * <tr><td>TIMESTAMP millis, micros, UTC-adjusted</td><td>timestamp(3), timestamp(6) with time zone</td> + * <td>kept; timestamp(9) through the metastore</td></tr> + * <tr><td>TIMESTAMP millis, micros, local</td><td>timestamp(3), timestamp(6)</td><td>kept; timestamp(9) through the metastore</td></tr> + * <tr><td>UUID</td><td>uuid</td><td>kept; varbinary through the metastore</td></tr> + * <tr><td>ARRAY, MAP, RECORD</td><td>array, map with varchar keys, row</td><td>kept</td></tr> + * <tr><td>ENUM</td><td>varchar</td><td>lost: Trino has no enum type</td></tr> + * <tr><td>FIXED</td><td>varbinary</td><td>lost: Trino has no fixed-length binary type</td></tr> + * <tr><td>VECTOR</td><td>array of real, double or tinyint</td><td>lost: the dimension; varbinary through the metastore</td></tr> + * <tr><td>BLOB, VARIANT</td><td>row of their fields</td><td>lost: Trino sees a plain row</td></tr> + * </table> + * A nullable type maps as its non-null type; Trino carries nullability on the column. Other unions and NULL have no + * Trino type and are rejected. + */ +public final class TrinoTypeMapper +{ + private final TypeOperators typeOperators; + + public TrinoTypeMapper(TypeOperators typeOperators) + { + this.typeOperators = requireNonNull(typeOperators, "typeOperators is null"); + } + + public Type toTypeSystem(HoodieSchema schema) Review Comment: Trino's convention for this exact mapping is toTrinoType (the iceberg TypeConverter uses the same name), and toTypeSystem also misrepresents the return value, a single Type, while this class holds the actual type-system machinery in its TypeOperators field. Rename the method to toTrinoType before the connector starts calling it. ########## hudi-trino/src/main/java/io/trino/plugin/hudi/util/TrinoTypeMapper.java: ########## @@ -0,0 +1,134 @@ +/* + * Licensed 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. + */ +package io.trino.plugin.hudi.util; + +import io.trino.spi.TrinoException; +import io.trino.spi.type.ArrayType; +import io.trino.spi.type.MapType; +import io.trino.spi.type.RowType; +import io.trino.spi.type.Type; +import io.trino.spi.type.TypeOperators; +import org.apache.hudi.common.schema.HoodieSchema; +import org.apache.hudi.common.schema.HoodieSchema.TimePrecision; +import org.apache.hudi.common.schema.HoodieSchemaType; + +import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED; +import static io.trino.spi.type.BigintType.BIGINT; +import static io.trino.spi.type.BooleanType.BOOLEAN; +import static io.trino.spi.type.DateType.DATE; +import static io.trino.spi.type.DecimalType.createDecimalType; +import static io.trino.spi.type.Decimals.MAX_PRECISION; +import static io.trino.spi.type.DoubleType.DOUBLE; +import static io.trino.spi.type.IntegerType.INTEGER; +import static io.trino.spi.type.RealType.REAL; +import static io.trino.spi.type.TimeType.createTimeType; +import static io.trino.spi.type.TimestampType.createTimestampType; +import static io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType; +import static io.trino.spi.type.TinyintType.TINYINT; +import static io.trino.spi.type.UuidType.UUID; +import static io.trino.spi.type.VarbinaryType.VARBINARY; +import static io.trino.spi.type.VarcharType.VARCHAR; +import static java.util.Objects.requireNonNull; + +/** + * Maps {@link HoodieSchema} to Trino types directly, one Trino type per {@link HoodieSchemaType}. + * <p> + * The connector does not use this mapping yet: its column types still come from the Hive type strings in the + * metastore. That path gives different types for the rows marked below, because it goes through what Hive can + * declare. Moving the connector onto this mapping changes those column types and is a separate, gated step. + * <table> + * <caption>HoodieSchema to Trino</caption> + * <tr><th>HoodieSchema</th><th>Trino</th><th>Type identity</th></tr> + * <tr><td>BOOLEAN, INT, LONG, FLOAT, DOUBLE</td><td>boolean, integer, bigint, real, double</td><td>kept</td></tr> + * <tr><td>STRING, BYTES</td><td>varchar, varbinary</td><td>kept</td></tr> + * <tr><td>DECIMAL(p, s)</td><td>decimal(p, s), up to precision 38</td><td>kept</td></tr> + * <tr><td>DATE</td><td>date</td><td>kept</td></tr> + * <tr><td>TIME millis, micros</td><td>time(3), time(6)</td><td>kept; integer or bigint through the metastore</td></tr> + * <tr><td>TIMESTAMP millis, micros, UTC-adjusted</td><td>timestamp(3), timestamp(6) with time zone</td> + * <td>kept; timestamp(9) through the metastore</td></tr> + * <tr><td>TIMESTAMP millis, micros, local</td><td>timestamp(3), timestamp(6)</td><td>kept; timestamp(9) through the metastore</td></tr> + * <tr><td>UUID</td><td>uuid</td><td>kept; varbinary through the metastore</td></tr> + * <tr><td>ARRAY, MAP, RECORD</td><td>array, map with varchar keys, row</td><td>kept</td></tr> + * <tr><td>ENUM</td><td>varchar</td><td>lost: Trino has no enum type</td></tr> + * <tr><td>FIXED</td><td>varbinary</td><td>lost: Trino has no fixed-length binary type</td></tr> + * <tr><td>VECTOR</td><td>array of real, double or tinyint</td><td>lost: the dimension; varbinary through the metastore</td></tr> + * <tr><td>BLOB, VARIANT</td><td>row of their fields</td><td>lost: Trino sees a plain row</td></tr> + * </table> + * A nullable type maps as its non-null type; Trino carries nullability on the column. Other unions and NULL have no + * Trino type and are rejected. + */ +public final class TrinoTypeMapper +{ + private final TypeOperators typeOperators; + + public TrinoTypeMapper(TypeOperators typeOperators) + { + this.typeOperators = requireNonNull(typeOperators, "typeOperators is null"); + } + + public Type toTypeSystem(HoodieSchema schema) Review Comment: toTypeSystem dereferences its argument with no guard while the constructor right above guards typeOperators with requireNonNull, so a null schema fails with a bare NPE. Add requireNonNull(schema, "schema is null") at the top of the method to match the class's own convention. ########## hudi-trino/src/main/java/io/trino/plugin/hudi/util/TrinoTypeMapper.java: ########## @@ -0,0 +1,134 @@ +/* + * Licensed 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. + */ +package io.trino.plugin.hudi.util; + +import io.trino.spi.TrinoException; +import io.trino.spi.type.ArrayType; +import io.trino.spi.type.MapType; +import io.trino.spi.type.RowType; +import io.trino.spi.type.Type; +import io.trino.spi.type.TypeOperators; +import org.apache.hudi.common.schema.HoodieSchema; +import org.apache.hudi.common.schema.HoodieSchema.TimePrecision; +import org.apache.hudi.common.schema.HoodieSchemaType; + +import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED; +import static io.trino.spi.type.BigintType.BIGINT; +import static io.trino.spi.type.BooleanType.BOOLEAN; +import static io.trino.spi.type.DateType.DATE; +import static io.trino.spi.type.DecimalType.createDecimalType; +import static io.trino.spi.type.Decimals.MAX_PRECISION; +import static io.trino.spi.type.DoubleType.DOUBLE; +import static io.trino.spi.type.IntegerType.INTEGER; +import static io.trino.spi.type.RealType.REAL; +import static io.trino.spi.type.TimeType.createTimeType; +import static io.trino.spi.type.TimestampType.createTimestampType; +import static io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType; +import static io.trino.spi.type.TinyintType.TINYINT; +import static io.trino.spi.type.UuidType.UUID; +import static io.trino.spi.type.VarbinaryType.VARBINARY; +import static io.trino.spi.type.VarcharType.VARCHAR; +import static java.util.Objects.requireNonNull; + +/** + * Maps {@link HoodieSchema} to Trino types directly, one Trino type per {@link HoodieSchemaType}. + * <p> + * The connector does not use this mapping yet: its column types still come from the Hive type strings in the + * metastore. That path gives different types for the rows marked below, because it goes through what Hive can + * declare. Moving the connector onto this mapping changes those column types and is a separate, gated step. + * <table> + * <caption>HoodieSchema to Trino</caption> + * <tr><th>HoodieSchema</th><th>Trino</th><th>Type identity</th></tr> + * <tr><td>BOOLEAN, INT, LONG, FLOAT, DOUBLE</td><td>boolean, integer, bigint, real, double</td><td>kept</td></tr> + * <tr><td>STRING, BYTES</td><td>varchar, varbinary</td><td>kept</td></tr> + * <tr><td>DECIMAL(p, s)</td><td>decimal(p, s), up to precision 38</td><td>kept</td></tr> + * <tr><td>DATE</td><td>date</td><td>kept</td></tr> + * <tr><td>TIME millis, micros</td><td>time(3), time(6)</td><td>kept; integer or bigint through the metastore</td></tr> + * <tr><td>TIMESTAMP millis, micros, UTC-adjusted</td><td>timestamp(3), timestamp(6) with time zone</td> + * <td>kept; timestamp(9) through the metastore</td></tr> + * <tr><td>TIMESTAMP millis, micros, local</td><td>timestamp(3), timestamp(6)</td><td>kept; timestamp(9) through the metastore</td></tr> + * <tr><td>UUID</td><td>uuid</td><td>kept; varbinary through the metastore</td></tr> + * <tr><td>ARRAY, MAP, RECORD</td><td>array, map with varchar keys, row</td><td>kept</td></tr> + * <tr><td>ENUM</td><td>varchar</td><td>lost: Trino has no enum type</td></tr> + * <tr><td>FIXED</td><td>varbinary</td><td>lost: Trino has no fixed-length binary type</td></tr> + * <tr><td>VECTOR</td><td>array of real, double or tinyint</td><td>lost: the dimension; varbinary through the metastore</td></tr> + * <tr><td>BLOB, VARIANT</td><td>row of their fields</td><td>lost: Trino sees a plain row</td></tr> + * </table> + * A nullable type maps as its non-null type; Trino carries nullability on the column. Other unions and NULL have no + * Trino type and are rejected. + */ +public final class TrinoTypeMapper +{ + private final TypeOperators typeOperators; + + public TrinoTypeMapper(TypeOperators typeOperators) + { + this.typeOperators = requireNonNull(typeOperators, "typeOperators is null"); + } + + public Type toTypeSystem(HoodieSchema schema) + { + HoodieSchema type = schema.isNullable() ? schema.getNonNullType() : schema; Review Comment: The local is a HoodieSchema node, not a Trino Type, so naming it `type` collides with the imported Type this class produces and reads badly at the recursive call sites. Rename it to something like nonNullSchema. ########## hudi-trino/src/test/java/io/trino/plugin/hudi/util/TestTrinoTypeMapper.java: ########## @@ -0,0 +1,124 @@ +/* + * Licensed 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. + */ +package io.trino.plugin.hudi.util; + +import io.trino.spi.TrinoException; +import io.trino.spi.type.ArrayType; +import io.trino.spi.type.MapType; +import io.trino.spi.type.RowType; +import io.trino.spi.type.Type; +import io.trino.spi.type.TypeOperators; +import org.apache.hudi.common.schema.HoodieSchema; +import org.apache.hudi.common.schema.HoodieSchemaField; +import org.apache.hudi.common.schema.HoodieSchemaType; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.List; +import java.util.stream.Stream; + +import static io.trino.spi.type.BigintType.BIGINT; +import static io.trino.spi.type.BooleanType.BOOLEAN; +import static io.trino.spi.type.DateType.DATE; +import static io.trino.spi.type.DecimalType.createDecimalType; +import static io.trino.spi.type.DoubleType.DOUBLE; +import static io.trino.spi.type.IntegerType.INTEGER; +import static io.trino.spi.type.RealType.REAL; +import static io.trino.spi.type.RowType.field; +import static io.trino.spi.type.TimeType.TIME_MICROS; +import static io.trino.spi.type.TimeType.TIME_MILLIS; +import static io.trino.spi.type.TimestampType.TIMESTAMP_MICROS; +import static io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS; +import static io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_MICROS; +import static io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_MILLIS; +import static io.trino.spi.type.TinyintType.TINYINT; +import static io.trino.spi.type.UuidType.UUID; +import static io.trino.spi.type.VarbinaryType.VARBINARY; +import static io.trino.spi.type.VarcharType.VARCHAR; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +final class TestTrinoTypeMapper +{ + private static final TypeOperators TYPE_OPERATORS = new TypeOperators(); + private static final TrinoTypeMapper MAPPER = new TrinoTypeMapper(TYPE_OPERATORS); + + @ParameterizedTest + @MethodSource("mappings") + void testToTypeSystem(HoodieSchema schema, Type expected) + { + assertThat(MAPPER.toTypeSystem(schema)).isEqualTo(expected); + // Trino carries nullability on the column, not in the type + assertThat(MAPPER.toTypeSystem(HoodieSchema.createNullable(schema))).isEqualTo(expected); + } + + static Stream<Arguments> mappings() + { + HoodieSchema struct = HoodieSchema.createRecord("point", null, null, List.of( + HoodieSchemaField.of("name", HoodieSchema.create(HoodieSchemaType.STRING), null, null), + HoodieSchemaField.of("at", HoodieSchema.createNullable(HoodieSchema.createLocalTimestampMicros()), null, HoodieSchema.NULL_VALUE))); + return Stream.of( + Arguments.of(HoodieSchema.create(HoodieSchemaType.BOOLEAN), BOOLEAN), + Arguments.of(HoodieSchema.create(HoodieSchemaType.INT), INTEGER), + Arguments.of(HoodieSchema.create(HoodieSchemaType.LONG), BIGINT), + Arguments.of(HoodieSchema.create(HoodieSchemaType.FLOAT), REAL), + Arguments.of(HoodieSchema.create(HoodieSchemaType.DOUBLE), DOUBLE), + Arguments.of(HoodieSchema.create(HoodieSchemaType.STRING), VARCHAR), + Arguments.of(HoodieSchema.create(HoodieSchemaType.BYTES), VARBINARY), + Arguments.of(HoodieSchema.createEnum("color", null, null, List.of("RED", "GREEN")), VARCHAR), + Arguments.of(HoodieSchema.createFixed("md5", null, null, 16), VARBINARY), + Arguments.of(HoodieSchema.createUUID(), UUID), + Arguments.of(HoodieSchema.createDate(), DATE), + Arguments.of(HoodieSchema.createDecimal(10, 2), createDecimalType(10, 2)), + Arguments.of(HoodieSchema.createDecimal("amount", null, null, 20, 4, 9), createDecimalType(20, 4)), + Arguments.of(HoodieSchema.createTimeMillis(), TIME_MILLIS), + Arguments.of(HoodieSchema.createTimeMicros(), TIME_MICROS), + Arguments.of(HoodieSchema.createTimestampMillis(), TIMESTAMP_TZ_MILLIS), + Arguments.of(HoodieSchema.createTimestampMicros(), TIMESTAMP_TZ_MICROS), + Arguments.of(HoodieSchema.createLocalTimestampMillis(), TIMESTAMP_MILLIS), + Arguments.of(HoodieSchema.createLocalTimestampMicros(), TIMESTAMP_MICROS), + Arguments.of(HoodieSchema.createArray(HoodieSchema.createNullable(HoodieSchema.create(HoodieSchemaType.LONG))), new ArrayType(BIGINT)), + Arguments.of(HoodieSchema.createMap(HoodieSchema.createDate()), new MapType(VARCHAR, DATE, TYPE_OPERATORS)), + Arguments.of(struct, RowType.from(List.of(field("name", VARCHAR), field("at", TIMESTAMP_MICROS)))), + Arguments.of(HoodieSchema.createVector(4), new ArrayType(REAL)), + Arguments.of(HoodieSchema.createVector(4, HoodieSchema.Vector.VectorElementType.DOUBLE), new ArrayType(DOUBLE)), + Arguments.of(HoodieSchema.createVector(4, HoodieSchema.Vector.VectorElementType.INT8), new ArrayType(TINYINT))); + } + + @Test + void testBlobAndVariantMapToRowsOfTheirFields() + { + for (HoodieSchema schema : List.of(HoodieSchema.createBlob(), HoodieSchema.createVariant())) { + RowType row = (RowType) MAPPER.toTypeSystem(schema); + assertThat(row.getFields()).extracting(rowField -> rowField.getName().orElseThrow()) Review Comment: testBlobAndVariantMapToRowsOfTheirFields asserts field names only, so a regression that mapped every field to the wrong Trino type would still pass; BLOB's default fields include a nested record, so asserting the full RowType would also cover recursion no other test exercises. Assert the expected RowType like the struct case does. ########## hudi-trino/src/main/java/io/trino/plugin/hudi/util/TrinoTypeMapper.java: ########## @@ -0,0 +1,134 @@ +/* + * Licensed 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. + */ +package io.trino.plugin.hudi.util; + +import io.trino.spi.TrinoException; +import io.trino.spi.type.ArrayType; +import io.trino.spi.type.MapType; +import io.trino.spi.type.RowType; +import io.trino.spi.type.Type; +import io.trino.spi.type.TypeOperators; +import org.apache.hudi.common.schema.HoodieSchema; +import org.apache.hudi.common.schema.HoodieSchema.TimePrecision; +import org.apache.hudi.common.schema.HoodieSchemaType; + +import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED; +import static io.trino.spi.type.BigintType.BIGINT; +import static io.trino.spi.type.BooleanType.BOOLEAN; +import static io.trino.spi.type.DateType.DATE; +import static io.trino.spi.type.DecimalType.createDecimalType; +import static io.trino.spi.type.Decimals.MAX_PRECISION; +import static io.trino.spi.type.DoubleType.DOUBLE; +import static io.trino.spi.type.IntegerType.INTEGER; +import static io.trino.spi.type.RealType.REAL; +import static io.trino.spi.type.TimeType.createTimeType; +import static io.trino.spi.type.TimestampType.createTimestampType; +import static io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType; +import static io.trino.spi.type.TinyintType.TINYINT; +import static io.trino.spi.type.UuidType.UUID; +import static io.trino.spi.type.VarbinaryType.VARBINARY; +import static io.trino.spi.type.VarcharType.VARCHAR; +import static java.util.Objects.requireNonNull; + +/** + * Maps {@link HoodieSchema} to Trino types directly, one Trino type per {@link HoodieSchemaType}. + * <p> + * The connector does not use this mapping yet: its column types still come from the Hive type strings in the + * metastore. That path gives different types for the rows marked below, because it goes through what Hive can + * declare. Moving the connector onto this mapping changes those column types and is a separate, gated step. + * <table> + * <caption>HoodieSchema to Trino</caption> + * <tr><th>HoodieSchema</th><th>Trino</th><th>Type identity</th></tr> + * <tr><td>BOOLEAN, INT, LONG, FLOAT, DOUBLE</td><td>boolean, integer, bigint, real, double</td><td>kept</td></tr> + * <tr><td>STRING, BYTES</td><td>varchar, varbinary</td><td>kept</td></tr> + * <tr><td>DECIMAL(p, s)</td><td>decimal(p, s), up to precision 38</td><td>kept</td></tr> + * <tr><td>DATE</td><td>date</td><td>kept</td></tr> + * <tr><td>TIME millis, micros</td><td>time(3), time(6)</td><td>kept; integer or bigint through the metastore</td></tr> + * <tr><td>TIMESTAMP millis, micros, UTC-adjusted</td><td>timestamp(3), timestamp(6) with time zone</td> + * <td>kept; timestamp(9) through the metastore</td></tr> + * <tr><td>TIMESTAMP millis, micros, local</td><td>timestamp(3), timestamp(6)</td><td>kept; timestamp(9) through the metastore</td></tr> + * <tr><td>UUID</td><td>uuid</td><td>kept; varbinary through the metastore</td></tr> + * <tr><td>ARRAY, MAP, RECORD</td><td>array, map with varchar keys, row</td><td>kept</td></tr> + * <tr><td>ENUM</td><td>varchar</td><td>lost: Trino has no enum type</td></tr> + * <tr><td>FIXED</td><td>varbinary</td><td>lost: Trino has no fixed-length binary type</td></tr> + * <tr><td>VECTOR</td><td>array of real, double or tinyint</td><td>lost: the dimension; varbinary through the metastore</td></tr> + * <tr><td>BLOB, VARIANT</td><td>row of their fields</td><td>lost: Trino sees a plain row</td></tr> + * </table> + * A nullable type maps as its non-null type; Trino carries nullability on the column. Other unions and NULL have no + * Trino type and are rejected. + */ +public final class TrinoTypeMapper +{ + private final TypeOperators typeOperators; + + public TrinoTypeMapper(TypeOperators typeOperators) + { + this.typeOperators = requireNonNull(typeOperators, "typeOperators is null"); + } + + public Type toTypeSystem(HoodieSchema schema) + { + HoodieSchema type = schema.isNullable() ? schema.getNonNullType() : schema; Review Comment: A lone-null union ["null"] passes isNullable() and then getNonNullType() throws a raw IllegalArgumentException, so it escapes the NOT_SUPPORTED TrinoException this class gives every other unrepresentable shape. Check isComplexUnion(), the API's own guard for exactly this case, before unwrapping and route it through unsupported(). ########## hudi-trino/src/main/java/io/trino/plugin/hudi/util/TrinoTypeMapper.java: ########## @@ -0,0 +1,134 @@ +/* + * Licensed 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. + */ +package io.trino.plugin.hudi.util; + +import io.trino.spi.TrinoException; +import io.trino.spi.type.ArrayType; +import io.trino.spi.type.MapType; +import io.trino.spi.type.RowType; +import io.trino.spi.type.Type; +import io.trino.spi.type.TypeOperators; +import org.apache.hudi.common.schema.HoodieSchema; +import org.apache.hudi.common.schema.HoodieSchema.TimePrecision; +import org.apache.hudi.common.schema.HoodieSchemaType; + +import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED; +import static io.trino.spi.type.BigintType.BIGINT; +import static io.trino.spi.type.BooleanType.BOOLEAN; +import static io.trino.spi.type.DateType.DATE; +import static io.trino.spi.type.DecimalType.createDecimalType; +import static io.trino.spi.type.Decimals.MAX_PRECISION; +import static io.trino.spi.type.DoubleType.DOUBLE; +import static io.trino.spi.type.IntegerType.INTEGER; +import static io.trino.spi.type.RealType.REAL; +import static io.trino.spi.type.TimeType.createTimeType; +import static io.trino.spi.type.TimestampType.createTimestampType; +import static io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType; +import static io.trino.spi.type.TinyintType.TINYINT; +import static io.trino.spi.type.UuidType.UUID; +import static io.trino.spi.type.VarbinaryType.VARBINARY; +import static io.trino.spi.type.VarcharType.VARCHAR; +import static java.util.Objects.requireNonNull; + +/** + * Maps {@link HoodieSchema} to Trino types directly, one Trino type per {@link HoodieSchemaType}. + * <p> + * The connector does not use this mapping yet: its column types still come from the Hive type strings in the + * metastore. That path gives different types for the rows marked below, because it goes through what Hive can + * declare. Moving the connector onto this mapping changes those column types and is a separate, gated step. + * <table> + * <caption>HoodieSchema to Trino</caption> + * <tr><th>HoodieSchema</th><th>Trino</th><th>Type identity</th></tr> + * <tr><td>BOOLEAN, INT, LONG, FLOAT, DOUBLE</td><td>boolean, integer, bigint, real, double</td><td>kept</td></tr> + * <tr><td>STRING, BYTES</td><td>varchar, varbinary</td><td>kept</td></tr> + * <tr><td>DECIMAL(p, s)</td><td>decimal(p, s), up to precision 38</td><td>kept</td></tr> + * <tr><td>DATE</td><td>date</td><td>kept</td></tr> + * <tr><td>TIME millis, micros</td><td>time(3), time(6)</td><td>kept; integer or bigint through the metastore</td></tr> + * <tr><td>TIMESTAMP millis, micros, UTC-adjusted</td><td>timestamp(3), timestamp(6) with time zone</td> + * <td>kept; timestamp(9) through the metastore</td></tr> Review Comment: The metastore column says timestamp(9) for both TIMESTAMP rows, but with the default hoodie.datasource.hive_sync.support_timestamp=false, Hive sync declares TIMESTAMP as BIGINT and Trino reports bigint, so the claim only holds with that flag on. Adjust the two rows to say bigint by default and timestamp(9) only when support_timestamp is enabled. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
