Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion core/src/main/java/org/opensearch/sql/ast/tree/SPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SPath extends UnresolvedPlan {

@Nullable private final String outField;

private final String path;
@Nullable private final String path;

@Override
public UnresolvedPlan attach(UnresolvedPlan child) {
Expand All @@ -48,7 +48,20 @@ public <T, C> T accept(AbstractNodeVisitor<T, C> nodeVisitor, C context) {
return nodeVisitor.visitSpath(this, context);
}

/**
* Rewrites this spath node to an equivalent {@link Eval} node.
*
* <p>In path mode, rewrites to {@code eval output = json_extract(input, path)}. In auto-extract
* mode (path is null), rewrites to {@code eval output = json_extract_all(input)}.
*/
public Eval rewriteAsEval() {
if (path != null) {
return rewritePathMode();
}
return rewriteAutoExtractMode();
}

private Eval rewritePathMode() {
String outField = this.outField;
String unquotedPath = unquoteText(this.path);
if (outField == null) {
Expand All @@ -62,4 +75,12 @@ public Eval rewriteAsEval() {
AstDSL.function(
"json_extract", AstDSL.field(inField), AstDSL.stringLiteral(unquotedPath))));
}

private Eval rewriteAutoExtractMode() {
String output = (outField != null) ? outField : inField;
return AstDSL.eval(
child,
AstDSL.let(
AstDSL.field(output), AstDSL.function("json_extract_all", AstDSL.field(inField))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.opensearch.sql.expression.function.jsonUDF;

import static java.util.stream.Collectors.toMap;
import static org.opensearch.sql.calcite.utils.OpenSearchTypeFactory.TYPE_FACTORY;

import com.fasterxml.jackson.core.JsonFactory;
Expand Down Expand Up @@ -51,7 +52,7 @@ public SqlReturnTypeInference getReturnTypeInference() {
return ReturnTypes.explicit(
TYPE_FACTORY.createMapType(
TYPE_FACTORY.createSqlType(SqlTypeName.VARCHAR),
TYPE_FACTORY.createSqlType(SqlTypeName.ANY),
TYPE_FACTORY.createSqlType(SqlTypeName.VARCHAR),
true));
}

Expand All @@ -72,6 +73,11 @@ public Expression implement(
}
}

/**
* Evaluate the JSON extract-all function. Returns a {@code Map<String, String>} where keys are
* dot-separated JSON paths (with {@code {}} suffix for arrays) and all values are strings. Merged
* array values use {@code [a, b, c]} format.
*/
public static Object eval(Object... args) {
if (args.length < 1) {
return null;
Expand All @@ -82,7 +88,18 @@ public static Object eval(Object... args) {
return null;
}

return parseJson(jsonStr);
Map<String, Object> parsed = parseJson(jsonStr);
return parsed == null ? null : stringifyMap(parsed);
}

// TODO: JSON parsing dominates cost; consider stringify scalars in place during parsing
// to avoid this extra pass.
private static Map<String, String> stringifyMap(Map<String, Object> map) {
return map.entrySet().stream()
.collect(
toMap(
Map.Entry::getKey,
e -> String.valueOf(e.getValue()))); // relies on List.toString() for [a, b, c]
}

private static Map<String, Object> parseJson(String jsonStr) {
Expand Down Expand Up @@ -150,7 +167,7 @@ private static Map<String, Object> parseJson(String jsonStr) {
@SuppressWarnings("unchecked")
private static void appendValue(Map<String, Object> resultMap, String path, Object value) {
Object existingValue = resultMap.get(path);
if (existingValue == null) {
if (existingValue == null && !resultMap.containsKey(path)) { // key absent, not null value
resultMap.put(path, value);
} else if (existingValue instanceof List) {
((List<Object>) existingValue).add(value);
Expand Down
Loading
Loading