laskoviymishka commented on code in PR #17520: URL: https://github.com/apache/iceberg/pull/17520#discussion_r3749663279
########## data/src/main/java/org/apache/iceberg/data/RecordVariantShreddingAnalyzer.java: ########## @@ -0,0 +1,118 @@ +/* + * 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.types.Types.NestedField; +import org.apache.iceberg.variants.Variant; +import org.apache.iceberg.variants.VariantValue; +import org.apache.parquet.schema.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * {@link Record} analyzer that resolves variant columns by position in {@link Schema#columns()}. + * Rows are read positionally, so an engine schema, if supplied, must place each variant column at + * its Iceberg-schema position. + */ +class RecordVariantShreddingAnalyzer extends VariantShreddingAnalyzer<Record, Schema> { + private static final Logger LOG = LoggerFactory.getLogger(RecordVariantShreddingAnalyzer.class); + + RecordVariantShreddingAnalyzer() {} + + @Override + public Map<Integer, Type> analyzeVariantColumns( + List<Record> bufferedRows, Schema icebergSchema, Schema engineSchema) { + // Record rows are built against the Iceberg schema; use it when no engine schema is supplied. + if (engineSchema == null) { + return super.analyzeVariantColumns(bufferedRows, icebergSchema, icebergSchema); + } + + checkVariantColumnPositionsAligned(icebergSchema, engineSchema); + return super.analyzeVariantColumns(bufferedRows, icebergSchema, engineSchema); + } + + @Override + protected int resolveColumnIndex(Schema engineSchema, String columnName) { + Preconditions.checkArgument(engineSchema != null, "Invalid engine schema: null"); + int index = positionOf(engineSchema, columnName); + if (index < 0) { + LOG.warn("Variant column {} not found in engine schema; skipping shredding", columnName); + } + + return index; + } + + // Rows are read positionally, so a variant column must sit at the same index in both schemas. + private static void checkVariantColumnPositionsAligned( + Schema icebergSchema, Schema engineSchema) { + List<NestedField> icebergColumns = icebergSchema.columns(); + for (int icebergIndex = 0; icebergIndex < icebergColumns.size(); icebergIndex++) { + NestedField col = icebergColumns.get(icebergIndex); + if (!col.type().isVariantType()) { + continue; + } + + int engineIndex = positionOf(engineSchema, col.name()); + Preconditions.checkArgument( + engineIndex < 0 || engineIndex == icebergIndex, Review Comment: this is the guard that closed my reorder concern from last round, thanks — rejecting a misaligned schema is the right call over silently shredding the wrong column. One thing it also catches: a projected engine schema like `[id, v]` over an Iceberg `[id, name, v]` gets `engineIndex(v)=1` vs `icebergIndex(v)=2` and throws right here, at flush time after rows are already buffered. KC never hits it since `engineSchema` is always null, so this is optional — but if projections aren't meant to be supported, a Javadoc line with a concrete example would save the next caller the surprise. Fine to leave for a follow-up. ########## data/src/main/java/org/apache/iceberg/data/RecordVariantShreddingAnalyzer.java: ########## @@ -0,0 +1,118 @@ +/* + * 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.types.Types.NestedField; +import org.apache.iceberg.variants.Variant; +import org.apache.iceberg.variants.VariantValue; +import org.apache.parquet.schema.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * {@link Record} analyzer that resolves variant columns by position in {@link Schema#columns()}. + * Rows are read positionally, so an engine schema, if supplied, must place each variant column at + * its Iceberg-schema position. + */ +class RecordVariantShreddingAnalyzer extends VariantShreddingAnalyzer<Record, Schema> { + private static final Logger LOG = LoggerFactory.getLogger(RecordVariantShreddingAnalyzer.class); + + RecordVariantShreddingAnalyzer() {} + + @Override + public Map<Integer, Type> analyzeVariantColumns( + List<Record> bufferedRows, Schema icebergSchema, Schema engineSchema) { + // Record rows are built against the Iceberg schema; use it when no engine schema is supplied. + if (engineSchema == null) { + return super.analyzeVariantColumns(bufferedRows, icebergSchema, icebergSchema); + } + + checkVariantColumnPositionsAligned(icebergSchema, engineSchema); + return super.analyzeVariantColumns(bufferedRows, icebergSchema, engineSchema); + } + + @Override + protected int resolveColumnIndex(Schema engineSchema, String columnName) { + Preconditions.checkArgument(engineSchema != null, "Invalid engine schema: null"); + int index = positionOf(engineSchema, columnName); + if (index < 0) { + LOG.warn("Variant column {} not found in engine schema; skipping shredding", columnName); + } + + return index; + } + + // Rows are read positionally, so a variant column must sit at the same index in both schemas. + private static void checkVariantColumnPositionsAligned( + Schema icebergSchema, Schema engineSchema) { + List<NestedField> icebergColumns = icebergSchema.columns(); + for (int icebergIndex = 0; icebergIndex < icebergColumns.size(); icebergIndex++) { + NestedField col = icebergColumns.get(icebergIndex); + if (!col.type().isVariantType()) { + continue; + } + + int engineIndex = positionOf(engineSchema, col.name()); + Preconditions.checkArgument( + engineIndex < 0 || engineIndex == icebergIndex, + "Variant column %s position mismatch between Iceberg and engine schemas: %s vs %s", + col.name(), + icebergIndex, + engineIndex); + } + } + + private static int positionOf(Schema schema, String columnName) { + List<NestedField> columns = schema.columns(); + for (int index = 0; index < columns.size(); index++) { + if (columns.get(index).name().equals(columnName)) { Review Comment: `.equals` here is case-sensitive, while Iceberg name resolution is case-insensitive by default. An engine schema built with different casing ("V" vs "v") resolves to -1, so shredding silently no-ops even with `shred-variants=true`. It matches Spark/Flink so it's not a regression — purely optional, but a Javadoc line noting the comparison is case-sensitive would help someone debugging a silent no-op. ########## kafka-connect/kafka-connect/src/test/java/org/apache/iceberg/connect/data/TestRecordConverter.java: ########## @@ -1387,6 +1387,30 @@ public void testConvertVariantValueFromNull() { assertThat(variant.value().type()).isEqualTo(PhysicalType.NULL); } + @Test + public void testConvertOrdersOutputByTableSchemaNotSourceFieldOrder() { Review Comment: this proves field ordering, but not the thing the KC rationale rests on — a variant going through `convert()` and landing in `typed_value`. All the round-trip coverage is in `TestRecordVariantShreddingAnalyzer` against hand-built `GenericRecord`s via `GenericFileWriterFactory`, so nothing exercises Connect Struct → `RecordConverter.convert()` → shredded Parquet. A KC-side test that writes a Struct with a variant field through `RecordUtils.createTableWriter` with `shred-variants=true` and asserts `typed_value` on read-back would close it — optional for this PR, but it's the regression I'd want caught if someone later touches field-ordering or the null-engineSchema fallback. ########## data/src/main/java/org/apache/iceberg/data/RecordVariantShreddingAnalyzer.java: ########## @@ -0,0 +1,118 @@ +/* + * 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.types.Types.NestedField; +import org.apache.iceberg.variants.Variant; +import org.apache.iceberg.variants.VariantValue; +import org.apache.parquet.schema.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * {@link Record} analyzer that resolves variant columns by position in {@link Schema#columns()}. + * Rows are read positionally, so an engine schema, if supplied, must place each variant column at + * its Iceberg-schema position. + */ +class RecordVariantShreddingAnalyzer extends VariantShreddingAnalyzer<Record, Schema> { + private static final Logger LOG = LoggerFactory.getLogger(RecordVariantShreddingAnalyzer.class); + + RecordVariantShreddingAnalyzer() {} + + @Override + public Map<Integer, Type> analyzeVariantColumns( + List<Record> bufferedRows, Schema icebergSchema, Schema engineSchema) { + // Record rows are built against the Iceberg schema; use it when no engine schema is supplied. + if (engineSchema == null) { + return super.analyzeVariantColumns(bufferedRows, icebergSchema, icebergSchema); + } + + checkVariantColumnPositionsAligned(icebergSchema, engineSchema); + return super.analyzeVariantColumns(bufferedRows, icebergSchema, engineSchema); + } + + @Override + protected int resolveColumnIndex(Schema engineSchema, String columnName) { + Preconditions.checkArgument(engineSchema != null, "Invalid engine schema: null"); + int index = positionOf(engineSchema, columnName); + if (index < 0) { + LOG.warn("Variant column {} not found in engine schema; skipping shredding", columnName); + } + + return index; + } + + // Rows are read positionally, so a variant column must sit at the same index in both schemas. + private static void checkVariantColumnPositionsAligned( + Schema icebergSchema, Schema engineSchema) { + List<NestedField> icebergColumns = icebergSchema.columns(); + for (int icebergIndex = 0; icebergIndex < icebergColumns.size(); icebergIndex++) { + NestedField col = icebergColumns.get(icebergIndex); + if (!col.type().isVariantType()) { + continue; + } + + int engineIndex = positionOf(engineSchema, col.name()); + Preconditions.checkArgument( + engineIndex < 0 || engineIndex == icebergIndex, + "Variant column %s position mismatch between Iceberg and engine schemas: %s vs %s", + col.name(), + icebergIndex, + engineIndex); + } + } + + private static int positionOf(Schema schema, String columnName) { + List<NestedField> columns = schema.columns(); + for (int index = 0; index < columns.size(); index++) { + if (columns.get(index).name().equals(columnName)) { + return index; + } + } + + return -1; + } + + @Override + protected List<VariantValue> extractVariantValues( + List<Record> bufferedRows, int variantFieldIndex) { + List<VariantValue> values = Lists.newArrayList(); + for (Record row : bufferedRows) { + Object fieldValue = row.get(variantFieldIndex); + if (fieldValue == null) { + continue; + } + + Preconditions.checkArgument( + fieldValue instanceof Variant, Review Comment: the instanceof hard-fail I flagged last round is still here. A single non-Variant value throws from inside `BufferedFileAppender.initialize()` at flush time, propagates out through `IcebergWriter` (which only catches IOException), and takes down the whole buffered batch — every record lost, no file committed. `null` right above just gets skipped, so we already have the degrade precedent. For the KC case I'd lean toward `LOG.warn` + dropping that column from analysis over failing the commit. Not a blocker — fine to leave for a follow-up if you'd rather, I'll keep the PR open in case someone wants to take it. wdyt? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
