Skip to content

Commit

Permalink
Update the test-case
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Kwok <[email protected]>
  • Loading branch information
andy-k-improving committed Feb 7, 2025
1 parent 701e073 commit c52db67
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ private DefaultFunctionResolver jsonExtract() {
private DefaultFunctionResolver jsonSet() {
return define(
BuiltinFunctionName.JSON_SET.getName(),

// Numeric types
impl(nullMissingHandling(JsonUtils::setJson), UNDEFINED, STRING, STRING, BYTE),
impl(nullMissingHandling(JsonUtils::setJson), UNDEFINED, STRING, STRING, SHORT),
impl(nullMissingHandling(JsonUtils::setJson), UNDEFINED, STRING, STRING, INTEGER),
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/org/opensearch/sql/utils/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,15 @@ private static ExprValue processJsonNode(JsonNode jsonNode) {
return LITERAL_NULL;
}

/** Converts a JSON encoded string to an Expression object. */
/**
* Perform an upsert operation against the incoming jsonString value with provided jsonPath and
* value.
*
* @param json jsonObject in String format.
* @param path upsert reference in the form of JsonPath.
* @param valueToInsert value to be added
* @return JsonString after the upsert operation.
*/
public static ExprValue setJson(ExprValue json, ExprValue path, ExprValue valueToInsert) {

String jsonString = json.stringValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,33 +295,21 @@ void json_set_InsertArray() {
assertEquals("{\"test\":[\"Alice\",\"Ben\"]}", functionExpression.valueOf().stringValue());
}

@Test
void json_set_insert_new_property_nested() {
FunctionExpression functionExpression =
DSL.jsonSet(
DSL.literal("{\"members\":[{\"name\":\"alice\"}]}"),
DSL.literal("$.members[0].age.innerAge"),
DSL.literal("18"));
assertEquals(
"{\"members\":[{\"name\":\"alice\",\"age\":{\"innerAge\":\"18\"}}]}",
functionExpression.valueOf().stringValue());
}

@Test
void json_set_insert_invalid_path() {
FunctionExpression functionExpression =
DSL.jsonSet(
DSL.literal("{\"members\":[{\"name\":\"alice\"}]}"),
DSL.literal("$$$$$$$$$"),
DSL.literal("18"));
assertThrows(SemanticCheckException.class, () -> functionExpression.valueOf());
assertThrows(SemanticCheckException.class, functionExpression::valueOf);
}

@Test
void json_set_insert_invalid_jsonObject() {
FunctionExpression functionExpression =
DSL.jsonSet(DSL.literal("[xxxx}}}}}"), DSL.literal("$.test"), DSL.literal("18"));
assertThrows(SemanticCheckException.class, () -> functionExpression.valueOf());
assertThrows(SemanticCheckException.class, functionExpression::valueOf);
}

@Test
Expand Down

0 comments on commit c52db67

Please sign in to comment.