Skip to content

Commit 69bc91e

Browse files
committed
Add more test cases for json_query function
1 parent 5f4b492 commit 69bc91e

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

core/trino-main/src/test/java/io/trino/sql/query/TestJsonQueryFunction.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public void testNullInput()
425425
}
426426

427427
@Test
428-
public void testDescendantMemberAccessor()
428+
public void testDescendantMemberAccessorWithArrayWrapper()
429429
{
430430
assertThat(assertions.query(
431431
"""
@@ -434,9 +434,58 @@ SELECT json_query(
434434
'lax $..c'
435435
WITH ARRAY WRAPPER)
436436
"""))
437+
.matches("""
438+
VALUES cast('[[true,{"c":{"c":null}}],{"c":null},null]'AS varchar)
439+
""");
440+
441+
// `json_query` must return a single JSON item.
442+
// `WITH ARRAY WRAPPER` wraps `Diana` and `John` into a single JSON array, which can then be successfully returned.
443+
assertThat(assertions.query(
444+
"""
445+
SELECT json_query(
446+
'{"author":"Diana","id":{"value":1, "author":"John"},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
447+
'lax $..author'
448+
WITH ARRAY WRAPPER)
449+
"""))
437450
.matches(
438451
"""
439-
VALUES cast('[[true,{"c":{"c":null}}],{"c":null},null]'AS varchar)
452+
VALUES cast('["Diana","John"]'AS varchar)
440453
""");
441454
}
455+
456+
@Test
457+
public void testDescendantMemberAccessorWithoutArrayWrapper()
458+
{
459+
// Test `json_query` to find a non-existent member in the JSON.
460+
assertThat(assertions.query(
461+
"""
462+
select json_query(
463+
'{"id":{"value":1},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
464+
'lax $..author'
465+
omit quotes)
466+
"""))
467+
.matches("VALUES cast(NULL as varchar)");
468+
469+
// Test `json_query` to find a single descendant member in the JSON.
470+
assertThat(assertions.query(
471+
"""
472+
select json_query(
473+
'{"author":"Diana","id":{"value":1},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
474+
'lax $..author'
475+
omit quotes)
476+
"""))
477+
.matches("VALUES cast('Diana' as varchar)");
478+
479+
// Test `json_query` to find multiple descendant members in the JSON.
480+
// `json_query` must return a single JSON item.
481+
// `WITHOUT ARRAY WRAPPER` returns `NULL`, which is the default result in case of an error.
482+
assertThat(assertions.query(
483+
"""
484+
select json_query(
485+
'{"author":"Diana","id":{"value":1, "author":"John"},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
486+
'lax $..author'
487+
omit quotes)
488+
"""))
489+
.matches("VALUES cast(NULL as varchar)");
490+
}
442491
}

0 commit comments

Comments
 (0)