rzhang10 commented on code in PR #4871:
URL: https://github.com/apache/iceberg/pull/4871#discussion_r918253590


##########
core/src/main/java/org/apache/iceberg/DefaultValueParser.java:
##########
@@ -0,0 +1,348 @@
+/*
+ * 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.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.math.BigDecimal;
+import java.nio.ByteBuffer;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.UUID;
+import org.apache.iceberg.data.GenericRecord;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+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.ByteBuffers;
+import org.apache.iceberg.util.DateTimeUtil;
+import org.apache.iceberg.util.JsonUtil;
+
+public class DefaultValueParser {
+  private DefaultValueParser() {
+  }
+
+  private static final String KEYS = "keys";
+
+  private static final String VALUES = "values";
+
+  @SuppressWarnings("checkstyle:CyclomaticComplexity")
+  public static Object fromJson(Type type, JsonNode defaultValue) {
+
+    if (defaultValue == null || defaultValue.isNull()) {
+      return null;
+    }
+
+    switch (type.typeId()) {
+      case BOOLEAN:
+        Preconditions.checkArgument(defaultValue.isBoolean(),
+            "Cannot parse default as a %s value: %s", type, defaultValue);
+        return defaultValue.booleanValue();
+      case INTEGER:
+        Preconditions.checkArgument(defaultValue.isIntegralNumber() && 
defaultValue.canConvertToInt(),
+            "Cannot parse default as a %s value: %s", type, defaultValue);
+        return defaultValue.intValue();
+      case LONG:
+        Preconditions.checkArgument(defaultValue.isIntegralNumber() && 
defaultValue.canConvertToLong(),
+            "Cannot parse default as a %s value: %s", type, defaultValue);
+        return defaultValue.longValue();
+      case FLOAT:
+        Preconditions.checkArgument(defaultValue.isFloatingPointNumber(),
+            "Cannot parse default as a %s value: %s", type, defaultValue);
+        return defaultValue.floatValue();
+      case DOUBLE:
+        Preconditions.checkArgument(defaultValue.isFloatingPointNumber(),
+            "Cannot parse default as a %s value: %s", type, defaultValue);
+        return defaultValue.doubleValue();
+      case DECIMAL:
+        Preconditions.checkArgument(defaultValue.isNumber(),
+            "Cannot parse default as a %s value: %s", type, defaultValue);
+        BigDecimal retDecimal = defaultValue.decimalValue();
+        Preconditions.checkArgument(
+            retDecimal.scale() == ((Types.DecimalType) type).scale(),
+            "Cannot parse default as a %s value: %s, the scale doesn't match", 
type, defaultValue);
+        return retDecimal;
+      case STRING:
+        Preconditions.checkArgument(defaultValue.isTextual(),
+            "Cannot parse default as a %s value: %s", type, defaultValue);
+        return defaultValue.textValue();
+      case UUID:
+        Preconditions.checkArgument(defaultValue.isTextual(),
+            "Cannot parse default as a %s value: %s", type, defaultValue);
+        UUID uuid;
+        try {
+          uuid = UUID.fromString(defaultValue.textValue());
+        } catch (IllegalArgumentException e) {
+          throw new IllegalArgumentException(String.format("Cannot parse 
default as a %s value: %s", type,
+              defaultValue), e);
+        }
+        return uuid;

Review Comment:
   Sorry, I forget this, I just adds testing the length to be 36.



-- 
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]

Reply via email to