summaryrefslogtreecommitdiffstats
path: root/test/cast.test
blob: ebfea5aa061d55ae58867e1475c0a66efc9ea13e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# 2005 June 25
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this file is testing the CAST operator.
#
# $Id: cast.test,v 1.10 2008/11/06 15:33:04 drh Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Only run these tests if the build includes the CAST operator
ifcapable !cast {
  finish_test
  return
}

# Tests for the CAST( AS blob), CAST( AS text) and CAST( AS numeric) built-ins
#
ifcapable bloblit {
  do_test cast-1.1 {
    execsql {SELECT x'616263'}
  } abc
  do_test cast-1.2 {
    execsql {SELECT typeof(x'616263')}
  } blob
  do_test cast-1.3 {
    execsql {SELECT CAST(x'616263' AS text)}
  } abc
  do_test cast-1.4 {
    execsql {SELECT typeof(CAST(x'616263' AS text))}
  } text
  do_test cast-1.5 {
    execsql {SELECT CAST(x'616263' AS numeric)}
  } 0
  do_test cast-1.6 {
    execsql {SELECT typeof(CAST(x'616263' AS numeric))}
  } integer
  do_test cast-1.7 {
    execsql {SELECT CAST(x'616263' AS blob)}
  } abc
  do_test cast-1.8 {
    execsql {SELECT typeof(CAST(x'616263' AS blob))}
  } blob
  do_test cast-1.9 {
    execsql {SELECT CAST(x'616263' AS integer)}
  } 0
  do_test cast-1.10 {
    execsql {SELECT typeof(CAST(x'616263' AS integer))}
  } integer
}
do_test cast-1.11 {
  execsql {SELECT null}
} {{}}
do_test cast-1.12 {
  execsql {SELECT typeof(NULL)}
} null
do_test cast-1.13 {
  execsql {SELECT CAST(NULL AS text)}
} {{}}
do_test cast-1.14 {
  execsql {SELECT typeof(CAST(NULL AS text))}
} null
do_test cast-1.15 {
  execsql {SELECT CAST(NULL AS numeric)}
} {{}}
do_test cast-1.16 {
  execsql {SELECT typeof(CAST(NULL AS numeric))}
} null
do_test cast-1.17 {
  execsql {SELECT CAST(NULL AS blob)}
} {{}}
do_test cast-1.18 {
  execsql {SELECT typeof(CAST(NULL AS blob))}
} null
do_test cast-1.19 {
  execsql {SELECT CAST(NULL AS integer)}
} {{}}
do_test cast-1.20 {
  execsql {SELECT typeof(CAST(NULL AS integer))}
} null
do_test cast-1.21 {
  execsql {SELECT 123}
} {123}
do_test cast-1.22 {
  execsql {SELECT typeof(123)}
} integer
do_test cast-1.23 {
  execsql {SELECT CAST(123 AS text)}
} {123}
do_test cast-1.24 {
  execsql {SELECT typeof(CAST(123 AS text))}
} text
do_test cast-1.25 {
  execsql {SELECT CAST(123 AS numeric)}
} 123
do_test cast-1.26 {
  execsql {SELECT typeof(CAST(123 AS numeric))}
} integer
do_test cast-1.27 {
  execsql {SELECT CAST(123 AS blob)}
} {123}
do_test cast-1.28 {
  execsql {SELECT typeof(CAST(123 AS blob))}
} blob
do_test cast-1.29 {
  execsql {SELECT CAST(123 AS integer)}
} {123}
do_test cast-1.30 {
  execsql {SELECT typeof(CAST(123 AS integer))}
} integer
do_test cast-1.31 {
  execsql {SELECT 123.456}
} {123.456}
do_test cast-1.32 {
  execsql {SELECT typeof(123.456)}
} real
do_test cast-1.33 {
  execsql {SELECT CAST(123.456 AS text)}
} {123.456}
do_test cast-1.34 {
  execsql {SELECT typeof(CAST(123.456 AS text))}
} text
do_test cast-1.35 {
  execsql {SELECT CAST(123.456 AS numeric)}
} 123.456
do_test cast-1.36 {
  execsql {SELECT typeof(CAST(123.456 AS numeric))}
} real
do_test cast-1.37 {
  execsql {SELECT CAST(123.456 AS blob)}
} {123.456}
do_test cast-1.38 {
  execsql {SELECT typeof(CAST(123.456 AS blob))}
} blob
do_test cast-1.39 {
  execsql {SELECT CAST(123.456 AS integer)}
} {123}
do_test cast-1.38 {
  execsql {SELECT typeof(CAST(123.456 AS integer))}
} integer
do_test cast-1.41 {
  execsql {SELECT '123abc'}
} {123abc}
do_test cast-1.42 {
  execsql {SELECT typeof('123abc')}
} text
do_test cast-1.43 {
  execsql {SELECT CAST('123abc' AS text)}
} {123abc}
do_test cast-1.44 {
  execsql {SELECT typeof(CAST('123abc' AS text))}
} text
do_test cast-1.45 {
  execsql {SELECT CAST('123abc' AS numeric)}
} 123
do_test cast-1.46 {
  execsql {SELECT typeof(CAST('123abc' AS numeric))}
} integer
do_test cast-1.47 {
  execsql {SELECT CAST('123abc' AS blob)}
} {123abc}
do_test cast-1.48 {
  execsql {SELECT typeof(CAST('123abc' AS blob))}
} blob
do_test cast-1.49 {
  execsql {SELECT CAST('123abc' AS integer)}
} 123
do_test cast-1.50 {
  execsql {SELECT typeof(CAST('123abc' AS integer))}
} integer
do_test cast-1.51 {
  execsql {SELECT CAST('123.5abc' AS numeric)}
} 123.5
do_test cast-1.53 {
  execsql {SELECT CAST('123.5abc' AS integer)}
} 123

