Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2bd5652

Browse files
author
Nikita Glukhov
committed
Add WIP extensions tests for json
1 parent a92bc84 commit 2bd5652

File tree

2 files changed

+358
-0
lines changed

2 files changed

+358
-0
lines changed

src/test/regress/expected/json_jsonpath.out

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,3 +1920,284 @@ select * from _jsonpath_query(json '{"a": 1, "b": 2}', 'lax $["b", "c", "b", "a"
19201920
{"a": 1, "b": 2}
19211921
(4 rows)
19221922

1923+
-- extension: outer item reference (@N)
1924+
select _jsonpath_query(json '[2,4,1,5,3]', '$[*] ? (!exists($[*] ? (@ < @1)))');
1925+
_jsonpath_query
1926+
-----------------
1927+
1
1928+
(1 row)
1929+
1930+
select _jsonpath_query(json '[2,4,1,5,3]', '$.map(@ + @1[0])');
1931+
_jsonpath_query
1932+
-----------------
1933+
[4, 6, 3, 7, 5]
1934+
(1 row)
1935+
1936+
-- the first @1 and @2 reference array, the second @1 -- current mapped array element
1937+
select _jsonpath_query(json '[2,4,1,5,3]', '$.map(@ + @1[@1 - @2[2]])');
1938+
_jsonpath_query
1939+
-----------------
1940+
[6, 9, 3, 8, 4]
1941+
(1 row)
1942+
1943+
select _jsonpath_query(json '[[2,4,1,5,3]]', '$.map(@.reduce($1 + $2 + @2[0][2] + @1[3]))');
1944+
_jsonpath_query
1945+
-----------------
1946+
[39]
1947+
(1 row)
1948+
1949+
-- extension: including subpaths into result
1950+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.(a[*].b)');
1951+
_jsonpath_query
1952+
-----------------------------
1953+
{"a": [{"b": 1}, {"b": 2}]}
1954+
(1 row)
1955+
1956+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.(a[*]).b');
1957+
_jsonpath_query
1958+
-----------------
1959+
{"a": [1, 2]}
1960+
(1 row)
1961+
1962+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.a.([*].b)');
1963+
_jsonpath_query
1964+
----------------------
1965+
[{"b": 1}, {"b": 2}]
1966+
(1 row)
1967+
1968+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.(a)[*].b');
1969+
_jsonpath_query
1970+
-----------------
1971+
{"a": 1}
1972+
{"a": 2}
1973+
(2 rows)
1974+
1975+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.a[*].(b)');
1976+
_jsonpath_query
1977+
-----------------
1978+
{"b": 1}
1979+
{"b": 2}
1980+
(2 rows)
1981+
1982+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.(a)[*].(b)');
1983+
_jsonpath_query
1984+
-----------------
1985+
{"a": {"b": 1}}
1986+
{"a": {"b": 2}}
1987+
(2 rows)
1988+
1989+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.(a.[0 to 1].b)');
1990+
_jsonpath_query
1991+
-----------------------------
1992+
{"a": [{"b": 1}, {"b": 2}]}
1993+
(1 row)
1994+
1995+
-- extension: custom operators and type casts
1996+
select _jsonpath_query(json '"aaa"', '$::text || "bbb"::text || $::text');
1997+
_jsonpath_query
1998+
-----------------
1999+
"aaabbbaaa"
2000+
(1 row)
2001+
2002+
select _jsonpath_query(json '"aaa"', '$::text || "bbb" || $');
2003+
_jsonpath_query
2004+
---------------------
2005+
"aaa\"bbb\"\"aaa\""
2006+
(1 row)
2007+
2008+
select _jsonpath_query(json '[null, true, 1, "aaa", {"a": 1}, [1, 2]]', '$.map(@::text || "xyz"::text)');
2009+
_jsonpath_query
2010+
-------------------------------------------------------------------
2011+
[null, "truexyz", "1xyz", "aaaxyz", "{\"a\": 1}xyz", "[1, 2]xyz"]
2012+
(1 row)
2013+
2014+
select _jsonpath_query(json '123.45', '$::int4');
2015+
_jsonpath_query
2016+
-----------------
2017+
123
2018+
(1 row)
2019+
2020+
select _jsonpath_query(json '123.45', '$::float4');
2021+
_jsonpath_query
2022+
-----------------
2023+
123.45
2024+
(1 row)
2025+
2026+
select _jsonpath_query(json '123.45', '$::text');
2027+
_jsonpath_query
2028+
-----------------
2029+
"123.45"
2030+
(1 row)
2031+
2032+
select _jsonpath_query(json '123.45', '$::text::int4');
2033+
ERROR: invalid input syntax for integer: "123.45"
2034+
select _jsonpath_query(json '123.45', '$::text::float4');
2035+
_jsonpath_query
2036+
-----------------
2037+
123.45
2038+
(1 row)
2039+
2040+
select _jsonpath_query(json '123.45', '$::text::float4::int4');
2041+
_jsonpath_query
2042+
-----------------
2043+
123
2044+
(1 row)
2045+
2046+
select _jsonpath_query(json '4000000000', '$::int8');
2047+
_jsonpath_query
2048+
-----------------
2049+
4000000000
2050+
(1 row)
2051+
2052+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4');
2053+
_jsonpath_query
2054+
-----------------
2055+
123
2056+
null
2057+
1
2058+
(3 rows)
2059+
2060+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::text');
2061+
_jsonpath_query
2062+
-----------------
2063+
"123.45"
2064+
null
2065+
"0.67"
2066+
(3 rows)
2067+
2068+
select _jsonpath_query(json '[123.45, null, 0.67, "8.9"]', '$[*]::text::float4::int4');
2069+
_jsonpath_query
2070+
-----------------
2071+
123
2072+
null
2073+
1
2074+
9
2075+
(4 rows)
2076+
2077+
select _jsonpath_query(json '[123.45, 0.67]', '$[*]::int4 > $[0]::int4');
2078+
_jsonpath_query
2079+
-----------------
2080+
false
2081+
(1 row)
2082+
2083+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4 > $[0]::int4');
2084+
_jsonpath_query
2085+
-----------------
2086+
null
2087+
(1 row)
2088+
2089+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4 > $[1]::int4');
2090+
_jsonpath_query
2091+
-----------------
2092+
null
2093+
(1 row)
2094+
2095+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4 > $[2]::int4');
2096+
_jsonpath_query
2097+
-----------------
2098+
true
2099+
(1 row)
2100+
2101+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[0]::int4 > $[*]::int4');
2102+
_jsonpath_query
2103+
-----------------
2104+
true
2105+
(1 row)
2106+
2107+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4 > $[2]::text::float4');
2108+
_jsonpath_query
2109+
-----------------
2110+
true
2111+
(1 row)
2112+
2113+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::text::float4 > $[2]::int4');
2114+
_jsonpath_query
2115+
-----------------
2116+
true
2117+
(1 row)
2118+
2119+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::text::float4 > $[2]::text::float4');
2120+
_jsonpath_query
2121+
-----------------
2122+
true
2123+
(1 row)
2124+
2125+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4 > $[0 to 1]::int4');
2126+
_jsonpath_query
2127+
-----------------
2128+
null
2129+
(1 row)
2130+
2131+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4 > $[1 to 2]::int4');
2132+
_jsonpath_query
2133+
-----------------
2134+
true
2135+
(1 row)
2136+
2137+
select _jsonpath_query(json '[123.45, 100000.2, 10000.67, "1"]', '$[0]::int8 > $[*]::int4::int8');
2138+
_jsonpath_query
2139+
-----------------
2140+
true
2141+
(1 row)
2142+
2143+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}]', '$[*] -> "a"::text');
2144+
ERROR: Singleton SQL/JSON item required
2145+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}]', '$[0] -> "a"::text');
2146+
_jsonpath_query
2147+
-----------------
2148+
"b"
2149+
(1 row)
2150+
2151+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}]', '$[1] -> $[0].a::text');
2152+
_jsonpath_query
2153+
-----------------
2154+
[1, "2"]
2155+
(1 row)
2156+
2157+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}]', '$[0] \? "a"::text');
2158+
_jsonpath_query
2159+
-----------------
2160+
true
2161+
(1 row)
2162+
2163+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}]', '$[*] \? "b"::text');
2164+
_jsonpath_query
2165+
-----------------
2166+
true
2167+
(1 row)
2168+
2169+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}]', '$[*] \? "c"::text');
2170+
_jsonpath_query
2171+
-----------------
2172+
false
2173+
(1 row)
2174+
2175+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}, null, 1]', '$[*] ? (@ \? "a"::text)');
2176+
_jsonpath_query
2177+
-----------------
2178+
{"a": "b"}
2179+
(1 row)
2180+
2181+
select _jsonpath_query(json '[1, "t", 0, "f", null]', '$[*] ? (@::int4)');
2182+
ERROR: expected cast to boolean type
2183+
select _jsonpath_query(json '[1, "t", 0, "f", null]', '$[*] ? (@::bool)');
2184+
_jsonpath_query
2185+
-----------------
2186+
1
2187+
"t"
2188+
(2 rows)
2189+
2190+
select _jsonpath_query(json '[1, "t", 0, "f", null]', '$[*] ? (!(@::bool))');
2191+
_jsonpath_query
2192+
-----------------
2193+
0
2194+
"f"
2195+
(2 rows)
2196+
2197+
select _jsonpath_query(json '[1, "t", 0, "f", null]', '$[*] ? (@::bool == false::bool)');
2198+
_jsonpath_query
2199+
-----------------
2200+
0
2201+
"f"
2202+
(2 rows)
2203+

