SQLite

Check-in [491f0f9b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:As a special case, casting '-0.0' into numeric should yield 0. Fix for ticket [674385aeba91c774].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 491f0f9bbddb6302536d99abd1ea481fd747ddcf6c6eaaacc0338d147b119081
User & Date: drh 2019-06-12 20:51:38
Context
2019-06-12
22:46
Adjust requirements marks and add new requirements tests. (check-in: ebb81dad user: drh tags: trunk)
20:51
As a special case, casting '-0.0' into numeric should yield 0. Fix for ticket [674385aeba91c774]. (check-in: 491f0f9b user: drh tags: trunk)
13:49
Handle expressions like "expr IS TRUE COLLATE xyz" in the same way as "expr IS TRUE". Fix for [4d01eda8115b10d1]. (check-in: 5c6146b5 user: dan tags: trunk)
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/vdbemem.c.

688
689
690
691
692
693
694


695
696
697
698
699
700
701

702
703
704
705
706
707
708
709
710
  pMem->u.r = sqlite3VdbeRealValue(pMem);
  MemSetTypeFlag(pMem, MEM_Real);
  return SQLITE_OK;
}

/* Compare a floating point value to an integer.  Return true if the two
** values are the same within the precision of the floating point value.


**
** For some versions of GCC on 32-bit machines, if you do the more obvious
** comparison of "r1==(double)i" you sometimes get an answer of false even
** though the r1 and (double)i values are bit-for-bit the same.
*/
int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
  double r2 = (double)i;

  return memcmp(&r1, &r2, sizeof(r1))==0
      && i >= -2251799813685248 && i < 2251799813685248;
}

/*
** Convert pMem so that it has type MEM_Real or MEM_Int.
** Invalidate any prior representations.
**
** Every effort is made to force the conversion, even if the input







>
>







>
|
|







688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
  pMem->u.r = sqlite3VdbeRealValue(pMem);
  MemSetTypeFlag(pMem, MEM_Real);
  return SQLITE_OK;
}

/* Compare a floating point value to an integer.  Return true if the two
** values are the same within the precision of the floating point value.
**
** This function assumes that i was obtained by assignment from r1.
**
** For some versions of GCC on 32-bit machines, if you do the more obvious
** comparison of "r1==(double)i" you sometimes get an answer of false even
** though the r1 and (double)i values are bit-for-bit the same.
*/
int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
  double r2 = (double)i;
  return r1==0.0
      || (memcmp(&r1, &r2, sizeof(r1))==0
          && i >= -2251799813685248 && i < 2251799813685248);
}

/*
** Convert pMem so that it has type MEM_Real or MEM_Int.
** Invalidate any prior representations.
**
** Every effort is made to force the conversion, even if the input

Changes to test/cast.test.

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
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 case-1.60 {
  execsql {SELECT CAST(null AS REAL)}
} {{}}
do_test case-1.61 {
  execsql {SELECT typeof(CAST(null AS REAL))}
} {null}
do_test case-1.62 {
  execsql {SELECT CAST(1 AS REAL)}
} {1.0}
do_test case-1.63 {
  execsql {SELECT typeof(CAST(1 AS REAL))}
} {real}
do_test case-1.64 {
  execsql {SELECT CAST('1' AS REAL)}
} {1.0}
do_test case-1.65 {
  execsql {SELECT typeof(CAST('1' AS REAL))}
} {real}
do_test case-1.66 {
  execsql {SELECT CAST('abc' AS REAL)}
} {0.0}
do_test case-1.67 {
  execsql {SELECT typeof(CAST('abc' AS REAL))}
} {real}
do_test case-1.68 {
  execsql {SELECT CAST(x'31' AS REAL)}
} {1.0}
do_test case-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 {







|


|


|


|


|


|


|


|


|


|







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
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 {
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
      execsql {
        SELECT CAST(CAST(x'39323233333732303336383534373734383030' AS real)
                    AS integer)
      }
    } 9223372036854774784
  }
}
do_test case-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"







|







295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
      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"
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
} {-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 case-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 case-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 case-7.1 {
  SELECT CAST('-' AS NUMERIC);
} {0}
do_execsql_test case-7.2 {
  SELECT CAST('-0' AS NUMERIC);
} {0}
do_execsql_test case-7.3 {
  SELECT CAST('+' AS NUMERIC);
} {0}
do_execsql_test case-7.4 {
  SELECT CAST('/' AS NUMERIC);
} {0}

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

# 2019-06-10
# https://www.sqlite.org/src/info/dd6bffbfb6e61db9
#
# EVIDENCE-OF: R-09295-61337 Casting a TEXT or BLOB value into NUMERIC







|









|












|


|


|


|





|


|


|







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
} {-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-09295-61337 Casting a TEXT or BLOB value into NUMERIC
442
443
444
445
446
447
448

















449
450
451
} 0
do_execsql_test cast-7.32 {
  SELECT CAST('.' AS numeric);
} 0
do_execsql_test cast-7.33 {
  SELECT -CAST('.' AS numeric);
} 0



















finish_test







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



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
} 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



finish_test