@@ -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+
0 commit comments