Skip to content

Commit

Permalink
Fix testes
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Carbonetto <[email protected]>
  • Loading branch information
acarbonetto committed Jan 9, 2025
1 parent d6c677e commit ef8d90e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static org.opensearch.sql.data.type.ExprCoreType.BOOLEAN;
import static org.opensearch.sql.data.type.ExprCoreType.STRING;
import static org.opensearch.sql.data.type.ExprCoreType.UNDEFINED;
import static org.opensearch.sql.expression.function.FunctionDSL.define;
import static org.opensearch.sql.expression.function.FunctionDSL.impl;
import static org.opensearch.sql.expression.function.FunctionDSL.nullMissingHandling;
Expand All @@ -21,11 +22,18 @@
public class JsonFunctions {
public void register(BuiltinFunctionRepository repository) {
repository.register(jsonValid());
repository.register(jsonFunction());
}

private DefaultFunctionResolver jsonValid() {
return define(
BuiltinFunctionName.JSON_VALID.getName(),
impl(nullMissingHandling(JsonUtils::isValidJson), BOOLEAN, STRING));
}

private DefaultFunctionResolver jsonFunction() {
return define(
BuiltinFunctionName.JSON.getName(),
impl(nullMissingHandling(JsonUtils::castJson), UNDEFINED, STRING));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.opensearch.sql.expression.function.FunctionDSL.implWithProperties;
import static org.opensearch.sql.expression.function.FunctionDSL.nullMissingHandling;
import static org.opensearch.sql.expression.function.FunctionDSL.nullMissingHandlingWithProperties;
import static org.opensearch.sql.utils.JsonUtils.castJson;

import java.util.Arrays;
import java.util.stream.Collectors;
Expand All @@ -44,6 +43,7 @@
import org.opensearch.sql.expression.function.BuiltinFunctionRepository;
import org.opensearch.sql.expression.function.DefaultFunctionResolver;
import org.opensearch.sql.expression.function.FunctionDSL;
import org.opensearch.sql.utils.JsonUtils;

@UtilityClass
public class TypeCastOperators {
Expand Down Expand Up @@ -189,7 +189,7 @@ private static DefaultFunctionResolver castToIp() {
private static DefaultFunctionResolver castToJson() {
return FunctionDSL.define(
BuiltinFunctionName.CAST_TO_JSON.getName(),
impl(nullMissingHandling((v) -> castJson(v.stringValue())), UNDEFINED, STRING));
impl(nullMissingHandling(JsonUtils::castJson), UNDEFINED, STRING));
}

private static DefaultFunctionResolver castToDate() {
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
Expand Up @@ -39,11 +39,11 @@ public static ExprValue isValidJson(ExprValue jsonExprValue) {
}

/** Converts a JSON encoded string to an Expression object. */
public static ExprValue castJson(String json) {
public static ExprValue castJson(ExprValue json) {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode;
try {
jsonNode = objectMapper.readTree(json);
jsonNode = objectMapper.readTree(json.stringValue());
} catch (JsonProcessingException e) {
final String errorFormat = "JSON string '%s' is not valid. Error details: %s";
throw new SemanticCheckException(String.format(errorFormat, json, e.getMessage()), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import static org.opensearch.sql.util.MatcherUtils.verifySchema;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -57,15 +59,15 @@ public void test_cast_json() throws IOException {
JSONObject result =
executeQuery(
String.format(
"source=%s | eval json=cast(json_string to json) | fields json",
"source=%s | where json_valid(json_string) | eval casted=cast(json_string as json) | fields test_name, casted",
TEST_INDEX_JSON_TEST));
verifySchema(result, schema("test_name", null, "string"));
verifySchema(result, schema("test_name", null, "string"), schema("casted", null, "undefined"));
verifyDataRows(
result,
rows("json object"),
rows("json array"),
rows("json scalar string"),
rows("json empty string"));
rows("json object", Map.of("a", "1", "b", "2")),
rows("json array", List.of(1,2,3,4)),
rows("json scalar string", "abc"),
rows("json empty string", null));
}

@Test
Expand All @@ -75,8 +77,13 @@ public void test_json() throws IOException {
result =
executeQuery(
String.format(
"source=%s | eval json=json(json_string) | fields json", TEST_INDEX_JSON_TEST));
verifySchema(result, schema("test_name", null, "string"));
verifyDataRows(result, rows("json invalid object"));
"source=%s | where json_valid(json_string) | eval casted=json(json_string) | fields test_name, casted", TEST_INDEX_JSON_TEST));
verifySchema(result, schema("test_name", null, "string"), schema("casted", null, "undefined"));
verifyDataRows(
result,
rows("json object", Map.of("a", "1", "b", "2")),
rows("json array", List.of(1,2,3,4)),
rows("json scalar string", "abc"),
rows("json empty string", null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "keyword"
},
"json_string": {
"type": "text"
"type": "keyword"
}
}
}
Expand Down

This file was deleted.

0 comments on commit ef8d90e

Please sign in to comment.