voonhous commented on code in PR #18065: URL: https://github.com/apache/hudi/pull/18065#discussion_r3426850802
########## hudi-spark-datasource/hudi-spark4-common/src/main/java/org/apache/hudi/variant/Spark4VariantShreddingProvider.java: ########## @@ -0,0 +1,405 @@ +/* + * 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.hudi.variant; + +import org.apache.hudi.avro.VariantShreddingProvider; +import org.apache.hudi.common.schema.HoodieSchema; + +import org.apache.avro.LogicalType; +import org.apache.avro.LogicalTypes; +import org.apache.avro.Schema; +import org.apache.avro.generic.GenericData; +import org.apache.avro.generic.GenericRecord; +import org.apache.spark.types.variant.Variant; +import org.apache.spark.types.variant.VariantSchema; +import org.apache.spark.types.variant.VariantShreddingWriter; +import org.apache.spark.types.variant.VariantShreddingWriter.ShreddedResult; +import org.apache.spark.types.variant.VariantShreddingWriter.ShreddedResultBuilder; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +/** + * Implementation of {@link VariantShreddingProvider} using Spark 4's variant parsing library. + * + * <p>This class bridges the Avro record path and Spark's {@link VariantShreddingWriter} + * to allow {@code HoodieRecordType.AVRO} to write shredded variant types. It converts + * the shredded output into Avro {@link GenericRecord}s that can be written via + * {@link org.apache.hudi.avro.HoodieAvroWriteSupport}.</p> + * + * <p>The shredding logic is delegated to {@link VariantShreddingWriter#castShredded}, + * which handles scalar, object, and array shredding including residual value construction + * for non-matching fields. This class implements the {@link ShreddedResult} and + * {@link ShreddedResultBuilder} interfaces to collect the shredded components into + * Avro GenericRecords.</p> + */ +public class Spark4VariantShreddingProvider implements VariantShreddingProvider { + + private static final String VALUE_FIELD = "value"; + private static final String METADATA_FIELD = "metadata"; + private static final String TYPED_VALUE_FIELD = "typed_value"; + + @Override + public GenericRecord shredVariantRecord( + GenericRecord unshreddedVariant, + Schema shreddedSchema, + HoodieSchema.Variant variantSchema) { + + ByteBuffer valueBuf = (ByteBuffer) unshreddedVariant.get(VALUE_FIELD); + ByteBuffer metadataBuf = (ByteBuffer) unshreddedVariant.get(METADATA_FIELD); + + if (valueBuf == null || metadataBuf == null) { + return null; + } + + byte[] valueBytes = toByteArray(valueBuf); + byte[] metadataBytes = toByteArray(metadataBuf); + + Variant variant = new Variant(valueBytes, metadataBytes); + + // Build VariantSchema from the Avro shredded schema, registering + // Avro schemas at each level for GenericRecord construction. + AvroShreddedResultBuilder builder = new AvroShreddedResultBuilder(); + VariantSchema sparkSchema = buildVariantSchema(shreddedSchema, true, builder); + + // Delegate to Spark's VariantShreddingWriter for the actual shredding logic. + AvroShreddedResult result = (AvroShreddedResult) + VariantShreddingWriter.castShredded(variant, sparkSchema, builder); + + return result.toGenericRecord(); + } + + /** + * Builds a {@link VariantSchema} from an Avro {@link Schema} representing a + * shredded variant structure ({@code value}, {@code metadata}, {@code typed_value}). + * + * <p>This method also registers the Avro schema mapping in the builder so that + * {@link AvroShreddedResultBuilder#createEmpty} can create results with the + * correct Avro schema at each nesting level.</p> + */ + private VariantSchema buildVariantSchema(Schema avroSchema, boolean isTopLevel, + AvroShreddedResultBuilder builder) { + Schema.Field valueField = avroSchema.getField(VALUE_FIELD); + Schema.Field metadataField = avroSchema.getField(METADATA_FIELD); + Schema.Field typedValueField = avroSchema.getField(TYPED_VALUE_FIELD); + + int idx = 0; Review Comment: Renamed to fieldCount. It's post-incremented as each shredded field is assigned a slot and its terminal value is numFields, so fieldCount reflects the running-count semantics better than idx. -- 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]
