Skip to content

Commit 47d6da0

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

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

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

Lines changed: 60 additions & 1 deletion
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 testDescendantMemberWithArrayWrapperAccessor()
429429
{
430430
assertThat(assertions.query(
431431
"""
@@ -438,5 +438,64 @@ SELECT json_query(
438438
"""
439439
VALUES cast('[[true,{"c":{"c":null}}],{"c":null},null]'AS varchar)
440440
""");
441+
442+
// `json_query` must return a single JSON item.
443+
// `WITH ARRAY WRAPPER` wraps `Diana` and `John` into a single JSON array, which can then be successfully returned.
444+
assertThat(assertions.query(
445+
"""
446+
SELECT json_query(
447+
'{"author":"Diana","id":{"value":1, "author":"John"},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
448+
'lax $..author'
449+
WITH ARRAY WRAPPER)
450+
"""))
451+
.matches(
452+
"""
453+
VALUES cast('["Diana","John"]'AS varchar)
454+
""");
455+
}
456+
457+
@Test
458+
public void testDescendantMemberWithoutArrayWrapperAccessor()
459+
{
460+
// Test `json_query` to find a non-descendant member in the JSON.
461+
assertThat(assertions.query(
462+
"""
463+
select json_query(
464+
'{"id":{"value":1},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
465+
'lax $..author'
466+
omit quotes)
467+
"""))
468+
.matches(
469+
"""
470+
VALUES cast(NULL as varchar)
471+
""");
472+
473+
// Test `json_query` to find a single descendant member in the JSON.
474+
assertThat(assertions.query(
475+
"""
476+
select json_query(
477+
'{"author":"Diana","id":{"value":1},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
478+
'lax $..author'
479+
omit quotes)
480+
"""))
481+
.matches(
482+
"""
483+
VALUES cast('Diana' as varchar)
484+
""");
485+
486+
// Test `json_query` to find multiple descendant members in the JSON.
487+
// `json_query` must return a single JSON item.
488+
// `WITHOUT ARRAY WRAPPER` returns `NULL`, which is the default result in case of an error.
489+
assertThat(assertions.query(
490+
"""
491+
select json_query(
492+
'{"author":"Diana","id":{"value":1, "author":"John"},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
493+
'lax $..author'
494+
omit quotes)
495+
"""))
496+
.matches(
497+
"""
498+
VALUES cast(NULL as varchar)
499+
""");
441500
}
442501
}

0 commit comments

Comments
 (0)