Skip to content
Open
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 @@ -18,6 +18,9 @@
*/
package org.apache.iceberg.data;

import java.util.function.Function;
import java.util.function.UnaryOperator;
import org.apache.iceberg.Schema;
import org.apache.iceberg.avro.AvroFormatModel;
import org.apache.iceberg.data.avro.DataWriter;
import org.apache.iceberg.data.avro.PlannedDataReader;
Expand All @@ -44,11 +47,13 @@ public static void register() {
FormatModelRegistry.register(
ParquetFormatModel.create(
Record.class,
Void.class,
Schema.class,
(icebergSchema, fileSchema, engineSchema) ->
GenericParquetWriter.create(icebergSchema, fileSchema),
(icebergSchema, fileSchema, engineSchema, idToConstant) ->
GenericParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant)));
GenericParquetReaders.buildReader(icebergSchema, fileSchema, idToConstant),
new RecordVariantShreddingAnalyzer(),
(Function<Schema, UnaryOperator<Record>>) engineSchema -> Record::copy));

FormatModelRegistry.register(ParquetFormatModel.forPositionDeletes());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.data;

import java.util.List;
import java.util.Map;
import org.apache.iceberg.Schema;
import org.apache.iceberg.parquet.VariantShreddingAnalyzer;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.types.Types.NestedField;
import org.apache.iceberg.variants.Variant;
import org.apache.iceberg.variants.VariantValue;

/**
* Generic {@link Record} implementation that extracts variant values from {@link Record#get(int)}
* using positional indices aligned with {@link Schema#columns()}.
*
* <p>Buffered rows must be laid out against the same {@link Schema} passed as {@code engineSchema};
* otherwise {@link Record#get(int)} positions will not match the resolved column indices.
*/
class RecordVariantShreddingAnalyzer extends VariantShreddingAnalyzer<Record, Schema> {

private final Map<Schema, Map<String, Integer>> columnIndicesBySchema = Maps.newHashMap();

RecordVariantShreddingAnalyzer() {}

@Override
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This javadoc describes pre-fix state ("is unused", "always produced -1", "never activated") rather than what the method
does. Suggest dropping it - {@inheritDoc} is the default for overrides and the override's behavior needs to be explained instead

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This override duplicates the loop from VariantShreddingAnalyzer.analyzeVariantColumns. Spark/Flink override only the protected hooks (resolveColumnIndex, extractVariantValues), not the public template.

protected int resolveColumnIndex(Schema engineSchema, String columnName) {
Preconditions.checkNotNull(engineSchema, "Invalid engine schema: null");

Map<String, Integer> indices =
columnIndicesBySchema.computeIfAbsent(
engineSchema, RecordVariantShreddingAnalyzer::indexByName);
Integer index = indices.get(columnName);
return index != null ? index : -1;
}

private static Map<String, Integer> indexByName(Schema schema) {
List<NestedField> cols = schema.columns();
Map<String, Integer> indices = Maps.newHashMapWithExpectedSize(cols.size());
for (int i = 0; i < cols.size(); i++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocker, but worth thinking about: this is O(n) per variant column, so O(n·k) for wide schemas (CDC tables with 200+ columns are not uncommon). Spark uses sparkSchema.fieldIndex(name) which is O(1). A precomputed Map<String, Integer> cached on the instance would match that.

Also, while we're here — the class Javadoc says "positional indices aligned with Schema#columns()", and extractVariantValues then uses record.get(int) against that same index. That's a real contract: the records being analyzed must have been built against the same schema passed as engineSchema. Worth one sentence in the Javadoc making that explicit so a future caller doesn't pass a projected schema and get silent misalignment.

indices.put(cols.get(i).name(), i);
}
return indices;
}

@Override
protected List<VariantValue> extractVariantValues(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instanceof Variant silently skips both nulls and non-Variant values. Should this throw on non-Variant so caller bugs surface instead of silently shrinking the analysis set? Worth referring to the Spark and Flink implementation.

List<Record> bufferedRows, int variantFieldIndex) {
List<VariantValue> values = Lists.newArrayList();
for (Record record : bufferedRows) {
Object fieldValue = record.get(variantFieldIndex);
if (fieldValue == null) {
continue;
}

Preconditions.checkArgument(
fieldValue instanceof Variant,
"Expected Variant at index %s but was: %s",
variantFieldIndex,
fieldValue.getClass().getName());
values.add(((Variant) fieldValue).value());
}
return values;
}
}
Loading
Loading