You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "Guosmilesmile (via GitHub)" <gi...@apache.org> on 2026/03/12 08:09:41 UTC
[PR] Core,Flink: Support writing shredded variant in Flink [iceberg]
Guosmilesmile opened a new pull request, #15596:
URL: https://github.com/apache/iceberg/pull/15596
This PR is mainly to add support in Flink for writing shredding-variant data to Iceberg tables, based on https://github.com/apache/iceberg/pull/14297.
Since we noticed there was a lot of duplicated code between Flink and Spark, we extracted the common parts into the core module.
**This PR is based on https://github.com/apache/iceberg/pull/14297 and will be adjusted in sync with it.**
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3248577239
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,874 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @Override
+ @AfterEach
+ public void clean() {
+ super.clean();
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "4");
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
Review Comment:
We should use blocks `"""` instead of this escaping. We could create a nicely readable string. If we need to remove the new lines we always can use `.repalceAll("\n", "")` in the end.
This is for all the ugly strings here.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3213337830
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteConf.java:
##########
@@ -262,4 +262,22 @@ public Duration tableRefreshInterval() {
.flinkConfig(FlinkWriteOptions.TABLE_REFRESH_INTERVAL)
.parseOptional();
}
+
+ public boolean parquetShredVariants() {
+ return confParser
+ .booleanConf()
+ .option(FlinkWriteOptions.PARQUET_SHRED_VARIANTS.key())
+ .tableProperty(TableProperties.PARQUET_SHRED_VARIANTS)
+ .defaultValue(TableProperties.PARQUET_SHRED_VARIANTS_DEFAULT)
+ .parse();
+ }
+
+ public int variantInferenceBufferSize() {
Review Comment:
Thanks for pointing out. Initially I intended to make it a general parameter. But after your suggestion, I realized that the table property it references is Parquet-specific, so it still needs to be defined as a Parquet-only parameter.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "nssalian (via GitHub)" <gi...@apache.org>.
nssalian commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3213897894
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteConf.java:
##########
@@ -262,4 +262,22 @@ public Duration tableRefreshInterval() {
.flinkConfig(FlinkWriteOptions.TABLE_REFRESH_INTERVAL)
.parseOptional();
}
+
+ public boolean parquetShredVariants() {
+ return confParser
+ .booleanConf()
+ .option(FlinkWriteOptions.PARQUET_SHRED_VARIANTS.key())
+ .tableProperty(TableProperties.PARQUET_SHRED_VARIANTS)
+ .defaultValue(TableProperties.PARQUET_SHRED_VARIANTS_DEFAULT)
+ .parse();
+ }
+
+ public int parquetVariantInferenceBufferSize() {
+ return confParser
+ .intConf()
+ .option(FlinkWriteOptions.PARQUET_VARIANT_INFERENCE_BUFFER_SIZE.key())
Review Comment:
Could you add these to the flink documentation similar to the #14297 for spark?
##########
parquet/src/test/java/org/apache/iceberg/parquet/TestParquetDataWriter.java:
##########
@@ -370,7 +370,7 @@ protected int resolveColumnIndex(Void engineSchema, String columnName) {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
GenericParquetReaders.buildReader(icebergSchema, fileSchema),
testAnalyzer,
- record -> record);
+ unused -> oriRecord -> oriRecord);
Review Comment:
What was this change for?
##########
parquet/src/main/java/org/apache/iceberg/parquet/ParquetFormatModel.java:
##########
@@ -51,7 +51,7 @@ public class ParquetFormatModel<D, S, R>
extends BaseFormatModel<D, S, ParquetValueWriter<?>, R, MessageType> {
private final boolean isBatchReader;
private final VariantShreddingAnalyzer<D, S> variantAnalyzer;
- private final UnaryOperator<D> copyFunc;
+ private final Function<S, UnaryOperator<D>> copyFuncFactory;
Review Comment:
Based on this comment thread we decided to keep the UnaryOperator: https://github.com/apache/iceberg/pull/14297#discussion_r3015349863. @pvary suggest BiFunction too? Worth exploring to see what is the best way or we can keep it to Unary too.
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteConf.java:
##########
@@ -262,4 +262,22 @@ public Duration tableRefreshInterval() {
.flinkConfig(FlinkWriteOptions.TABLE_REFRESH_INTERVAL)
.parseOptional();
}
+
+ public boolean shredVariants() {
+ return confParser
+ .booleanConf()
+ .option(FlinkWriteOptions.SHRED_VARIANTS.key())
+ .tableProperty(TableProperties.PARQUET_SHRED_VARIANTS)
+ .defaultValue(TableProperties.PARQUET_SHRED_VARIANTS_DEFAULT)
Review Comment:
Let's do parquet for now since we followed that pattern for the Spark implementation.
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteOptions.java:
##########
@@ -105,4 +105,10 @@ private FlinkWriteOptions() {}
// specify the uidSuffix to be used for the underlying IcebergSink
public static final ConfigOption<String> UID_SUFFIX =
ConfigOptions.key("uid-suffix").stringType().defaultValue("");
+
+ public static final ConfigOption<Boolean> PARQUET_SHRED_VARIANTS =
+ ConfigOptions.key("parquet-shred-variants").booleanType().defaultValue(false);
+
+ public static final ConfigOption<Integer> PARQUET_VARIANT_INFERENCE_BUFFER_SIZE =
Review Comment:
Could you rename this to VARIANT_INFERENCE_BUFFER_SIZE to be consistent with Spark>
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile closed pull request #15596: Flink: Support writing shredded variant in Flink
URL: https://github.com/apache/iceberg/pull/15596
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3214332405
##########
spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/source/SparkFormatModels.java:
##########
@@ -53,7 +53,7 @@ public static void register() {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new SparkVariantShreddingAnalyzer(),
- InternalRow::copy));
+ structType -> InternalRow::copy));
Review Comment:
Since we changed `UnaryOperator<D>` to `Function<S, UnaryOperator<D>>`, the Spark part should be adjusted accordingly.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3246786791
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkVariantShreddingAnalyzer.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.flink.data;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.List;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.types.variant.BinaryVariant;
+import org.apache.iceberg.parquet.VariantShreddingAnalyzer;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.VariantMetadata;
+import org.apache.iceberg.variants.VariantValue;
+
+/**
+ * Analyzes Variant fields in Flink {@link RowData} and converts Flink's binary Variant
+ * representation to Iceberg {@link VariantValue} instances for Variant shredding.
+ */
+public class FlinkVariantShreddingAnalyzer extends VariantShreddingAnalyzer<RowData, RowType> {
+
+ @Override
+ protected List<VariantValue> extractVariantValues(
+ List<RowData> bufferedRows, int variantFieldIndex) {
+ List<VariantValue> values = Lists.newArrayList();
+
+ for (RowData row : bufferedRows) {
+ if (!row.isNullAt(variantFieldIndex)) {
+ // Flink currently has only BinaryVariant as its Variant implementation, so this analyzer
+ // intentionally narrows RowData#getVariant's return value to BinaryVariant here.
+ BinaryVariant flinkVariant = (BinaryVariant) row.getVariant(variantFieldIndex);
+ if (flinkVariant != null) {
+ VariantValue variantValue =
+ VariantValue.from(
+ VariantMetadata.from(
+ ByteBuffer.wrap(flinkVariant.getMetadata()).order(ByteOrder.LITTLE_ENDIAN)),
+ ByteBuffer.wrap(flinkVariant.getValue()).order(ByteOrder.LITTLE_ENDIAN));
+
+ values.add(variantValue);
+ }
+ }
+ }
+
+ return values;
+ }
+
+ @Override
+ protected int resolveColumnIndex(RowType flinkSchema, String columnName) {
+ try {
Review Comment:
Good Catche, remove try catch now.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3248454441
##########
spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/SparkFormatModels.java:
##########
@@ -53,7 +53,7 @@ public static void register() {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new SparkVariantShreddingAnalyzer(),
- InternalRow::copy));
+ structType -> InternalRow::copy));
Review Comment:
Is this:
```suggestion
unused -> InternalRow::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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3248452184
##########
spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/source/SparkFormatModels.java:
##########
@@ -53,7 +53,7 @@ public static void register() {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new SparkVariantShreddingAnalyzer(),
- InternalRow::copy));
+ structType -> InternalRow::copy));
Review Comment:
Is this:
```suggestion
unused -> InternalRow::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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3270790493
##########
spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/source/SparkFormatModels.java:
##########
@@ -53,7 +53,7 @@ public static void register() {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new SparkVariantShreddingAnalyzer(),
- InternalRow::copy));
+ structType -> InternalRow::copy));
Review Comment:
Thanks for pointing out . Changed now.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#issuecomment-4494057287
@pvary @talatuyarer @nssalian @aihuaxu Hey all, I rebased main but ran into some CI failures. Looks like a new check was added recently that doesn't allow modifying the ParquetFormatModel parameter types directly.
As a workaround, I added a new method createWithCopyFuncFactory in ParquetFormatModel. The original create method now delegates to it, so the Spark code stays untouched, while FlinkFormatModels calls createWithCopyFuncFactory explicitly.
Would really appreciate it if you could help take another look at these changes. Thanks a lot!
```
java.method.parameterTypeChanged: The type of the parameter changed from 'java.util.function.UnaryOperator<D extends java.lang.Object>' to 'java.util.function.Function<S extends java.lang.Object, java.util.function.UnaryOperator<D extends java.lang.Object>>'.
old: parameter <D, S> org.apache.iceberg.parquet.ParquetFormatModel<D, S, org.apache.iceberg.parquet.ParquetValueReader<?>> org.apache.iceberg.parquet.ParquetFormatModel<D, S, R>::create(java.lang.Class<D>, java.lang.Class<S>, org.apache.iceberg.formats.BaseFormatModel.WriterFunction<org.apache.iceberg.parquet.ParquetValueWriter<?>, S, org.apache.parquet.schema.MessageType>, org.apache.iceberg.formats.BaseFormatModel.ReaderFunction<org.apache.iceberg.parquet.ParquetValueReader<?>, S, org.apache.parquet.schema.MessageType>, org.apache.iceberg.parquet.VariantShreddingAnalyzer<D, S>, ===java.util.function.UnaryOperator<D>===)
new: parameter <D, S> org.apache.iceberg.parquet.ParquetFormatModel<D, S, org.apache.iceberg.parquet.ParquetValueReader<?>> org.apache.iceberg.parquet.ParquetFormatModel<D, S, R>::create(java.lang.Class<D>, java.lang.Class<S>, org.apache.iceberg.formats.BaseFormatModel.WriterFunction<org.apache.iceberg.parquet.ParquetValueWriter<?>, S, org.apache.parquet.schema.MessageType>, org.apache.iceberg.formats.BaseFormatModel.ReaderFunction<org.apache.iceberg.parquet.ParquetValueReader<?>, S, org.apache.parquet.schema.MessageType>, org.apache.iceberg.parquet.VariantShreddingAnalyzer<D, S>, ===java.util.function.Function<S, java.util.function.UnaryOperator<D>>===)
```
https://github.com/apache/iceberg/actions/runs/26136650474/job/76873207059?pr=15596
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3279107723
##########
parquet/src/main/java/org/apache/iceberg/parquet/ParquetFormatModel.java:
##########
@@ -267,6 +289,9 @@ public FileAppender<D> build() throws IOException {
* top-level fields.
*/
private FileAppender<D> buildShreddedAppender() {
+ UnaryOperator<D> copyFunc = copyFuncFactory.apply(engineSchema);
+ Preconditions.checkState(copyFunc != null, "copyFunc must not return null");
Review Comment:
Ok, add a check for copyFuncFactory first.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "talatuyarer (via GitHub)" <gi...@apache.org>.
talatuyarer commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3213394357
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkVariantShreddingAnalyzer.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.flink.data;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.List;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.types.variant.BinaryVariant;
+import org.apache.iceberg.parquet.VariantShreddingAnalyzer;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.VariantMetadata;
+import org.apache.iceberg.variants.VariantValue;
+
+public class FlinkVariantShreddingAnalyzer extends VariantShreddingAnalyzer<RowData, RowType> {
Review Comment:
nit: Can we have Javadoc
##########
spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/SparkFormatModels.java:
##########
@@ -53,7 +53,7 @@ public static void register() {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new SparkVariantShreddingAnalyzer(),
- InternalRow::copy));
+ structType -> InternalRow::copy));
Review Comment:
nit: We dont need this change anymore. Can you revert it ?
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,851 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','parquet-variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @Override
+ @AfterEach
+ public void clean() {
+ super.clean();
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "4");
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(7);
+ }
+
+ @TestTemplate
+ public void testColumnIndexTruncateLength() throws IOException {
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ int customTruncateLength = 10;
+ sql(
+ "ALTER TABLE %s SET ('%s'='%d')",
+ TABLE_NAME, "parquet.columnindex.truncate.length", customTruncateLength);
+
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ String longValue = "A".repeat(20);
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"description\": \"%s\", \"id\": %d}'))", i, longValue, i));
+ }
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ GroupType description =
+ field(
+ "description",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType id =
+ field(
+ "id",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(description, id));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(10);
Review Comment:
open the Parquet file and read the column index for the shredded description typed_value column, then assert the min/max byte-length is ≤ 10. Something like:
```
try (ParquetFileReader reader = ParquetFileReader.open(inputFile)) {
ColumnIndex idx = reader.readColumnIndex(/* shredded description chunk */);
// assert min/max bytes truncated to <= 10
}
```
##########
spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/source/SparkFormatModels.java:
##########
@@ -53,7 +53,7 @@ public static void register() {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new SparkVariantShreddingAnalyzer(),
- InternalRow::copy));
+ structType -> InternalRow::copy));
Review Comment:
nit: We dont need this change anymore. Can you revert it ?
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkVariantShreddingAnalyzer.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.flink.data;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.List;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.types.variant.BinaryVariant;
+import org.apache.iceberg.parquet.VariantShreddingAnalyzer;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.VariantMetadata;
+import org.apache.iceberg.variants.VariantValue;
+
+public class FlinkVariantShreddingAnalyzer extends VariantShreddingAnalyzer<RowData, RowType> {
+
+ @Override
+ protected List<VariantValue> extractVariantValues(
+ List<RowData> bufferedRows, int variantFieldIndex) {
+ List<VariantValue> values = Lists.newArrayList();
+
+ for (RowData row : bufferedRows) {
+ if (!row.isNullAt(variantFieldIndex)) {
+ BinaryVariant flinkVariant = (BinaryVariant) row.getVariant(variantFieldIndex);
Review Comment:
nit: This assumes `RowData.getVariant()` returns `BinaryVariant`. A comment explaining this contract would prevent a silent `ClassCastException` if the return type changes.
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,851 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','parquet-variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @Override
+ @AfterEach
+ public void clean() {
+ super.clean();
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "4");
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(7);
+ }
+
+ @TestTemplate
+ public void testColumnIndexTruncateLength() throws IOException {
Review Comment:
The name is misleading. either rename this test to testRoundTripWithLargeStringValues which is what it actually tests, or add a column-index assertion.
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,851 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','parquet-variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @Override
+ @AfterEach
+ public void clean() {
+ super.clean();
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "4");
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(7);
+ }
+
+ @TestTemplate
+ public void testColumnIndexTruncateLength() throws IOException {
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ int customTruncateLength = 10;
+ sql(
+ "ALTER TABLE %s SET ('%s'='%d')",
+ TABLE_NAME, "parquet.columnindex.truncate.length", customTruncateLength);
+
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ String longValue = "A".repeat(20);
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"description\": \"%s\", \"id\": %d}'))", i, longValue, i));
+ }
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ GroupType description =
+ field(
+ "description",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType id =
+ field(
+ "id",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(description, id));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(10);
+ }
+
+ @TestTemplate
+ public void testIntegerFamilyPromotion() throws IOException {
+
+ // Mix of INT8, INT16, INT32, INT64 - should promote to INT64
+ String values =
+ "(1, parse_json('{\"value\": 10}')),"
+ + " (2, parse_json('{\"value\": 1000}')),"
+ + " (3, parse_json('{\"value\": 100000}')),"
+ + " (4, parse_json('{\"value\": 10000000000}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT64, LogicalTypeAnnotation.intType(64, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalFamilyPromotion() throws IOException {
+
+ // Test that they get promoted to the most capable decimal type observed
+ String values =
+ "(1, parse_json('{\"value\": 1.5}')),"
+ + " (2, parse_json('{\"value\": 123.456789}')),"
+ + " (3, parse_json('{\"value\": 123456789123456.789}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY,
+ 16,
+ LogicalTypeAnnotation.decimalType(6, 21)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDataRoundTripWithShredding() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify that we can read the data back correctly
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.age' RETURNING int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(3);
+ assertThat(rows.get(0).getField(0)).isEqualTo(1);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isEqualTo(30);
+ assertThat(rows.get(1).getField(0)).isEqualTo(2);
+ assertThat(rows.get(1).getField(1)).isEqualTo("Bob");
+ assertThat(rows.get(1).getField(2)).isEqualTo(25);
+ assertThat(rows.get(2).getField(0)).isEqualTo(3);
+ assertThat(rows.get(2).getField(1)).isEqualTo("Charlie");
+ assertThat(rows.get(2).getField(2)).isEqualTo(35);
+ }
+
+ @TestTemplate
+ public void testVariantWithNullValues() throws IOException {
+
+ String values =
+ "(1, parse_json('null'))," + " (2, parse_json('null'))," + " (3, parse_json('null'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayOfNullElementsWithShredding() throws IOException {
+
+ sql(
+ "INSERT INTO %s VALUES (1, parse_json('[null, null, null]')), "
+ + "(2, parse_json('[null]'))",
+ TABLE_NAME);
+
+ // Array elements are all null, element type is null, falls back to unshredded
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testInfrequentFieldPruning() throws IOException {
Review Comment:
The test is implicitly coupled to `MIN_FIELD_FREQUENCY = 0.10`. The ratio of 1/11 (≈ 0.0909) is just below this threshold. If this constant is adjusted, the test's outcome will change without an obvious failure. Either reference the constant or add a comment to document this dependency.
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,851 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','parquet-variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @Override
+ @AfterEach
+ public void clean() {
+ super.clean();
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "4");
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(7);
+ }
+
+ @TestTemplate
+ public void testColumnIndexTruncateLength() throws IOException {
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ int customTruncateLength = 10;
+ sql(
+ "ALTER TABLE %s SET ('%s'='%d')",
+ TABLE_NAME, "parquet.columnindex.truncate.length", customTruncateLength);
+
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ String longValue = "A".repeat(20);
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"description\": \"%s\", \"id\": %d}'))", i, longValue, i));
+ }
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ GroupType description =
+ field(
+ "description",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType id =
+ field(
+ "id",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(description, id));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(10);
+ }
+
+ @TestTemplate
+ public void testIntegerFamilyPromotion() throws IOException {
+
+ // Mix of INT8, INT16, INT32, INT64 - should promote to INT64
+ String values =
+ "(1, parse_json('{\"value\": 10}')),"
+ + " (2, parse_json('{\"value\": 1000}')),"
+ + " (3, parse_json('{\"value\": 100000}')),"
+ + " (4, parse_json('{\"value\": 10000000000}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT64, LogicalTypeAnnotation.intType(64, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalFamilyPromotion() throws IOException {
+
+ // Test that they get promoted to the most capable decimal type observed
+ String values =
+ "(1, parse_json('{\"value\": 1.5}')),"
+ + " (2, parse_json('{\"value\": 123.456789}')),"
+ + " (3, parse_json('{\"value\": 123456789123456.789}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY,
+ 16,
+ LogicalTypeAnnotation.decimalType(6, 21)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDataRoundTripWithShredding() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify that we can read the data back correctly
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.age' RETURNING int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(3);
+ assertThat(rows.get(0).getField(0)).isEqualTo(1);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isEqualTo(30);
+ assertThat(rows.get(1).getField(0)).isEqualTo(2);
+ assertThat(rows.get(1).getField(1)).isEqualTo("Bob");
+ assertThat(rows.get(1).getField(2)).isEqualTo(25);
+ assertThat(rows.get(2).getField(0)).isEqualTo(3);
+ assertThat(rows.get(2).getField(1)).isEqualTo("Charlie");
+ assertThat(rows.get(2).getField(2)).isEqualTo(35);
+ }
+
+ @TestTemplate
+ public void testVariantWithNullValues() throws IOException {
+
+ String values =
+ "(1, parse_json('null'))," + " (2, parse_json('null'))," + " (3, parse_json('null'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayOfNullElementsWithShredding() throws IOException {
+
+ sql(
+ "INSERT INTO %s VALUES (1, parse_json('[null, null, null]')), "
+ + "(2, parse_json('[null]'))",
+ TABLE_NAME);
+
+ // Array elements are all null, element type is null, falls back to unshredded
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testInfrequentFieldPruning() throws IOException {
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='11')", TABLE_NAME);
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 11; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ if (i == 1) {
+ // Only the first row has rare_field
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"name\": \"User%d\", \"rare_field\": \"rare\"}'))", i, i));
+ } else {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"name\": \"User%d\"}'))", i, i));
+ }
+ }
+
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ // rare_field appears in 1/11 rows, should be pruned
+ // name appears in 11/11 rows and should be kept
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testMixedTypeTieBreaking() throws IOException {
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ if (i <= 5) {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"val\": %d}'))", i, i));
+ } else {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"val\": \"text%d\"}'))", i, i));
+ }
+ }
+
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ // 5 ints + 5 strings is a tie so STRING wins (higher TIE_BREAK_PRIORITY)
+ GroupType val =
+ field(
+ "val",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(val));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testFieldOnlyAfterBuffer() throws IOException {
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "1");
+
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ sql(
+ "CREATE TEMPORARY VIEW tmp_source AS "
+ + "SELECT * FROM (VALUES "
+ + "(1, parse_json('{\"name\": \"Alice\"}')), "
+ + "(2, parse_json('{\"name\": \"Bob\"}')), "
+ + "(3, parse_json('{\"name\": \"Charlie\"}')), "
+ + "(4, parse_json('{\"name\": \"David\", \"score\": 95}')), "
+ + "(5, parse_json('{\"name\": \"Eve\", \"score\": 88}')), "
+ + "(6, parse_json('{\"name\": \"Frank\", \"score\": 72}')), "
+ + "(7, parse_json('{\"name\": \"Grace\", \"score\": 91}'))"
+ + ") AS t(id, address)");
+
+ sql("INSERT INTO %s SELECT id, address FROM tmp_source ORDER BY id", TABLE_NAME);
+
+ // Schema is determined from buffer (rows 1-3) which only has "name".
+ // "score" is not shredded
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify all data round-trips despite "score" not being shredded
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.score' returning int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(7);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isNull();
+ assertThat(rows.get(3).getField(1)).isEqualTo("David");
+ assertThat(rows.get(3).getField(2)).isEqualTo(95);
+ assertThat(rows.get(6).getField(1)).isEqualTo("Grace");
+ assertThat(rows.get(6).getField(2)).isEqualTo(91);
+
+ sql("DROP TEMPORARY VIEW IF EXISTS tmp_source");
+ }
+
+ @TestTemplate
+ public void testCrossFileDifferentShreddedType() throws IOException {
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ // File 1: "score" is always integer → shredded as INT8
+ String batch1 =
+ "(1, parse_json('{\"score\": 95}')),"
+ + " (2, parse_json('{\"score\": 88}')),"
+ + " (3, parse_json('{\"score\": 72}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, batch1);
+
+ // Verify file 1 schema: score shredded as INT8
+ GroupType scoreInt =
+ field(
+ "score",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema1 =
+ parquetSchema(variant("address", 2, Type.Repetition.REQUIRED, objectFields(scoreInt)));
+ verifyParquetSchema(icebergTable, expectedSchema1);
+
+ // File 2: "score" is always string → shredded as STRING
+ String batch2 =
+ "(4, parse_json('{\"score\": \"high\"}')),"
+ + " (5, parse_json('{\"score\": \"medium\"}')),"
+ + " (6, parse_json('{\"score\": \"low\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, batch2);
+
+ // Query across both files, reader must handle different shredded types
+ List<Row> rows =
+ sql("SELECT id, json_value(address, '$.score') FROM %s ORDER BY id", TABLE_NAME);
+ assertThat(rows).hasSize(6);
+ assertThat(rows.get(0).getField(1)).isEqualTo("95");
+ assertThat(rows.get(1).getField(1)).isEqualTo("88");
+ assertThat(rows.get(3).getField(1)).isEqualTo("high");
+ assertThat(rows.get(5).getField(1)).isEqualTo("low");
+ }
+
+ @TestTemplate
+ public void testAllNullVariantColumn() throws IOException {
+
+ String variantNullAbleTableName = "test_all_null_variant_column";
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant) with ('write.format.default'='%s','format-version'='3','shred-variants'='true','parquet-variant-inference-buffer-size'='10')",
+ variantNullAbleTableName, FileFormat.PARQUET.name());
+
+ sql(
+ "INSERT INTO %s VALUES (1, CAST(null AS VARIANT)), (2, CAST(null AS VARIANT)), (3, CAST(null AS VARIANT))",
+ variantNullAbleTableName);
+
+ // All variant values are SQL NULL, so no shredding should occur
+ MessageType expectedSchema = parquetSchema(variant("address", 2, Type.Repetition.OPTIONAL));
+ Table variantNullAbleTable =
+ validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, variantNullAbleTableName));
+ verifyParquetSchema(variantNullAbleTable, expectedSchema);
+
+ List<Row> rows = sql("SELECT id, address FROM %s ORDER BY id", variantNullAbleTableName);
+ assertThat(rows).hasSize(3);
+ assertThat(rows.get(0).getField(1)).isNull();
+ assertThat(rows.get(1).getField(1)).isNull();
+ assertThat(rows.get(2).getField(1)).isNull();
+ }
+
+ @TestTemplate
+ public void testBufferSizeOne() throws IOException {
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='1')", TABLE_NAME);
+
+ sql(
+ "INSERT INTO %s VALUES "
+ + "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))",
+ TABLE_NAME);
+
+ // Schema inferred from first row only, should still shred name and age
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ List<Row> rows =
+ sql("SELECT id, json_value(address, '$.name') FROM %s ORDER BY id", TABLE_NAME);
+ assertThat(rows).hasSize(3);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(2).getField(1)).isEqualTo("Charlie");
+ }
+
+ @TestTemplate
+ public void testDecimalFallbackAfterBuffer() {
Review Comment:
My understanding is that this test verifies that rows which don't fit the inferred shredded type are written to the unshredded value field. However, the test only performs a round trip verification. The schema assertion would be the most informative part of this test.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3246800104
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,874 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @Override
+ @AfterEach
+ public void clean() {
+ super.clean();
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "4");
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(7);
+ }
+
+ @TestTemplate
+ public void testColumnIndexTruncateLength() throws IOException {
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ int customTruncateLength = 10;
+ sql(
+ "ALTER TABLE %s SET ('%s'='%d')",
+ TABLE_NAME, "parquet.columnindex.truncate.length", customTruncateLength);
+
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ String longValue = "A".repeat(20);
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"description\": \"%s\", \"id\": %d}'))", i, longValue, i));
+ }
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ GroupType description =
+ field(
+ "description",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType id =
+ field(
+ "id",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(description, id));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(10);
+ }
+
+ @TestTemplate
+ public void testIntegerFamilyPromotion() throws IOException {
+
+ // Mix of INT8, INT16, INT32, INT64 - should promote to INT64
+ String values =
+ "(1, parse_json('{\"value\": 10}')),"
+ + " (2, parse_json('{\"value\": 1000}')),"
+ + " (3, parse_json('{\"value\": 100000}')),"
+ + " (4, parse_json('{\"value\": 10000000000}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT64, LogicalTypeAnnotation.intType(64, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalFamilyPromotion() throws IOException {
+
+ // Test that they get promoted to the most capable decimal type observed
+ String values =
+ "(1, parse_json('{\"value\": 1.5}')),"
+ + " (2, parse_json('{\"value\": 123.456789}')),"
+ + " (3, parse_json('{\"value\": 123456789123456.789}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY,
+ 16,
+ LogicalTypeAnnotation.decimalType(6, 21)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDataRoundTripWithShredding() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify that we can read the data back correctly
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.age' RETURNING int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(3);
+ assertThat(rows.get(0).getField(0)).isEqualTo(1);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isEqualTo(30);
+ assertThat(rows.get(1).getField(0)).isEqualTo(2);
+ assertThat(rows.get(1).getField(1)).isEqualTo("Bob");
+ assertThat(rows.get(1).getField(2)).isEqualTo(25);
+ assertThat(rows.get(2).getField(0)).isEqualTo(3);
+ assertThat(rows.get(2).getField(1)).isEqualTo("Charlie");
+ assertThat(rows.get(2).getField(2)).isEqualTo(35);
+ }
+
+ @TestTemplate
+ public void testVariantWithNullValues() throws IOException {
+
+ String values =
+ "(1, parse_json('null'))," + " (2, parse_json('null'))," + " (3, parse_json('null'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayOfNullElementsWithShredding() throws IOException {
+
+ sql(
+ "INSERT INTO %s VALUES (1, parse_json('[null, null, null]')), "
+ + "(2, parse_json('[null]'))",
+ TABLE_NAME);
+
+ // Array elements are all null, element type is null, falls back to unshredded
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testInfrequentFieldPruning() throws IOException {
+ // This test relies on the current VariantShreddingAnalyzer MIN_FIELD_FREQUENCY threshold of
+ // 0.10: rare_field appears in 1/11 rows (~0.09), so it should be pruned.
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='11')", TABLE_NAME);
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 11; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ if (i == 1) {
+ // Only the first row has rare_field
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"name\": \"User%d\", \"rare_field\": \"rare\"}'))", i, i));
+ } else {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"name\": \"User%d\"}'))", i, i));
+ }
+ }
+
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ // rare_field appears in 1/11 rows, should be pruned
+ // name appears in 11/11 rows and should be kept
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testMixedTypeTieBreaking() throws IOException {
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ if (i <= 5) {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"val\": %d}'))", i, i));
+ } else {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"val\": \"text%d\"}'))", i, i));
+ }
+ }
+
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ // 5 ints + 5 strings is a tie so STRING wins (higher TIE_BREAK_PRIORITY)
+ GroupType val =
+ field(
+ "val",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(val));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testFieldOnlyAfterBuffer() throws IOException {
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "1");
+
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ sql(
+ "CREATE TEMPORARY VIEW tmp_source AS "
+ + "SELECT * FROM (VALUES "
+ + "(1, parse_json('{\"name\": \"Alice\"}')), "
+ + "(2, parse_json('{\"name\": \"Bob\"}')), "
+ + "(3, parse_json('{\"name\": \"Charlie\"}')), "
+ + "(4, parse_json('{\"name\": \"David\", \"score\": 95}')), "
+ + "(5, parse_json('{\"name\": \"Eve\", \"score\": 88}')), "
+ + "(6, parse_json('{\"name\": \"Frank\", \"score\": 72}')), "
+ + "(7, parse_json('{\"name\": \"Grace\", \"score\": 91}'))"
+ + ") AS t(id, address)");
+
+ sql("INSERT INTO %s SELECT id, address FROM tmp_source ORDER BY id", TABLE_NAME);
+
+ // Schema is determined from buffer (rows 1-3) which only has "name".
+ // "score" is not shredded
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify all data round-trips despite "score" not being shredded
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.score' returning int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(7);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isNull();
+ assertThat(rows.get(3).getField(1)).isEqualTo("David");
+ assertThat(rows.get(3).getField(2)).isEqualTo(95);
+ assertThat(rows.get(6).getField(1)).isEqualTo("Grace");
+ assertThat(rows.get(6).getField(2)).isEqualTo(91);
+
+ sql("DROP TEMPORARY VIEW IF EXISTS tmp_source");
+ }
+
+ @TestTemplate
+ public void testCrossFileDifferentShreddedType() throws IOException {
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ // File 1: "score" is always integer → shredded as INT8
+ String batch1 =
+ "(1, parse_json('{\"score\": 95}')),"
+ + " (2, parse_json('{\"score\": 88}')),"
+ + " (3, parse_json('{\"score\": 72}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, batch1);
+
+ // Verify file 1 schema: score shredded as INT8
+ GroupType scoreInt =
+ field(
+ "score",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema1 =
+ parquetSchema(variant("address", 2, Type.Repetition.REQUIRED, objectFields(scoreInt)));
+ verifyParquetSchema(icebergTable, expectedSchema1);
+
+ // File 2: "score" is always string → shredded as STRING
+ String batch2 =
+ "(4, parse_json('{\"score\": \"high\"}')),"
+ + " (5, parse_json('{\"score\": \"medium\"}')),"
+ + " (6, parse_json('{\"score\": \"low\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, batch2);
+
+ // Query across both files, reader must handle different shredded types
+ List<Row> rows =
+ sql("SELECT id, json_value(address, '$.score') FROM %s ORDER BY id", TABLE_NAME);
+ assertThat(rows).hasSize(6);
+ assertThat(rows.get(0).getField(1)).isEqualTo("95");
+ assertThat(rows.get(1).getField(1)).isEqualTo("88");
+ assertThat(rows.get(3).getField(1)).isEqualTo("high");
+ assertThat(rows.get(5).getField(1)).isEqualTo("low");
+ }
+
+ @TestTemplate
+ public void testAllNullVariantColumn() throws IOException {
+
+ String variantNullAbleTableName = "test_all_null_variant_column";
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant) with ('write.format.default'='%s','format-version'='3','shred-variants'='true','variant-inference-buffer-size'='10')",
Review Comment:
Yeah, you're right. The config was wrong, I've already updated it. The tests passed because we were inserting all nulls, so the result should be identical with or without shredding enabled. The whole point here was just to verify that shredding handles nulls correctly. Good catch, thanks!
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3212666749
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,844 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(7);
+ }
+
+ @TestTemplate
+ public void testColumnIndexTruncateLength() throws IOException {
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ int customTruncateLength = 10;
+ sql(
+ "ALTER TABLE %s SET ('%s'='%d')",
+ TABLE_NAME, "parquet.columnindex.truncate.length", customTruncateLength);
+
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ String longValue = "A".repeat(20);
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"description\": \"%s\", \"id\": %d}'))", i, longValue, i));
+ }
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ GroupType description =
+ field(
+ "description",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType id =
+ field(
+ "id",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(description, id));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(10);
+ }
+
+ @TestTemplate
+ public void testIntegerFamilyPromotion() throws IOException {
+
+ // Mix of INT8, INT16, INT32, INT64 - should promote to INT64
+ String values =
+ "(1, parse_json('{\"value\": 10}')),"
+ + " (2, parse_json('{\"value\": 1000}')),"
+ + " (3, parse_json('{\"value\": 100000}')),"
+ + " (4, parse_json('{\"value\": 10000000000}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT64, LogicalTypeAnnotation.intType(64, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalFamilyPromotion() throws IOException {
+
+ // Test that they get promoted to the most capable decimal type observed
+ String values =
+ "(1, parse_json('{\"value\": 1.5}')),"
+ + " (2, parse_json('{\"value\": 123.456789}')),"
+ + " (3, parse_json('{\"value\": 123456789123456.789}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY,
+ 16,
+ LogicalTypeAnnotation.decimalType(6, 21)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDataRoundTripWithShredding() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify that we can read the data back correctly
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.age' RETURNING int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(3);
+ assertThat(rows.get(0).getField(0)).isEqualTo(1);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isEqualTo(30);
+ assertThat(rows.get(1).getField(0)).isEqualTo(2);
+ assertThat(rows.get(1).getField(1)).isEqualTo("Bob");
+ assertThat(rows.get(1).getField(2)).isEqualTo(25);
+ assertThat(rows.get(2).getField(0)).isEqualTo(3);
+ assertThat(rows.get(2).getField(1)).isEqualTo("Charlie");
+ assertThat(rows.get(2).getField(2)).isEqualTo(35);
+ }
+
+ @TestTemplate
+ public void testVariantWithNullValues() throws IOException {
+
+ String values =
+ "(1, parse_json('null'))," + " (2, parse_json('null'))," + " (3, parse_json('null'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayOfNullElementsWithShredding() throws IOException {
+
+ sql(
+ "INSERT INTO %s VALUES (1, parse_json('[null, null, null]')), "
+ + "(2, parse_json('[null]'))",
+ TABLE_NAME);
+
+ // Array elements are all null, element type is null, falls back to unshredded
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testInfrequentFieldPruning() throws IOException {
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='11')", TABLE_NAME);
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 11; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ if (i == 1) {
+ // Only the first row has rare_field
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"name\": \"User%d\", \"rare_field\": \"rare\"}'))", i, i));
+ } else {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"name\": \"User%d\"}'))", i, i));
+ }
+ }
+
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ // rare_field appears in 1/11 rows, should be pruned
+ // name appears in 11/11 rows and should be kept
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testMixedTypeTieBreaking() throws IOException {
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ if (i <= 5) {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"val\": %d}'))", i, i));
+ } else {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"val\": \"text%d\"}'))", i, i));
+ }
+ }
+
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ // 5 ints + 5 strings is a tie so STRING wins (higher TIE_BREAK_PRIORITY)
+ GroupType val =
+ field(
+ "val",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(val));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testFieldOnlyAfterBuffer() throws IOException {
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "1");
+
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ sql(
+ "CREATE TEMPORARY VIEW tmp_source AS "
+ + "SELECT * FROM (VALUES "
+ + "(1, parse_json('{\"name\": \"Alice\"}')), "
+ + "(2, parse_json('{\"name\": \"Bob\"}')), "
+ + "(3, parse_json('{\"name\": \"Charlie\"}')), "
+ + "(4, parse_json('{\"name\": \"David\", \"score\": 95}')), "
+ + "(5, parse_json('{\"name\": \"Eve\", \"score\": 88}')), "
+ + "(6, parse_json('{\"name\": \"Frank\", \"score\": 72}')), "
+ + "(7, parse_json('{\"name\": \"Grace\", \"score\": 91}'))"
+ + ") AS t(id, address)");
+
+ sql("INSERT INTO %s SELECT id, address FROM tmp_source ORDER BY id", TABLE_NAME);
+
+ // Schema is determined from buffer (rows 1-3) which only has "name".
+ // "score" is not shredded
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify all data round-trips despite "score" not being shredded
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.score' returning int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(7);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isNull();
+ assertThat(rows.get(3).getField(1)).isEqualTo("David");
+ assertThat(rows.get(3).getField(2)).isEqualTo(95);
+ assertThat(rows.get(6).getField(1)).isEqualTo("Grace");
+ assertThat(rows.get(6).getField(2)).isEqualTo(91);
+
+ getTableEnv()
Review Comment:
Thanks for pointing it out . Move it to clean method.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3214357004
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,851 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','parquet-variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @Override
+ @AfterEach
+ public void clean() {
+ super.clean();
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "4");
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(7);
+ }
+
+ @TestTemplate
+ public void testColumnIndexTruncateLength() throws IOException {
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ int customTruncateLength = 10;
+ sql(
+ "ALTER TABLE %s SET ('%s'='%d')",
+ TABLE_NAME, "parquet.columnindex.truncate.length", customTruncateLength);
+
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ String longValue = "A".repeat(20);
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"description\": \"%s\", \"id\": %d}'))", i, longValue, i));
+ }
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ GroupType description =
+ field(
+ "description",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType id =
+ field(
+ "id",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(description, id));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(10);
+ }
+
+ @TestTemplate
+ public void testIntegerFamilyPromotion() throws IOException {
+
+ // Mix of INT8, INT16, INT32, INT64 - should promote to INT64
+ String values =
+ "(1, parse_json('{\"value\": 10}')),"
+ + " (2, parse_json('{\"value\": 1000}')),"
+ + " (3, parse_json('{\"value\": 100000}')),"
+ + " (4, parse_json('{\"value\": 10000000000}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT64, LogicalTypeAnnotation.intType(64, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalFamilyPromotion() throws IOException {
+
+ // Test that they get promoted to the most capable decimal type observed
+ String values =
+ "(1, parse_json('{\"value\": 1.5}')),"
+ + " (2, parse_json('{\"value\": 123.456789}')),"
+ + " (3, parse_json('{\"value\": 123456789123456.789}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY,
+ 16,
+ LogicalTypeAnnotation.decimalType(6, 21)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDataRoundTripWithShredding() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify that we can read the data back correctly
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.age' RETURNING int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(3);
+ assertThat(rows.get(0).getField(0)).isEqualTo(1);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isEqualTo(30);
+ assertThat(rows.get(1).getField(0)).isEqualTo(2);
+ assertThat(rows.get(1).getField(1)).isEqualTo("Bob");
+ assertThat(rows.get(1).getField(2)).isEqualTo(25);
+ assertThat(rows.get(2).getField(0)).isEqualTo(3);
+ assertThat(rows.get(2).getField(1)).isEqualTo("Charlie");
+ assertThat(rows.get(2).getField(2)).isEqualTo(35);
+ }
+
+ @TestTemplate
+ public void testVariantWithNullValues() throws IOException {
+
+ String values =
+ "(1, parse_json('null'))," + " (2, parse_json('null'))," + " (3, parse_json('null'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayOfNullElementsWithShredding() throws IOException {
+
+ sql(
+ "INSERT INTO %s VALUES (1, parse_json('[null, null, null]')), "
+ + "(2, parse_json('[null]'))",
+ TABLE_NAME);
+
+ // Array elements are all null, element type is null, falls back to unshredded
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testInfrequentFieldPruning() throws IOException {
Review Comment:
Good point. Add a comment to clarify it
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3214338443
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkVariantShreddingAnalyzer.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.flink.data;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.List;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.types.variant.BinaryVariant;
+import org.apache.iceberg.parquet.VariantShreddingAnalyzer;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.VariantMetadata;
+import org.apache.iceberg.variants.VariantValue;
+
+public class FlinkVariantShreddingAnalyzer extends VariantShreddingAnalyzer<RowData, RowType> {
+
+ @Override
+ protected List<VariantValue> extractVariantValues(
+ List<RowData> bufferedRows, int variantFieldIndex) {
+ List<VariantValue> values = Lists.newArrayList();
+
+ for (RowData row : bufferedRows) {
+ if (!row.isNullAt(variantFieldIndex)) {
+ BinaryVariant flinkVariant = (BinaryVariant) row.getVariant(variantFieldIndex);
Review Comment:
Thanks for pointing out. I will add one .
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3279106339
##########
parquet/src/main/java/org/apache/iceberg/parquet/ParquetFormatModel.java:
##########
@@ -67,15 +67,36 @@ public static <D, S> ParquetFormatModel<D, S, ParquetValueReader<?>> create(
type, schemaType, writerFunction, readerFunction, false, null, null);
}
+ /**
+ * @deprecated Will be removed in 1.12.0; use {@link #create(Class, Class, WriterFunction,
Review Comment:
Thanks for pointing it out. Adjusted it now.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3248525595
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkVariantShreddingAnalyzer.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.flink.data;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.List;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.types.variant.BinaryVariant;
+import org.apache.iceberg.parquet.VariantShreddingAnalyzer;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.VariantMetadata;
+import org.apache.iceberg.variants.VariantValue;
+
+/**
+ * Analyzes Variant fields in Flink {@link RowData} and converts Flink's binary Variant
+ * representation to Iceberg {@link VariantValue} instances for Variant shredding.
+ */
+public class FlinkVariantShreddingAnalyzer extends VariantShreddingAnalyzer<RowData, RowType> {
+
+ @Override
+ protected List<VariantValue> extractVariantValues(
+ List<RowData> bufferedRows, int variantFieldIndex) {
+ List<VariantValue> values = Lists.newArrayList();
+
+ for (RowData row : bufferedRows) {
+ if (!row.isNullAt(variantFieldIndex)) {
+ // Flink currently has only BinaryVariant as its Variant implementation, so this analyzer
+ // intentionally narrows RowData#getVariant's return value to BinaryVariant here.
+ BinaryVariant flinkVariant = (BinaryVariant) row.getVariant(variantFieldIndex);
+ if (flinkVariant != null) {
+ VariantValue variantValue =
+ VariantValue.from(
+ VariantMetadata.from(
+ ByteBuffer.wrap(flinkVariant.getMetadata()).order(ByteOrder.LITTLE_ENDIAN)),
+ ByteBuffer.wrap(flinkVariant.getValue()).order(ByteOrder.LITTLE_ENDIAN));
+
+ values.add(variantValue);
+ }
Review Comment:
I would prefer to have a check and a meaningful exception
```suggestion
Variant flinkVariant = rowData.getVariant(i);
if (flinkVariant != null) {
if (flinkVariant instanceof BinaryVariant binaryVariant) {
VariantValue variantValue =
VariantValue.from(
VariantMetadata.from(
ByteBuffer.wrap(binaryVariant.getMetadata()).order(ByteOrder.LITTLE_ENDIAN)),
ByteBuffer.wrap(binaryVariant.getValue()).order(ByteOrder.LITTLE_ENDIAN));
values.add(variantValue);
} else {
throw new UnsupportedOperationException("Not a supported type: " + flinkVariant.getClass());
}
}
```
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#issuecomment-4395514113
Hi @aihuaxu @nssalian @pvary @mxm . Since the Spark part has been merged, the Flink part has been adjusted accordingly. If you have time, please help review it.
Thanks!
GuoYu.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3212596279
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkFormatModels.java:
##########
@@ -33,7 +34,9 @@ public static void register() {
RowType.class,
FlinkParquetWriters::buildWriter,
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
- FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant)));
+ FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
+ new FlinkVariantShreddingAnalyzer(),
+ (row, rowType) -> new RowDataSerializer(rowType).copy(row)));
Review Comment:
Can we reuse the RowDataSerializer?
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "talatuyarer (via GitHub)" <gi...@apache.org>.
talatuyarer commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3212609700
##########
parquet/src/main/java/org/apache/iceberg/parquet/ParquetFormatModel.java:
##########
Review Comment:
You can built once per write
```suggestion
UnaryOperator<D> copier = copierFactory.apply(engineSchema);
return new BufferedFileAppender<>(
```
##########
parquet/src/main/java/org/apache/iceberg/parquet/ParquetFormatModel.java:
##########
@@ -283,7 +283,7 @@ private FileAppender<D> buildShreddedAppender() {
throw new UncheckedIOException("Failed to create shredded variant writer", e);
}
},
- copyFunc);
+ datum -> copyFunc.apply(datum, engineSchema));
Review Comment:
```suggestion
copier);
```
##########
parquet/src/main/java/org/apache/iceberg/parquet/ParquetFormatModel.java:
##########
@@ -51,7 +51,7 @@ public class ParquetFormatModel<D, S, R>
extends BaseFormatModel<D, S, ParquetValueWriter<?>, R, MessageType> {
private final boolean isBatchReader;
private final VariantShreddingAnalyzer<D, S> variantAnalyzer;
- private final UnaryOperator<D> copyFunc;
+ private final BiFunction<D, S, D> copyFunc;
Review Comment:
BiFunction<D, S, D> works, but I think a factory better matches.
```suggestion
private final Function<S, UnaryOperator<D>> copierFactory;
```
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkFormatModels.java:
##########
@@ -33,7 +34,9 @@ public static void register() {
RowType.class,
FlinkParquetWriters::buildWriter,
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
- FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant)));
+ FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
+ new FlinkVariantShreddingAnalyzer(),
+ (row, rowType) -> new RowDataSerializer(rowType).copy(row)));
Review Comment:
With factory Pattern we call one serializer per write, not per row
```suggestion
rowType -> {
RowDataSerializer serializer = new RowDataSerializer(rowType);
return serializer::copy;
} );
```
##########
spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/source/SparkFormatModels.java:
##########
@@ -53,7 +53,7 @@ public static void register() {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new SparkVariantShreddingAnalyzer(),
- InternalRow::copy));
+ (internalRow, structType) -> internalRow.copy()));
Review Comment:
And also we dont need to change this if we use FactoryPattern
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkFormatModels.java:
##########
@@ -33,7 +34,9 @@ public static void register() {
RowType.class,
FlinkParquetWriters::buildWriter,
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
- FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant)));
+ FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
+ new FlinkVariantShreddingAnalyzer(),
+ (row, rowType) -> new RowDataSerializer(rowType).copy(row)));
Review Comment:
With the current BiFunction, `(row, rowType) -> new RowDataSerializer(rowType).copy(row)` creates a new `RowDataSerializer` for every buffered row (default buffer = 100). This construction is not free, as it involves walking `rowType.getChildren()`, building a `TypeSerializer[]` via `InternalSerializers.create`, a `BinaryRowDataSerializer`, and a `RowData.FieldGetter[]`. Since the engine schema is fixed for the entire file, a factory allows us to build it once and reuse it. Using the Factory Pattern, we can avoid recreating the serializer for a given table schema with every incoming record.
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,844 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(7);
+ }
+
+ @TestTemplate
+ public void testColumnIndexTruncateLength() throws IOException {
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ int customTruncateLength = 10;
+ sql(
+ "ALTER TABLE %s SET ('%s'='%d')",
+ TABLE_NAME, "parquet.columnindex.truncate.length", customTruncateLength);
+
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ String longValue = "A".repeat(20);
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"description\": \"%s\", \"id\": %d}'))", i, longValue, i));
+ }
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ GroupType description =
+ field(
+ "description",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType id =
+ field(
+ "id",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(description, id));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(10);
+ }
+
+ @TestTemplate
+ public void testIntegerFamilyPromotion() throws IOException {
+
+ // Mix of INT8, INT16, INT32, INT64 - should promote to INT64
+ String values =
+ "(1, parse_json('{\"value\": 10}')),"
+ + " (2, parse_json('{\"value\": 1000}')),"
+ + " (3, parse_json('{\"value\": 100000}')),"
+ + " (4, parse_json('{\"value\": 10000000000}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT64, LogicalTypeAnnotation.intType(64, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalFamilyPromotion() throws IOException {
+
+ // Test that they get promoted to the most capable decimal type observed
+ String values =
+ "(1, parse_json('{\"value\": 1.5}')),"
+ + " (2, parse_json('{\"value\": 123.456789}')),"
+ + " (3, parse_json('{\"value\": 123456789123456.789}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY,
+ 16,
+ LogicalTypeAnnotation.decimalType(6, 21)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDataRoundTripWithShredding() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify that we can read the data back correctly
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.age' RETURNING int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(3);
+ assertThat(rows.get(0).getField(0)).isEqualTo(1);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isEqualTo(30);
+ assertThat(rows.get(1).getField(0)).isEqualTo(2);
+ assertThat(rows.get(1).getField(1)).isEqualTo("Bob");
+ assertThat(rows.get(1).getField(2)).isEqualTo(25);
+ assertThat(rows.get(2).getField(0)).isEqualTo(3);
+ assertThat(rows.get(2).getField(1)).isEqualTo("Charlie");
+ assertThat(rows.get(2).getField(2)).isEqualTo(35);
+ }
+
+ @TestTemplate
+ public void testVariantWithNullValues() throws IOException {
+
+ String values =
+ "(1, parse_json('null'))," + " (2, parse_json('null'))," + " (3, parse_json('null'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayOfNullElementsWithShredding() throws IOException {
+
+ sql(
+ "INSERT INTO %s VALUES (1, parse_json('[null, null, null]')), "
+ + "(2, parse_json('[null]'))",
+ TABLE_NAME);
+
+ // Array elements are all null, element type is null, falls back to unshredded
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testInfrequentFieldPruning() throws IOException {
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='11')", TABLE_NAME);
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 11; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ if (i == 1) {
+ // Only the first row has rare_field
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"name\": \"User%d\", \"rare_field\": \"rare\"}'))", i, i));
+ } else {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"name\": \"User%d\"}'))", i, i));
+ }
+ }
+
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ // rare_field appears in 1/11 rows, should be pruned
+ // name appears in 11/11 rows and should be kept
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testMixedTypeTieBreaking() throws IOException {
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ if (i <= 5) {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"val\": %d}'))", i, i));
+ } else {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"val\": \"text%d\"}'))", i, i));
+ }
+ }
+
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ // 5 ints + 5 strings is a tie so STRING wins (higher TIE_BREAK_PRIORITY)
+ GroupType val =
+ field(
+ "val",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(val));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testFieldOnlyAfterBuffer() throws IOException {
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "1");
+
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ sql(
+ "CREATE TEMPORARY VIEW tmp_source AS "
+ + "SELECT * FROM (VALUES "
+ + "(1, parse_json('{\"name\": \"Alice\"}')), "
+ + "(2, parse_json('{\"name\": \"Bob\"}')), "
+ + "(3, parse_json('{\"name\": \"Charlie\"}')), "
+ + "(4, parse_json('{\"name\": \"David\", \"score\": 95}')), "
+ + "(5, parse_json('{\"name\": \"Eve\", \"score\": 88}')), "
+ + "(6, parse_json('{\"name\": \"Frank\", \"score\": 72}')), "
+ + "(7, parse_json('{\"name\": \"Grace\", \"score\": 91}'))"
+ + ") AS t(id, address)");
+
+ sql("INSERT INTO %s SELECT id, address FROM tmp_source ORDER BY id", TABLE_NAME);
+
+ // Schema is determined from buffer (rows 1-3) which only has "name".
+ // "score" is not shredded
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify all data round-trips despite "score" not being shredded
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.score' returning int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(7);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isNull();
+ assertThat(rows.get(3).getField(1)).isEqualTo("David");
+ assertThat(rows.get(3).getField(2)).isEqualTo(95);
+ assertThat(rows.get(6).getField(1)).isEqualTo("Grace");
+ assertThat(rows.get(6).getField(2)).isEqualTo(91);
+
+ getTableEnv()
Review Comment:
nit: This mutates table.exec.resource.default-parallelism to "1" and resets to "4" only at the end. If any assertion throws, the reset never runs and downstream tests inherit the override. I believe you can use @AfterEach.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3214370731
##########
parquet/src/main/java/org/apache/iceberg/parquet/ParquetFormatModel.java:
##########
@@ -51,7 +51,7 @@ public class ParquetFormatModel<D, S, R>
extends BaseFormatModel<D, S, ParquetValueWriter<?>, R, MessageType> {
private final boolean isBatchReader;
private final VariantShreddingAnalyzer<D, S> variantAnalyzer;
- private final UnaryOperator<D> copyFunc;
+ private final Function<S, UnaryOperator<D>> copyFuncFactory;
Review Comment:
The previous version used `BiFunction`, which caused `RowDataSerializer` to be repeatedly created. To avoid this, we use `Function<S, UnaryOperator<D>>` instead, so that `RowDataSerializer` can be initialized once before `BufferedFileAppender` is created.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#issuecomment-4484076190
Hi @pvary @talatuyarer @nssalian , I'd be grateful if you could take a look when you have time.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "nssalian (via GitHub)" <gi...@apache.org>.
nssalian commented on PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#issuecomment-4413698131
Thanks for cleaning up this PR @Guosmilesmile. Do we need to back port this to additional Flink versions as well? CC: @pvary @stevenzwu
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3214345137
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,851 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','parquet-variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @Override
+ @AfterEach
+ public void clean() {
+ super.clean();
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "4");
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(7);
+ }
+
+ @TestTemplate
+ public void testColumnIndexTruncateLength() throws IOException {
Review Comment:
Good point. Since this test only verifies round-trip behavior with large string values and does not assert the column index directly, I’ll rename it to testRoundTripWithLargeStringValues.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3214373916
##########
parquet/src/test/java/org/apache/iceberg/parquet/TestParquetDataWriter.java:
##########
@@ -370,7 +370,7 @@ protected int resolveColumnIndex(Void engineSchema, String columnName) {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
GenericParquetReaders.buildReader(icebergSchema, fileSchema),
testAnalyzer,
- record -> record);
+ unused -> oriRecord -> oriRecord);
Review Comment:
We change `UnaryOperator<D> `to `Function<S, UnaryOperator<D>>`
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#issuecomment-4487830106
I don't have more comments.
Anybody else?
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3209196550
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkFormatModels.java:
##########
@@ -33,7 +34,9 @@ public static void register() {
RowType.class,
FlinkParquetWriters::buildWriter,
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
- FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant)));
+ FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
+ new FlinkVariantShreddingAnalyzer(),
+ (row, rowType) -> new RowDataSerializer(rowType).copy(row)));
Review Comment:
Isn't this costly to recreate every time when we copy a row?
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary merged PR #15596:
URL: https://github.com/apache/iceberg/pull/15596
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#issuecomment-4509523890
@nssalian: Any more comments?
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3282431732
##########
docs/docs/flink-configuration.md:
##########
@@ -160,6 +160,8 @@ INSERT INTO tableName /*+ OPTIONS('upsert-enabled'='true') */
| compression-strategy | Table write.orc.compression-strategy | Overrides this table's compression strategy for ORC tables for this write |
| write-parallelism | Upstream operator parallelism | Overrides the writer parallelism |
| uid-suffix | As per table property | Overrides the uid suffix used in the underlying IcebergSink for this table |
+| shred-variants | Table write.parquet.shred-variants | Overrides this table's shred variants for this write |
+| variant-inference-buffer-size | Table write.parquet.variant-inference-buffer-size | Overrides this table's variant inference buffer size this write |
Review Comment:
Thanks . Add it now.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "talatuyarer (via GitHub)" <gi...@apache.org>.
talatuyarer commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3213332825
##########
spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/SparkFormatModels.java:
##########
@@ -53,7 +53,7 @@ public static void register() {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new SparkVariantShreddingAnalyzer(),
- InternalRow::copy));
+ structType -> InternalRow::copy));
Review Comment:
We dont need this change anymore. Can you revert it ?
##########
spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/source/SparkFormatModels.java:
##########
@@ -53,7 +53,7 @@ public static void register() {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new SparkVariantShreddingAnalyzer(),
- InternalRow::copy));
+ structType -> InternalRow::copy));
Review Comment:
We dont need this change anymore. Can you revert it ?
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3214338027
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkVariantShreddingAnalyzer.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.flink.data;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.List;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.types.variant.BinaryVariant;
+import org.apache.iceberg.parquet.VariantShreddingAnalyzer;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.VariantMetadata;
+import org.apache.iceberg.variants.VariantValue;
+
+public class FlinkVariantShreddingAnalyzer extends VariantShreddingAnalyzer<RowData, RowType> {
Review Comment:
Yes, I have add it .
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#issuecomment-4414539302
> Thanks for cleaning up this PR @Guosmilesmile. Do we need to back port this to additional Flink versions as well? CC: @pvary @stevenzwu
@nssalian Flink support variant type in 2.1 . So we only add this feature in version of 2.1 .
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3214388917
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,851 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','parquet-variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @Override
+ @AfterEach
+ public void clean() {
+ super.clean();
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "4");
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(7);
+ }
+
+ @TestTemplate
+ public void testColumnIndexTruncateLength() throws IOException {
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ int customTruncateLength = 10;
+ sql(
+ "ALTER TABLE %s SET ('%s'='%d')",
+ TABLE_NAME, "parquet.columnindex.truncate.length", customTruncateLength);
+
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ String longValue = "A".repeat(20);
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"description\": \"%s\", \"id\": %d}'))", i, longValue, i));
+ }
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ GroupType description =
+ field(
+ "description",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType id =
+ field(
+ "id",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(description, id));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(10);
+ }
+
+ @TestTemplate
+ public void testIntegerFamilyPromotion() throws IOException {
+
+ // Mix of INT8, INT16, INT32, INT64 - should promote to INT64
+ String values =
+ "(1, parse_json('{\"value\": 10}')),"
+ + " (2, parse_json('{\"value\": 1000}')),"
+ + " (3, parse_json('{\"value\": 100000}')),"
+ + " (4, parse_json('{\"value\": 10000000000}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT64, LogicalTypeAnnotation.intType(64, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalFamilyPromotion() throws IOException {
+
+ // Test that they get promoted to the most capable decimal type observed
+ String values =
+ "(1, parse_json('{\"value\": 1.5}')),"
+ + " (2, parse_json('{\"value\": 123.456789}')),"
+ + " (3, parse_json('{\"value\": 123456789123456.789}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY,
+ 16,
+ LogicalTypeAnnotation.decimalType(6, 21)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDataRoundTripWithShredding() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify that we can read the data back correctly
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.age' RETURNING int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(3);
+ assertThat(rows.get(0).getField(0)).isEqualTo(1);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isEqualTo(30);
+ assertThat(rows.get(1).getField(0)).isEqualTo(2);
+ assertThat(rows.get(1).getField(1)).isEqualTo("Bob");
+ assertThat(rows.get(1).getField(2)).isEqualTo(25);
+ assertThat(rows.get(2).getField(0)).isEqualTo(3);
+ assertThat(rows.get(2).getField(1)).isEqualTo("Charlie");
+ assertThat(rows.get(2).getField(2)).isEqualTo(35);
+ }
+
+ @TestTemplate
+ public void testVariantWithNullValues() throws IOException {
+
+ String values =
+ "(1, parse_json('null'))," + " (2, parse_json('null'))," + " (3, parse_json('null'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayOfNullElementsWithShredding() throws IOException {
+
+ sql(
+ "INSERT INTO %s VALUES (1, parse_json('[null, null, null]')), "
+ + "(2, parse_json('[null]'))",
+ TABLE_NAME);
+
+ // Array elements are all null, element type is null, falls back to unshredded
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testInfrequentFieldPruning() throws IOException {
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='11')", TABLE_NAME);
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 11; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ if (i == 1) {
+ // Only the first row has rare_field
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"name\": \"User%d\", \"rare_field\": \"rare\"}'))", i, i));
+ } else {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"name\": \"User%d\"}'))", i, i));
+ }
+ }
+
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ // rare_field appears in 1/11 rows, should be pruned
+ // name appears in 11/11 rows and should be kept
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testMixedTypeTieBreaking() throws IOException {
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ if (i <= 5) {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"val\": %d}'))", i, i));
+ } else {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"val\": \"text%d\"}'))", i, i));
+ }
+ }
+
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ // 5 ints + 5 strings is a tie so STRING wins (higher TIE_BREAK_PRIORITY)
+ GroupType val =
+ field(
+ "val",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(val));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testFieldOnlyAfterBuffer() throws IOException {
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "1");
+
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ sql(
+ "CREATE TEMPORARY VIEW tmp_source AS "
+ + "SELECT * FROM (VALUES "
+ + "(1, parse_json('{\"name\": \"Alice\"}')), "
+ + "(2, parse_json('{\"name\": \"Bob\"}')), "
+ + "(3, parse_json('{\"name\": \"Charlie\"}')), "
+ + "(4, parse_json('{\"name\": \"David\", \"score\": 95}')), "
+ + "(5, parse_json('{\"name\": \"Eve\", \"score\": 88}')), "
+ + "(6, parse_json('{\"name\": \"Frank\", \"score\": 72}')), "
+ + "(7, parse_json('{\"name\": \"Grace\", \"score\": 91}'))"
+ + ") AS t(id, address)");
+
+ sql("INSERT INTO %s SELECT id, address FROM tmp_source ORDER BY id", TABLE_NAME);
+
+ // Schema is determined from buffer (rows 1-3) which only has "name".
+ // "score" is not shredded
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify all data round-trips despite "score" not being shredded
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.score' returning int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(7);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isNull();
+ assertThat(rows.get(3).getField(1)).isEqualTo("David");
+ assertThat(rows.get(3).getField(2)).isEqualTo(95);
+ assertThat(rows.get(6).getField(1)).isEqualTo("Grace");
+ assertThat(rows.get(6).getField(2)).isEqualTo(91);
+
+ sql("DROP TEMPORARY VIEW IF EXISTS tmp_source");
+ }
+
+ @TestTemplate
+ public void testCrossFileDifferentShreddedType() throws IOException {
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ // File 1: "score" is always integer → shredded as INT8
+ String batch1 =
+ "(1, parse_json('{\"score\": 95}')),"
+ + " (2, parse_json('{\"score\": 88}')),"
+ + " (3, parse_json('{\"score\": 72}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, batch1);
+
+ // Verify file 1 schema: score shredded as INT8
+ GroupType scoreInt =
+ field(
+ "score",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema1 =
+ parquetSchema(variant("address", 2, Type.Repetition.REQUIRED, objectFields(scoreInt)));
+ verifyParquetSchema(icebergTable, expectedSchema1);
+
+ // File 2: "score" is always string → shredded as STRING
+ String batch2 =
+ "(4, parse_json('{\"score\": \"high\"}')),"
+ + " (5, parse_json('{\"score\": \"medium\"}')),"
+ + " (6, parse_json('{\"score\": \"low\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, batch2);
+
+ // Query across both files, reader must handle different shredded types
+ List<Row> rows =
+ sql("SELECT id, json_value(address, '$.score') FROM %s ORDER BY id", TABLE_NAME);
+ assertThat(rows).hasSize(6);
+ assertThat(rows.get(0).getField(1)).isEqualTo("95");
+ assertThat(rows.get(1).getField(1)).isEqualTo("88");
+ assertThat(rows.get(3).getField(1)).isEqualTo("high");
+ assertThat(rows.get(5).getField(1)).isEqualTo("low");
+ }
+
+ @TestTemplate
+ public void testAllNullVariantColumn() throws IOException {
+
+ String variantNullAbleTableName = "test_all_null_variant_column";
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant) with ('write.format.default'='%s','format-version'='3','shred-variants'='true','parquet-variant-inference-buffer-size'='10')",
+ variantNullAbleTableName, FileFormat.PARQUET.name());
+
+ sql(
+ "INSERT INTO %s VALUES (1, CAST(null AS VARIANT)), (2, CAST(null AS VARIANT)), (3, CAST(null AS VARIANT))",
+ variantNullAbleTableName);
+
+ // All variant values are SQL NULL, so no shredding should occur
+ MessageType expectedSchema = parquetSchema(variant("address", 2, Type.Repetition.OPTIONAL));
+ Table variantNullAbleTable =
+ validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, variantNullAbleTableName));
+ verifyParquetSchema(variantNullAbleTable, expectedSchema);
+
+ List<Row> rows = sql("SELECT id, address FROM %s ORDER BY id", variantNullAbleTableName);
+ assertThat(rows).hasSize(3);
+ assertThat(rows.get(0).getField(1)).isNull();
+ assertThat(rows.get(1).getField(1)).isNull();
+ assertThat(rows.get(2).getField(1)).isNull();
+ }
+
+ @TestTemplate
+ public void testBufferSizeOne() throws IOException {
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='1')", TABLE_NAME);
+
+ sql(
+ "INSERT INTO %s VALUES "
+ + "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))",
+ TABLE_NAME);
+
+ // Schema inferred from first row only, should still shred name and age
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ List<Row> rows =
+ sql("SELECT id, json_value(address, '$.name') FROM %s ORDER BY id", TABLE_NAME);
+ assertThat(rows).hasSize(3);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(2).getField(1)).isEqualTo("Charlie");
+ }
+
+ @TestTemplate
+ public void testDecimalFallbackAfterBuffer() {
Review Comment:
Good point. I will add a asset to verify the schema.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3212535951
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkFormatModels.java:
##########
@@ -33,7 +34,9 @@ public static void register() {
RowType.class,
FlinkParquetWriters::buildWriter,
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
- FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant)));
+ FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
+ new FlinkVariantShreddingAnalyzer(),
+ (row, rowType) -> new RowDataSerializer(rowType).copy(row)));
Review Comment:
It will increase the cost, but without copying, there would be issues with data corruption when buffer data. We ran into this during early development, and the unit tests can reproduce it.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3249006802
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,874 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @Override
+ @AfterEach
+ public void clean() {
+ super.clean();
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "4");
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
Review Comment:
Ok,change it now.Thanks for pointing it out.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3248566237
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteOptions.java:
##########
@@ -105,4 +105,10 @@ private FlinkWriteOptions() {}
// specify the uidSuffix to be used for the underlying IcebergSink
public static final ConfigOption<String> UID_SUFFIX =
ConfigOptions.key("uid-suffix").stringType().defaultValue("");
+
+ public static final ConfigOption<Boolean> PARQUET_SHRED_VARIANTS =
+ ConfigOptions.key("parquet-shred-variants").booleanType().defaultValue(false);
+
+ public static final ConfigOption<Integer> VARIANT_INFERENCE_BUFFER_SIZE =
Review Comment:
parquet-variant-inference-buffer-size
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "talatuyarer (via GitHub)" <gi...@apache.org>.
talatuyarer commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3245187310
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkVariantShreddingAnalyzer.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.flink.data;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.List;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.types.variant.BinaryVariant;
+import org.apache.iceberg.parquet.VariantShreddingAnalyzer;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.VariantMetadata;
+import org.apache.iceberg.variants.VariantValue;
+
+/**
+ * Analyzes Variant fields in Flink {@link RowData} and converts Flink's binary Variant
+ * representation to Iceberg {@link VariantValue} instances for Variant shredding.
+ */
+public class FlinkVariantShreddingAnalyzer extends VariantShreddingAnalyzer<RowData, RowType> {
+
+ @Override
+ protected List<VariantValue> extractVariantValues(
+ List<RowData> bufferedRows, int variantFieldIndex) {
+ List<VariantValue> values = Lists.newArrayList();
+
+ for (RowData row : bufferedRows) {
+ if (!row.isNullAt(variantFieldIndex)) {
+ // Flink currently has only BinaryVariant as its Variant implementation, so this analyzer
+ // intentionally narrows RowData#getVariant's return value to BinaryVariant here.
+ BinaryVariant flinkVariant = (BinaryVariant) row.getVariant(variantFieldIndex);
+ if (flinkVariant != null) {
+ VariantValue variantValue =
+ VariantValue.from(
+ VariantMetadata.from(
+ ByteBuffer.wrap(flinkVariant.getMetadata()).order(ByteOrder.LITTLE_ENDIAN)),
+ ByteBuffer.wrap(flinkVariant.getValue()).order(ByteOrder.LITTLE_ENDIAN));
+
+ values.add(variantValue);
+ }
+ }
+ }
+
+ return values;
+ }
+
+ @Override
+ protected int resolveColumnIndex(RowType flinkSchema, String columnName) {
+ try {
Review Comment:
This is redundant try catch. The catch is unreachable; the whole method should just be return
flinkSchema.getFieldIndex(columnName); I checked RowType code it does not throw IllegalArgumentException. It returns -1 https://github.com/apache/flink/blob/release-2.1/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/RowType.java#L187
##########
docs/docs/flink-configuration.md:
##########
@@ -160,6 +160,8 @@ INSERT INTO tableName /*+ OPTIONS('upsert-enabled'='true') */
| compression-strategy | Table write.orc.compression-strategy | Overrides this table's compression strategy for ORC tables for this write |
| write-parallelism | Upstream operator parallelism | Overrides the writer parallelism |
| uid-suffix | As per table property | Overrides the uid suffix used in the underlying IcebergSink for this table |
+| parquet-shred-variants | Table write.parquet.shred-variants | Overrides this table's shred variants for this write
Review Comment:
Both new rows are missing the trailing |
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,874 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @Override
+ @AfterEach
+ public void clean() {
+ super.clean();
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "4");
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(7);
+ }
+
+ @TestTemplate
+ public void testColumnIndexTruncateLength() throws IOException {
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ int customTruncateLength = 10;
+ sql(
+ "ALTER TABLE %s SET ('%s'='%d')",
+ TABLE_NAME, "parquet.columnindex.truncate.length", customTruncateLength);
+
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ String longValue = "A".repeat(20);
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"description\": \"%s\", \"id\": %d}'))", i, longValue, i));
+ }
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ GroupType description =
+ field(
+ "description",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType id =
+ field(
+ "id",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(description, id));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(10);
+ }
+
+ @TestTemplate
+ public void testIntegerFamilyPromotion() throws IOException {
+
+ // Mix of INT8, INT16, INT32, INT64 - should promote to INT64
+ String values =
+ "(1, parse_json('{\"value\": 10}')),"
+ + " (2, parse_json('{\"value\": 1000}')),"
+ + " (3, parse_json('{\"value\": 100000}')),"
+ + " (4, parse_json('{\"value\": 10000000000}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT64, LogicalTypeAnnotation.intType(64, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalFamilyPromotion() throws IOException {
+
+ // Test that they get promoted to the most capable decimal type observed
+ String values =
+ "(1, parse_json('{\"value\": 1.5}')),"
+ + " (2, parse_json('{\"value\": 123.456789}')),"
+ + " (3, parse_json('{\"value\": 123456789123456.789}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType value =
+ field(
+ "value",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY,
+ 16,
+ LogicalTypeAnnotation.decimalType(6, 21)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(value));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDataRoundTripWithShredding() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify that we can read the data back correctly
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.age' RETURNING int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(3);
+ assertThat(rows.get(0).getField(0)).isEqualTo(1);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isEqualTo(30);
+ assertThat(rows.get(1).getField(0)).isEqualTo(2);
+ assertThat(rows.get(1).getField(1)).isEqualTo("Bob");
+ assertThat(rows.get(1).getField(2)).isEqualTo(25);
+ assertThat(rows.get(2).getField(0)).isEqualTo(3);
+ assertThat(rows.get(2).getField(1)).isEqualTo("Charlie");
+ assertThat(rows.get(2).getField(2)).isEqualTo(35);
+ }
+
+ @TestTemplate
+ public void testVariantWithNullValues() throws IOException {
+
+ String values =
+ "(1, parse_json('null'))," + " (2, parse_json('null'))," + " (3, parse_json('null'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayOfNullElementsWithShredding() throws IOException {
+
+ sql(
+ "INSERT INTO %s VALUES (1, parse_json('[null, null, null]')), "
+ + "(2, parse_json('[null]'))",
+ TABLE_NAME);
+
+ // Array elements are all null, element type is null, falls back to unshredded
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testInfrequentFieldPruning() throws IOException {
+ // This test relies on the current VariantShreddingAnalyzer MIN_FIELD_FREQUENCY threshold of
+ // 0.10: rare_field appears in 1/11 rows (~0.09), so it should be pruned.
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='11')", TABLE_NAME);
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 11; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ if (i == 1) {
+ // Only the first row has rare_field
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"name\": \"User%d\", \"rare_field\": \"rare\"}'))", i, i));
+ } else {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"name\": \"User%d\"}'))", i, i));
+ }
+ }
+
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ // rare_field appears in 1/11 rows, should be pruned
+ // name appears in 11/11 rows and should be kept
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testMixedTypeTieBreaking() throws IOException {
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ if (i <= 5) {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"val\": %d}'))", i, i));
+ } else {
+ valuesBuilder.append(String.format("(%d, parse_json('{\"val\": \"text%d\"}'))", i, i));
+ }
+ }
+
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ // 5 ints + 5 strings is a tie so STRING wins (higher TIE_BREAK_PRIORITY)
+ GroupType val =
+ field(
+ "val",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(val));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testFieldOnlyAfterBuffer() throws IOException {
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "1");
+
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ sql(
+ "CREATE TEMPORARY VIEW tmp_source AS "
+ + "SELECT * FROM (VALUES "
+ + "(1, parse_json('{\"name\": \"Alice\"}')), "
+ + "(2, parse_json('{\"name\": \"Bob\"}')), "
+ + "(3, parse_json('{\"name\": \"Charlie\"}')), "
+ + "(4, parse_json('{\"name\": \"David\", \"score\": 95}')), "
+ + "(5, parse_json('{\"name\": \"Eve\", \"score\": 88}')), "
+ + "(6, parse_json('{\"name\": \"Frank\", \"score\": 72}')), "
+ + "(7, parse_json('{\"name\": \"Grace\", \"score\": 91}'))"
+ + ") AS t(id, address)");
+
+ sql("INSERT INTO %s SELECT id, address FROM tmp_source ORDER BY id", TABLE_NAME);
+
+ // Schema is determined from buffer (rows 1-3) which only has "name".
+ // "score" is not shredded
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+
+ // Verify all data round-trips despite "score" not being shredded
+ List<Row> rows =
+ sql(
+ "SELECT id, JSON_VALUE(address, '$.name'),"
+ + " JSON_VALUE(address, '$.score' returning int)"
+ + " FROM %s ORDER BY id",
+ TABLE_NAME);
+ assertThat(rows).hasSize(7);
+ assertThat(rows.get(0).getField(1)).isEqualTo("Alice");
+ assertThat(rows.get(0).getField(2)).isNull();
+ assertThat(rows.get(3).getField(1)).isEqualTo("David");
+ assertThat(rows.get(3).getField(2)).isEqualTo(95);
+ assertThat(rows.get(6).getField(1)).isEqualTo("Grace");
+ assertThat(rows.get(6).getField(2)).isEqualTo(91);
+
+ sql("DROP TEMPORARY VIEW IF EXISTS tmp_source");
+ }
+
+ @TestTemplate
+ public void testCrossFileDifferentShreddedType() throws IOException {
+ sql("ALTER TABLE %s SET('variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ // File 1: "score" is always integer → shredded as INT8
+ String batch1 =
+ "(1, parse_json('{\"score\": 95}')),"
+ + " (2, parse_json('{\"score\": 88}')),"
+ + " (3, parse_json('{\"score\": 72}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, batch1);
+
+ // Verify file 1 schema: score shredded as INT8
+ GroupType scoreInt =
+ field(
+ "score",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema1 =
+ parquetSchema(variant("address", 2, Type.Repetition.REQUIRED, objectFields(scoreInt)));
+ verifyParquetSchema(icebergTable, expectedSchema1);
+
+ // File 2: "score" is always string → shredded as STRING
+ String batch2 =
+ "(4, parse_json('{\"score\": \"high\"}')),"
+ + " (5, parse_json('{\"score\": \"medium\"}')),"
+ + " (6, parse_json('{\"score\": \"low\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, batch2);
+
+ // Query across both files, reader must handle different shredded types
+ List<Row> rows =
+ sql("SELECT id, json_value(address, '$.score') FROM %s ORDER BY id", TABLE_NAME);
+ assertThat(rows).hasSize(6);
+ assertThat(rows.get(0).getField(1)).isEqualTo("95");
+ assertThat(rows.get(1).getField(1)).isEqualTo("88");
+ assertThat(rows.get(3).getField(1)).isEqualTo("high");
+ assertThat(rows.get(5).getField(1)).isEqualTo("low");
+ }
+
+ @TestTemplate
+ public void testAllNullVariantColumn() throws IOException {
+
+ String variantNullAbleTableName = "test_all_null_variant_column";
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant) with ('write.format.default'='%s','format-version'='3','shred-variants'='true','variant-inference-buffer-size'='10')",
Review Comment:
This test uses wrong settings `shred-variants` and shredding is never enabled. The test then asserts an unshredded schema and passes for the wrong reason.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3209088968
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteConf.java:
##########
@@ -262,4 +262,22 @@ public Duration tableRefreshInterval() {
.flinkConfig(FlinkWriteOptions.TABLE_REFRESH_INTERVAL)
.parseOptional();
}
+
+ public boolean shredVariants() {
+ return confParser
+ .booleanConf()
+ .option(FlinkWriteOptions.SHRED_VARIANTS.key())
+ .tableProperty(TableProperties.PARQUET_SHRED_VARIANTS)
+ .defaultValue(TableProperties.PARQUET_SHRED_VARIANTS_DEFAULT)
Review Comment:
How will we handle when ORC supports shredding variants?
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#issuecomment-4496155538
After discussing with @pvary , we decided to keep the same create name and go with deprecating the old method instead.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "aihuaxu (via GitHub)" <gi...@apache.org>.
aihuaxu commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3213274601
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteConf.java:
##########
@@ -262,4 +262,22 @@ public Duration tableRefreshInterval() {
.flinkConfig(FlinkWriteOptions.TABLE_REFRESH_INTERVAL)
.parseOptional();
}
+
+ public boolean parquetShredVariants() {
+ return confParser
+ .booleanConf()
+ .option(FlinkWriteOptions.PARQUET_SHRED_VARIANTS.key())
+ .tableProperty(TableProperties.PARQUET_SHRED_VARIANTS)
+ .defaultValue(TableProperties.PARQUET_SHRED_VARIANTS_DEFAULT)
+ .parse();
+ }
+
+ public int variantInferenceBufferSize() {
Review Comment:
Should this be Parquet specific as well?
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkFormatModels.java:
##########
@@ -33,7 +34,9 @@ public static void register() {
RowType.class,
FlinkParquetWriters::buildWriter,
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
- FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant)));
+ FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
+ new FlinkVariantShreddingAnalyzer(),
+ (row, rowType) -> new RowDataSerializer(rowType).copy(row)));
Review Comment:
+1. We should be able to reuse RowDataSerializer so we don't need to create new instance for every row.
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteOptions.java:
##########
@@ -105,4 +105,10 @@ private FlinkWriteOptions() {}
// specify the uidSuffix to be used for the underlying IcebergSink
public static final ConfigOption<String> UID_SUFFIX =
ConfigOptions.key("uid-suffix").stringType().defaultValue("");
+
+ public static final ConfigOption<Boolean> PARQUET_SHRED_VARIANTS =
+ ConfigOptions.key("parquet-shred-variants").booleanType().defaultValue(false);
+
+ public static final ConfigOption<Integer> VARIANT_INFERENCE_BUFFER_SIZE =
+ ConfigOptions.key("variant-inference-buffer-size").intType().defaultValue(10);
Review Comment:
Maybe default to 100 to align with TableProperties.PARQUET_VARIANT_BUFFER_SIZE_DEFAULT value?
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3214376384
##########
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/TestFlinkVariantShreddingType.java:
##########
@@ -0,0 +1,851 @@
+/*
+ * 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.flink;
+
+import static org.apache.parquet.schema.Types.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.types.Row;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.Parameters;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.source.DataIterator;
+import org.apache.iceberg.flink.source.reader.ReaderUtil;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.Variant;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.schema.GroupType;
+import org.apache.parquet.schema.LogicalTypeAnnotation;
+import org.apache.parquet.schema.MessageType;
+import org.apache.parquet.schema.PrimitiveType;
+import org.apache.parquet.schema.Type;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestTemplate;
+
+class TestFlinkVariantShreddingType extends CatalogTestBase {
+
+ private static final String TABLE_NAME = "test_table";
+ private Table icebergTable;
+
+ @Parameters(name = "catalogName={0}, baseNamespace={1}")
+ protected static List<Object[]> parameters() {
+ List<Object[]> parameters = Lists.newArrayList();
+ parameters.add(new Object[] {"testhadoop", Namespace.empty()});
+ parameters.add(new Object[] {"testhadoop_basenamespace", Namespace.of("l0", "l1")});
+ return parameters;
+ }
+
+ @Override
+ @BeforeEach
+ public void before() {
+ super.before();
+ sql("CREATE DATABASE %s", flinkDatabase);
+ sql("USE CATALOG %s", catalogName);
+ sql("USE %s", DATABASE);
+ sql(
+ "CREATE TABLE %s (id int NOT NULL, address variant NOT NULL) with ('write.format.default'='%s','format-version'='3','parquet-shred-variants'='true','parquet-variant-inference-buffer-size'='10')",
+ TABLE_NAME, FileFormat.PARQUET.name());
+ icebergTable = validationCatalog.loadTable(TableIdentifier.of(icebergNamespace, TABLE_NAME));
+ }
+
+ @Override
+ @AfterEach
+ public void clean() {
+ super.clean();
+ getTableEnv()
+ .getConfig()
+ .getConfiguration()
+ .setString("table.exec.resource.default-parallelism", "4");
+ }
+
+ @TestTemplate
+ public void testExcludingNullValue() throws IOException {
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30, \"dummy\": null}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testConsistentType() throws IOException {
+ String values =
+ "(1, parse_json('{\"age\": \"25\"}')),"
+ + " (2, parse_json('{\"age\": 30}')),"
+ + " (3, parse_json('{\"age\": \"35\"}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveType() throws IOException {
+ String values = "(1, parse_json('123')), (2, parse_json('\"abc\"')), (3, parse_json('12'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ MessageType expectedSchema = parquetSchema(address);
+
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testPrimitiveDecimalType() throws IOException {
+ String values =
+ "(1, parse_json('123.56')), (2, parse_json('\"abc\"')), (3, parse_json('12.56'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType address =
+ variant(
+ "address",
+ 2,
+ Type.Repetition.REQUIRED,
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ MessageType expectedSchema = parquetSchema(address);
+ assertThat(SimpleDataUtil.tableRecords(icebergTable)).hasSize(3);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testBooleanType() throws IOException {
+ String values =
+ "(1, parse_json('{\"active\": true}')),"
+ + " (2, parse_json('{\"active\": false}')),"
+ + " (3, parse_json('{\"active\": true}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType active = field("active", shreddedPrimitive(PrimitiveType.PrimitiveTypeName.BOOLEAN));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(active));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithInconsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.456789}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(6, 9)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testDecimalTypeWithConsistentScales() throws IOException {
+ String values =
+ "(1, parse_json('{\"price\": 123.45}')),"
+ + " (2, parse_json('{\"price\": 678.90}')),"
+ + " (3, parse_json('{\"price\": 999.99}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType price =
+ field(
+ "price",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.decimalType(2, 5)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(price));
+ MessageType expectedSchema = parquetSchema(address);
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testArrayType() throws IOException {
+ String values =
+ "(1, parse_json('[\"java\", \"scala\", \"python\"]')),"
+ + " (2, parse_json('[\"rust\", \"go\"]')),"
+ + " (3, parse_json('[\"javascript\"]'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType arr =
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType())));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, arr);
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedArrayType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"tags\": [\"rust\", \"go\"]}')),"
+ + " (3, parse_json('{\"tags\": [\"javascript\"]}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testNestedObjectType() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"location\": {\"city\": \"Seattle\", \"zip\": 98101}, \"tags\": [\"java\", \"scala\", \"python\"]}')),"
+ + " (2, parse_json('{\"location\": {\"city\": \"Portland\", \"zip\": 97201}}')),"
+ + " (3, parse_json('{\"location\": {\"city\": \"NYC\", \"zip\": 10001}}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType city =
+ field(
+ "city",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType zip =
+ field(
+ "zip",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(32, true)));
+ GroupType location = field("location", objectFields(city, zip));
+ GroupType tags =
+ field(
+ "tags",
+ list(
+ element(
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY,
+ LogicalTypeAnnotation.stringType()))));
+
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(location, tags));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ }
+
+ @TestTemplate
+ public void testLazyInitializationWithBufferedRows() throws IOException {
+
+ String values =
+ "(1, parse_json('{\"name\": \"Alice\", \"age\": 30}')),"
+ + " (2, parse_json('{\"name\": \"Bob\", \"age\": 25}')),"
+ + " (3, parse_json('{\"name\": \"Charlie\", \"age\": 35}')),"
+ + " (4, parse_json('{\"name\": \"David\", \"age\": 28}')),"
+ + " (5, parse_json('{\"name\": \"Eve\", \"age\": 32}')),"
+ + " (6, parse_json('{\"name\": \"Frank\", \"age\": 40}')),"
+ + " (7, parse_json('{\"name\": \"Grace\", \"age\": 27}'))";
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, values);
+
+ GroupType name =
+ field(
+ "name",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType age =
+ field(
+ "age",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address = variant("address", 2, Type.Repetition.REQUIRED, objectFields(age, name));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(7);
+ }
+
+ @TestTemplate
+ public void testColumnIndexTruncateLength() throws IOException {
+ sql("ALTER TABLE %s SET('parquet-variant-inference-buffer-size'='3')", TABLE_NAME);
+
+ int customTruncateLength = 10;
+ sql(
+ "ALTER TABLE %s SET ('%s'='%d')",
+ TABLE_NAME, "parquet.columnindex.truncate.length", customTruncateLength);
+
+ StringBuilder valuesBuilder = new StringBuilder();
+ for (int i = 1; i <= 10; i++) {
+ if (i > 1) {
+ valuesBuilder.append(", ");
+ }
+
+ String longValue = "A".repeat(20);
+ valuesBuilder.append(
+ String.format(
+ "(%d, parse_json('{\"description\": \"%s\", \"id\": %d}'))", i, longValue, i));
+ }
+ sql("INSERT INTO %s VALUES %s", TABLE_NAME, valuesBuilder.toString());
+
+ GroupType description =
+ field(
+ "description",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.BINARY, LogicalTypeAnnotation.stringType()));
+ GroupType id =
+ field(
+ "id",
+ shreddedPrimitive(
+ PrimitiveType.PrimitiveTypeName.INT32, LogicalTypeAnnotation.intType(8, true)));
+ GroupType address =
+ variant("address", 2, Type.Repetition.REQUIRED, objectFields(description, id));
+ MessageType expectedSchema = parquetSchema(address);
+
+ verifyParquetSchema(icebergTable, expectedSchema);
+ assertThat(genericRowData()).hasSize(10);
Review Comment:
Change ut name above.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "nssalian (via GitHub)" <gi...@apache.org>.
nssalian commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3282355676
##########
docs/docs/flink-configuration.md:
##########
@@ -160,6 +160,8 @@ INSERT INTO tableName /*+ OPTIONS('upsert-enabled'='true') */
| compression-strategy | Table write.orc.compression-strategy | Overrides this table's compression strategy for ORC tables for this write |
| write-parallelism | Upstream operator parallelism | Overrides the writer parallelism |
| uid-suffix | As per table property | Overrides the uid suffix used in the underlying IcebergSink for this table |
+| shred-variants | Table write.parquet.shred-variants | Overrides this table's shred variants for this write |
+| variant-inference-buffer-size | Table write.parquet.variant-inference-buffer-size | Overrides this table's variant inference buffer size this write |
Review Comment:
```suggestion
| variant-inference-buffer-size | Table write.parquet.variant-inference-buffer-size | Overrides this table's variant inference buffer size for this write |
```
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "nssalian (via GitHub)" <gi...@apache.org>.
nssalian commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3267849536
##########
spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/source/SparkFormatModels.java:
##########
@@ -53,7 +53,7 @@ public static void register() {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new SparkVariantShreddingAnalyzer(),
- InternalRow::copy));
+ structType -> InternalRow::copy));
Review Comment:
@Guosmilesmile could you update the Spark 4.0 with the above suggestion as well
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3246795400
##########
docs/docs/flink-configuration.md:
##########
@@ -160,6 +160,8 @@ INSERT INTO tableName /*+ OPTIONS('upsert-enabled'='true') */
| compression-strategy | Table write.orc.compression-strategy | Overrides this table's compression strategy for ORC tables for this write |
| write-parallelism | Upstream operator parallelism | Overrides the writer parallelism |
| uid-suffix | As per table property | Overrides the uid suffix used in the underlying IcebergSink for this table |
+| parquet-shred-variants | Table write.parquet.shred-variants | Overrides this table's shred variants for this write
Review Comment:
Good!Add it.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3249004704
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkVariantShreddingAnalyzer.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.flink.data;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.List;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.types.variant.BinaryVariant;
+import org.apache.iceberg.parquet.VariantShreddingAnalyzer;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.variants.VariantMetadata;
+import org.apache.iceberg.variants.VariantValue;
+
+/**
+ * Analyzes Variant fields in Flink {@link RowData} and converts Flink's binary Variant
+ * representation to Iceberg {@link VariantValue} instances for Variant shredding.
+ */
+public class FlinkVariantShreddingAnalyzer extends VariantShreddingAnalyzer<RowData, RowType> {
+
+ @Override
+ protected List<VariantValue> extractVariantValues(
+ List<RowData> bufferedRows, int variantFieldIndex) {
+ List<VariantValue> values = Lists.newArrayList();
+
+ for (RowData row : bufferedRows) {
+ if (!row.isNullAt(variantFieldIndex)) {
+ // Flink currently has only BinaryVariant as its Variant implementation, so this analyzer
+ // intentionally narrows RowData#getVariant's return value to BinaryVariant here.
+ BinaryVariant flinkVariant = (BinaryVariant) row.getVariant(variantFieldIndex);
+ if (flinkVariant != null) {
+ VariantValue variantValue =
+ VariantValue.from(
+ VariantMetadata.from(
+ ByteBuffer.wrap(flinkVariant.getMetadata()).order(ByteOrder.LITTLE_ENDIAN)),
+ ByteBuffer.wrap(flinkVariant.getValue()).order(ByteOrder.LITTLE_ENDIAN));
+
+ values.add(variantValue);
+ }
Review Comment:
Good suggest. Change it.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3278868128
##########
parquet/src/main/java/org/apache/iceberg/parquet/ParquetFormatModel.java:
##########
@@ -67,15 +67,36 @@ public static <D, S> ParquetFormatModel<D, S, ParquetValueReader<?>> create(
type, schemaType, writerFunction, readerFunction, false, null, null);
}
+ /**
+ * @deprecated Will be removed in 1.12.0; use {@link #create(Class, Class, WriterFunction,
Review Comment:
Since the 1.11.0 is released now, this change will be in 1.12.0 and we will remove it in 1.13.0.
Please adjust the comment
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3248557341
##########
docs/docs/flink-configuration.md:
##########
@@ -160,6 +160,8 @@ INSERT INTO tableName /*+ OPTIONS('upsert-enabled'='true') */
| compression-strategy | Table write.orc.compression-strategy | Overrides this table's compression strategy for ORC tables for this write |
| write-parallelism | Upstream operator parallelism | Overrides the writer parallelism |
| uid-suffix | As per table property | Overrides the uid suffix used in the underlying IcebergSink for this table |
+| parquet-shred-variants | Table write.parquet.shred-variants | Overrides this table's shred variants for this write |
+| variant-inference-buffer-size | Table write.parquet.variant-inference-buffer-size | Overrides this table's variant inference buffer size this write |
Review Comment:
parquet-variant-inference-buffer-size
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3212662941
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkFormatModels.java:
##########
@@ -33,7 +34,9 @@ public static void register() {
RowType.class,
FlinkParquetWriters::buildWriter,
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
- FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant)));
+ FlinkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
+ new FlinkVariantShreddingAnalyzer(),
+ (row, rowType) -> new RowDataSerializer(rowType).copy(row)));
Review Comment:
Yes, we can use `Function<S, UnaryOperator<D>>` instead of `BiFunction<D, S, D>` to implement this.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3213339198
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteOptions.java:
##########
@@ -105,4 +105,10 @@ private FlinkWriteOptions() {}
// specify the uidSuffix to be used for the underlying IcebergSink
public static final ConfigOption<String> UID_SUFFIX =
ConfigOptions.key("uid-suffix").stringType().defaultValue("");
+
+ public static final ConfigOption<Boolean> PARQUET_SHRED_VARIANTS =
+ ConfigOptions.key("parquet-shred-variants").booleanType().defaultValue(false);
+
+ public static final ConfigOption<Integer> VARIANT_INFERENCE_BUFFER_SIZE =
+ ConfigOptions.key("variant-inference-buffer-size").intType().defaultValue(10);
Review Comment:
You are right, at the first implement in spark is 10, last it change to 100 . I have make it the same .
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3214375483
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteConf.java:
##########
@@ -262,4 +262,22 @@ public Duration tableRefreshInterval() {
.flinkConfig(FlinkWriteOptions.TABLE_REFRESH_INTERVAL)
.parseOptional();
}
+
+ public boolean parquetShredVariants() {
+ return confParser
+ .booleanConf()
+ .option(FlinkWriteOptions.PARQUET_SHRED_VARIANTS.key())
+ .tableProperty(TableProperties.PARQUET_SHRED_VARIANTS)
+ .defaultValue(TableProperties.PARQUET_SHRED_VARIANTS_DEFAULT)
+ .parse();
+ }
+
+ public int parquetVariantInferenceBufferSize() {
+ return confParser
+ .intConf()
+ .option(FlinkWriteOptions.PARQUET_VARIANT_INFERENCE_BUFFER_SIZE.key())
Review Comment:
Right, change it.
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteOptions.java:
##########
@@ -105,4 +105,10 @@ private FlinkWriteOptions() {}
// specify the uidSuffix to be used for the underlying IcebergSink
public static final ConfigOption<String> UID_SUFFIX =
ConfigOptions.key("uid-suffix").stringType().defaultValue("");
+
+ public static final ConfigOption<Boolean> PARQUET_SHRED_VARIANTS =
+ ConfigOptions.key("parquet-shred-variants").booleanType().defaultValue(false);
+
+ public static final ConfigOption<Integer> PARQUET_VARIANT_INFERENCE_BUFFER_SIZE =
Review Comment:
Right, change it.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3214332614
##########
spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/SparkFormatModels.java:
##########
@@ -53,7 +53,7 @@ public static void register() {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new SparkVariantShreddingAnalyzer(),
- InternalRow::copy));
+ structType -> InternalRow::copy));
Review Comment:
The same above.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "nssalian (via GitHub)" <gi...@apache.org>.
nssalian commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3267849536
##########
spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/source/SparkFormatModels.java:
##########
@@ -53,7 +53,7 @@ public static void register() {
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new SparkVariantShreddingAnalyzer(),
- InternalRow::copy));
+ structType -> InternalRow::copy));
Review Comment:
@Guosmilesmile update the Spark 4.0 with the above suggestion as well?
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#issuecomment-4460795095
I found these configs in Spark:
> spark.sql.iceberg.shred-variants
> spark.sql.iceberg.variant-inference-buffer-size
> shred-variants
> variant-inference-buffer-size
All of them already hide the format-related info, while TableProperties keeps the parquet prefix. So I've adjusted the configs on our side to align with Spark's approach — Flink now uses shred-variants, which overrides the table-level configs write.parquet.shred-variants and write.orc.shred-variants (if/when ORC supports it).
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#issuecomment-4493830994
@nssalian Please take an another look if you have time. Thanks
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#issuecomment-4513811533
Seem unrelate error in kafka connect . Retrigger CI.
```
TestIntegrationDynamicTable > testIcebergSink(String) > [1] null FAILED
java.lang.AssertionError at IntegrationTestBase.java:237
```
https://github.com/apache/iceberg/actions/runs/26236304974/job/77210319934?pr=15596
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#issuecomment-4515015445
Merged to main.
Thanks @Guosmilesmile for the PR and @nssalian, @talatuyarer and @aihuaxu for the reviews!
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "pvary (via GitHub)" <gi...@apache.org>.
pvary commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3278889634
##########
parquet/src/main/java/org/apache/iceberg/parquet/ParquetFormatModel.java:
##########
@@ -267,6 +289,9 @@ public FileAppender<D> build() throws IOException {
* top-level fields.
*/
private FileAppender<D> buildShreddedAppender() {
+ UnaryOperator<D> copyFunc = copyFuncFactory.apply(engineSchema);
+ Preconditions.checkState(copyFunc != null, "copyFunc must not return null");
Review Comment:
Checking only the copyFunc seems a bit odd to me. Should we check the factory first?
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3212533846
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteConf.java:
##########
@@ -262,4 +262,22 @@ public Duration tableRefreshInterval() {
.flinkConfig(FlinkWriteOptions.TABLE_REFRESH_INTERVAL)
.parseOptional();
}
+
+ public boolean shredVariants() {
+ return confParser
+ .booleanConf()
+ .option(FlinkWriteOptions.SHRED_VARIANTS.key())
+ .tableProperty(TableProperties.PARQUET_SHRED_VARIANTS)
+ .defaultValue(TableProperties.PARQUET_SHRED_VARIANTS_DEFAULT)
Review Comment:
Good catch . I rename shred-variants to parquet-shred-variants to clarify this feature is only support parquet . If orc support this, then we can add another config.
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "nssalian (via GitHub)" <gi...@apache.org>.
nssalian commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3213900220
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteOptions.java:
##########
@@ -105,4 +105,10 @@ private FlinkWriteOptions() {}
// specify the uidSuffix to be used for the underlying IcebergSink
public static final ConfigOption<String> UID_SUFFIX =
ConfigOptions.key("uid-suffix").stringType().defaultValue("");
+
+ public static final ConfigOption<Boolean> PARQUET_SHRED_VARIANTS =
+ ConfigOptions.key("parquet-shred-variants").booleanType().defaultValue(false);
+
+ public static final ConfigOption<Integer> PARQUET_VARIANT_INFERENCE_BUFFER_SIZE =
Review Comment:
Could you rename this to VARIANT_INFERENCE_BUFFER_SIZE to be consistent with Spark?
--
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] Flink: Support writing shredded variant in Flink [iceberg]
Posted by "Guosmilesmile (via GitHub)" <gi...@apache.org>.
Guosmilesmile commented on code in PR #15596:
URL: https://github.com/apache/iceberg/pull/15596#discussion_r3214375483
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkWriteConf.java:
##########
@@ -262,4 +262,22 @@ public Duration tableRefreshInterval() {
.flinkConfig(FlinkWriteOptions.TABLE_REFRESH_INTERVAL)
.parseOptional();
}
+
+ public boolean parquetShredVariants() {
+ return confParser
+ .booleanConf()
+ .option(FlinkWriteOptions.PARQUET_SHRED_VARIANTS.key())
+ .tableProperty(TableProperties.PARQUET_SHRED_VARIANTS)
+ .defaultValue(TableProperties.PARQUET_SHRED_VARIANTS_DEFAULT)
+ .parse();
+ }
+
+ public int parquetVariantInferenceBufferSize() {
+ return confParser
+ .intConf()
+ .option(FlinkWriteOptions.PARQUET_VARIANT_INFERENCE_BUFFER_SIZE.key())
Review Comment:
Right, will add it.
--
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