voonhous commented on code in PR #20680:
URL: https://github.com/apache/hudi/pull/20680#discussion_r4609326633


##########
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:
   Renamed to `nonNullSchema` in 3e9e6113.



##########
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:
   Done in 3e9e6113: BLOB and VARIANT moved into the parameterized mappings 
with their full row types, so BLOB's nested `reference` row is covered too.



-- 
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]

Reply via email to