do_test cast-1.60 {
  execsql {SELECT CAST(null AS REAL)}
} {{}}
do_test cast-1.61 {
  execsql {SELECT typeof(CAST(null AS REAL))}
} {null}
do_test cast-1.62 {
  execsql {SELECT CAST(1 AS REAL)}
} {1.0}
do_test cast-1.63 {
  execsql {SELECT typeof(CAST(1 AS REAL))}
} {real}
do_test cast-1.64 {
  execsql {SELECT CAST('1' AS REAL)}
} {1.0}
do_test cast-1.65 {
  execsql {SELECT typeof(CAST('1' AS REAL))}
} {real}
do_test cast-1.66 {
  execsql {SELECT CAST('abc' AS REAL)}
} {0.0}
do_test cast-1.67 {
  execsql {SELECT typeof(CAST('abc' AS REAL))}
} {real}
do_test cast-1.68 {
  execsql {SELECT CAST(x'31' AS REAL)}
} {1.0}
do_test cast-1.69 {
  execsql {SELECT typeof(CAST(x'31' AS REAL))}
} {real}


# Ticket #1662.  Ignore leading spaces in numbers when casting.
#
do_test cast-2.1 {
  execsql {SELECT CAST('   123' AS integer)}
} 123
do_test cast-2.2 {
  execsql {SELECT CAST('   -123.456' AS real)}
} -123.456

