| 1 |
# 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 |
|
| 14 |
set testdir [file dirname $argv0] |
| 15 |
source $testdir/tester.tcl |
| 16 |
|
| 17 |
do_execsql_test json103-100 { |
| 18 |
CREATE TABLE t1(a,b,c); |
| 19 |
WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<100) |
| 20 |
INSERT INTO t1(a,b,c) SELECT x, x%3, printf('n%d',x) FROM c; |
| 21 |
UPDATE t1 SET a='orange' WHERE rowid=39; |
| 22 |
UPDATE t1 SET a=32.5 WHERE rowid=31; |
| 23 |
UPDATE t1 SET a=x'303132' WHERE rowid=29; |
| 24 |
UPDATE t1 SET a=NULL WHERE rowid=37; |
| 25 |
SELECT json_group_array(a) FROM t1 WHERE a<0 AND typeof(a)!='blob'; |
| 26 |
} {{[]}} |
| 27 |
do_catchsql_test json103-101 { |
| 28 |
SELECT json_group_array(a) FROM t1; |
| 29 |
} {1 {JSON cannot hold BLOB values}} |
| 30 |
do_execsql_test json103-102 { |
| 31 |
SELECT quote(jsonb_group_array(a)) FROM t1 WHERE a<0 AND typeof(a)!='blob'; |
| 32 |
} {X'0B'} |
| 33 |
do_execsql_test json103-110 { |
| 34 |
SELECT json_group_array(a) FROM t1 |
| 35 |
WHERE rowid BETWEEN 31 AND 39; |
| 36 |
} {{[32.5,32,33,34,35,36,null,38,"orange"]}} |
| 37 |
do_execsql_test json103-111 { |
| 38 |
SELECT json_array_length(json_group_array(a)) FROM t1 |
| 39 |
WHERE rowid BETWEEN 31 AND 39; |
| 40 |
} {9} |
| 41 |
do_execsql_test json103-120 { |
| 42 |
SELECT b, json_group_array(a) FROM t1 WHERE rowid<10 GROUP BY b ORDER BY b; |
| 43 |
} {0 {[3,6,9]} 1 {[1,4,7]} 2 {[2,5,8]}} |
| 44 |
|
| 45 |
do_execsql_test json103-200 { |
| 46 |
SELECT json_group_object(c,a) FROM t1 WHERE a<0 AND typeof(a)!='blob'; |
| 47 |
} {{{}}} |
| 48 |
do_catchsql_test json103-201 { |
| 49 |
SELECT json_group_object(c,a) FROM t1; |
| 50 |
} {1 {JSON cannot hold BLOB values}} |
| 51 |
do_execsql_test json103-202 { |
| 52 |
SELECT quote(jsonb_group_object(c,a)) FROM t1 WHERE a<0 AND typeof(a)!='blob'; |
| 53 |
} {X'0C'} |
| 54 |
|
| 55 |
|
| 56 |
do_execsql_test json103-210 { |
| 57 |
SELECT json_group_object(c,a) FROM t1 |
| 58 |
WHERE rowid BETWEEN 31 AND 39 AND rowid%2==1; |
| 59 |
} {{{"n31":32.5,"n33":33,"n35":35,"n37":null,"n39":"orange"}}} |
| 60 |
do_execsql_test json103-220 { |
| 61 |
SELECT b, json_group_object(c,a) FROM t1 |
| 62 |
WHERE rowid<7 GROUP BY b ORDER BY b; |
| 63 |
} {0 {{"n3":3,"n6":6}} 1 {{"n1":1,"n4":4}} 2 {{"n2":2,"n5":5}}} |
| 64 |
|
| 65 |
# ticket https://sqlite.org/src/info/f45ac567eaa9f93c 2016-01-30 |
| 66 |
# Invalid JSON generated by json_group_array() |
| 67 |
# |
| 68 |
# The underlying problem is a failure to reset Mem.eSubtype |
| 69 |
# |
| 70 |
do_execsql_test json103-300 { |
| 71 |
DROP TABLE IF EXISTS t1; |
| 72 |
CREATE TABLE t1(x); |
| 73 |
INSERT INTO t1 VALUES(1),('abc'); |
| 74 |
SELECT |
| 75 |
json_group_array(x), |
| 76 |
json_group_array(json_object('x',x)) |
| 77 |
FROM t1; |
| 78 |
} {{[1,"abc"]} {[{"x":1},{"x":"abc"}]}} |
| 79 |
|
| 80 |
# json_group_array() and json_group_object() work as window functions. |
| 81 |
# |
| 82 |
ifcapable windowfunc { |
| 83 |
do_execsql_test json103-400 { |
| 84 |
CREATE TABLE t4(x); |
| 85 |
INSERT INTO t4 VALUES |
| 86 |
(1), |
| 87 |
('a,b'), |
| 88 |
(3), |
| 89 |
('x"y'), |
| 90 |
(5), |
| 91 |
(6), |
| 92 |
(7); |
| 93 |
SELECT json_group_array(x) OVER (ROWS 2 PRECEDING) FROM t4; |
| 94 |
} {{[1]} {[1,"a,b"]} {[1,"a,b",3]} {["a,b",3,"x\"y"]} {[3,"x\"y",5]} {["x\"y",5,6]} {[5,6,7]}} |
| 95 |
do_execsql_test json103-410 { |
| 96 |
SELECT json_group_object(rowid, x) OVER (ROWS 2 PRECEDING) FROM t4; |
| 97 |
} {{{"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}}} |
| 98 |
} |
| 99 |
|
| 100 |
finish_test |