rdblue commented on code in PR #4871: URL: https://github.com/apache/iceberg/pull/4871#discussion_r883934727
########## core/src/main/java/org/apache/iceberg/DefaultValueParser.java: ########## @@ -0,0 +1,249 @@ +/* + * 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.databind.JsonNode; +import java.nio.ByteBuffer; +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.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; +import org.apache.iceberg.util.DateTimeUtil; + +public class DefaultValueParser { + private DefaultValueParser() { + } + + public static Object parseDefaultFromJson(Type type, JsonNode defaultValue) { + validateDefault(type, defaultValue); + + if (defaultValue == null) { + return null; + } + + switch (type.typeId()) { + case BOOLEAN: + return defaultValue.booleanValue(); + case INTEGER: + return defaultValue.intValue(); + case LONG: + return defaultValue.longValue(); + case FLOAT: + return defaultValue.floatValue(); + case DOUBLE: + return defaultValue.doubleValue(); + case DECIMAL: + return defaultValue.decimalValue(); + case STRING: + case UUID: + return defaultValue.textValue(); + case DATE: + case TIME: + case TIMESTAMP: + return Literal.of(defaultValue.textValue()).to(type).value(); + case FIXED: + byte[] fixedBytes = BaseEncoding.base16().decode(defaultValue.textValue().toUpperCase(Locale.ROOT).replaceFirst( + "^0X", + "")); + return ByteBuffer.allocate(((Types.FixedType) type).length()).put(fixedBytes); + case BINARY: + byte[] binaryBytes = + BaseEncoding.base16().decode(defaultValue.textValue().toUpperCase(Locale.ROOT).replaceFirst( + "^0X", "")); + return ByteBuffer.wrap(binaryBytes); + case LIST: + List<Object> defaultList = Lists.newArrayList(); + for (JsonNode element : defaultValue) { + defaultList.add(parseDefaultFromJson(type.asListType().elementType(), element)); + } + return defaultList; + case MAP: + Map<Object, Object> defaultMap = Maps.newHashMap(); + List<JsonNode> keysAndValues = StreamSupport + .stream(defaultValue.spliterator(), false) + .collect(Collectors.toList()); + JsonNode keys = keysAndValues.get(0); + JsonNode values = keysAndValues.get(1); Review Comment: According to the spec, the JSON node should be an object with two fields: `keys` and `values`. I think it would be much easier to validate that the node is an object and then read the fields, rather than trying to convert to a list. This needs to respect the names, not the order. -- 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]
