Skip to content

Commit 3662661

Browse files
committed
add more IT
Signed-off-by: xinyual <xinyual@amazon.com>
1 parent a2cf460 commit 3662661

5 files changed

Lines changed: 46 additions & 8 deletions

File tree

core/src/main/java/org/opensearch/sql/expression/function/jsonUDF/JsonAppendFunctionImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ public static Object eval(Object... args) throws JsonProcessingException {
6868
for (String expandedPath : expandedPaths) {
6969
expands.add(
7070
expandedPath
71-
+ ".meaninglessKey"); // We add meaningless Key since calcite json_insert can only
72-
// insert when the path point to null
71+
+ MEANING_LESS_KEY_FOR_APPEND_AND_EXTEND); // We add meaningless Key since calcite
72+
// json_insert can only
73+
// insert when the path point to null see:
74+
// https://github.com/apache/calcite/blob/d96709c4cc7ca962601317d0a70914ad95e306e1/core/src/main/java/org/apache/calcite/runtime/JsonFunctions.java#L737
7375
expands.add(keys.get(i + 1));
7476
}
7577
}

core/src/main/java/org/opensearch/sql/expression/function/jsonUDF/JsonExtendFunctionImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,18 @@ public static Object eval(Object... args) throws JsonProcessingException {
6767
}
6868
} else if (value instanceof String stringValue) {
6969
try {
70-
List<Object> targetValues = gson.fromJson(stringValue, List.class);
70+
List<Object> targetValues =
71+
gson.fromJson(stringValue, List.class); // We first try to extend it as an array
7172
for (Object targetValue : targetValues) {
72-
expands.add(expandedPath + ".meaninglessKey");
73+
expands.add(expandedPath + MEANING_LESS_KEY_FOR_APPEND_AND_EXTEND);
7374
expands.add(targetValue);
7475
}
7576
} catch (Exception e) {
76-
expands.add(expandedPath + ".meaninglessKey");
77+
expands.add(expandedPath + MEANING_LESS_KEY_FOR_APPEND_AND_EXTEND);
7778
expands.add(value);
7879
}
7980
} else {
80-
expands.add(expandedPath + ".meaninglessKey");
81+
expands.add(expandedPath + MEANING_LESS_KEY_FOR_APPEND_AND_EXTEND);
8182
expands.add(value);
8283
}
8384
}

core/src/main/java/org/opensearch/sql/expression/function/jsonUDF/JsonUtils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
public class JsonUtils {
1515
static ObjectMapper objectMapper = new ObjectMapper();
1616
public static Gson gson = new Gson();
17+
public static String MEANING_LESS_KEY_FOR_APPEND_AND_EXTEND = ".meaningless_key";
1718

1819
/**
1920
* @param input candidate json path like a.b{}.c{2}
@@ -53,6 +54,12 @@ public static String convertToJsonPath(String input) {
5354
return sb.toString();
5455
}
5556

57+
/**
58+
* Transfer the object input to json node
59+
*
60+
* @param input
61+
* @return
62+
*/
5663
public static JsonNode convertInputToJsonNode(Object input) {
5764
try {
5865
JsonNode root;

integ-test/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,8 @@ integTest {
448448

449449
dependsOn ':opensearch-sql-plugin:bundlePlugin'
450450
if(getOSFamilyType() != "windows") {
451-
//dependsOn startPrometheus
452-
//finalizedBy stopPrometheus
451+
dependsOn startPrometheus
452+
finalizedBy stopPrometheus
453453
}
454454

455455
// enable calcite codegen in IT

integ-test/src/test/java/org/opensearch/sql/calcite/standalone/CalcitePPLJsonBuiltinFunctionIT.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,34 @@ public void testJsonSet() {
247247
verifyDataRows(actual, rows("{\"a\":[{\"b\":\"3\"},{\"b\":\"3\"}]}"));
248248
}
249249

250+
@Test
251+
public void testJsonSetWithWrongPath() {
252+
JSONObject actual =
253+
executeQuery(
254+
String.format(
255+
"source=%s | eval a =json_set('{\"a\":[{\"b\":1},{\"b\":2}]}', 'a{}.b.d', '3')|"
256+
+ " fields a | head 1",
257+
TEST_INDEX_PEOPLE2));
258+
259+
verifySchema(actual, schema("a", "string"));
260+
261+
verifyDataRows(actual, rows("{\"a\":[{\"b\":1},{\"b\":2}]}"));
262+
}
263+
264+
@Test
265+
public void testJsonSetPartialSet() {
266+
JSONObject actual =
267+
executeQuery(
268+
String.format(
269+
"source=%s | eval a =json_set('{\"a\":[{\"b\":1},{\"b\":{\"c\": 2}}]}', 'a{}.b.c',"
270+
+ " '3')| fields a | head 1",
271+
TEST_INDEX_PEOPLE2));
272+
273+
verifySchema(actual, schema("a", "string"));
274+
275+
verifyDataRows(actual, rows("{\"a\":[{\"b\":1},{\"b\":{\"c\":\"3\"}}]}"));
276+
}
277+
250278
@Test
251279
public void testJsonDelete() {
252280
JSONObject actual =

0 commit comments

Comments
 (0)