aihuaxu commented on code in PR #12457: URL: https://github.com/apache/iceberg/pull/12457#discussion_r1982799286
########## core/src/main/java/org/apache/iceberg/avro/ValueReaders.java: ########## @@ -141,6 +144,13 @@ public static ValueReader<byte[]> decimalBytesReader(Schema schema) { } } + @SuppressWarnings("unchecked") + public static ValueReader<Variant> variants( + ValueReader<?> metadataReader, ValueReader<?> valueReader) { + return new VariantReader( Review Comment: Similarly, can we reuse the same VariantReader instance? ########## core/src/main/java/org/apache/iceberg/avro/GenericAvroReader.java: ########## @@ -163,6 +163,12 @@ public ValueReader<?> map(Type partner, Schema map, ValueReader<?> valueReader) return ValueReaders.map(ValueReaders.strings(), valueReader); } + @Override + public ValueReader<?> variant( + Type partner, ValueReader<?> metadataReader, ValueReader<?> valueReader) { + return ValueReaders.variants(metadataReader, valueReader); Review Comment: Do we reuse the same variant reader similar to the writer? ########## core/src/main/java/org/apache/iceberg/data/avro/DataWriter.java: ########## @@ -101,6 +101,12 @@ public ValueWriter<?> map(Schema map, ValueWriter<?> valueWriter) { return ValueWriters.map(ValueWriters.strings(), valueWriter); } + @Override + public ValueWriter<?> variant( + Schema variant, ValueWriter<?> metadataResult, ValueWriter<?> valueResult) { Review Comment: nit: => metadataWriter, => valueWriter to be consistent. ########## core/src/test/java/org/apache/iceberg/RandomVariants.java: ########## @@ -0,0 +1,127 @@ +/* + * 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; + +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Random; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.util.RandomUtil; +import org.apache.iceberg.variants.PhysicalType; +import org.apache.iceberg.variants.ShreddedObject; +import org.apache.iceberg.variants.Variant; +import org.apache.iceberg.variants.VariantMetadata; +import org.apache.iceberg.variants.VariantTestUtil; +import org.apache.iceberg.variants.VariantValue; +import org.apache.iceberg.variants.Variants; + +public class RandomVariants { + private RandomVariants() {} + + public static Variant randomVariant(Random random) { + PhysicalType type = randomType(random); + VariantMetadata metadata; + if (type == PhysicalType.OBJECT || type == PhysicalType.ARRAY) { + int numFields = random.nextInt(25) + 5; // at least 5 keys + List<String> randomNames = Lists.newArrayList(); + for (int i = 0; i < numFields; i += 1) { + randomNames.add(RandomUtil.generateString(10, random)); + } + metadata = + VariantMetadata.from(VariantTestUtil.createMetadata(randomNames, random.nextBoolean())); + } else { + metadata = VariantMetadata.empty(); + } + + return Variant.of(metadata, randomVariant(random, metadata, randomType(random))); + } + + private static PhysicalType randomType(Random random) { + PhysicalType[] types = PhysicalType.values(); + return types[random.nextInt(types.length)]; + } + + private static VariantValue randomVariant( + Random random, VariantMetadata metadata, PhysicalType type) { + switch (type) { + case NULL: + return Variants.ofNull(); + case BOOLEAN_TRUE: + return Variants.of(true); + case BOOLEAN_FALSE: + return Variants.of(false); + case INT8: + return Variants.of( + type, + ((Integer) RandomUtil.generatePrimitive(Types.IntegerType.get(), random)).byteValue()); + case INT16: + return Variants.of( + type, + ((Integer) RandomUtil.generatePrimitive(Types.IntegerType.get(), random)).shortValue()); + case INT32: + return Variants.of(type, RandomUtil.generatePrimitive(Types.IntegerType.get(), random)); + case INT64: + return Variants.of(type, RandomUtil.generatePrimitive(Types.LongType.get(), random)); + case DOUBLE: + return Variants.of(type, RandomUtil.generatePrimitive(Types.DoubleType.get(), random)); + case DECIMAL4: + return Variants.of( + type, RandomUtil.generatePrimitive(Types.DecimalType.of(9, random.nextInt(9)), random)); + case DECIMAL8: + return Variants.of( + type, + RandomUtil.generatePrimitive(Types.DecimalType.of(18, random.nextInt(18)), random)); + case DECIMAL16: + return Variants.of( + type, + RandomUtil.generatePrimitive(Types.DecimalType.of(38, random.nextInt(38)), random)); + case DATE: + return Variants.of(type, RandomUtil.generatePrimitive(Types.DateType.get(), random)); + case TIMESTAMPTZ: + return Variants.of( + type, RandomUtil.generatePrimitive(Types.TimestampType.withZone(), random)); + case TIMESTAMPNTZ: + return Variants.of( + type, RandomUtil.generatePrimitive(Types.TimestampType.withoutZone(), random)); + case FLOAT: + return Variants.of(type, RandomUtil.generatePrimitive(Types.FloatType.get(), random)); + case BINARY: + byte[] randomBytes = (byte[]) RandomUtil.generatePrimitive(Types.BinaryType.get(), random); + return Variants.of(type, ByteBuffer.wrap(randomBytes)); + case STRING: + return Variants.of(type, RandomUtil.generatePrimitive(Types.StringType.get(), random)); + case ARRAY: + // for now, generate an object instead of an array + case OBJECT: + ShreddedObject object = Variants.object(metadata); Review Comment: Seems we only have fully shredded objects here. Should we add partial shredded objects? -- 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