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


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

Review Comment:
   Avro allows recursive schemas (e.g. the [spec 
page](https://avro.apache.org/docs/1.12.0/specification/) includes a linked 
list example called "LongList")
   
   If someone uses the new public constructor this PR introduces from the 
DataStream API and they have a recursive schema, what happens? Without a way of 
terminating on records we've already seen, do we just roll into a 
StackOverflowError?
   
   I havent tried this so I might be wrong, but a test with a recursive schema 
would be good, even if I'm worrying about nothing :) 



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