# ticket #2364.  Use full percision integers if possible when casting
# to numeric.  Do not fallback to real (and the corresponding 48-bit
# mantissa) unless absolutely necessary.
#
do_test cast-3.1 {
  execsql {SELECT CAST(9223372036854774800 AS integer)}
} 9223372036854774800
do_test cast-3.2 {
  execsql {SELECT CAST(9223372036854774800 AS numeric)}
} 9223372036854774800
do_realnum_test cast-3.3 {
  execsql {SELECT CAST(9223372036854774800 AS real)}
} 9.22337203685477e+18
do_test cast-3.4 {
  execsql {SELECT CAST(CAST(9223372036854774800 AS real) AS integer)}
} 9223372036854774784
do_test cast-3.5 {
  execsql {SELECT CAST(-9223372036854774800 AS integer)}
} -9223372036854774800
do_test cast-3.6 {
  execsql {SELECT CAST(-9223372036854774800 AS numeric)}
} -9223372036854774800
do_realnum_test cast-3.7 {
  execsql {SELECT CAST(-9223372036854774800 AS real)}
} -9.22337203685477e+18
do_test cast-3.8 {
  execsql {SELECT CAST(CAST(-9223372036854774800 AS real) AS integer)}
} -9223372036854774784
do_test cast-3.11 {
  execsql {SELECT CAST('9223372036854774800' AS integer)}
} 9223372036854774800
do_test cast-3.12 {
  execsql {SELECT CAST('9223372036854774800' AS numeric)}
} 9223372036854774800
do_realnum_test cast-3.13 {
  execsql {SELECT CAST('9223372036854774800' AS real)}
} 9.22337203685477e+18
ifcapable long_double {
  do_test cast-3.14 {
    execsql {SELECT CAST(CAST('9223372036854774800' AS real) AS integer)}
  } 9223372036854774784
}
do_test cast-3.15 {
  execsql {SELECT CAST('-9223372036854774800' AS integer)}
} -9223372036854774800
do_test cast-3.16 {
  execsql {SELECT CAST('-9223372036854774800' AS numeric)}
} -9223372036854774800
do_realnum_test cast-3.17 {
  execsql {SELECT CAST('-9223372036854774800' AS real)}
} -9.22337203685477e+18
ifcapable long_double {
  do_test cast-3.18 {
    execsql {SELECT CAST(CAST('-9223372036854774800' AS real) AS integer)}
  } -9223372036854774784
}
if {[db eval {PRAGMA encoding}]=="UTF-8"} {
  do_test cast-3.21 {
    execsql {SELECT CAST(x'39323233333732303336383534373734383030' AS integer)}
  } 9223372036854774800
  do_test cast-3.22 {
    execsql {SELECT CAST(x'39323233333732303336383534373734383030' AS numeric)}
  } 9223372036854774800
  do_realnum_test cast-3.23 {
    execsql {SELECT CAST(x'39323233333732303336383534373734383030' AS real)}
  } 9.22337203685477e+18
  ifcapable long_double {
    do_test cast-3.24 {
      execsql {
        SELECT CAST(CAST(x'39323233333732303336383534373734383030' AS real)
                    AS integer)
      }
    } 9223372036854774784
  }
}
do_test cast-3.31 {
  execsql {SELECT CAST(NULL AS numeric)}
} {{}}

# Test to see if it is possible to trick SQLite into reading past 
# the end of a blob when converting it to a number.
do_test cast-3.32.1 {
  set blob "1234567890"
  set DB [sqlite3_connection_pointer db]
  set ::STMT [sqlite3_prepare $DB {SELECT CAST(? AS real)} -1 TAIL]
  sqlite3_bind_blob -static $::STMT 1 $blob 5
  sqlite3_step $::STMT
} {SQLITE_ROW}
do_test cast-3.32.2 {
  sqlite3_column_int $::STMT 0
} {12345}
do_test cast-3.32.3 {
  sqlite3_finalize $::STMT
} {SQLITE_OK}


do_test cast-4.1 {
  db eval {
    CREATE TABLE t1(a);
    INSERT INTO t1 VALUES('abc');
    SELECT a, CAST(a AS integer) FROM t1;
  }
} {abc 0}
do_test cast-4.2 {
  db eval {
    SELECT CAST(a AS integer), a FROM t1;
  }
} {0 abc}
do_test cast-4.3 {
  db eval {
    SELECT a, CAST(a AS integer), a FROM t1;
  }
} {abc 0 abc}
do_test cast-4.4 {
  db eval {
    SELECT CAST(a AS integer), a, CAST(a AS real), a FROM t1;
  }
} {0 abc 0.0 abc}

# Added 2018-01-26
#
# EVIDENCE-OF: R-48741-32454 If the prefix integer is greater than
# +9223372036854775807 then the result of the cast is exactly
# +9223372036854775807.
do_execsql_test cast-5.1 {
  SELECT CAST('9223372036854775808' AS integer);
  SELECT CAST('  +000009223372036854775808' AS integer);
  SELECT CAST('12345678901234567890123' AS INTEGER);
} {9223372036854775807 9223372036854775807 9223372036854775807}

