Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,12 @@ private SparkSQLProperties() {}

// Prefix for custom snapshot properties
public static final String SNAPSHOT_PROPERTY_PREFIX = "spark.sql.iceberg.snapshot-property.";

// Controls whether to shred variant columns during write operations
public static final String SHRED_VARIANTS = "spark.sql.iceberg.shred-variants";

// Controls the buffer size for variant schema inference during writes
// This determines how many rows are buffered before inferring shredded schema
public static final String VARIANT_INFERENCE_BUFFER_SIZE =
"spark.sql.iceberg.variant-inference-buffer-size";
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import static org.apache.iceberg.TableProperties.ORC_COMPRESSION_STRATEGY;
import static org.apache.iceberg.TableProperties.PARQUET_COMPRESSION;
import static org.apache.iceberg.TableProperties.PARQUET_COMPRESSION_LEVEL;
import static org.apache.iceberg.TableProperties.PARQUET_SHRED_VARIANTS;
import static org.apache.iceberg.TableProperties.PARQUET_VARIANT_BUFFER_SIZE;
import static org.apache.spark.sql.connector.write.RowLevelOperation.Command.DELETE;

import java.util.Locale;
Expand Down Expand Up @@ -529,6 +531,14 @@ private Map<String, String> dataWriteProperties() {
if (parquetCompressionLevel != null) {
writeProperties.put(PARQUET_COMPRESSION_LEVEL, parquetCompressionLevel);
}
boolean shouldShredVariants = shredVariants();
writeProperties.put(PARQUET_SHRED_VARIANTS, String.valueOf(shouldShredVariants));

// Add variant shredding configuration properties
if (shouldShredVariants) {
writeProperties.put(
PARQUET_VARIANT_BUFFER_SIZE, String.valueOf(variantInferenceBufferSize()));
}
break;

case AVRO:
Expand Down Expand Up @@ -749,4 +759,24 @@ public DeleteGranularity deleteGranularity() {
.defaultValue(DeleteGranularity.FILE)
.parse();
}

public boolean shredVariants() {
return confParser
.booleanConf()
.option(SparkWriteOptions.SHRED_VARIANTS)
.sessionConf(SparkSQLProperties.SHRED_VARIANTS)
.tableProperty(TableProperties.PARQUET_SHRED_VARIANTS)
.defaultValue(TableProperties.PARQUET_SHRED_VARIANTS_DEFAULT)
.parse();
}

public int variantInferenceBufferSize() {
return confParser
.intConf()
.option(SparkWriteOptions.VARIANT_INFERENCE_BUFFER_SIZE)
.sessionConf(SparkSQLProperties.VARIANT_INFERENCE_BUFFER_SIZE)
.tableProperty(TableProperties.PARQUET_VARIANT_BUFFER_SIZE)
.defaultValue(TableProperties.PARQUET_VARIANT_BUFFER_SIZE_DEFAULT)
.parse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,10 @@ private SparkWriteOptions() {}

// Overrides the delete granularity
public static final String DELETE_GRANULARITY = "delete-granularity";

// Controls whether to shred variant columns during write operations
public static final String SHRED_VARIANTS = "shred-variants";

// Controls the buffer size for variant schema inference during writes
public static final String VARIANT_INFERENCE_BUFFER_SIZE = "variant-inference-buffer-size";
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public static void register() {
StructType.class,
SparkParquetWriters::buildWriter,
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant)));
SparkParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new SparkVariantShreddingAnalyzer(),
InternalRow::copy));

