jonvex commented on code in PR #13711:
URL: https://github.com/apache/hudi/pull/13711#discussion_r2294237907


##########
hudi-common/src/main/java/org/apache/hudi/avro/ValueType.java:
##########
@@ -0,0 +1,637 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.hudi.avro;
+
+import org.apache.hudi.avro.model.ArrayWrapper;
+import org.apache.hudi.common.util.DateTimeUtils;
+import org.apache.hudi.common.util.collection.ArrayComparable;
+
+import org.apache.avro.LogicalTypes;
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.util.Utf8;
+import org.apache.parquet.io.api.Binary;
+import org.apache.parquet.schema.LogicalTypeTokenParser;
+import org.apache.parquet.schema.PrimitiveType;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.math.MathContext;
+import java.math.RoundingMode;
+import java.nio.ByteBuffer;
+import java.sql.Date;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.ZoneOffset;
+import java.util.Objects;
+import java.util.UUID;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+
+import static org.apache.hudi.avro.HoodieAvroUtils.convertBytesToBigDecimal;
+import static org.apache.hudi.common.util.DateTimeUtils.instantToMicros;
+import static org.apache.hudi.common.util.DateTimeUtils.instantToNanos;
+import static org.apache.hudi.common.util.DateTimeUtils.microsToInstant;
+import static org.apache.hudi.common.util.DateTimeUtils.nanosToInstant;
+import static org.apache.hudi.common.util.StringUtils.getUTF8Bytes;
+
+public enum ValueType {
+  V1(HoodieAvroWrapperUtils.PrimitiveWrapperType.V1.getClazz(), 
HoodieAvroWrapperUtils.PrimitiveWrapperType.V1,
+      ValueType::passThrough, ValueType::passThrough, ValueType::passThrough),
+  NULL(HoodieAvroWrapperUtils.PrimitiveWrapperType.NULL.getClazz(), 
HoodieAvroWrapperUtils.PrimitiveWrapperType.NULL,
+      ValueType::passThrough, ValueType::passThrough, ValueType::passThrough),
+  BOOLEAN(HoodieAvroWrapperUtils.PrimitiveWrapperType.BOOLEAN, 
ValueType::castToBoolean),
+  INT(HoodieAvroWrapperUtils.PrimitiveWrapperType.INT, 
ValueType::castToInteger),
+  LONG(HoodieAvroWrapperUtils.PrimitiveWrapperType.LONG, 
ValueType::castToLong),
+  FLOAT(HoodieAvroWrapperUtils.PrimitiveWrapperType.FLOAT, 
ValueType::castToFloat),
+  DOUBLE(HoodieAvroWrapperUtils.PrimitiveWrapperType.DOUBLE, 
ValueType::castToDouble),
+  STRING(HoodieAvroWrapperUtils.PrimitiveWrapperType.STRING, 
ValueType::castToString),
+  BYTES(HoodieAvroWrapperUtils.PrimitiveWrapperType.BYTES, 
ValueType::castToBytes),
+  FIXED(HoodieAvroWrapperUtils.PrimitiveWrapperType.BYTES, 
ValueType::castToFixed),
+  DECIMAL(BigDecimal.class, HoodieAvroWrapperUtils.PrimitiveWrapperType.BYTES,
+      ValueType::castToDecimal, ValueType::toDecimal, ValueType::fromDecimal),
+  UUID(UUID.class, HoodieAvroWrapperUtils.PrimitiveWrapperType.STRING,
+      ValueType::castToUUID, ValueType::toUUID, ValueType::fromUUID),
+  DATE(Date.class, HoodieAvroWrapperUtils.PrimitiveWrapperType.INT,
+      ValueType::castToDate, ValueType::toDate, ValueType::fromDate),
+  TIME_MILLIS(LocalTime.class, HoodieAvroWrapperUtils.PrimitiveWrapperType.INT,
+      ValueType::castToTimeMillis, ValueType::toTimeMillis, 
ValueType::fromTimeMillis),
+  TIME_MICROS(LocalTime.class, 
HoodieAvroWrapperUtils.PrimitiveWrapperType.LONG,
+      ValueType::castToTimeMicros, ValueType::toTimeMicros, 
ValueType::fromTimeMicros),
+  TIMESTAMP_MILLIS(Instant.class, 
HoodieAvroWrapperUtils.PrimitiveWrapperType.LONG,
+      ValueType::castToTimestampMillis, ValueType::toTimestampMillis, 
ValueType::fromTimestampMillis),
+  TIMESTAMP_MICROS(Instant.class, 
HoodieAvroWrapperUtils.PrimitiveWrapperType.LONG,
+      ValueType::castToTimestampMicros, ValueType::toTimestampMicros, 
ValueType::fromTimestampMicros),
+  TIMESTAMP_NANOS(Instant.class, 
HoodieAvroWrapperUtils.PrimitiveWrapperType.LONG,
+      ValueType::castToTimestampNanos, ValueType::toTimestampNanos, 
ValueType::fromTimestampNanos),
+  LOCAL_TIMESTAMP_MILLIS(LocalDateTime.class, 
HoodieAvroWrapperUtils.PrimitiveWrapperType.LONG,
+      ValueType::castToLocalTimestampMillis, 
ValueType::toLocalTimestampMillis, ValueType::fromLocalTimestampMillis),
+  LOCAL_TIMESTAMP_MICROS(LocalDateTime.class, 
HoodieAvroWrapperUtils.PrimitiveWrapperType.LONG,
+      ValueType::castToLocalTimestampMicros, 
ValueType::toLocalTimestampMicros, ValueType::fromLocalTimestampMicros),
+  LOCAL_TIMESTAMP_NANOS(LocalDateTime.class, 
HoodieAvroWrapperUtils.PrimitiveWrapperType.LONG,
+      ValueType::castToLocalTimestampNanos, ValueType::toLocalTimestampNanos, 
ValueType::fromLocalTimestampNanos);
+
+  private final Class<?> internalType;
+  private final HoodieAvroWrapperUtils.PrimitiveWrapperType 
primitiveWrapperType;
+  private final BiFunction<Object, ValueMetadata, Comparable<?>> standardize;
+  private final BiFunction<Comparable<?>, ValueMetadata, Comparable<?>> 
toComplex;
+  private final BiFunction<Comparable<?>, ValueMetadata, Comparable<?>> 
toPrimitive;

Review Comment:
   Will add to the code.
   Standardize puts all types into the correct internal type. So fixed will 
become byte buffer, and also type promotion is handled. 
   
   toComplex converts logical types from the wrapper type to the internal type. 
Timestamps are stored as longs etc, so those get converted to Instants or 
LocalDateTimes. 
   
   toPrimitive reverses what toComplex does. 
   
   Whenever we unwrap a value from the mdt, we call toComplex on it. And before 
we wrap vales to write to the mdt, we call toPrimitive.



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