interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / subtype1.test

164 lines · 4840 bytes Raw Blame History
1# 2015-09-10
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 sqlite3_value_subtype() and
12# sqlite3_result_subtype() interfaces.
13#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17
18do_execsql_test subtype1-100 {
19 SELECT test_getsubtype('hello');
20} {0}
21do_execsql_test subtype1-110 {
22 SELECT test_getsubtype(test_setsubtype('hello',123));
23} {123}
24do_execsql_test subtype1-120 {
25 SELECT typeof(test_setsubtype('hello',123));
26} {text}
27do_execsql_test subtype1-130 {
28 SELECT test_setsubtype('hello',123);
29} {hello}
30
31# 2022-06-09
32# https://sqlite.org/forum/forumpost/3d9caa45cbe38c78
33#
34# Avoid carrying subtypes through into a subquery that has been flattened
35# or to which the outer WHERE clause has been pushed down.
36#
37reset_db
38do_execsql_test subtype1-200 {
39 CREATE TABLE t1(a); INSERT INTO t1 VALUES ('x');
40 CREATE VIEW t2(b) AS SELECT json(TRUE);
41 CREATE TABLE t3(b); INSERT INTO t3 VALUES(json(TRUE));
42}
43do_execsql_test subtype1-210 {
44 SELECT * FROM t3, t1 WHERE NOT json_quote(b);
45} {1 x}
46do_execsql_test subtype1-220 {
47 SELECT * FROM t2, t1 WHERE NOT json_quote(b);
48} {1 x}
49do_execsql_test subtype1-230 {
50 WITH t4(a) AS MATERIALIZED (SELECT json(1)) SELECT subtype(a) FROM t4;
51} {0}
52do_execsql_test subtype1-231 {
53 WITH t4(a) AS NOT MATERIALIZED (SELECT json(1)) SELECT subtype(a) FROM t4;
54} {0}
55
56# 2023-03-01
57# https://sqlite.org/forum/forumpost/37dd14a538
58#
59# Additional tests to show that subtypes do not traverse subquery boundaries.
60#
61do_execsql_test subtype1-300 {
62 CREATE TABLE t0(c0);
63 INSERT INTO t0 VALUES ('1');
64 CREATE VIEW v0(c0) AS SELECT CASE WHEN 1 THEN json_patch('1', '1') END
65 FROM t0 GROUP BY t0.c0;
66 SELECT * FROM v0 WHERE json_quote(v0.c0) != '1';
67} {1}
68do_execsql_test subtype1-310 {
69 SELECT *, json_quote(y) FROM (SELECT +json('1') AS y);
70} {1 {"1"}}
71do_execsql_test subtype1-320 {
72 SELECT *, json_quote(y) FROM (SELECT +json('1') AS y)
73 WHERE json_quote(y)='"1"';
74} {1 {"1"}}
75
76# 2024-11-26
77# subtype survives if()
78#
79do_execsql_test subtype1-400 {
80 WITH t400(id,j) AS (VALUES
81 (1,'{a:{x:1,y:2},b:{x:3,y:4}}'),
82 (2,'not json')
83 )
84 SELECT id, subtype(if(json_valid(j,6),j->'a')) FROM t400;
85} {1 74 2 0}
86
87#-------------------------------------------------------------------------
88# Test that indexes on expressions do not alter subtypes.
89#
90reset_db
91do_execsql_test subtype1-500 {
92 CREATE TABLE t1(id, j);
93 INSERT INTO t1 VALUES (1,'{a:{x:1,y:2},b:{x:3,y:4}}'), (2,'not json');
94}
95
96foreach {tn expr st} {
97 510 "(CASE WHEN json_valid(j, 6) THEN j->'a' ELSE j END)" 74
98 520 "+(CASE WHEN json_valid(j, 6) THEN j->'a' ELSE j END)" 74
99 530 "-(CASE WHEN json_valid(j, 6) THEN j->'a' ELSE j END)" 0
100 540 "if( json_valid(j, 6), j->'a' ) " 74
101 550 "if( json_valid(j, 6), j->'a' ) COLLATE nocase" 74
102 560 "CAST( if( json_valid(j, 6), j->'a' ) AS TEXT )" 74
103} {
104 do_execsql_test subtype1-$tn.1 "
105 SELECT id, subtype( $expr ) FROM t1;
106 " [list 1 $st 2 0]
107
108 do_execsql_test subtype1-$tn.2 "
109 DROP INDEX IF EXISTS i1;
110 CREATE INDEX i1 ON t1( $expr );
111 SELECT id, subtype( $expr ) FROM t1 INDEXED BY i1 ORDER BY id;
112 " [list 1 $st 2 0]
113}
114
115# Bug 2026-08-12T09:26:04Z
116#
117# Do not use the propagate constant optimization on a constant
118# function that might return a subtype.
119#
120reset_db
121do_execsql_test subtype1-600 {
122 CREATE TABLE t1(c1 TEXT);
123 INSERT INTO t1 VALUES(0);
124 SELECT count(*) FROM t1
125 WHERE (c1=json_quote(0)) AND (json_object('c',c1) IS '{"c":0}');
126} 0
127do_execsql_test subtype1-610 {
128 CREATE TABLE t2(c2 TEXT);
129 INSERT INTO t2 VALUES(0);
130 SELECT count(*) FROM t2
131 WHERE (c2=json_quote(0)) AND (json_array(c2)<'[0]');
132} 1
133
134# Bug 2026-08-25T05:36:33Z
135#
136# When substituting an expression from the WHERE clause of an index
137# for a column, clear any subtype on the value generated by the
138# expression.
139#
140do_execsql_test subtype1.700 {
141 CREATE TABLE t0(c0 TEXT, c1 INT);
142 INSERT INTO t0 VALUES ('1', 10);
143 CREATE INDEX i0 ON t0(c1) WHERE c0=json_quote(1);
144}
145
146do_execsql_test subtype1.710 {
147 SELECT c0, (c0=json_quote(1) AND json_array(c0)='[1]') AS p FROM t0;
148} {1 0}
149
150do_execsql_test subtype1.720 {
151 SELECT c0, json_quote(1), json_array(c0) FROM t0;
152 SELECT typeof(c0), typeof(json_quote(1)), typeof(json_array(c0)) FROM t0;
153} {1 1 {["1"]} text text text}
154
155do_execsql_test subtype1.730 {
156 SELECT * FROM t0 NOT INDEXED
157 WHERE (c0=json_quote(1) AND json_array(c0)='[1]');
158} {}
159
160do_execsql_test subtype1.740 {
161 SELECT * FROM t0 WHERE (c0=json_quote(1) AND json_array(c0)='[1]');
162}
163
164finish_test