deniskuzZ commented on code in PR #6069: URL: https://github.com/apache/hive/pull/6069#discussion_r2341022374
########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFParseJson.java: ########## @@ -0,0 +1,603 @@ +/* + * 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.hadoop.hive.ql.udf.generic; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException; +import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; + +@Description(name = "parse_json", value = "_FUNC_(json_string) - Parses a JSON string into a VARIANT type", extended = """ + Example: + > SELECT _FUNC_('{"a":5}'); + {"a":5}""") +public class GenericUDFParseJson extends GenericUDF { + private PrimitiveObjectInspector inputOI; + + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { + if (arguments.length != 1) { + throw new UDFArgumentLengthException("parse_json requires one argument"); + } + if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE + || ((PrimitiveObjectInspector) arguments[0]).getPrimitiveCategory() + != PrimitiveObjectInspector.PrimitiveCategory.STRING) { + throw new UDFArgumentTypeException(0, "Only string input is accepted"); + } + inputOI = (PrimitiveObjectInspector) arguments[0]; + + // Return a Variant OI + return ObjectInspectorFactory.getVariantObjectInspector(); + } + + @Override + public Object evaluate(DeferredObject[] arguments) throws HiveException { + if (arguments[0] == null || arguments[0].get() == null) { + return null; + } + String json = inputOI.getPrimitiveJavaObject(arguments[0].get()).toString(); + + try { + return VariantJsonEncoder.fromJson(json); + } catch (IOException e) { + throw new HiveException("Failed to parse JSON: " + json, e); + } + } + + @Override + public String getDisplayString(String[] children) { + return "parse_json(" + children[0] + ")"; + } + + static final class VariantJsonEncoder { + private static final ObjectMapper mapper = new ObjectMapper(); + + private static final int MAX_FIELD_ID_SIZE = 4; + private static final int MAX_FIELD_OFFSET_SIZE = 4; + + public static List<Object> fromJson(String json) throws IOException { + JsonNode root = mapper.readTree(json); + + // 1) build sorted dictionary of keys + List<String> dictionary = collectDictionary(root); + + // 2) build dict index map + Map<String, Integer> dictIndex = HashMap.newHashMap(dictionary.size()); + for (int i = 0; i < dictionary.size(); i++) { + dictIndex.put(dictionary.get(i), i); + } + + // 3) encode metadata and value + byte[] metadataBytes = encodeMetadata(dictionary); + byte[] valueBytes = encodeValue(root, dictIndex); + + return Arrays.asList(metadataBytes, valueBytes); + } + + private static List<String> collectDictionary(JsonNode node) { + Set<String> keys = new TreeSet<>(Comparator.naturalOrder()); Review Comment: why do we need this? -- 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: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org