Skip to content

Commit

Permalink
fix code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Kenrick Yap <[email protected]>
  • Loading branch information
14yapkc1 committed Jan 30, 2025
1 parent 6678be4 commit e57fa21
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
4 changes: 0 additions & 4 deletions core/src/main/java/org/opensearch/sql/expression/DSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -985,10 +985,6 @@ public static FunctionExpression utc_timestamp(
return compile(functionProperties, BuiltinFunctionName.UTC_TIMESTAMP, args);
}

public static FunctionExpression json_function(Expression value) {
return compile(FunctionProperties.None, BuiltinFunctionName.JSON, value);
}

@SuppressWarnings("unchecked")
private static <T extends FunctionImplementation> T compile(
FunctionProperties functionProperties, BuiltinFunctionName bfn, Expression... args) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/opensearch/sql/utils/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.opensearch.sql.utils;

import static org.opensearch.sql.data.model.ExprValueUtils.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -23,8 +25,6 @@
import org.opensearch.sql.data.model.ExprValueUtils;
import org.opensearch.sql.exception.SemanticCheckException;

import static org.opensearch.sql.data.model.ExprValueUtils.*;

@UtilityClass
public class JsonUtils {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,43 +250,50 @@ void json_extract_returns_null() {
"false",
"");

jsonStrings.forEach(
str -> execute_extract_json(LITERAL_NULL, str, "$.a.path_not_found_key")
);
jsonStrings.forEach(str -> execute_extract_json(LITERAL_NULL, str, "$.a.path_not_found_key"));

// null json
assertEquals(LITERAL_NULL, DSL.jsonExtract(DSL.literal(LITERAL_NULL), DSL.literal(new ExprStringValue("$.a"))).valueOf());
assertEquals(
LITERAL_NULL,
DSL.jsonExtract(DSL.literal(LITERAL_NULL), DSL.literal(new ExprStringValue("$.a")))
.valueOf());

// missing json
assertEquals(LITERAL_MISSING, DSL.jsonExtract(DSL.literal(LITERAL_MISSING), DSL.literal(new ExprStringValue("$.a"))).valueOf());
assertEquals(
LITERAL_MISSING,
DSL.jsonExtract(DSL.literal(LITERAL_MISSING), DSL.literal(new ExprStringValue("$.a")))
.valueOf());
}

@Test
void json_extract_throws_SemanticCheckException() {
// invalid path
SemanticCheckException invalidPathError = assertThrows(
SemanticCheckException.class,
() ->
DSL.jsonExtract(
DSL.literal(new ExprStringValue("{\"a\":1}")),
DSL.literal(new ExprStringValue("$a")))
.valueOf());
SemanticCheckException invalidPathError =
assertThrows(
SemanticCheckException.class,
() ->
DSL.jsonExtract(
DSL.literal(new ExprStringValue("{\"a\":1}")),
DSL.literal(new ExprStringValue("$a")))
.valueOf());
assertEquals(
"JSON path '\"$a\"' is not valid. Error details: Illegal character at position 1 expected '.' or '['",
invalidPathError.getMessage());

"JSON path '\"$a\"' is not valid. Error details: Illegal character at position 1 expected"
+ " '.' or '['",
invalidPathError.getMessage());

// invalid json
SemanticCheckException invalidJsonError = assertThrows(
SemanticCheckException.class,
() ->
DSL.jsonExtract(
DSL.literal(new ExprStringValue("{\"invalid\":\"json\", \"string\"}")),
DSL.literal(new ExprStringValue("$.a")))
.valueOf());
SemanticCheckException invalidJsonError =
assertThrows(
SemanticCheckException.class,
() ->
DSL.jsonExtract(
DSL.literal(new ExprStringValue("{\"invalid\":\"json\", \"string\"}")),
DSL.literal(new ExprStringValue("$.a")))
.valueOf());
assertEquals(
"JSON string '\"{\"invalid\":\"json\", \"string\"}\"' is not valid. Error details: net.minidev.json.parser.ParseException: Unexpected character (}) at position 26.",
invalidJsonError.getMessage());
"JSON string '\"{\"invalid\":\"json\", \"string\"}\"' is not valid. Error details:"
+ " net.minidev.json.parser.ParseException: Unexpected character (}) at position 26.",
invalidJsonError.getMessage());
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions docs/user/ppl/functions/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ ____________
Description
>>>>>>>>>>>

Usage: `json_extract(doc, path)` Extracts a json value or scalar from a json document based on the path specified.
Usage: `json_extract(doc, path)` Extracts a JSON value from a json document based on the path specified.

Argument type: STRING, STRING

Return type: BOOLEAN/DOUBLE/INTEGER/NULL/STRUCT/ARRAY
Return type: STRING/BOOLEAN/DOUBLE/INTEGER/NULL/STRUCT/ARRAY

- Returns a JSON array if path points to multiple results (e.g. $.a[*]) or if the path points to an array.
- Return null if path is not valid is MISSING or NULL.
Expand Down

0 comments on commit e57fa21

Please sign in to comment.