clairemcginty commented on code in PR #1078:
URL: https://github.com/apache/parquet-mr/pull/1078#discussion_r1174108640


##########
parquet-avro/src/main/java/org/apache/parquet/avro/AvroRecordConverter.java:
##########
@@ -169,6 +172,46 @@ public void add(Object value) {
     }
   }
 
+  /**
+   * Returns the specific data model for a given SpecificRecord schema by 
reflecting the underlying
+   * Avro class's `MODEL$` field, or Null if the class is not on the classpath 
or reflection fails.
+   */
+  static SpecificData getModelForSchema(Schema schema) {
+    final Class<?> clazz;
+
+    if (schema != null && (schema.getType() == Schema.Type.RECORD || 
schema.getType() == Schema.Type.UNION)) {
+      clazz = SpecificData.get().getClass(schema);
+    } else {
+      return null;
+    }
+
+    final SpecificData model;
+    try {
+      final Field modelField = clazz.getDeclaredField("MODEL$");
+      modelField.setAccessible(true);
+
+      model = (SpecificData) modelField.get(null);
+    } catch (Exception e) {
+      return null;
+    }
+
+    try {
+      final String avroVersion = 
Schema.Parser.class.getPackage().getImplementationVersion();
+      // Avro 1.8 doesn't include conversions in the MODEL$ field
+      if (avroVersion.startsWith("1.8.")) {

Review Comment:
   For reference, Avro 1.11 compiles SpecificRecord implementations that look 
like:
   
   ```java
   @org.apache.avro.specific.AvroGenerated
   public class LogicalTypesTest extends 
org.apache.avro.specific.SpecificRecordBase implements 
org.apache.avro.specific.SpecificRecord {
     ...
     private static final SpecificData MODEL$ = new SpecificData();
     static {
       MODEL$.addLogicalTypeConversion(new 
org.apache.avro.data.TimeConversions.TimestampMillisConversion());
     }
   }
   ```
   
   Whereas for Avro 1.8, there is no static initialization, but there _is_ a 
`conversions` field:
   
   ```java
   @org.apache.avro.specific.AvroGenerated
   public class LogicalTypesTest extends 
org.apache.avro.specific.SpecificRecordBase implements 
org.apache.avro.specific.SpecificRecord {
     ...
     private static final SpecificData MODEL$ = new SpecificData();
     
     protected static final 
org.apache.avro.data.TimeConversions.TimestampConversion TIMESTAMP_CONVERSION = 
new org.apache.avro.data.TimeConversions.TimestampConversion();
   
     private static final org.apache.avro.Conversion<?>[] conversions =
         new org.apache.avro.Conversion<?>[] {
         TIMESTAMP_CONVERSION,
         null
     };
   }
   ```



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