dalelane commented on code in PR #30216:
URL: https://github.com/apache/flink/pull/30216#discussion_r4773165180


##########
flink-formats/flink-avro/src/main/java/org/apache/flink/formats/avro/RegistryAvroDeserializationSchema.java:
##########
@@ -115,6 +157,91 @@ void checkAvroInitialized() throws IOException {
         if (schemaCoder == null) {
             this.schemaCoder = schemaCoderProvider.get();
         }
+        if (enumResolvedReaders == null) {
+            this.enumResolvedReaders = new HashMap<>();
+        }
+    }
+
+    /**
+     * Replaces the strings of the reader schema with the enum that the writer 
schema declares in
+     * the same place, so that Avro reads them as enum symbols. Fields and 
their order are those of
+     * the reader schema. Returns the reader schema itself if there is nothing 
to replace.
+     */
+    private static Schema resolveEnums(Schema reader, Schema writer) {
+        if (reader.getType() == Schema.Type.UNION || writer.getType() == 
Schema.Type.UNION) {
+            // only nullable types are resolved, a table schema cannot declare 
any other union
+            final Schema readerType = nonNullType(reader);
+            final Schema writerType = nonNullType(writer);
+            if (readerType == null || writerType == null) {
+                return reader;
+            }
+            final Schema resolved = resolveEnums(readerType, writerType);
+            if (resolved == readerType) {
+                return reader;
+            }
+            if (reader.getType() != Schema.Type.UNION) {
+                return resolved;
+            }
+            return Schema.createUnion(
+                    reader.getTypes().stream()
+                            .map(type -> type == readerType ? resolved : type)
+                            .collect(Collectors.toList()));
+        }
+        if (reader.getType() == Schema.Type.STRING && writer.getType() == 
Schema.Type.ENUM) {
+            return writer;
+        }
+        if (reader.getType() != writer.getType()) {
+            return reader;
+        }
+        switch (reader.getType()) {
+            case ARRAY:
+                final Schema element =
+                        resolveEnums(reader.getElementType(), 
writer.getElementType());
+                return element == reader.getElementType() ? reader : 
Schema.createArray(element);
+            case MAP:
+                final Schema value = resolveEnums(reader.getValueType(), 
writer.getValueType());
+                return value == reader.getValueType() ? reader : 
Schema.createMap(value);
+            case RECORD:
+                final List<Schema.Field> fields = new ArrayList<>();
+                boolean resolved = false;
+                for (Schema.Field field : reader.getFields()) {
+                    final Schema.Field writerField = 
writer.getField(field.name());

Review Comment:
   given that you're respecting record aliases below (line 227), any reason not 
to also cover field aliases here? 



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