generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 180
Add JSON_EXTRACT_ALL internal function for Calcite PPL #4489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
218 changes: 218 additions & 0 deletions
218
.../main/java/org/opensearch/sql/expression/function/jsonUDF/JsonExtractAllFunctionImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.expression.function.jsonUDF; | ||
|
|
||
| import static org.opensearch.sql.calcite.utils.OpenSearchTypeFactory.TYPE_FACTORY; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonFactory; | ||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.core.JsonToken; | ||
| import java.io.IOException; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Stack; | ||
| import org.apache.calcite.adapter.enumerable.NotNullImplementor; | ||
| import org.apache.calcite.adapter.enumerable.NullPolicy; | ||
| import org.apache.calcite.adapter.enumerable.RexImpTable; | ||
| import org.apache.calcite.adapter.enumerable.RexToLixTranslator; | ||
| import org.apache.calcite.linq4j.tree.Expression; | ||
| import org.apache.calcite.linq4j.tree.Types; | ||
| import org.apache.calcite.rex.RexCall; | ||
| import org.apache.calcite.schema.impl.ScalarFunctionImpl; | ||
| import org.apache.calcite.sql.type.OperandTypes; | ||
| import org.apache.calcite.sql.type.ReturnTypes; | ||
| import org.apache.calcite.sql.type.SqlReturnTypeInference; | ||
| import org.apache.calcite.sql.type.SqlTypeFamily; | ||
| import org.apache.calcite.sql.type.SqlTypeName; | ||
| import org.opensearch.sql.expression.function.ImplementorUDF; | ||
| import org.opensearch.sql.expression.function.UDFOperandMetadata; | ||
|
|
||
| /** | ||
| * UDF which extract all the fields from JSON to a MAP. Items are collected from input JSON and | ||
| * stored with the key of their path in the JSON. This UDF is designed to be used for `spath` | ||
| * command without path param. See {@ref JsonExtractAllFunctionImplTest} for the detailed spec. | ||
| */ | ||
| public class JsonExtractAllFunctionImpl extends ImplementorUDF { | ||
| private static final String ARRAY_SUFFIX = "{}"; | ||
| private static final JsonFactory JSON_FACTORY = new JsonFactory(); | ||
|
|
||
| public JsonExtractAllFunctionImpl() { | ||
| super(new JsonExtractAllImplementor(), NullPolicy.ANY); | ||
| } | ||
|
|
||
| @Override | ||
| public SqlReturnTypeInference getReturnTypeInference() { | ||
| return ReturnTypes.explicit( | ||
| TYPE_FACTORY.createMapType( | ||
| TYPE_FACTORY.createSqlType(SqlTypeName.VARCHAR), | ||
| TYPE_FACTORY.createSqlType(SqlTypeName.ANY), | ||
| true)); | ||
| } | ||
|
|
||
| @Override | ||
| public UDFOperandMetadata getOperandMetadata() { | ||
| return UDFOperandMetadata.wrap(OperandTypes.family(SqlTypeFamily.STRING)); | ||
| } | ||
|
|
||
| public static class JsonExtractAllImplementor implements NotNullImplementor { | ||
| @Override | ||
| public Expression implement( | ||
| RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) { | ||
| ScalarFunctionImpl function = | ||
| (ScalarFunctionImpl) | ||
| ScalarFunctionImpl.create( | ||
| Types.lookupMethod(JsonExtractAllFunctionImpl.class, "eval", Object[].class)); | ||
| return function.getImplementor().implement(translator, call, RexImpTable.NullAs.NULL); | ||
| } | ||
| } | ||
|
|
||
| public static Object eval(Object... args) { | ||
| if (args.length < 1) { | ||
| return null; | ||
| } | ||
|
|
||
| String jsonStr = (String) args[0]; | ||
| if (jsonStr == null || jsonStr.trim().isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| return parseJson(jsonStr); | ||
| } | ||
|
|
||
| private static Map<String, Object> parseJson(String jsonStr) { | ||
| Map<String, Object> resultMap = new HashMap<>(); | ||
| Stack<String> pathStack = new Stack<>(); | ||
|
|
||
| try (JsonParser parser = JSON_FACTORY.createParser(jsonStr)) { | ||
| JsonToken token; | ||
|
|
||
| while ((token = parser.nextToken()) != null) { | ||
| switch (token) { | ||
| case START_OBJECT: | ||
| break; | ||
|
|
||
| case END_OBJECT: | ||
| if (!pathStack.isEmpty() && !isInArray(pathStack)) { | ||
| pathStack.pop(); | ||
| } | ||
| break; | ||
|
|
||
| case START_ARRAY: | ||
| pathStack.push(ARRAY_SUFFIX); | ||
| break; | ||
|
|
||
| case END_ARRAY: | ||
| pathStack.pop(); | ||
| if (!pathStack.isEmpty() && !isInArray(pathStack)) { | ||
| pathStack.pop(); | ||
| } | ||
| break; | ||
|
|
||
| case FIELD_NAME: | ||
| String fieldName = parser.currentName(); | ||
| pathStack.push(fieldName); | ||
| break; | ||
|
|
||
| case VALUE_STRING: | ||
| case VALUE_NUMBER_INT: | ||
| case VALUE_NUMBER_FLOAT: | ||
| case VALUE_TRUE: | ||
| case VALUE_FALSE: | ||
| case VALUE_NULL: | ||
| if (pathStack.isEmpty()) { | ||
| // ignore top level value | ||
| return null; | ||
| } | ||
|
|
||
| appendValue(resultMap, buildPath(pathStack), extractValue(parser, token)); | ||
|
|
||
| if (!isInArray(pathStack)) { | ||
| pathStack.pop(); | ||
| } | ||
| break; | ||
| default: | ||
| // Skip other tokens | ||
| break; | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| // ignore exception, and current result will be returned | ||
| } | ||
| return resultMap; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static void appendValue(Map<String, Object> resultMap, String path, Object value) { | ||
| Object existingValue = resultMap.get(path); | ||
| if (existingValue == null) { | ||
| resultMap.put(path, value); | ||
| } else if (existingValue instanceof List) { | ||
| ((List<Object>) existingValue).add(value); | ||
| } else { | ||
| resultMap.put(path, list(existingValue, value)); | ||
dai-chen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| private static List<Object> list(Object... args) { | ||
| List<Object> result = new LinkedList<>(); | ||
| for (Object arg : args) { | ||
| result.add(arg); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static boolean isInArray(List<String> path) { | ||
| return path.size() >= 1 && path.getLast().equals(ARRAY_SUFFIX); | ||
| } | ||
|
|
||
| private static Object extractValue(JsonParser parser, JsonToken token) throws IOException { | ||
| switch (token) { | ||
| case VALUE_STRING: | ||
| return parser.getValueAsString(); | ||
| case VALUE_NUMBER_INT: | ||
| return getIntValue(parser); | ||
| case VALUE_NUMBER_FLOAT: | ||
| return parser.getDoubleValue(); | ||
| case VALUE_TRUE: | ||
| return true; | ||
| case VALUE_FALSE: | ||
| return false; | ||
| case VALUE_NULL: | ||
| return null; | ||
| default: | ||
| return parser.getValueAsString(); | ||
| } | ||
| } | ||
|
|
||
| private static Object getIntValue(JsonParser parser) throws IOException { | ||
| if (parser.getNumberType() == JsonParser.NumberType.INT) { | ||
| return parser.getIntValue(); | ||
| } else if (parser.getNumberType() == JsonParser.NumberType.LONG) { | ||
| return parser.getLongValue(); | ||
| } else { | ||
| // store as double in case of BIG_INTEGER (exceed LONG precision) | ||
| return parser.getBigIntegerValue().doubleValue(); | ||
| } | ||
| } | ||
|
|
||
| private static String buildPath(Collection<String> pathStack) { | ||
| StringBuilder builder = new StringBuilder(); | ||
| for (String path : pathStack) { | ||
| if (ARRAY_SUFFIX.equals(path)) { | ||
| builder.append(ARRAY_SUFFIX); | ||
| } else if (!path.isEmpty()) { | ||
| if (!builder.isEmpty()) { | ||
| builder.append("."); | ||
| } | ||
| builder.append(path); | ||
| } | ||
| } | ||
| return builder.toString(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.