FormatModelRegistry.register(
ParquetFormatModel.create(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.spark.source;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
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;
import org.apache.spark.sql.catalyst.InternalRow;
import org.apache.spark.sql.types.StructType;
import org.apache.spark.unsafe.types.VariantVal;

/**
* Spark-specific implementation that extracts variant values from {@link InternalRow} instances.
*/
class SparkVariantShreddingAnalyzer extends VariantShreddingAnalyzer<InternalRow, StructType> {

SparkVariantShreddingAnalyzer() {}

@Override
protected int resolveColumnIndex(StructType sparkSchema, String columnName) {
try {
return sparkSchema.fieldIndex(columnName);
} catch (IllegalArgumentException e) {
return -1;
}
}

@Override
protected List<VariantValue> extractVariantValues(
List<InternalRow> bufferedRows, int variantFieldIndex) {
List<VariantValue> values = Lists.newArrayList();

for (InternalRow row : bufferedRows) {
if (!row.isNullAt(variantFieldIndex)) {
VariantVal variantVal = row.getVariant(variantFieldIndex);
if (variantVal != null) {
VariantValue variantValue =
VariantValue.from(
VariantMetadata.from(
ByteBuffer.wrap(variantVal.getMetadata()).order(ByteOrder.LITTLE_ENDIAN)),
ByteBuffer.wrap(variantVal.getValue()).order(ByteOrder.LITTLE_ENDIAN));
values.add(variantValue);
}
}
}

return values;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static org.apache.iceberg.TableProperties.ORC_COMPRESSION_STRATEGY;
import static org.apache.iceberg.TableProperties.PARQUET_COMPRESSION;
import static org.apache.iceberg.TableProperties.PARQUET_COMPRESSION_LEVEL;
import static org.apache.iceberg.TableProperties.PARQUET_SHRED_VARIANTS;
import static org.apache.iceberg.TableProperties.UPDATE_DISTRIBUTION_MODE;
import static org.apache.iceberg.TableProperties.WRITE_DISTRIBUTION_MODE;
import static org.apache.iceberg.TableProperties.WRITE_DISTRIBUTION_MODE_HASH;
Expand Down Expand Up @@ -61,6 +62,7 @@
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.spark.sql.internal.SQLConf;
import org.apache.spark.sql.util.CaseInsensitiveStringMap;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
Expand Down Expand Up @@ -340,6 +342,8 @@ public void testSparkConfOverride() {
TableProperties.DELETE_PARQUET_COMPRESSION,
"snappy"),
ImmutableMap.of(
PARQUET_SHRED_VARIANTS,
"false",
DELETE_PARQUET_COMPRESSION,
"zstd",
PARQUET_COMPRESSION,
Expand Down Expand Up @@ -461,6 +465,8 @@ public void testDataPropsDefaultsAsDeleteProps() {
PARQUET_COMPRESSION_LEVEL,
"5"),
ImmutableMap.of(
PARQUET_SHRED_VARIANTS,
"false",
DELETE_PARQUET_COMPRESSION,
"zstd",
PARQUET_COMPRESSION,
Expand Down Expand Up @@ -532,6 +538,8 @@ public void testDeleteFileWriteConf() {
DELETE_PARQUET_COMPRESSION_LEVEL,
"6"),
ImmutableMap.of(
PARQUET_SHRED_VARIANTS,
"false",
DELETE_PARQUET_COMPRESSION,
"zstd",
PARQUET_COMPRESSION,
Expand Down Expand Up @@ -686,4 +694,81 @@ private void checkMode(DistributionMode expectedMode, SparkWriteConf writeConf)
assertThat(writeConf.copyOnWriteDistributionMode(MERGE)).isEqualTo(expectedMode);
assertThat(writeConf.positionDeltaDistributionMode(MERGE)).isEqualTo(expectedMode);
}

@TestTemplate
public void testShredVariantsDefault() {
Table table = validationCatalog.loadTable(tableIdent);
SparkWriteConf writeConf = new SparkWriteConf(spark, table, ImmutableMap.of());
assertThat(writeConf.shredVariants()).isFalse();
}

@TestTemplate
public void testVariantInferenceBufferSizeDefault() {
Table table = validationCatalog.loadTable(tableIdent);
SparkWriteConf writeConf = new SparkWriteConf(spark, table, ImmutableMap.of());
assertThat(writeConf.variantInferenceBufferSize())
.isEqualTo(TableProperties.PARQUET_VARIANT_BUFFER_SIZE_DEFAULT);
}

@TestTemplate
public void testVariantInferenceBufferSizeTableProperty() {
Table table = validationCatalog.loadTable(tableIdent);

table.updateProperties().set(TableProperties.PARQUET_VARIANT_BUFFER_SIZE, "500").commit();

SparkWriteConf writeConf = new SparkWriteConf(spark, table, ImmutableMap.of());
assertThat(writeConf.variantInferenceBufferSize()).isEqualTo(500);
}

@TestTemplate
public void testShredVariantsSessionOverridesTableProperty() {
Table table = validationCatalog.loadTable(tableIdent);
table.updateProperties().set(TableProperties.PARQUET_SHRED_VARIANTS, "false").commit();

withSQLConf(
ImmutableMap.of(SparkSQLProperties.SHRED_VARIANTS, "true"),
() -> {
SparkWriteConf writeConf = new SparkWriteConf(spark, table, ImmutableMap.of());
assertThat(writeConf.shredVariants()).isTrue();
});
}

@TestTemplate
public void testShredVariantsWriteOptionOverridesSessionConf() {
withSQLConf(
ImmutableMap.of(SparkSQLProperties.SHRED_VARIANTS, "false"),
() -> {
Table table = validationCatalog.loadTable(tableIdent);
SparkWriteConf writeConf =
new SparkWriteConf(
spark,
table,
new CaseInsensitiveStringMap(
ImmutableMap.of(SparkWriteOptions.SHRED_VARIANTS, "true")));
assertThat(writeConf.shredVariants()).isTrue();
});
}

@TestTemplate
public void testVariantInferenceBufferSizeSessionConf() {
withSQLConf(
ImmutableMap.of(SparkSQLProperties.VARIANT_INFERENCE_BUFFER_SIZE, "250"),
() -> {
Table table = validationCatalog.loadTable(tableIdent);
SparkWriteConf writeConf = new SparkWriteConf(spark, table, ImmutableMap.of());
assertThat(writeConf.variantInferenceBufferSize()).isEqualTo(250);
});
}

@TestTemplate
public void testWritePropertiesIncludeVariantShredding() {
Table table = validationCatalog.loadTable(tableIdent);
table.updateProperties().set(TableProperties.PARQUET_SHRED_VARIANTS, "true").commit();
table.updateProperties().set(TableProperties.PARQUET_VARIANT_BUFFER_SIZE, "200").commit();

SparkWriteConf writeConf = new SparkWriteConf(spark, table, ImmutableMap.of());
Map<String, String> writeProperties = writeConf.writeProperties();
assertThat(writeProperties).containsEntry(PARQUET_SHRED_VARIANTS, "true");
assertThat(writeProperties).containsEntry(TableProperties.PARQUET_VARIANT_BUFFER_SIZE, "200");
}
}
Loading