nssalian commented on code in PR #17520: URL: https://github.com/apache/iceberg/pull/17520#discussion_r3778719539
########## 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); Review Comment: It's there from @laskoviymishka's earlier point - the base resolves the variant's index off the engine schema but reads it from a Record built against the Iceberg schema, so if the variants sit at different positions it reads the wrong field. With two variants swapped it slips past the instanceof check and shreds one column's data under the other's field ID; a single variant just fails loudly. In practice engineSchema is always null on the Record path though, so this only ever matters for the direct dataWriteBuilder().engineSchema() API. I'm ok to drop it and always use the Iceberg schema. @laskoviymishka let me know what you think. -- 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]
