You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "rdblue (via GitHub)" <gi...@apache.org> on 2025/03/05 02:48:13 UTC
[PR] Avro: Add variant readers and writers [iceberg]
rdblue opened a new pull request, #12457:
URL: https://github.com/apache/iceberg/pull/12457
This adds readers and writers for Variants in Avro. Avro generics, Iceberg generics, and the internal data model are supported.
Variant values are tested using `RandomVariants` to extend random data generation.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Avro: Add variant readers and writers [iceberg]
Posted by "danielcweeks (via GitHub)" <gi...@apache.org>.
danielcweeks commented on code in PR #12457:
URL: https://github.com/apache/iceberg/pull/12457#discussion_r1999833881
##########
core/src/main/java/org/apache/iceberg/avro/VariantConversion.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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 java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.avro.Conversion;
+import org.apache.avro.LogicalType;
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.generic.IndexedRecord;
+import org.apache.iceberg.variants.Variant;
+import org.apache.iceberg.variants.VariantMetadata;
+import org.apache.iceberg.variants.VariantValue;
+
+public class VariantConversion extends Conversion<Variant> {
+ @Override
+ public Class<Variant> getConvertedType() {
+ return Variant.class;
+ }
+
+ @Override
+ public String getLogicalTypeName() {
+ return VariantLogicalType.NAME;
+ }
+
+ @Override
+ public Variant fromRecord(IndexedRecord record, Schema schema, LogicalType type) {
+ int metadataPos = schema.getField("metadata").pos();
Review Comment:
nit: since we're using the field names in multiple places, it would be nice to pull them into a static member and not use a literal (e.g. `VARIENT_METADATA_FIELD` and `VARIANT_VALUE_FIELD`)
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Avro: Add variant readers and writers [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #12457:
URL: https://github.com/apache/iceberg/pull/12457#discussion_r1999841799
##########
core/src/main/java/org/apache/iceberg/avro/ValueWriters.java:
##########
@@ -373,6 +382,82 @@ public void write(BigDecimal decimal, Encoder encoder) throws IOException {
}
}
+ private abstract static class VariantBinaryWriter<T> implements ValueWriter<T> {
+ private ByteBuffer reusedBuffer = ByteBuffer.allocate(2048).order(ByteOrder.LITTLE_ENDIAN);
+
+ protected abstract int sizeInBytes(T value);
+
+ protected abstract int writeTo(ByteBuffer buffer, int offset, T value);
+
+ @Override
+ public void write(T datum, Encoder encoder) throws IOException {
+ if (datum instanceof Serialized) {
+ encoder.writeBytes(((Serialized) datum).buffer());
Review Comment:
Yeah, that's correct. It seems reasonable to me to avoid the copy.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Avro: Add variant readers and writers [iceberg]
Posted by "aihuaxu (via GitHub)" <gi...@apache.org>.
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Avro: Add variant readers and writers [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue merged PR #12457:
URL: https://github.com/apache/iceberg/pull/12457
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Avro: Add variant readers and writers [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #12457:
URL: https://github.com/apache/iceberg/pull/12457#discussion_r1983699001
##########
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:
Fixed.
##########
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:
Updated.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Avro: Add variant readers and writers [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on PR #12457:
URL: https://github.com/apache/iceberg/pull/12457#issuecomment-2731190545
Thanks for reviewing, @danielcweeks and @aihuaxu!
I'll follow up on some of Dan's comments in later PRs. I'd like to get this one in so I can open PRs for other readers that will use the `RandomVariants` code.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Avro: Add variant readers and writers [iceberg]
Posted by "danielcweeks (via GitHub)" <gi...@apache.org>.
danielcweeks commented on code in PR #12457:
URL: https://github.com/apache/iceberg/pull/12457#discussion_r1999836842
##########
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) {
Review Comment:
A little bit of docs around the expected structure and what this produces would be nice for future testing. It takes a bit of visual inspection to understand the shape of what is returned by this method.
At some point it might be nice to enhance this so you can provide a `size` so that it will keep generating values until you reach a certain size. More importantly, it might be nice to take an Iceberg schema since that will be important for shredding.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Avro: Add variant readers and writers [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #12457:
URL: https://github.com/apache/iceberg/pull/12457#discussion_r1983693589
##########
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:
This produces a variant in memory, not a serialized variant. The writer is free to serialize however it chooses, although this is currently not intended to exercise shredding paths that need records to be make sense.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Avro: Add variant readers and writers [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #12457:
URL: https://github.com/apache/iceberg/pull/12457#discussion_r1999843486
##########
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) {
Review Comment:
Yeah, I think when we add fuzzing tests for shredding, we'll want to pass in an Iceberg schema like you said.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Avro: Add variant readers and writers [iceberg]
Posted by "danielcweeks (via GitHub)" <gi...@apache.org>.
danielcweeks commented on code in PR #12457:
URL: https://github.com/apache/iceberg/pull/12457#discussion_r1999827309
##########
core/src/main/java/org/apache/iceberg/avro/ValueWriters.java:
##########
@@ -373,6 +382,82 @@ public void write(BigDecimal decimal, Encoder encoder) throws IOException {
}
}
+ private abstract static class VariantBinaryWriter<T> implements ValueWriter<T> {
+ private ByteBuffer reusedBuffer = ByteBuffer.allocate(2048).order(ByteOrder.LITTLE_ENDIAN);
+
+ protected abstract int sizeInBytes(T value);
+
+ protected abstract int writeTo(ByteBuffer buffer, int offset, T value);
+
+ @Override
+ public void write(T datum, Encoder encoder) throws IOException {
+ if (datum instanceof Serialized) {
+ encoder.writeBytes(((Serialized) datum).buffer());
Review Comment:
I assume this is why we're making `Serialized` public? I was looking to see if there were some other way, but this looks as good as anything.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] Avro: Add variant readers and writers [iceberg]
Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #12457:
URL: https://github.com/apache/iceberg/pull/12457#discussion_r1983693589
##########
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:
This produces a variant in memory, not a serialized variant. The writer is free to serialize however it chooses, although this is currently not intended to exercise shredding paths that need records to have a more stable structure in order to make sense.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org