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


##########
hudi-common/src/main/java/org/apache/hudi/avro/AvroCastingGenericRecord.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.common.util.ValidationUtils;
+
+import org.apache.avro.AvroRuntimeException;
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.generic.IndexedRecord;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.UnaryOperator;
+
+import static org.apache.avro.Schema.Type.ARRAY;
+import static org.apache.avro.Schema.Type.MAP;
+import static org.apache.avro.Schema.Type.STRING;
+import static org.apache.avro.Schema.Type.UNION;
+import static org.apache.hudi.common.util.StringUtils.getUTF8Bytes;
+
+
+/**
+ * Implementation of avro generic record that uses casting for implicit schema 
evolution
+ */
+public class AvroCastingGenericRecord implements GenericRecord {
+  private final IndexedRecord record;
+  private final Schema readerSchema;
+  private final Map<Integer, UnaryOperator<Object>> fieldConverters;
+
+  private AvroCastingGenericRecord(IndexedRecord record, Schema readerSchema, 
Map<Integer, UnaryOperator<Object>> fieldConverters) {
+    this.record = record;
+    this.readerSchema = readerSchema;
+    this.fieldConverters = fieldConverters;
+  }
+
+  @Override
+  public void put(int i, Object v) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public Object get(int i) {
+    if (fieldConverters.containsKey(i)) {
+      return fieldConverters.get(i).apply(record.get(i));
+    }
+    return record.get(i);
+  }
+
+  @Override
+  public Schema getSchema() {
+    return readerSchema;
+  }
+
+  @Override
+  public void put(String key, Object v) {
+    Schema.Field field = getSchema().getField(key);
+    if (field == null) {
+      throw new AvroRuntimeException("Not a valid schema field: " + key);
+    }
+    put(field.pos(), v);
+  }
+
+  @Override
+  public Object get(String key) {
+    Schema.Field field = getSchema().getField(key);
+    if (field == null) {
+      throw new AvroRuntimeException("Not a valid schema field: " + key);
+    }
+    return get(field.pos());
+  }
+
+
+  /**
+   * Avro schema evolution does not support promotion from numbers to string. 
This function returns true if
+   * it will be necessary to rewrite the record to support the evolution.
+   * NOTE: this does not determine whether the schema evolution is valid. It 
is just trying to find if the schema evolves from
+   * a number to string, as quick as possible.
+   */
+  public static Boolean 
recordNeedsRewriteForExtendedAvroSchemaEvolution(Schema writerSchema, Schema 
readerSchema) {

Review Comment:
   The deltastreamer schema evolution tests contain end to end testing



-- 
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: commits-unsubscr...@hudi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to