# EVIDENCE-OF: R-06028-16857 Similarly, if the prefix integer is less
# than -9223372036854775808 then the result of the cast is exactly
# -9223372036854775808.
do_execsql_test cast-5.2 {
  SELECT CAST('-9223372036854775808' AS integer);
  SELECT CAST('-9223372036854775809' AS integer);
  SELECT CAST('-12345678901234567890123' AS INTEGER);
} {-9223372036854775808 -9223372036854775808 -9223372036854775808}

# EVIDENCE-OF: R-33990-33527 When casting to INTEGER, if the text looks
# like a floating point value with an exponent, the exponent will be
# ignored because it is no part of the integer prefix.
# EVIDENCE-OF: R-24225-46995 For example, "(CAST '123e+5' AS INTEGER)"
# results in 123, not in 12300000.
do_execsql_test cast-5.3 {
  SELECT CAST('123e+5' AS INTEGER);
  SELECT CAST('123e+5' AS NUMERIC);
  SELECT CAST('123e+5' AS REAL);
} {123 12300000 12300000.0}


# The following does not have anything to do with the CAST operator,
# but it does deal with affinity transformations.
#
do_execsql_test cast-6.1 {
  DROP TABLE IF EXISTS t1;
  CREATE TABLE t1(a NUMERIC);
  INSERT INTO t1 VALUES
     ('9000000000000000001'),
     ('9000000000000000001 '),
     (' 9000000000000000001'),
     (' 9000000000000000001 ');
  SELECT * FROM t1;
} {9000000000000000001 9000000000000000001 9000000000000000001 9000000000000000001}

# 2019-06-07
# https://www.sqlite.org/src/info/4c2d7639f076aa7c
do_execsql_test cast-7.1 {
  SELECT CAST('-' AS NUMERIC);
} {0}
do_execsql_test cast-7.2 {
  SELECT CAST('-0' AS NUMERIC);
} {0}
do_execsql_test cast-7.3 {
  SELECT CAST('+' AS NUMERIC);
} {0}
do_execsql_test cast-7.4 {
  SELECT CAST('/' AS NUMERIC);
} {0}

# 2019-06-07
# https://www.sqlite.org/src/info/e8bedb2a184001bb
do_execsql_test cast-7.10 {
  SELECT '' - 2851427734582196970;
} {-2851427734582196970}
do_execsql_test cast-7.11 {
  SELECT 0 - 2851427734582196970;
} {-2851427734582196970}
do_execsql_test cast-7.12 {
  SELECT '' - 1;
} {-1}

# 2019-06-10
# https://www.sqlite.org/src/info/dd6bffbfb6e61db9
#
# EVIDENCE-OF: R-55084-10555 Casting a TEXT or BLOB value into NUMERIC
# yields either an INTEGER or a REAL result.
#
do_execsql_test cast-7.20 {
  DROP TABLE IF EXISTS t0;
  CREATE TABLE t0 (c0 TEXT);
  INSERT INTO t0(c0) VALUES ('1.0');
  SELECT CAST(c0 AS NUMERIC) FROM t0;
} {1}

# 2019-06-10
# https://sqlite.org/src/info/27de823723a41df45af3
#
do_execsql_test cast-7.30 {
  SELECT -'.';
} 0
do_execsql_test cast-7.31 {
  SELECT '.'+0;
} 0
do_execsql_test cast-7.32 {
  SELECT CAST('.' AS numeric);
} 0
do_execsql_test cast-7.33 {
  SELECT -CAST('.' AS numeric);
} 0

# 2019-06-12
# https://www.sqlite.org/src/info/674385aeba91c774
#
do_execsql_test cast-7.40 {
  SELECT CAST('-0.0' AS numeric);
} 0
do_execsql_test cast-7.41 {
  SELECT CAST('0.0' AS numeric);
} 0
do_execsql_test cast-7.42 {
  SELECT CAST('+0.0' AS numeric);
} 0
do_execsql_test cast-7.43 {
  SELECT CAST('-1.0' AS numeric);
} -1

ifcapable utf16 {
  reset_db
  execsql { PRAGMA encoding='utf16' }

  do_execsql_test cast-8.1 {
    SELECT quote(X'310032003300')==quote(substr(X'310032003300', 1))
  } 1
  do_execsql_test cast-8.2 {
    SELECT CAST(X'310032003300' AS TEXT)
         ==CAST(substr(X'310032003300', 1) AS TEXT)
  } 1
}

