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

Commit 97d7993

Browse files
author
Nikita Glukhov
committed
Add json tests for jsonpath extensions
1 parent c814a30 commit 97d7993

File tree

2 files changed

+382
-3
lines changed

2 files changed

+382
-3
lines changed

src/test/regress/expected/json_jsonpath.out

Lines changed: 310 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,23 @@ select _jsonpath_exists(json '[1]', '$.[1.2]');
111111
select _jsonpath_exists(json '[1]', 'strict $.[1.2]');
112112
ERROR: Invalid SQL/JSON subscript
113113
select _jsonpath_exists(json '{}', 'strict $.[0.3]');
114-
ERROR: SQL/JSON array not found
114+
ERROR: Invalid SQL/JSON subscript
115115
select _jsonpath_exists(json '{}', 'lax $.[0.3]');
116116
_jsonpath_exists
117117
------------------
118118
t
119119
(1 row)
120120

121121
select _jsonpath_exists(json '{}', 'strict $.[1.2]');
122-
ERROR: SQL/JSON array not found
122+
ERROR: Invalid SQL/JSON subscript
123123
select _jsonpath_exists(json '{}', 'lax $.[1.2]');
124124
_jsonpath_exists
125125
------------------
126126
f
127127
(1 row)
128128

129129
select _jsonpath_exists(json '{}', 'strict $.[-2 to 3]');
130-
ERROR: SQL/JSON array not found
130+
ERROR: Invalid SQL/JSON subscript
131131
select _jsonpath_exists(json '{}', 'lax $.[-2 to 3]');
132132
_jsonpath_exists
133133
------------------
@@ -1160,6 +1160,25 @@ select _jsonpath_query(json '{}', '$.datetime()');
11601160
ERROR: Invalid argument for SQL/JSON datetime function
11611161
select _jsonpath_query(json '""', '$.datetime()');
11621162
ERROR: Invalid argument for SQL/JSON datetime function
1163+
-- Standard extension: UNIX epoch to timestamptz
1164+
select _jsonpath_query(json '0', '$.datetime()');
1165+
_jsonpath_query
1166+
--------------------------------
1167+
"Wed Dec 31 16:00:00 1969 PST"
1168+
(1 row)
1169+
1170+
select _jsonpath_query(json '0', '$.datetime().type()');
1171+
_jsonpath_query
1172+
----------------------------
1173+
"timestamp with time zone"
1174+
(1 row)
1175+
1176+
select _jsonpath_query(json '1490216035.5', '$.datetime()');
1177+
_jsonpath_query
1178+
----------------------------------
1179+
"Wed Mar 22 13:53:55.5 2017 PDT"
1180+
(1 row)
1181+
11631182
select _jsonpath_query(json '"10-03-2017"', '$.datetime("dd-mm-yyyy")');
11641183
_jsonpath_query
11651184
-----------------
@@ -1601,6 +1620,12 @@ SELECT json '[{"a": 1}, {"a": 2}]' @* '$[*] ? (@.a > 10)';
16011620
----------
16021621
(0 rows)
16031622

1623+
SELECT json '[{"a": 1}, {"a": 2}]' @* '[$[*].a]';
1624+
?column?
1625+
----------
1626+
[1, 2]
1627+
(1 row)
1628+
16041629
SELECT json '[{"a": 1}, {"a": 2}]' @? '$[*].a > 1';
16051630
?column?
16061631
----------
@@ -1613,3 +1638,285 @@ SELECT json '[{"a": 1}, {"a": 2}]' @? '$[*].a > 2';
16131638
f
16141639
(1 row)
16151640

