Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/intreal.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / intreal.test` 331 lines ยท 9978 bytes Raw Blame History 1 # 2019-05-03 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 # Tests to exercise the MEM_IntReal representation of Mem objects. 12 # 13 set testdir [file dirname $argv0] 14 source $testdir/tester.tcl 15 set ::testprefix intreal 16 17 sqlite3_create_function db 18 do_execsql_test 100 { 19 SELECT intreal(5); 20 } {5.0} 21 do_execsql_test 110 { 22 SELECT intreal(5)=5, 6=intreal(6); 23 } {1 1} 24 do_execsql_test 120 { 25 SELECT intreal(7)=7.0, 8.0=intreal(8); 26 } {1 1} 27 do_execsql_test 130 { 28 SELECT typeof(intreal(9)); 29 } {real} 30 do_execsql_test 140 { 31 SELECT 'a'||intreal(11)||'z'; 32 } {a11.0z} 33 34 do_execsql_test 150 { 35 SELECT max(1.0,intreal(2),3.0), max(1,intreal(2),3); 36 } {3.0 3} 37 do_execsql_test 160 { 38 SELECT max(1.0,intreal(4),3.0), max(1,intreal(4),3); 39 } {4.0 4.0} 40 do_execsql_test 170 { 41 SELECT max(1.0,intreal(2),intreal(3),4.0), 42 max(1,intreal(2),intreal(3),4); 43 } {4.0 4} 44 do_execsql_test 180 { 45 SELECT max(1.0,intreal(5),intreal(3),4.0), 46 max(1,intreal(5),intreal(3),4); 47 } {5.0 5.0} 48 49 #------------------------------------------------------------------------- 50 do_execsql_test 2.1 { 51 CREATE TABLE t2(a REAL); 52 INSERT INTO t2 VALUES( `836627109860825358` ); 53 SELECT substr(a,1,4) FROM t2 WHERE a = CAST(`836627109860825358` AS REAL); 54 } {8.36} 55 56 do_execsql_test 2.2 { 57 CREATE INDEX i2 ON t2(a); 58 SELECT substr(a,1,4) FROM t2 WHERE a = CAST(`836627109860825358` AS REAL); 59 } {8.36} 60 61 do_execsql_test 2.3 { 62 CREATE TABLE t0 (c0); 63 CREATE TABLE t1 (c1 REAL); 64 INSERT INTO t1(c1) VALUES (`8366271098608253588`); 65 INSERT INTO t0(c0) VALUES ('a'); 66 } 67 set D [db one {SELECT c1 FROM t1}] 68 69 do_execsql_test 2.4 { 70 SELECT * FROM t1 WHERE (t1.c1 = CAST(`8366271098608253588` AS REAL)); 71 } $D 72 73 do_execsql_test 2.5 { 74 SELECT * FROM t0, t1 WHERE (t1.c1 = CAST(`8366271098608253588` AS REAL)); 75 } [list a $D] 76 77 do_execsql_test 2.6 { 78 SELECT * FROM t0, t1 79 WHERE ( 80 t1.c1 >= CAST(`8366271098608253588` AS REAL) 81 AND t1.c1 <= CAST(`8366271098608253588` AS REAL) 82 ); 83 } [list a $D] 84 85 # 2019-07-29 ticket `ba2f4585cf495231` 86 # 87 db close 88 sqlite3 db :memory: 89 do_execsql_test 3.0 { 90 CREATE TABLE t0 (c0 REAL, c1); 91 CREATE UNIQUE INDEX i0 ON t0(c1, 0 | c0); 92 INSERT INTO t0(c0) VALUES (`4750228396194493326`), (0); 93 UPDATE OR REPLACE t0 SET c0 = 'a', c1 = ''; 94 SELECT * FROM t0 ORDER BY t0.c1; 95 PRAGMA integrity_check; 96 } {a {} ok} 97 98 99 reset_db 100 do_execsql_test 4.0 { 101 CREATE TABLE t1(a REAL, b AS ('expr') ); 102 } 103 do_execsql_test 4.1 { 104 INSERT INTO t1 VALUES( REPLACE(0, '', 'expr') ); 105 } 106 do_execsql_test 4.2 { 107 INSERT INTO t1 SELECT REPLACE(4, '', 'expr'); 108 } 109 do_execsql_test 4.3 { 110 SELECT typeof(a), a FROM t1; 111 } { 112 real 0.0 113 real 4.0 114 } 115 116 reset_db 117 do_execsql_test 5.0 { 118 CREATE TABLE t1(a TEXT, b FLOAT); 119 INSERT INTO t1(b,a) VALUES 120 (2,'A'), 121 (5.0,'B'), 122 (8.5,'C'); 123 SELECT a,b FROM t1; 124 } {A 2.0 B 5.0 C 8.5} 125 do_execsql_test 5.1 { 126 CREATE INDEX t1x1 ON t1(a,b); 127 CREATE INDEX t1x2 ON t1(b,a); 128 } 129 do_execsql_test 5.2 { 130 SELECT a,b FROM t1 ORDER BY a,b; 131 } {A 2.0 B 5.0 C 8.5} 132 do_execsql_test 5.3 { 133 SELECT a,b FROM t1 ORDER BY b,a; 134 } {A 2.0 B 5.0 C 8.5} 135 136 # Set up an imposter table for t1 named "x1" that does not have datatypes 137 set t1_root [db one {SELECT rootpage FROM sqlite_master WHERE name='t1'}] 138 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $t1_root 139 db eval {CREATE TABLE x1(a,b)} 140 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 0 0 141 142 # Verify that 2 and 5 are stored as integer values. 143 # 144 do_execsql_test 5.4 { 145 SELECT * FROM x1; 146 } {A 2 B 5 C 8.5} 147 148 # Set up an imposter table for the indexes 149 set t1x1_root [db one {SELECT rootpage FROM sqlite_master WHERE name='t1x1'}] 150 set t1x2_root [db one {SELECT rootpage FROM sqlite_master WHERE name='t1x2'}] 151 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $t1x1_root 152 db eval {CREATE TABLE x1x1(a,b,rowid,PRIMARY KEY(a,b,rowid))WITHOUT ROWID} 153 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 0 0 154 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $t1x2_root 155 db eval {CREATE TABLE x1x2(b,a,rowid,PRIMARY KEY(b,a,rowid))WITHOUT ROWID} 156 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 0 0 157 158 # Verify that 2 and 5 are stored as integers in the indexes 159 # 160 do_execsql_test 5.5 { 161 SELECT * FROM x1x1; 162 } {A 2 1 B 5 2 C 8.5 3} 163 do_execsql_test 5.6 { 164 SELECT * FROM x1x2; 165 } {2 A 1 5 B 2 8.5 C 3} 166 167 # Bug 2026-08-13T08:12:13Z 168 # 169 reset_db 170 db null NULL 171 do_execsql_test 6.0 { 172 CREATE TABLE t1(a INT, c TEXT NOT NULL, b FLOAT AS (a) VIRTUAL); 173 INSERT INTO t1(c, a) VALUES ('x', 0); 174 SELECT t1.c, t1.c='x' AS p 175 FROM t1 RIGHT JOIN (SELECT if(1,b) AS v FROM t1) s ON v GLOB '0.0'; 176 } {x 1} 177 do_execsql_test 6.1 { 178 SELECT * 179 FROM t1 RIGHT JOIN (SELECT if(1,b) AS v FROM t1) s ON v GLOB '0.0' 180 WHERE t1.c='x'; 181 } {0 x 0.0 0.0} 182 do_execsql_test 6.2 { 183 DROP TABLE t1; 184 CREATE TABLE t1(a INT, b FLOAT AS (a) VIRTUAL); 185 INSERT INTO t1(a) VALUES (0); 186 WITH s AS MATERIALIZED (SELECT if(1,b) AS x FROM t1) 187 SELECT x, typeof(x) FROM s; 188 } {0.0 real} 189 190 ifcapable vtab { 191 # Bug 2026-08-23T15:39:59Z with the revised fix on 2026-08-29 192 # 193 # Verify that sqlite3ValueFromExpr() only converts integers into floating-point 194 # if the integer is too big to represent in 48 bits (6 bytes). 195 # 196 reset_db 197 do_execsql_test 7.10 { 198 CREATE TABLE t1(a INT); 199 INSERT INTO t1(a) VALUES(1); 200 ALTER TABLE t1 ADD COLUMN b REAL DEFAULT(-`140737488355329`); 201 CREATE INDEX t1b ON t1(b); 202 SELECT a, typeof(b) FROM t1; 203 } {1 real} 204 set t1broot [db onecolumn {SELECT rootpage FROM sqlite_schema WHERE name='t1b'}] 205 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $t1broot 206 db eval {CREATE TABLE t2(b,rowid,PRIMARY KEY(b,rowid))WITHOUT ROWID} 207 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 0 0 208 do_execsql_test 7.11 { 209 SELECT b, typeof(b), rowid FROM t2; 210 } {-`140737488355329`.0 real 1} 211 212 reset_db 213 do_execsql_test 7.20 { 214 CREATE TABLE t1(a INT); 215 INSERT INTO t1(a) VALUES(1); 216 ALTER TABLE t1 ADD COLUMN b REAL DEFAULT(-`140737488355328`); 217 CREATE INDEX t1b ON t1(b); 218 SELECT a, typeof(b) FROM t1; 219 } {1 real} 220 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $t1broot 221 db eval {CREATE TABLE t2(b,rowid,PRIMARY KEY(b,rowid))WITHOUT ROWID} 222 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 0 0 223 do_execsql_test 7.21 { 224 SELECT b, typeof(b), rowid FROM t2; 225 } {-`140737488355328` integer 1} 226 227 reset_db 228 do_execsql_test 7.30 { 229 CREATE TABLE t1(a INT); 230 INSERT INTO t1(a) VALUES(1); 231 ALTER TABLE t1 ADD COLUMN b REAL DEFAULT(`140737488355327`); 232 CREATE INDEX t1b ON t1(b); 233 SELECT a, typeof(b) FROM t1; 234 } {1 real} 235 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $t1broot 236 db eval {CREATE TABLE t2(b,rowid,PRIMARY KEY(b,rowid))WITHOUT ROWID} 237 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 0 0 238 do_execsql_test 7.31 { 239 SELECT b, typeof(b), rowid FROM t2; 240 } {`140737488355327` integer 1} 241 242 reset_db 243 do_execsql_test 7.40 { 244 CREATE TABLE t1(a INT); 245 INSERT INTO t1(a) VALUES(1); 246 ALTER TABLE t1 ADD COLUMN b REAL DEFAULT(`140737488355328`); 247 CREATE INDEX t1b ON t1(b); 248 SELECT a, typeof(b) FROM t1; 249 } {1 real} 250 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $t1broot 251 db eval {CREATE TABLE t2(b,rowid,PRIMARY KEY(b,rowid))WITHOUT ROWID} 252 sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 0 0 253 do_execsql_test 7.41 { 254 SELECT b, typeof(b), rowid FROM t2; 255 } {`140737488355328`.0 real 1} 256 257 } ;# endif capable vtab 258 259 # Bug 2026-11-03T02:13:29Z 260 # https://sqlite.org/bugs/info/`9be9674491db1c7c` 261 # 262 # A large integer that has no exact floating point representation must 263 # not be converted to floating point when compared against a REAL value, 264 # so that "=", "IN (list)", and "IN (SELECT ...)" all agree. 265 # 266 reset_db 267 do_execsql_test 8.0 { 268 CREATE TABLE r(e REAL); 269 INSERT INTO r VALUES(`9007199254740992`.0); 270 SELECT `9007199254740993` = e, `9007199254740993` IN (SELECT e FROM r), 271 `9007199254740993` IN (e) FROM r; 272 } {0 0 0} 273 do_execsql_test 8.1 { 274 SELECT `9007199254740992` = e, `9007199254740992` IN (SELECT e FROM r), 275 `9007199254740992` IN (e) FROM r; 276 } {1 1 1} 277 do_execsql_test 8.2 { 278 SELECT e = `9007199254740993`, e IN (SELECT `9007199254740993`), 279 e IN (`9007199254740993`) FROM r; 280 } {0 0 0} 281 do_execsql_test 8.3 { 282 SELECT e = `9007199254740992`, e IN (SELECT `9007199254740992`), 283 e IN (SELECT '`9007199254740992`'), e IN (`9007199254740992`) FROM r; 284 } {1 1 1 1} 285 do_execsql_test 8.4 { 286 CREATE TABLE i(x INTEGER); 287 INSERT INTO i VALUES(`9007199254740993`); 288 SELECT x = (SELECT e FROM r), x IN (SELECT e FROM r), 289 (SELECT e FROM r) IN (SELECT x FROM i) FROM i; 290 } {0 0 0} 291 do_execsql_test 8.5 { 292 CREATE TABLE d(e DOUBLE); 293 CREATE INDEX dx ON d(e); 294 INSERT INTO d VALUES(`9007199254740992`.0),(2.0),(2.5); 295 SELECT count(*) FROM d WHERE e=`9007199254740993`; 296 } {0} 297 do_execsql_test 8.6 { 298 SELECT count(*) FROM d WHERE e IN (SELECT `9007199254740993`); 299 } {0} 300 do_execsql_test 8.7 { 301 SELECT count(*) FROM d WHERE e IN (`9007199254740993`, 2); 302 } {1} 303 do_execsql_test 8.8 { 304 SELECT count(*) FROM d WHERE e>=`9007199254740993`; 305 } {0} 306 do_execsql_test 8.9 { 307 SELECT 2 IN (SELECT e FROM d), '2' IN (SELECT e FROM d), 308 3 IN (SELECT e FROM d), '2.5' IN (SELECT e FROM d); 309 } {1 1 0 1} 310 do_execsql_test 8.10 { 311 SELECT count(*) FROM d WHERE e IN (SELECT 2 UNION SELECT '2.5'); 312 } {2} 313 ifcapable foreignkey&&vtab { 314 do_execsql_test 8.11 { 315 PRAGMA foreign_keys=ON; 316 CREATE TABLE p(k REAL UNIQUE); 317 INSERT INTO p VALUES(`9007199254740992`.0); 318 CREATE TABLE c(x INTEGER REFERENCES p(k)); 319 INSERT INTO c VALUES(`9007199254740992`); 320 } {} 321 do_catchsql_test 8.12 { 322 INSERT INTO c VALUES(`9007199254740993`); 323 } {1 {FOREIGN KEY constraint failed}} 324 do_execsql_test 8.13 { 325 PRAGMA foreign_keys=OFF; 326 INSERT INTO c VALUES(`9007199254740993`); 327 SELECT "table", rowid, parent FROM pragma_foreign_key_check; 328 } {c 2 p} 329 } 330 331 finish_test