reset_db
do_execsql_test cast-9.0 {
  CREATE TABLE t0(c0);
  INSERT INTO t0(c0) VALUES (0);
  CREATE VIEW v1(c0, c1) AS 
    SELECT CAST(0.0 AS NUMERIC), COUNT(*) OVER () FROM t0;
  SELECT v1.c0 FROM v1, t0 WHERE v1.c0=0; 
} {0.0}

# Set the 2022-12-10 "reopen" of ticket [https://sqlite.org/src/tktview/57c47526c3]
#
do_execsql_test cast-9.1 {
  CREATE TABLE dual(dummy TEXT);
  INSERT INTO dual VALUES('X');
  SELECT CAST(4 AS NUMERIC);
} {4}
do_execsql_test cast-9.2 {
  SELECT CAST(4.0 AS NUMERIC);
} {4.0}
do_execsql_test cast-9.3 {
  SELECT CAST(4.5 AS NUMERIC);
} {4.5}
do_execsql_test cast-9.4 {
  SELECT x, typeof(x) FROM (SELECT CAST(4 AS NUMERIC) AS x) JOIN dual;
} {4 integer}
do_execsql_test cast-9.5 {
  SELECT x, typeof(x) FROM dual CROSS JOIN (SELECT CAST(4 AS NUMERIC) AS x);
} {4 integer}
do_execsql_test cast-9.10 {
  SELECT x, typeof(x) FROM (SELECT CAST(4.0 AS NUMERIC) AS x) JOIN dual;
} {4.0 real}
do_execsql_test cast-9.11 {
  SELECT x, typeof(x) FROM dual CROSS JOIN (SELECT CAST(4.0 AS NUMERIC) AS x);
} {4.0 real}
do_execsql_test cast-9.12 {
  SELECT x, typeof(x) FROM (SELECT CAST(4.5 AS NUMERIC) AS x) JOIN dual;
} {4.5 real}
do_execsql_test cast-9.13 {
  SELECT x, typeof(x) FROM dual CROSS JOIN (SELECT CAST(4.5 AS NUMERIC) AS x);
} {4.5 real}

# 2022-12-15 dbsqlfuzz c9ee6f9a0a8b8fefb02cf69de2a8b67ca39525c8
#
# Added a new SQLITE_AFF_FLEXNUM that does not try to convert int to real or
# real to int.
#
do_execsql_test cast-10.1 {
  VALUES(CAST(44 AS REAL)),(55);
} {44.0 55}
do_execsql_test cast-10.2 {
  SELECT CAST(44 AS REAL) AS 'm' UNION ALL SELECT 55;
} {44.0 55}
do_execsql_test cast-10.3 {
  SELECT * FROM (VALUES(CAST(44 AS REAL)),(55));
} {44.0 55}
do_execsql_test cast-10.4 {
  SELECT * FROM (SELECT CAST(44 AS REAL) AS 'm' UNION ALL SELECT 55);
} {44.0 55}
do_execsql_test cast-10.5 {
  SELECT * FROM dual CROSS JOIN (VALUES(CAST(44 AS REAL)),(55));
} {X 44.0 X 55}
do_execsql_test cast-10.6 {
  SELECT * FROM dual CROSS JOIN (SELECT CAST(44 AS REAL) AS 'm'
                                 UNION ALL SELECT 55);
} {X 44.0 X 55}
ifcapable vtab {
  do_execsql_test cast-10.7 {
    DROP VIEW v1;
    CREATE VIEW v1 AS SELECT CAST(44 AS REAL) AS 'm' UNION ALL SELECT 55;
    SELECT name, type FROM pragma_table_info('v1');
  } {m NUM}
  do_execsql_test cast-10.8 {
    CREATE VIEW v2 AS VALUES(CAST(44 AS REAL)),(55);
    SELECT type FROM pragma_table_info('v2');
  } {NUM}
  do_execsql_test cast-10.9 {
    SELECT * FROM v1;
  } {44.0 55}
  do_execsql_test cast-10.10 {
    SELECT * FROM v2;
  } {44.0 55}
}

finish_test