Skip to content

Commit dd49ec8

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

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,50 @@ SELECT json_query(
439439
VALUES cast('[[true,{"c":{"c":null}}],{"c":null},null]'AS varchar)
440440
""");
441441
}
442+
443+
@Test
444+
public void testDescendantMemberWithoutArrayWrapperAccessor()
445+
{
446+
// Test `json_query` to read the result of a non-descendant member in the JSON.
447+
assertThat(assertions.query(
448+
"""
449+
select json_query(
450+
'{"id":{"value":1},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
451+
'lax $..author'
452+
omit quotes)
453+
"""))
454+
.matches(
455+
"""
456+
VALUES cast(NULL as varchar)
457+
""");
458+
459+
// Test `json_query` to read the result of a single descendant member in the JSON.
460+
assertThat(assertions.query(
461+
"""
462+
select json_query(
463+
'{"author":"Diana","id":{"value":1},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
464+
'lax $..author'
465+
omit quotes)
466+
"""))
467+
.matches(
468+
"""
469+
VALUES cast('Diana' as varchar)
470+
""");
471+
472+
// Test `json_query` to read the result of multiple descendant members in the JSON.
473+
// `json_query` must return a single JSON item.
474+
// `WITH ARRAY WRAPPER` wraps `Diana` and `John` into a single JSON array, which can then be successfully returned.
475+
// `WITHOUT ARRAY WRAPPER` returns `NULL`, which is the default result in case of an error.
476+
assertThat(assertions.query(
477+
"""
478+
select json_query(
479+
'{"author":"Diana","id":{"value":1, "author":"John"},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
480+
'lax $..author'
481+
omit quotes)
482+
"""))
483+
.matches(
484+
"""
485+
VALUES cast(NULL as varchar)
486+
""");
487+
}
442488
}

0 commit comments

Comments
 (0)