1641+
-- extension: map item method
1642+
select _jsonpath_query(json '1', 'strict $.map(@ + 10)');
1643+
ERROR: SQL/JSON array not found
1644+
select _jsonpath_query(json '1', 'lax $.map(@ + 10)');
1645+
_jsonpath_query
1646+
-----------------
1647+
11
1648+
(1 row)
1649+
1650+
select _jsonpath_query(json '[1, 2, 3]', '$.map(@ + 10)');
1651+
_jsonpath_query
1652+
-----------------
1653+
[11, 12, 13]
1654+
(1 row)
1655+
1656+
select _jsonpath_query(json '[[1, 2], [3, 4, 5], [], [6, 7]]', '$.map(@.map(@ + 10))');
1657+
_jsonpath_query
1658+
----------------------------------------
1659+
[[11, 12], [13, 14, 15], [], [16, 17]]
1660+
(1 row)
1661+
1662+
-- extension: reduce/fold item methods
1663+
select _jsonpath_query(json '1', 'strict $.reduce($1 + $2)');
1664+
ERROR: SQL/JSON array not found
1665+
select _jsonpath_query(json '1', 'lax $.reduce($1 + $2)');
1666+
_jsonpath_query
1667+
-----------------
1668+
1
1669+
(1 row)
1670+
1671+
select _jsonpath_query(json '1', 'strict $.fold($1 + $2, 10)');
1672+
ERROR: SQL/JSON array not found
1673+
select _jsonpath_query(json '1', 'lax $.fold($1 + $2, 10)');
1674+
_jsonpath_query
1675+
-----------------
1676+
11
1677+
(1 row)
1678+
1679+
select _jsonpath_query(json '[1, 2, 3]', '$.reduce($1 + $2)');
1680+
_jsonpath_query
1681+
-----------------
1682+
6
1683+
(1 row)
1684+
1685+
select _jsonpath_query(json '[1, 2, 3]', '$.fold($1 + $2, 100)');
1686+
_jsonpath_query
1687+
-----------------
1688+
106
1689+
(1 row)
1690+
1691+
select _jsonpath_query(json '[]', '$.reduce($1 + $2)');
1692+
_jsonpath_query
1693+
-----------------
1694+
(0 rows)
1695+
1696+
select _jsonpath_query(json '[]', '$.fold($1 + $2, 100)');
1697+
_jsonpath_query
1698+
-----------------
1699+
100
1700+
(1 row)
1701+
1702+
select _jsonpath_query(json '[1]', '$.reduce($1 + $2)');
1703+
_jsonpath_query
1704+
-----------------
1705+
1
1706+
(1 row)
1707+
1708+
select _jsonpath_query(json '[1, 2, 3]', '$.foldl([$1, $2], [])');
1709+
_jsonpath_query
1710+
-------------------
1711+
[[[[], 1], 2], 3]
1712+
(1 row)
1713+
1714+
select _jsonpath_query(json '[1, 2, 3]', '$.foldr([$2, $1], [])');
1715+
_jsonpath_query
1716+
-------------------
1717+
[[[[], 3], 2], 1]
1718+
(1 row)
1719+
1720+
select _jsonpath_query(json '[[1, 2], [3, 4, 5], [], [6, 7]]', '$.fold($1 + $2.fold($1 + $2, 100), 1000)');
1721+
_jsonpath_query
1722+
-----------------
1723+
1428
1724+
(1 row)
1725+
1726+
-- extension: min/max item methods
1727+
select _jsonpath_query(json '1', 'strict $.min()');
1728+
ERROR: SQL/JSON array not found
1729+
select _jsonpath_query(json '1', 'lax $.min()');
1730+
_jsonpath_query
1731+
-----------------
1732+
1
1733+
(1 row)
1734+
1735+
select _jsonpath_query(json '[]', '$.min()');
1736+
_jsonpath_query
1737+
-----------------
1738+
(0 rows)
1739+
1740+
select _jsonpath_query(json '[]', '$.max()');
1741+
_jsonpath_query
1742+
-----------------
1743+
(0 rows)
1744+
1745+
select _jsonpath_query(json '[1, 2, 3]', '$.min()');
1746+
ERROR: json comparison is not implemented
1747+
select _jsonpath_query(json '[1, 2, 3]', '$.max()');
1748+
ERROR: json comparison is not implemented
1749+
select _jsonpath_query(json '[2, 3, 5, 1, 4]', '$.min()');
1750+
ERROR: json comparison is not implemented
1751+
select _jsonpath_query(json '[2, 3, 5, 1, 4]', '$.max()');
1752+
ERROR: json comparison is not implemented
1753+
-- extension: path sequences
1754+
select _jsonpath_query(json '[1,2,3,4,5]', '10, 20, $[*], 30');
1755+
_jsonpath_query
1756+
-----------------
1757+
10
1758+
20
1759+
1
1760+
2
1761+
3
1762+
4
1763+
5
1764+
30
1765+
(8 rows)
1766+
1767+
select _jsonpath_query(json '[1,2,3,4,5]', 'lax 10, 20, $[*].a, 30');
1768+
_jsonpath_query
1769+
-----------------
1770+
10
1771+
20
1772+
30
1773+
(3 rows)
1774+
1775+
select _jsonpath_query(json '[1,2,3,4,5]', 'strict 10, 20, $[*].a, 30');
1776+
ERROR: SQL/JSON member not found
1777+
select _jsonpath_query(json '[1,2,3,4,5]', '-(10, 20, $[1 to 3], 30)');
1778+
_jsonpath_query
1779+
-----------------
1780+
-10
1781+
-20
1782+
-2
1783+
-3
1784+
-4
1785+
-30
1786+
(6 rows)
1787+
1788+
select _jsonpath_query(json '[1,2,3,4,5]', 'lax (10, 20, $[1 to 3], 30).map(@ + 100)');
1789+
_jsonpath_query
1790+
-----------------
1791+
110
1792+
120
1793+
102
1794+
103
1795+
104
1796+
130
1797+
(6 rows)
1798+
1799+
select _jsonpath_query(json '[1,2,3,4,5]', '$[(0, $[*], 5) ? (@ == 3)]');
1800+
_jsonpath_query
1801+
-----------------
1802+
4
1803+
(1 row)
1804+
1805+
select _jsonpath_query(json '[1,2,3,4,5]', '$[(0, $[*], 3) ? (@ == 3)]');
1806+
ERROR: Invalid SQL/JSON subscript
1807+
-- extension: array constructors
1808+
select _jsonpath_query(json '[1, 2, 3]', '[]');
1809+
_jsonpath_query
1810+
-----------------
1811+
[]
1812+
(1 row)
1813+
1814+
select _jsonpath_query(json '[1, 2, 3]', '[1, 2, $.map(@ + 100)[*], 4, 5]');
1815+
_jsonpath_query
1816+
-----------------------------
1817+
[1, 2, 101, 102, 103, 4, 5]
1818+
(1 row)
1819+
1820+
select _jsonpath_query(json '[1, 2, 3]', '[1, 2, $.map(@ + 100)[*], 4, 5][*]');
1821+
_jsonpath_query
1822+
-----------------
1823+
1
1824+
2
1825+
101
1826+
102
1827+
103
1828+
4
1829+
5
1830+
(7 rows)
1831+
1832+
select _jsonpath_query(json '[1, 2, 3]', '[(1, (2, $.map(@ + 100)[*])), (4, 5)]');
1833+
_jsonpath_query
1834+
-----------------------------
1835+
[1, 2, 101, 102, 103, 4, 5]
1836+
(1 row)
1837+
1838+
select _jsonpath_query(json '[1, 2, 3]', '[[1, 2], [$.map(@ + 100)[*], 4], 5, [(1,2)?(@ > 5)]]');
1839+
_jsonpath_query
1840+
-------------------------------------
1841+
[[1, 2], [101, 102, 103, 4], 5, []]
1842+
(1 row)
1843+
1844+
select _jsonpath_query(json '[1, 2, 3]', 'strict [1, 2, $.map(@.a)[*], 4, 5]');
1845+
ERROR: SQL/JSON member not found
1846+
select _jsonpath_query(json '[[1, 2], [3, 4, 5], [], [6, 7]]', '[$[*].map(@ + 10)[*] ? (@ > 13)]');
1847+
_jsonpath_query
1848+
------------------
1849+
[14, 15, 16, 17]
1850+
(1 row)
1851+
1852+
-- extension: object constructors
1853+
select _jsonpath_query(json '[1, 2, 3]', '{}');
1854+
_jsonpath_query
1855+
-----------------
1856+
{}
1857+
(1 row)
1858+
1859+
select _jsonpath_query(json '[1, 2, 3]', '{a: 2 + 3, "b": [$[*], 4, 5]}');
1860+
_jsonpath_query
1861+
--------------------------------
1862+
{"a": 5, "b": [1, 2, 3, 4, 5]}
1863+
(1 row)
1864+
1865+
select _jsonpath_query(json '[1, 2, 3]', '{a: 2 + 3, "b": [$[*], 4, 5]}.*');
1866+
_jsonpath_query
1867+
-----------------
1868+
5
1869+
[1, 2, 3, 4, 5]
1870+
(2 rows)
1871+
1872+
select _jsonpath_query(json '[1, 2, 3]', '{a: 2 + 3, "b": ($[*], 4, 5)}');
1873+
ERROR: Singleton SQL/JSON item required
1874+
select _jsonpath_query(json '[1, 2, 3]', '{a: 2 + 3, "b": [$.map({x: @, y: @ < 3})[*], {z: "foo"}]}');
1875+
_jsonpath_query
1876+
-----------------------------------------------------------------------------------------------
1877+
{"a": 5, "b": [{"x": 1, "y": true}, {"x": 2, "y": true}, {"x": 3, "y": false}, {"z": "foo"}]}
1878+
(1 row)
1879+
1880+
-- extension: object subscripting
1881+
select _jsonpath_exists(json '{"a": 1}', '$["a"]');
1882+
_jsonpath_exists
1883+
------------------
1884+
t
1885+
(1 row)
1886+
1887+
select _jsonpath_exists(json '{"a": 1}', '$["b"]');
1888+
_jsonpath_exists
1889+
------------------
1890+
f
1891+
(1 row)
1892+
1893+
select _jsonpath_exists(json '{"a": 1}', 'strict $["b"]');
1894+
ERROR: SQL/JSON member not found
1895+
select _jsonpath_exists(json '{"a": 1}', '$["b", "a"]');
1896+
_jsonpath_exists
1897+
------------------
1898+
t
1899+
(1 row)
1900+
1901+
select * from _jsonpath_query(json '{"a": 1}', '$["a"]');
1902+
_jsonpath_query
1903+
-----------------
1904+
1
1905+
(1 row)
1906+
1907+
select * from _jsonpath_query(json '{"a": 1}', 'strict $["b"]');
1908+
ERROR: SQL/JSON member not found
1909+
select * from _jsonpath_query(json '{"a": 1}', 'lax $["b"]');
1910+
_jsonpath_query
1911+
-----------------
1912+
(0 rows)
1913+
1914+
select * from _jsonpath_query(json '{"a": 1, "b": 2}', 'lax $["b", "c", "b", "a", 0 to 3]');
1915+
_jsonpath_query
1916+
------------------
1917+
2
1918+
2
1919+
1
1920+
{"a": 1, "b": 2}
1921+
(4 rows)
1922+

0 commit comments

Comments
 (0)