jonvex commented on code in PR #13711: URL: https://github.com/apache/hudi/pull/13711#discussion_r2370842255
########## hudi-common/src/main/java/org/apache/hudi/stats/ValueMetadata.java: ########## @@ -0,0 +1,242 @@ +/* + * 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.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.LogicalTypeTokenParser; +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; + +public class ValueMetadata implements Serializable { + 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(); + } + + public 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 static class V1EmptyMetadata extends ValueMetadata { + private static final V1EmptyMetadata V_1_EMPTY_METADATA = new V1EmptyMetadata(); + public static V1EmptyMetadata get() { + return V_1_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); + + public 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])); Review Comment: ok added javadoc comment. I also have a test class TestValueMetadata to ensure that this doesn't get changed without care in handling. I also added a few comments saying not to change this without handling upgrade/downgrade and backwards compat -- 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]
