rdblue commented on code in PR #12139: URL: https://github.com/apache/iceberg/pull/12139#discussion_r1960602946
########## parquet/src/main/java/org/apache/iceberg/parquet/ParquetVariantVisitor.java: ########## @@ -0,0 +1,285 @@ +/* + * 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.parquet; + +import java.util.List; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.parquet.schema.GroupType; +import org.apache.parquet.schema.LogicalTypeAnnotation.ListLogicalTypeAnnotation; +import org.apache.parquet.schema.PrimitiveType; +import org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName; +import org.apache.parquet.schema.Type; + +public abstract class ParquetVariantVisitor<R> { + static final String METADATA = "metadata"; + static final String VALUE = "value"; + static final String TYPED_VALUE = "typed_value"; + + /** + * Handles the root variant column group. + * + * <p>The value and typed_value results are combined by calling {@link #value}. + * + * <pre> + * group v (VARIANT) { <-- metadata result and combined value and typed_value result + * required binary metadata; + * optional binary value; + * optional ... typed_value; + * } + * </pre> + */ + public R variant(GroupType variant, R metadataResult, R valueResult) { + return null; + } + + /** + * Handles a serialized variant metadata column. + * + * <pre> + * group v (VARIANT) { + * required binary metadata; <-- this column + * optional binary value; + * optional ... typed_value; + * } + * </pre> + */ + public R metadata(PrimitiveType metadata) { + return null; + } + + /** + * Handles a serialized variant value column. + * + * <pre> + * group variant_value_pair { + * optional binary value; <-- this column + * optional ... typed_value; + * } + * </pre> + */ + public R serialized(PrimitiveType value) { + return null; + } + + /** + * Handles a shredded primitive typed_value column. + * + * <pre> + * group variant_value_pair { + * optional binary value; + * optional int32 typed_value; <-- this column when it is any primitive + * } + * </pre> + */ + public R primitive(PrimitiveType primitive) { + return null; + } + + /** + * Handles a variant value result and typed_value result pair. + * + * <p>The value and typed_value pair may be nested in an object field, array element, or in the + * root group of a variant. + * + * <p>This method is also called when the typed_value field is missing. + * + * <pre> + * group variant_value_pair { <-- value result and typed_value result + * optional binary value; + * optional ... typed_value; + * } + * </pre> + */ + public R value(GroupType value, R valueResult, R typedResult) { + return null; + } + + /** + * Handles a shredded object value result and a list of field value results. + * + * <p>Each field's value and typed_value results are combined by calling {@link #value}. + * + * <pre> + * group variant_value_pair { <-- value result and typed_value field results + * optional binary value; + * optional group typed_value { + * required group a { + * optional binary value; + * optional binary typed_value (UTF8); + * } + * ... + * } + * } + * </pre> + */ + public R object(GroupType object, R valueResult, List<R> fieldResults) { + return null; + } + + /** + * Handles a shredded array value result and an element value result. + * + * <p>The element's value and typed_value results are combined by calling {@link #value}. + * + * <pre> + * group variant_value_pair { <-- value result and element result + * optional binary value; + * optional group typed_value (LIST) { + * repeated group list { + * required group element { + * optional binary value; + * optional binary typed_value (UTF8); + * } + * } + * } + * } + * </pre> + */ + public R array(GroupType array, R valueResult, R elementResult) { + return null; + } + + /** Handler called before visiting any primitive or group type. */ + public void beforeField(Type type) {} + + /** Handler called after visiting any primitive or group type. */ + public void afterField(Type type) {} + + public static <R> R visit(GroupType type, ParquetVariantVisitor<R> visitor) { + Preconditions.checkArgument( + ParquetSchemaUtil.hasField(type, METADATA), "Invalid variant, missing metadata: %s", type); + + Type metadataType = type.getType(METADATA); + Preconditions.checkArgument( + isBinary(metadataType), "Invalid variant metadata, expecting BINARY: %s", metadataType); + + R metadataResult = + withBeforeAndAfter( + () -> visitor.metadata(metadataType.asPrimitiveType()), metadataType, visitor); + R valueResult = visitValue(type, visitor); + + return visitor.variant(type, metadataResult, valueResult); + } + + public static <R> R visitValue(GroupType valueGroup, ParquetVariantVisitor<R> visitor) { Review Comment: Fixed. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org