src/test/regress/sql/json_jsonpath.sql

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,80 @@ select * from _jsonpath_query(json '{"a": 1}', '$["a"]');
449449
select * from _jsonpath_query(json '{"a": 1}', 'strict $["b"]');
450450
select * from _jsonpath_query(json '{"a": 1}', 'lax $["b"]');
451451
select * from _jsonpath_query(json '{"a": 1, "b": 2}', 'lax $["b", "c", "b", "a", 0 to 3]');
452+
453+
-- extension: outer item reference (@N)
454+
select _jsonpath_query(json '[2,4,1,5,3]', '$[*] ? (!exists($[*] ? (@ < @1)))');
455+
select _jsonpath_query(json '[2,4,1,5,3]', '$.map(@ + @1[0])');
456+
-- the first @1 and @2 reference array, the second @1 -- current mapped array element
457+
select _jsonpath_query(json '[2,4,1,5,3]', '$.map(@ + @1[@1 - @2[2]])');
458+
select _jsonpath_query(json '[[2,4,1,5,3]]', '$.map(@.reduce($1 + $2 + @2[0][2] + @1[3]))');
459+
460+
-- extension: including subpaths into result
461+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.(a[*].b)');
462+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.(a[*]).b');
463+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.a.([*].b)');
464+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.(a)[*].b');
465+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.a[*].(b)');
466+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.(a)[*].(b)');
467+
select _jsonpath_query(json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}', '$.(a.[0 to 1].b)');
468+
469+
-- extension: custom operators and type casts
470+
select _jsonpath_query(json '"aaa"', '$::text || "bbb"::text || $::text');
471+
select _jsonpath_query(json '"aaa"', '$::text || "bbb" || $');
472+
select _jsonpath_query(json '[null, true, 1, "aaa", {"a": 1}, [1, 2]]', '$.map(@::text || "xyz"::text)');
473+
474+
select _jsonpath_query(json '123.45', '$::int4');
475+
select _jsonpath_query(json '123.45', '$::float4');
476+
select _jsonpath_query(json '123.45', '$::text');
477+
select _jsonpath_query(json '123.45', '$::text::int4');
478+
select _jsonpath_query(json '123.45', '$::text::float4');
479+
select _jsonpath_query(json '123.45', '$::text::float4::int4');
480+
select _jsonpath_query(json '4000000000', '$::int8');
481+
482+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4');
483+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::text');
484+
select _jsonpath_query(json '[123.45, null, 0.67, "8.9"]', '$[*]::text::float4::int4');
485+
486+
487+
select _jsonpath_query(json '[123.45, 0.67]', '$[*]::int4 > $[0]::int4');
488+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4 > $[0]::int4');
489+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4 > $[1]::int4');
490+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4 > $[2]::int4');
491+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[0]::int4 > $[*]::int4');
492+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4 > $[2]::text::float4');
493+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::text::float4 > $[2]::int4');
494+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::text::float4 > $[2]::text::float4');
495+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4 > $[0 to 1]::int4');
496+
select _jsonpath_query(json '[123.45, null, 0.67]', '$[*]::int4 > $[1 to 2]::int4');
497+
select _jsonpath_query(json '[123.45, 100000.2, 10000.67, "1"]', '$[0]::int8 > $[*]::int4::int8');
498+
499+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}]', '$[*] -> "a"::text');
500+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}]', '$[0] -> "a"::text');
501+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}]', '$[1] -> $[0].a::text');
502+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}]', '$[0] \? "a"::text');
503+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}]', '$[*] \? "b"::text');
504+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}]', '$[*] \? "c"::text');
505+
select _jsonpath_query(json '[{"a": "b"}, {"b": [1, "2"]}, null, 1]', '$[*] ? (@ \? "a"::text)');
506+
507+
select _jsonpath_query(json '[1, "t", 0, "f", null]', '$[*] ? (@::int4)');
508+
select _jsonpath_query(json '[1, "t", 0, "f", null]', '$[*] ? (@::bool)');
509+
select _jsonpath_query(json '[1, "t", 0, "f", null]', '$[*] ? (!(@::bool))');
510+
select _jsonpath_query(json '[1, "t", 0, "f", null]', '$[*] ? (@::bool == false::bool)');
511+
select _jsonpath_query(json '[1, "t", 0, "f", null]', '$[*] ? (@::bool || !(@::bool))');
512+
513+
514+
select _jsonpath_query(json '[1, 2, 3]', '$[*] ? (@::int4 > 1::int4)');
515+
516+
select json '1' @* '$ ? (@ \@> 1)';
517+
select json '1' @* '$ ? (@ \@> 2)';
518+
519+
select jsonpath '$::a' + '1';
520+
521+
select jsonpath '$ ? ($.a::jsonb || 1)';
522+
select jsonpath '$ ? ($.a::jsonb || ==== 1)';
523+
select jsonpath '$::jsonb';
524+
525+
select json '{"tags": [{"term": "a"}, {"term": "NYC"}]}' @? 'strict $.tags \@> [{term: "NYC"}]';
526+
select json '{"tags": [{"term": "a"}, {"term": "NYC"}]}' @? 'lax $.tags \@> {term: "NYC"}';
527+
528+
select json '"str"' @? '$ == "str"::jsonb';

0 commit comments

Comments
 (0)