aihuaxu commented on code in PR #12238: URL: https://github.com/apache/iceberg/pull/12238#discussion_r1964351443
########## core/src/main/java/org/apache/iceberg/avro/BaseWriteBuilder.java: ########## @@ -64,6 +64,11 @@ public ValueWriter<?> map(Schema map, ValueWriter<?> valueWriter) { return ValueWriters.map(ValueWriters.strings(), valueWriter); } + @Override + public ValueWriter<?> variant(Schema variant, List<ValueWriter<?>> fields) { + return createRecordWriter(fields); + } + Review Comment: To test out the write, It needs the changes in https://github.com/apache/iceberg/pull/11934/commits/125fa6a56d6b6d741d1bc9c5cf9fe94825ffa44a so I haven't added the coverage yet. ########## core/src/main/java/org/apache/iceberg/avro/VariantLogicalType.java: ########## @@ -0,0 +1,47 @@ +/* + * 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.iceberg.avro; + +import org.apache.avro.LogicalType; +import org.apache.avro.Schema; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; + +public class VariantLogicalType extends LogicalType { + static final String NAME = "variant"; + private static final VariantLogicalType INSTANCE = new VariantLogicalType(); + + static VariantLogicalType get() { + return INSTANCE; + } + + private VariantLogicalType() { + super(NAME); + } + + @Override + public void validate(Schema schema) { + super.validate(schema); + Preconditions.checkArgument( Review Comment: This is calling `AvroSchemaUtil.isVariantSchema()` to do the actual validation. So when variant logical type is added to a record, it will check the fields . ########## core/src/main/java/org/apache/iceberg/avro/TypeToSchema.java: ########## @@ -187,6 +187,21 @@ public Schema map(Types.MapType map, Schema keySchema, Schema valueSchema) { return mapSchema; } + @Override + public Schema variant() { + String recordName = "r" + fieldIds.peek(); Review Comment: Updated to the name `variant` when fieldIds.peek() is null. ########## core/src/test/java/org/apache/iceberg/avro/TestSchemaConversions.java: ########## @@ -76,7 +78,8 @@ public void testPrimitiveTypes() { Schema.createFixed("fixed_12", null, null, 12), Schema.create(Schema.Type.BYTES), LogicalTypes.decimal(9, 4) - .addToSchema(Schema.createFixed("decimal_9_4", null, null, 4))); + .addToSchema(Schema.createFixed("decimal_9_4", null, null, 4)), + variant("rnull")); Review Comment: Updated to `variant` if ID is null. ########## core/src/test/java/org/apache/iceberg/avro/TestAvroSchemaProjection.java: ########## @@ -150,4 +150,58 @@ public void projectWithMapSchemaChanged() { .as("Result of buildAvroProjection is missing some IDs") .isFalse(); } + + @Test + public void projectWithVariantSchemaChanged() { + final org.apache.avro.Schema currentAvroSchema = + SchemaBuilder.record("myrecord") + .fields() + .name("f11") + .type() + .nullable() + .intType() + .noDefault() + .endRecord(); + + final org.apache.avro.Schema variantSchema = + SchemaBuilder.record("v") + .fields() + .name("metadata") + .type() + .bytesType() + .noDefault() + .name("value") + .type() + .bytesType() + .noDefault() + .endRecord(); + Variant.get().addToSchema(variantSchema); + + final org.apache.avro.Schema updatedAvroSchema = + SchemaBuilder.record("myrecord") + .fields() + .name("f11") + .type() + .nullable() + .intType() + .noDefault() Review Comment: Checked the doc. If nullable, it means it can default to an int or noDefault. But I updated the test to simplify and I don't have the code anymore. ########## core/src/main/java/org/apache/iceberg/avro/AvroSchemaVisitor.java: ########## @@ -103,6 +107,10 @@ public T map(Schema map, T value) { return null; } + public T variant(Schema variant) { + throw new UnsupportedOperationException("Unsupported type: variant"); Review Comment: Got it. Some visitor implementations need to access the fields. ########## core/src/main/java/org/apache/iceberg/avro/BuildAvroProjection.java: ########## @@ -265,6 +265,11 @@ public Schema map(Schema map, Supplier<Schema> value) { } } + @Override + public Schema variant(Schema variant) { + return variant; Review Comment: I'm adding the validation in visit() method so we have validation in common place. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org