blob: 35580ce4ea9fde4883c5ab878ecec06729a8ac75 [file] [log] [blame]
drhff135ae2015-12-30 01:07:02 +00001# 2015-12-30
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11# This file implements tests for JSON aggregate SQL functions
12#
13
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16
17ifcapable !json1 {
18 finish_test
19 return
20}
21
22do_execsql_test json103-100 {
23 CREATE TABLE t1(a,b,c);
24 WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<100)
25 INSERT INTO t1(a,b,c) SELECT x, x%3, printf('n%d',x) FROM c;
26 UPDATE t1 SET a='orange' WHERE rowid=39;
27 UPDATE t1 SET a=32.5 WHERE rowid=31;
28 UPDATE t1 SET a=x'303132' WHERE rowid=29;
29 UPDATE t1 SET a=NULL WHERE rowid=37;
30 SELECT json_group_array(a) FROM t1 WHERE a<0 AND typeof(a)!='blob';
31} {{[]}}
32do_catchsql_test json103-101 {
33 SELECT json_group_array(a) FROM t1;
34} {1 {JSON cannot hold BLOB values}}
35do_execsql_test json103-110 {
36 SELECT json_group_array(a) FROM t1
37 WHERE rowid BETWEEN 31 AND 39;
38} {{[32.5,32,33,34,35,36,null,38,"orange"]}}
39do_execsql_test json103-111 {
40 SELECT json_array_length(json_group_array(a)) FROM t1
41 WHERE rowid BETWEEN 31 AND 39;
42} {9}
43do_execsql_test json103-120 {
44 SELECT b, json_group_array(a) FROM t1 WHERE rowid<10 GROUP BY b ORDER BY b;
45} {0 {[3,6,9]} 1 {[1,4,7]} 2 {[2,5,8]}}
46
47do_execsql_test json103-200 {
48 SELECT json_group_object(c,a) FROM t1 WHERE a<0 AND typeof(a)!='blob';
49} {{{}}}
50do_catchsql_test json103-201 {
51 SELECT json_group_object(c,a) FROM t1;
52} {1 {JSON cannot hold BLOB values}}
53
54do_execsql_test json103-210 {
55 SELECT json_group_object(c,a) FROM t1
56 WHERE rowid BETWEEN 31 AND 39 AND rowid%2==1;
57} {{{"n31":32.5,"n33":33,"n35":35,"n37":null,"n39":"orange"}}}
58do_execsql_test json103-220 {
59 SELECT b, json_group_object(c,a) FROM t1
60 WHERE rowid<7 GROUP BY b ORDER BY b;
61} {0 {{"n3":3,"n6":6}} 1 {{"n1":1,"n4":4}} 2 {{"n2":2,"n5":5}}}
62
dan5b6c8e42016-01-30 15:46:03 +000063# ticket https://www.sqlite.org/src/info/f45ac567eaa9f93c 2016-01-30
64# Invalid JSON generated by json_group_array()
65#
66# The underlying problem is a failure to reset Mem.eSubtype
67#
68do_execsql_test json103-300 {
69 DROP TABLE IF EXISTS t1;
70 CREATE TABLE t1(x);
71 INSERT INTO t1 VALUES(1),('abc');
72 SELECT
73 json_group_array(x),
74 json_group_array(json_object('x',x))
75 FROM t1;
76} {{[1,"abc"]} {[{"x":1},{"x":"abc"}]}}
drhff135ae2015-12-30 01:07:02 +000077
drh8be47a72018-07-05 20:05:29 +000078# json_group_array() and json_group_object() work as window functions.
79#
80ifcapable windowfunc {
81 do_execsql_test json103-400 {
82 CREATE TABLE t4(x);
83 INSERT INTO t4 VALUES
84 (1),
85 ('a,b'),
86 (3),
87 ('x"y'),
88 (5),
89 (6),
90 (7);
91 SELECT json_group_array(x) OVER (ROWS 2 PRECEDING) FROM t4;
92 } {{[1]} {[1,"a,b"]} {[1,"a,b",3]} {["a,b",3,"x\"y"]} {[3,"x\"y",5]} {["x\"y",5,6]} {[5,6,7]}}
93 do_execsql_test json103-410 {
94 SELECT json_group_object(rowid, x) OVER (ROWS 2 PRECEDING) FROM t4;
95 } {{{"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}}}
96}
97
drhff135ae2015-12-30 01:07:02 +000098finish_test