clairemcginty commented on code in PR #16824:
URL: https://github.com/apache/iceberg/pull/16824#discussion_r3416624156


##########
core/src/main/java/org/apache/iceberg/avro/TypeToSchema.java:
##########
@@ -46,20 +49,45 @@ abstract class TypeToSchema extends 
TypeUtil.SchemaVisitor<Schema> {
       
LogicalTypes.timestampMicros().addToSchema(Schema.create(Schema.Type.LONG));
   private static final Schema TIMESTAMPTZ_SCHEMA =
       
LogicalTypes.timestampMicros().addToSchema(Schema.create(Schema.Type.LONG));
-  private static final Schema TIMESTAMP_NANO_SCHEMA =
-      
LogicalTypes.timestampNanos().addToSchema(Schema.create(Schema.Type.LONG));
-  private static final Schema TIMESTAMPTZ_NANO_SCHEMA =
-      
LogicalTypes.timestampNanos().addToSchema(Schema.create(Schema.Type.LONG));
   private static final Schema STRING_SCHEMA = 
Schema.create(Schema.Type.STRING);
   private static final Schema UUID_SCHEMA =
       LogicalTypes.uuid().addToSchema(Schema.createFixed("uuid_fixed", null, 
null, 16));
   private static final Schema BINARY_SCHEMA = Schema.create(Schema.Type.BYTES);
 
+  // Logical types available in Avro 1.12 only; evaluate lazily to keep compat 
with Avro 1.11
+  private static Schema TIMESTAMP_NANO_SCHEMA;
+  private static Schema TIMESTAMPTZ_NANO_SCHEMA;
+
+  private static void initializeTimestampNanoSchema() {
+    TIMESTAMP_NANO_SCHEMA =
+        
LogicalTypes.timestampNanos().addToSchema(Schema.create(Schema.Type.LONG));
+  }
+
+  private static void initializeTimestampTzNanoSchema() {
+    TIMESTAMPTZ_NANO_SCHEMA =
+        
LogicalTypes.timestampNanos().addToSchema(Schema.create(Schema.Type.LONG));
+  }
+
   static {
     TIMESTAMP_SCHEMA.addProp(AvroSchemaUtil.ADJUST_TO_UTC_PROP, false);
     TIMESTAMPTZ_SCHEMA.addProp(AvroSchemaUtil.ADJUST_TO_UTC_PROP, true);
-    TIMESTAMP_NANO_SCHEMA.addProp(AvroSchemaUtil.ADJUST_TO_UTC_PROP, false);
-    TIMESTAMPTZ_NANO_SCHEMA.addProp(AvroSchemaUtil.ADJUST_TO_UTC_PROP, true);
+
+    final Optional<String> runtimeAvroVersion =
+        
Optional.ofNullable(Schema.Parser.class.getPackage().getImplementationVersion());
+
+    // NanoTimestamp and UUID logical types are only supported in Avro 1.12 
and above;
+    // Gate initialization to avoid runtime errors for users on earlier Avro 
versions
+    if (runtimeAvroVersion
+        .map(semVer -> Pattern.compile("^\\d+\\.(\\d+)").matcher(semVer))
+        .filter(Matcher::find)
+        .filter(matcher -> Integer.parseInt(matcher.group(1)) < 12)
+        .isEmpty()) {
+      initializeTimestampNanoSchema();

Review Comment:
   🤔 this design _will_ render a somewhat confusing error further downstream if 
a user on Avro 1.11 tries to read an Iceberg source with a timestamp-nanos 
field (a NPE on `TIMESTAMP_NANO_SCHEMA`/`TIMESTAMPTZ_NANO_SCHEMA`). I could add 
a clearer error message there that explains the root cause (Avro version 
incompatibility).



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to