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


##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchema.java:
##########
@@ -96,11 +98,22 @@ public class HoodieSchema implements Serializable {
    * @throws IllegalArgumentException if avroSchema is null
    */
   private HoodieSchema(Schema avroSchema) {
+    this(avroSchema, null);
+  }
+
+  /**
+   * Creates a new HoodieSchema with the given Avro schema and fields.
+   * @param avroSchema the Avro schema to wrap, cannot be null
+   * @param fields the list of HoodieSchemaField objects, can be null
+   * @throws IllegalArgumentException if avroSchema is null
+   */
+  private HoodieSchema(Schema avroSchema, List<HoodieSchemaField> fields) {

Review Comment:
   Do we have a test to cover fields being an empty list?



##########
hudi-common/src/main/java/org/apache/parquet/schema/HoodieSchemaRepair.java:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.parquet.schema;
+
+import org.apache.hudi.common.schema.HoodieSchema;
+import org.apache.hudi.common.schema.HoodieSchemaCache;
+import org.apache.hudi.common.schema.HoodieSchemaField;
+import org.apache.hudi.common.schema.HoodieSchemaType;
+import org.apache.hudi.common.schema.HoodieSchemaUtils;
+import org.apache.hudi.common.util.Option;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class HoodieSchemaRepair {
+  public static HoodieSchema repairLogicalTypes(HoodieSchema fileSchema, 
HoodieSchema tableSchema) {
+    HoodieSchema repairedSchema = repairSchema(fileSchema, tableSchema);
+    if (repairedSchema != fileSchema) {
+      return HoodieSchemaCache.intern(repairedSchema);
+    }
+    return fileSchema;
+  }
+
+  /**
+   * Performs schema repair on a schema, handling nullable unions.
+   */
+  private static HoodieSchema repairSchema(HoodieSchema fileSchema, 
HoodieSchema tableSchema) {
+    // Always resolve nullable schemas first (returns unchanged if not a union)
+    HoodieSchema nonNullFileSchema = fileSchema.getNonNullType();
+    HoodieSchema nonNullTableSchema = tableSchema.getNonNullType();
+
+    // Perform repair on the non-null types
+    HoodieSchema nonNullRepairedSchema = 
repairSchemaNonNull(nonNullFileSchema, nonNullTableSchema);
+
+    // If nothing changed, return the original schema
+    if (nonNullRepairedSchema == nonNullFileSchema) {
+      return fileSchema;
+    }
+
+    // If the original was a union, wrap the repaired schema back in a 
nullable union
+    if (fileSchema.getType() == HoodieSchemaType.UNION) {
+      return HoodieSchema.createNullable(nonNullRepairedSchema);
+    }
+
+    return nonNullRepairedSchema;
+  }
+
+  private static boolean canRepair(HoodieSchema fileSchema, HoodieSchema 
tableSchema) {
+    return fileSchema.getType() == tableSchema.getType() || 
fileSchema.getType() == HoodieSchemaType.LONG && tableSchema.getType() == 
HoodieSchemaType.TIMESTAMP;

Review Comment:
   Add a comment here to explain the significance of LONG and TIMESTAMP?



##########
hudi-hadoop-common/src/main/java/org/apache/hudi/io/storage/hadoop/HoodieAvroParquetReader.java:
##########
@@ -186,12 +186,11 @@ private ClosableIterator<IndexedRecord> 
getIndexedRecordIteratorInternal(HoodieS
     //       sure that in case the file-schema is not equal to read-schema 
we'd still
     //       be able to read that file (in case projection is a proper one)
     Configuration hadoopConf = 
storage.getConf().unwrapCopyAs(Configuration.class);
-    //TODO boundary for now to revisit in later pr to use HoodieSchema
-    Schema repairedFileSchema = 
AvroSchemaRepair.repairLogicalTypes(getSchema().toAvroSchema(), 
schema.toAvroSchema());
+    HoodieSchema repairedFileSchema = 
HoodieSchemaRepair.repairLogicalTypes(getSchema(), schema);
     Option<Schema> promotedSchema = Option.empty();

Review Comment:
   It's possible for us to remove `Avro.Schema` imports here. In line 207 we 
are converting this back to HoodieSchema again. Let's remove this back and 
forth.
   
   My bad, this was an artefact left by me when i was doing `HoodieAvroUtils` 
migration.



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