jonvex commented on code in PR #13711: URL: https://github.com/apache/hudi/pull/13711#discussion_r2370847456
########## hudi-common/src/main/java/org/apache/hudi/stats/ValueMetadata.java: ########## @@ -0,0 +1,254 @@ +/* + * 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.stats; + +import org.apache.hudi.ParquetAdapter; +import org.apache.hudi.avro.model.HoodieValueTypeInfo; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.metadata.HoodieIndexVersion; + +import org.apache.avro.LogicalTypes; +import org.apache.avro.Schema; +import org.apache.avro.generic.GenericRecord; +import org.apache.parquet.schema.PrimitiveType; + +import java.io.Serializable; + +import static org.apache.hudi.avro.AvroSchemaUtils.resolveNullableSchema; +import static org.apache.hudi.metadata.HoodieMetadataPayload.COLUMN_STATS_FIELD_VALUE_TYPE; +import static org.apache.hudi.metadata.HoodieMetadataPayload.COLUMN_STATS_FIELD_VALUE_TYPE_ADDITIONAL_INFO; +import static org.apache.hudi.metadata.HoodieMetadataPayload.COLUMN_STATS_FIELD_VALUE_TYPE_ORDINAL; + +/** + * Holder for VaueType and additional info + * Used for wrapping and unwrapping col stat values + * as well as for type promotion + */ +public class ValueMetadata implements Serializable { + + private static final ParquetAdapter PARQUET_ADAPTER = ParquetAdapter.getAdapter(); + + private final ValueType valueType; + + protected ValueMetadata(ValueType valueType) { + this.valueType = valueType; + } + + public ValueType getValueType() { + return valueType; + } + + public HoodieValueTypeInfo getValueTypeInfo() { + return HoodieValueTypeInfo.newBuilder() + .setTypeOrdinal(valueType.ordinal()) + .setAdditionalInfo(getAdditionalInfo()) + .build(); + } + + String getAdditionalInfo() { + return null; + } + + public Comparable<?> standardizeJavaTypeAndPromote(Object val) { + return this.getValueType().standardizeJavaTypeAndPromote(val, this); + } + + public Object wrapValue(Comparable<?> value) { + return this.getValueType().wrapValue(value, this); + } + + public Comparable<?> unwrapValue(Object value) { + return this.getValueType().unwrapValue(value, this); + } + + public void validate(Object minVal, Object maxVal) { + if (getValueType() == ValueType.V1) { + return; + } + this.getValueType().validate(minVal); + this.getValueType().validate(maxVal); + } + + public boolean isV1() { + return this.getValueType() == ValueType.V1; + } + + public static class V1EmptyMetadata extends ValueMetadata { + private static final V1EmptyMetadata V1_EMPTY_METADATA = new V1EmptyMetadata(); + public static V1EmptyMetadata get() { + return V1_EMPTY_METADATA; + } + + private V1EmptyMetadata() { + super(ValueType.V1); + } + + @Override + public HoodieValueTypeInfo getValueTypeInfo() { + // V1 should never be persisted to the MDT. It is only for in memory + return null; + } + } + + public static final ValueMetadata NULL_METADATA = new ValueMetadata(ValueType.NULL); + + interface DecimalValueMetadata { + + int getPrecision(); + + int getScale(); + + static String encodeData(DecimalValueMetadata decimalValueMetadata) { + return String.format("%d,%d", decimalValueMetadata.getPrecision(), decimalValueMetadata.getScale()); + } + + static Pair<Integer, Integer> decodeData(String data) { + //TODO: decide if we want to store things in a better way + String[] splits = data.split(","); + return Pair.of(Integer.parseInt(splits[0]), Integer.parseInt(splits[1])); + } + } + + static class DecimalMetadata extends ValueMetadata implements DecimalValueMetadata { + + static DecimalMetadata create(String additionalInfo) { + if (additionalInfo == null) { + throw new IllegalArgumentException("additionalInfo cannot be null"); + } + Pair<Integer, Integer> data = DecimalValueMetadata.decodeData(additionalInfo); + return new DecimalMetadata(data.getLeft(), data.getRight()); + } + + static DecimalMetadata create(LogicalTypes.Decimal decimal) { + return new DecimalMetadata(decimal.getPrecision(), decimal.getScale()); + } + + static DecimalMetadata create(PrimitiveType primitiveType) { + return new DecimalMetadata(PARQUET_ADAPTER.getPrecision(primitiveType), PARQUET_ADAPTER.getScale(primitiveType)); + } + + static DecimalMetadata create(int precision, int scale) { + return new DecimalMetadata(precision, scale); + } + + private final int precision; + private final int scale; + + private DecimalMetadata(int precision, int scale) { + super(ValueType.DECIMAL); + this.precision = precision; + this.scale = scale; + } + + @Override + public int getPrecision() { + return precision; + } + + @Override + public int getScale() { + return scale; + } + + @Override + String getAdditionalInfo() { + return DecimalValueMetadata.encodeData(this); + } + } + + public static ValueMetadata getEmptyValueMetadata(HoodieIndexVersion indexVersion) { + if (indexVersion.lowerThan(HoodieIndexVersion.V2)) { + return V1EmptyMetadata.get(); + } + return NULL_METADATA; + } + + public static ValueMetadata getValueMetadata(HoodieValueTypeInfo valueTypeInfo) { + // valueTypeInfo will always be null when version is v1 + if (valueTypeInfo == null) { + return V1EmptyMetadata.get(); + } + + ValueType valueType = ValueType.fromOrdinal(valueTypeInfo.getTypeOrdinal()); + if (valueType == ValueType.V1) { + return V1EmptyMetadata.get(); + } else if (valueType == ValueType.DECIMAL) { + return DecimalMetadata.create(valueTypeInfo.getAdditionalInfo()); + } else { + return new ValueMetadata(valueType); + } + } + + public static ValueMetadata getValueMetadata(GenericRecord columnStatsRecord) { + if (columnStatsRecord == null) { + // TODO: Should we return V1EmptyMetadata here? + return NULL_METADATA; Review Comment: Otherwise, we will need to pass index version because we don't want to assign null if it's v1, or v1 if we are on v2 -- 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]
