rdblue commented on code in PR #4525: URL: https://github.com/apache/iceberg/pull/4525#discussion_r863327981
########## core/src/main/java/org/apache/iceberg/DefaultValueParser.java: ########## @@ -0,0 +1,253 @@ +/* + * 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; + +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.databind.ser.std.ByteBufferSerializer; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; +import org.apache.iceberg.exceptions.ValidationException; +import org.apache.iceberg.expressions.Literal; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.relocated.com.google.common.io.BaseEncoding; +import org.apache.iceberg.types.Type; +import org.apache.iceberg.types.Types; + +public class DefaultValueParser { + private DefaultValueParser() { + } + + private static final JsonFactory FACTORY = new JsonFactory(); + private static final ObjectMapper MAPPER; + + static { + MAPPER = new ObjectMapper(FACTORY); + SimpleModule customModule = new SimpleModule(); + customModule.addSerializer(ByteBuffer.class, new HexStringCustomByteBufferSerializer()); + MAPPER.registerModule(customModule); + } + + public static ObjectMapper mapper() { + return MAPPER; + } + + public static Object parseDefaultFromJson(Type type, JsonNode jsonNode) { + if (jsonNode == null) { + return null; + } + + switch (type.typeId()) { + case BOOLEAN: + return jsonNode.booleanValue(); + case INTEGER: + return jsonNode.intValue(); + case LONG: + return jsonNode.longValue(); + case FLOAT: + return jsonNode.floatValue(); + case DOUBLE: + return jsonNode.doubleValue(); + case DECIMAL: + return jsonNode.decimalValue(); + case STRING: + case UUID: + return jsonNode.textValue(); + case DATE: + case TIME: + case TIMESTAMP: + return Literal.of(jsonNode.textValue()).to(type).value(); + case FIXED: + byte[] fixedBytes = BaseEncoding.base16().decode(jsonNode.textValue().toUpperCase(Locale.ROOT).replaceFirst( + "^0X", + "")); + return ByteBuffer.allocate(((Types.FixedType) type).length()).put(fixedBytes); + case BINARY: + byte[] binaryBytes = BaseEncoding.base16().decode(jsonNode.textValue().toUpperCase(Locale.ROOT).replaceFirst( + "^0X", "")); + return ByteBuffer.wrap(binaryBytes); + case LIST: + List<Object> defaultList = Lists.newArrayList(); + for (JsonNode element : jsonNode) { + defaultList.add(parseDefaultFromJson(type.asListType().elementType(), element)); + } + return defaultList; + case MAP: + Map<Object, Object> defaultMap = Maps.newHashMap(); + List<JsonNode> keysAndValues = StreamSupport + .stream(jsonNode.spliterator(), false) + .collect(Collectors.toList()); + JsonNode keys = keysAndValues.get(0); + JsonNode values = keysAndValues.get(1); + + List<JsonNode> keyList = Lists.newArrayList(keys.iterator()); + List<JsonNode> valueList = Lists.newArrayList(values.iterator()); + + for (int i = 0; i < keyList.size(); i++) { + defaultMap.put( + parseDefaultFromJson(type.asMapType().keyType(), keyList.get(i)), + parseDefaultFromJson(type.asMapType().valueType(), valueList.get(i))); + } + return defaultMap; + case STRUCT: + Map<Integer, Object> defaultStruct = Maps.newHashMap(); + for (Types.NestedField subField : type.asStructType().fields()) { + String fieldIdAsString = String.valueOf(subField.fieldId()); + Object value = jsonNode.has(fieldIdAsString) ? parseDefaultFromJson( + subField.type(), + jsonNode.get(fieldIdAsString)) : null; + if (value != null) { + defaultStruct.put(subField.fieldId(), value); + } + } + return defaultStruct; + default: + return null; + } + } + + public static JsonNode validateDefault(String name, Type type, JsonNode defaultValue) { + if (defaultValue != null && !isValidDefault(type, defaultValue)) { + throw new ValidationException("Invalid default value for field %s: %s not a %s", name, defaultValue, type); + } + return defaultValue; + } + + @SuppressWarnings("checkstyle:CyclomaticComplexity") Review Comment: We generally don't suppress this warning unless we are trying to avoid major code changes. Since this is all new, it is best to follow the recommendation. -- 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]
