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

Commit b8068a7

Browse files
author
Nikita Glukhov
committed
Add WIP extensions tests for json
1 parent 0af3626 commit b8068a7

File tree

2 files changed

+356
-0
lines changed

2 files changed

+356
-0
lines changed

src/test/regress/expected/json_jsonpath.out

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,3 +2013,294 @@ select json 'null' @* '{"a": 1}["b"]';
20132013
----------
20142014
(0 rows)
20152015

2016+
-- extension: outer item reference (@N)
2017+
select json '[2,4,1,5,3]' @* '$[*] ? (!exists($[*] ? (@ < @1)))';
2018+
?column?
2019+
----------
2020+
1
2021+
(1 row)
2022+
2023+
select json '[2,4,1,5,3]' @* '$.map(@ + @1[0])';
2024+
?column?
2025+
-----------------
2026+
[4, 6, 3, 7, 5]
2027+
(1 row)
2028+
2029+
-- the first @1 and @2 reference array, the second @1 -- current mapped array element
2030+
select json '[2,4,1,5,3]' @* '$.map(@ + @1[@1 - @2[2]])';
2031+
?column?
2032+
-----------------
2033+
[6, 9, 3, 8, 4]
2034+
(1 row)
2035+
2036+
select json '[[2,4,1,5,3]]' @* '$.map(@.reduce($1 + $2 + @2[0][2] + @1[3]))';
2037+
?column?
2038+
----------
2039+
[39]
2040+
(1 row)
2041+
2042+
-- extension: including subpaths into result
2043+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.(a[*].b)';
2044+
?column?
2045+
-----------------------------
2046+
{"a": [{"b": 1}, {"b": 2}]}
2047+
(1 row)
2048+
2049+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.(a[*]).b';
2050+
?column?
2051+
---------------
2052+
{"a": [1, 2]}
2053+
(1 row)
2054+
2055+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.a.([*].b)';
2056+
?column?
2057+
----------------------
2058+
[{"b": 1}, {"b": 2}]
2059+
(1 row)
2060+
2061+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.(a)[*].b';
2062+
?column?
2063+
----------
2064+
{"a": 1}
2065+
{"a": 2}
2066+
(2 rows)
2067+
2068+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.a[*].(b)';
2069+
?column?
2070+
----------
2071+
{"b": 1}
2072+
{"b": 2}
2073+
(2 rows)
2074+
2075+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.(a)[*].(b)';
2076+
?column?
2077+
-----------------
2078+
{"a": {"b": 1}}
2079+
{"a": {"b": 2}}
2080+
(2 rows)
2081+
2082+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.(a.[0 to 1].b)';
2083+
?column?
2084+
-----------------------------
2085+
{"a": [{"b": 1}, {"b": 2}]}
2086+
(1 row)
2087+
2088+
-- extension: custom operators and type casts
2089+
select json '"aaa"' @* '$::text || "bbb"::text || $::text';
2090+
?column?
2091+
-------------
2092+
"aaabbbaaa"
2093+
(1 row)
2094+
2095+
select json '"aaa"' @* '$::text || "bbb" || $';
2096+
?column?
2097+
---------------------
2098+
"aaa\"bbb\"\"aaa\""
2099+
(1 row)
2100+
2101+
select json '[null, true, 1, "aaa", {"a": 1}, [1, 2]]' @* '$.map(@::text || "xyz"::text)';
2102+
?column?
2103+
-------------------------------------------------------------------
2104+
[null, "truexyz", "1xyz", "aaaxyz", "{\"a\": 1}xyz", "[1, 2]xyz"]
2105+
(1 row)
2106+
2107+
select json '123.45' @* '$::int4';
2108+
?column?
2109+
----------
2110+
123
2111+
(1 row)
2112+
2113+
select json '123.45' @* '$::float4';
2114+
?column?
2115+
----------
2116+
123.45
2117+
(1 row)
2118+
2119+
select json '123.45' @* '$::text';
2120+
?column?
2121+
----------
2122+
"123.45"
2123+
(1 row)
2124+
2125+
select json '123.45' @* '$::text::int4';
2126+
ERROR: invalid input syntax for integer: "123.45"
2127+
select json '123.45' @* '$::text::float4';
2128+
?column?
2129+
----------
2130+
123.45
2131+
(1 row)
2132+
2133+
select json '123.45' @* '$::text::float4::int4';
2134+
?column?
2135+
----------
2136+
123
2137+
(1 row)
2138+
2139+
select json '4000000000' @* '$::int8';
2140+
?column?
2141+
------------
2142+
4000000000
2143+
(1 row)
2144+
2145+
select json '[123.45, null, 0.67]' @* '$[*]::int4';
2146+
?column?
2147+
----------
2148+
123
2149+
null
2150+
1
2151+
(3 rows)
2152+
2153+
select json '[123.45, null, 0.67]' @* '$[*]::text';
2154+
?column?
2155+
----------
2156+
"123.45"
2157+
null
2158+
"0.67"
2159+
(3 rows)
2160+
2161+
select json '[123.45, null, 0.67, "8.9"]' @* '$[*]::text::float4::int4';
2162+
?column?
2163+
----------
2164+
123
2165+
null
2166+
1
2167+
9
2168+
(4 rows)
2169+
2170+
select json '[123.45, 0.67]' @* '$[*]::int4 > $[0]::int4';
2171+
?column?
2172+
----------
2173+
false
2174+
(1 row)
2175+
2176+
select json '[123.45, null, 0.67]' @* '$[*]::int4 > $[0]::int4';
2177+
?column?
2178+
----------
2179+
null
2180+
(1 row)
2181+
2182+
select json '[123.45, null, 0.67]' @* '$[*]::int4 > $[1]::int4';
2183+
?column?
2184+
----------
2185+
null
2186+
(1 row)
2187+
2188+
select json '[123.45, null, 0.67]' @* '$[*]::int4 > $[2]::int4';
2189+
?column?
2190+
----------
2191+
true
2192+
(1 row)
2193+
2194+
select json '[123.45, null, 0.67]' @* '$[0]::int4 > $[*]::int4';
2195+
?column?
2196+
----------
2197+
true
2198+
(1 row)
2199+
2200+
select json '[123.45, null, 0.67]' @* '$[*]::int4 > $[2]::text::float4';
2201+
?column?
2202+
----------
2203+
true
2204+
(1 row)
2205+
2206+
select json '[123.45, null, 0.67]' @* '$[*]::text::float4 > $[2]::int4';
2207+
?column?
2208+
----------
2209+
true
2210+
(1 row)
2211+
2212+
select json '[123.45, null, 0.67]' @* '$[*]::text::float4 > $[2]::text::float4';
2213+
?column?
2214+
----------
2215+
true
2216+
(1 row)
2217+
2218+
select json '[123.45, null, 0.67]' @* '$[*]::int4 > $[0 to 1]::int4';
2219+
?column?
2220+
----------
2221+
null
2222+
(1 row)
2223+
2224+
select json '[123.45, null, 0.67]' @* '$[*]::int4 > $[1 to 2]::int4';
2225+
?column?
2226+
----------
2227+
true
2228+
(1 row)
2229+
2230+
select json '[123.45, 100000.2, 10000.67, "1"]' @* '$[0]::int8 > $[*]::int4::int8';
2231+
?column?
2232+
----------
2233+
true
2234+
(1 row)
2235+
2236+
select json '[{"a": "b"}, {"b": [1, "2"]}]' @* '$[*] -> "a"::text';
2237+
ERROR: Singleton SQL/JSON item required
2238+
select json '[{"a": "b"}, {"b": [1, "2"]}]' @* '$[0] -> "a"::text';
2239+
?column?
2240+
----------
2241+
"b"
2242+
(1 row)
2243+
2244+
select json '[{"a": "b"}, {"b": [1, "2"]}]' @* '$[1] -> $[0].a::text';
2245+
?column?
2246+
----------
2247+
[1, "2"]
2248+
(1 row)
2249+
2250+
select json '[{"a": "b"}, {"b": [1, "2"]}]' @* '$[0] \? "a"::text';
2251+
ERROR: operator does not exist: json ? text
2252+
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
2253+
select json '[{"a": "b"}, {"b": [1, "2"]}]' @* '$[*] \? "b"::text';
2254+
ERROR: operator does not exist: json ? text
2255+
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
2256+
select json '[{"a": "b"}, {"b": [1, "2"]}]' @* '$[*] \? "c"::text';
2257+
ERROR: operator does not exist: json ? text
2258+
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
2259+
select json '[{"a": "b"}, {"b": [1, "2"]}, null, 1]' @* '$[*] ? (@ \? "a"::text)';
2260+
ERROR: operator does not exist: json ? text
2261+
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
2262+
select json '[1, "t", 0, "f", null]' @* '$[*] ? (@::int4)';
2263+
ERROR: expected cast to boolean type
2264+
select json '[1, "t", 0, "f", null]' @* '$[*] ? (@::bool)';
2265+
?column?
2266+
----------
2267+
1
2268+
"t"
2269+
(2 rows)
2270+
2271+
select json '[1, "t", 0, "f", null]' @* '$[*] ? (!(@::bool))';
2272+
?column?
2273+
----------
2274+
0
2275+
"f"
2276+
(2 rows)
2277+
2278+
select json '[1, "t", 0, "f", null]' @* '$[*] ? (@::bool == false::bool)';
2279+
?column?
2280+
----------
2281+
0
2282+
"f"
2283+
(2 rows)
2284+
2285+
select json '[1, "t", 0, "f", null]' @* '$[*] ? (@::bool || !(@::bool))';
2286+
ERROR: operator does not exist: boolean || json
2287+
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
2288+
select json '[1, 2, 3]' @* '$[*] ? (@::int4 > 1::int4)';
2289+
?column?
2290+
----------
2291+
2
2292+
3
2293+
(2 rows)
2294+
2295+
select json '"str"' @* '$::json';
2296+
?column?
2297+
----------
2298+
"str"
2299+
(1 row)
2300+
2301+
select json '"str"' @* '$::jsonb';
2302+
?column?
2303+
----------
2304+
"str"
2305+
(1 row)
2306+

