xref: /sqlite-3.40.0/test/json103.test (revision e229ca03)
1ff135ae4Sdrh# 2015-12-30
2ff135ae4Sdrh#
3ff135ae4Sdrh# The author disclaims copyright to this source code.  In place of
4ff135ae4Sdrh# a legal notice, here is a blessing:
5ff135ae4Sdrh#
6ff135ae4Sdrh#    May you do good and not evil.
7ff135ae4Sdrh#    May you find forgiveness for yourself and forgive others.
8ff135ae4Sdrh#    May you share freely, never taking more than you give.
9ff135ae4Sdrh#
10ff135ae4Sdrh#***********************************************************************
11ff135ae4Sdrh# This file implements tests for JSON aggregate SQL functions
12ff135ae4Sdrh#
13ff135ae4Sdrh
14ff135ae4Sdrhset testdir [file dirname $argv0]
15ff135ae4Sdrhsource $testdir/tester.tcl
16ff135ae4Sdrh
17ff135ae4Sdrhdo_execsql_test json103-100 {
18ff135ae4Sdrh  CREATE TABLE t1(a,b,c);
19ff135ae4Sdrh  WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<100)
20ff135ae4Sdrh  INSERT INTO t1(a,b,c) SELECT x, x%3, printf('n%d',x)  FROM c;
21ff135ae4Sdrh  UPDATE t1 SET a='orange' WHERE rowid=39;
22ff135ae4Sdrh  UPDATE t1 SET a=32.5 WHERE rowid=31;
23ff135ae4Sdrh  UPDATE t1 SET a=x'303132' WHERE rowid=29;
24ff135ae4Sdrh  UPDATE t1 SET a=NULL WHERE rowid=37;
25ff135ae4Sdrh  SELECT json_group_array(a) FROM t1 WHERE a<0 AND typeof(a)!='blob';
26ff135ae4Sdrh} {{[]}}
27ff135ae4Sdrhdo_catchsql_test json103-101 {
28ff135ae4Sdrh  SELECT json_group_array(a) FROM t1;
29ff135ae4Sdrh} {1 {JSON cannot hold BLOB values}}
30ff135ae4Sdrhdo_execsql_test json103-110 {
31ff135ae4Sdrh  SELECT json_group_array(a) FROM t1
32ff135ae4Sdrh   WHERE rowid BETWEEN 31 AND 39;
33ff135ae4Sdrh} {{[32.5,32,33,34,35,36,null,38,"orange"]}}
34ff135ae4Sdrhdo_execsql_test json103-111 {
35ff135ae4Sdrh  SELECT json_array_length(json_group_array(a)) FROM t1
36ff135ae4Sdrh   WHERE rowid BETWEEN 31 AND 39;
37ff135ae4Sdrh} {9}
38ff135ae4Sdrhdo_execsql_test json103-120 {
39ff135ae4Sdrh  SELECT b, json_group_array(a) FROM t1 WHERE rowid<10 GROUP BY b ORDER BY b;
40ff135ae4Sdrh} {0 {[3,6,9]} 1 {[1,4,7]} 2 {[2,5,8]}}
41ff135ae4Sdrh
42ff135ae4Sdrhdo_execsql_test json103-200 {
43ff135ae4Sdrh  SELECT json_group_object(c,a) FROM t1 WHERE a<0 AND typeof(a)!='blob';
44ff135ae4Sdrh} {{{}}}
45ff135ae4Sdrhdo_catchsql_test json103-201 {
46ff135ae4Sdrh  SELECT json_group_object(c,a) FROM t1;
47ff135ae4Sdrh} {1 {JSON cannot hold BLOB values}}
48ff135ae4Sdrh
49ff135ae4Sdrhdo_execsql_test json103-210 {
50ff135ae4Sdrh  SELECT json_group_object(c,a) FROM t1
51ff135ae4Sdrh   WHERE rowid BETWEEN 31 AND 39 AND rowid%2==1;
52ff135ae4Sdrh} {{{"n31":32.5,"n33":33,"n35":35,"n37":null,"n39":"orange"}}}
53ff135ae4Sdrhdo_execsql_test json103-220 {
54ff135ae4Sdrh  SELECT b, json_group_object(c,a) FROM t1
55ff135ae4Sdrh   WHERE rowid<7 GROUP BY b ORDER BY b;
56ff135ae4Sdrh} {0 {{"n3":3,"n6":6}} 1 {{"n1":1,"n4":4}} 2 {{"n2":2,"n5":5}}}
57ff135ae4Sdrh
585b6c8e4eSdan# ticket https://www.sqlite.org/src/info/f45ac567eaa9f93c 2016-01-30
595b6c8e4eSdan# Invalid JSON generated by json_group_array()
605b6c8e4eSdan#
615b6c8e4eSdan# The underlying problem is a failure to reset Mem.eSubtype
625b6c8e4eSdan#
635b6c8e4eSdando_execsql_test json103-300 {
645b6c8e4eSdan  DROP TABLE IF EXISTS t1;
655b6c8e4eSdan  CREATE TABLE t1(x);
665b6c8e4eSdan  INSERT INTO t1 VALUES(1),('abc');
675b6c8e4eSdan  SELECT
685b6c8e4eSdan     json_group_array(x),
695b6c8e4eSdan     json_group_array(json_object('x',x))
705b6c8e4eSdan    FROM t1;
715b6c8e4eSdan} {{[1,"abc"]} {[{"x":1},{"x":"abc"}]}}
72ff135ae4Sdrh
73*8be47a7eSdrh# json_group_array() and json_group_object() work as window functions.
74*8be47a7eSdrh#
75*8be47a7eSdrhifcapable windowfunc {
76*8be47a7eSdrh  do_execsql_test json103-400 {
77*8be47a7eSdrh    CREATE TABLE t4(x);
78*8be47a7eSdrh    INSERT INTO t4 VALUES
79*8be47a7eSdrh      (1),
80*8be47a7eSdrh      ('a,b'),
81*8be47a7eSdrh      (3),
82*8be47a7eSdrh      ('x"y'),
83*8be47a7eSdrh      (5),
84*8be47a7eSdrh      (6),
85*8be47a7eSdrh      (7);
86*8be47a7eSdrh    SELECT json_group_array(x) OVER (ROWS 2 PRECEDING) FROM t4;
87*8be47a7eSdrh  } {{[1]} {[1,"a,b"]} {[1,"a,b",3]} {["a,b",3,"x\"y"]} {[3,"x\"y",5]} {["x\"y",5,6]} {[5,6,7]}}
88*8be47a7eSdrh  do_execsql_test json103-410 {
89*8be47a7eSdrh    SELECT json_group_object(rowid, x) OVER (ROWS 2 PRECEDING) FROM t4;
90*8be47a7eSdrh  } {{{"1":1}} {{"1":1,"2":"a,b"}} {{"1":1,"2":"a,b","3":3}} {{"2":"a,b","3":3,"4":"x\"y"}} {{"3":3,"4":"x\"y","5":5}} {{"4":"x\"y","5":5,"6":6}} {{"5":5,"6":6,"7":7}}}
91*8be47a7eSdrh}
92*8be47a7eSdrh
93ff135ae4Sdrhfinish_test
94