src/test/regress/sql/json_jsonpath.sql

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,68 @@ select json '{"a": 1, "b": 2}' @* 'lax $["b", "c", "b", "a", 0 to 3]';
438438

439439
select json 'null' @* '{"a": 1}["a"]';
440440
select json 'null' @* '{"a": 1}["b"]';
441+
442+
-- extension: outer item reference (@N)
443+
select json '[2,4,1,5,3]' @* '$[*] ? (!exists($[*] ? (@ < @1)))';
444+
select json '[2,4,1,5,3]' @* '$.map(@ + @1[0])';
445+
-- the first @1 and @2 reference array, the second @1 -- current mapped array element
446+
select json '[2,4,1,5,3]' @* '$.map(@ + @1[@1 - @2[2]])';
447+
select json '[[2,4,1,5,3]]' @* '$.map(@.reduce($1 + $2 + @2[0][2] + @1[3]))';
448+
449+
-- extension: including subpaths into result
450+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.(a[*].b)';
451+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.(a[*]).b';
452+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.a.([*].b)';
453+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.(a)[*].b';
454+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.a[*].(b)';
455+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.(a)[*].(b)';
456+
select json '{"a": [{"b": 1, "c": 10}, {"b": 2, "c": 20}]}' @* '$.(a.[0 to 1].b)';
457+
458+
-- extension: custom operators and type casts
459+
select json '"aaa"' @* '$::text || "bbb"::text || $::text';
460+
select json '"aaa"' @* '$::text || "bbb" || $';
461+
select json '[null, true, 1, "aaa", {"a": 1}, [1, 2]]' @* '$.map(@::text || "xyz"::text)';
462+
463+
select json '123.45' @* '$::int4';
464+
select json '123.45' @* '$::float4';
465+
select json '123.45' @* '$::text';
466+
select json '123.45' @* '$::text::int4';
467+
select json '123.45' @* '$::text::float4';
468+
select json '123.45' @* '$::text::float4::int4';
469+
select json '4000000000' @* '$::int8';
470+
471+
select json '[123.45, null, 0.67]' @* '$[*]::int4';
472+
select json '[123.45, null, 0.67]' @* '$[*]::text';
473+
select json '[123.45, null, 0.67, "8.9"]' @* '$[*]::text::float4::int4';
474+
475+
476+
select json '[123.45, 0.67]' @* '$[*]::int4 > $[0]::int4';
477+
select json '[123.45, null, 0.67]' @* '$[*]::int4 > $[0]::int4';
478+
select json '[123.45, null, 0.67]' @* '$[*]::int4 > $[1]::int4';
479+
select json '[123.45, null, 0.67]' @* '$[*]::int4 > $[2]::int4';
480+
select json '[123.45, null, 0.67]' @* '$[0]::int4 > $[*]::int4';
481+
select json '[123.45, null, 0.67]' @* '$[*]::int4 > $[2]::text::float4';
482+
select json '[123.45, null, 0.67]' @* '$[*]::text::float4 > $[2]::int4';
483+
select json '[123.45, null, 0.67]' @* '$[*]::text::float4 > $[2]::text::float4';
484+
select json '[123.45, null, 0.67]' @* '$[*]::int4 > $[0 to 1]::int4';
485+
select json '[123.45, null, 0.67]' @* '$[*]::int4 > $[1 to 2]::int4';
486+
select json '[123.45, 100000.2, 10000.67, "1"]' @* '$[0]::int8 > $[*]::int4::int8';
487+
488+
select json '[{"a": "b"}, {"b": [1, "2"]}]' @* '$[*] -> "a"::text';
489+
select json '[{"a": "b"}, {"b": [1, "2"]}]' @* '$[0] -> "a"::text';
490+
select json '[{"a": "b"}, {"b": [1, "2"]}]' @* '$[1] -> $[0].a::text';
491+
select json '[{"a": "b"}, {"b": [1, "2"]}]' @* '$[0] \? "a"::text';
492+
select json '[{"a": "b"}, {"b": [1, "2"]}]' @* '$[*] \? "b"::text';
493+
select json '[{"a": "b"}, {"b": [1, "2"]}]' @* '$[*] \? "c"::text';
494+
select json '[{"a": "b"}, {"b": [1, "2"]}, null, 1]' @* '$[*] ? (@ \? "a"::text)';
495+
496+
select json '[1, "t", 0, "f", null]' @* '$[*] ? (@::int4)';
497+
select json '[1, "t", 0, "f", null]' @* '$[*] ? (@::bool)';
498+
select json '[1, "t", 0, "f", null]' @* '$[*] ? (!(@::bool))';
499+
select json '[1, "t", 0, "f", null]' @* '$[*] ? (@::bool == false::bool)';
500+
select json '[1, "t", 0, "f", null]' @* '$[*] ? (@::bool || !(@::bool))';
501+
502+
select json '[1, 2, 3]' @* '$[*] ? (@::int4 > 1::int4)';
503+
504+
select json '"str"' @* '$::json';
505+
select json '"str"' @* '$::jsonb';

0 commit comments

Comments
 (0)