diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index 8ea1df80f5ee8f0ea3fbf87625323049d1aaeaec..6ebc5f0278fe54398959d0fe35f48dbcda8c8ef2 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -322,10 +322,13 @@ mem_apply_type(struct Mem *record, enum field_type type) if ((record->flags & MEM_UInt) == MEM_UInt) return 0; if ((record->flags & MEM_Real) == MEM_Real) { - int64_t i = (int64_t) record->u.r; - if (i == record->u.r) - mem_set_int(record, record->u.r, - record->u.r <= -1); + double d = record->u.r; + int64_t i = (int64_t) d; + uint64_t u = (uint64_t) d; + if (i == d) + mem_set_int(record, i, i <= -1); + else if (u == d) + mem_set_u64(record, u); return 0; } if (sqlVdbeMemIntegerify(record) != 0) diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c index e2e2b7aa8c7241c5061e3cbc1d3b1fcbdec69b5e..407b42e95ea86f99136a490bed8a33b923764e10 100644 --- a/src/box/sql/vdbemem.c +++ b/src/box/sql/vdbemem.c @@ -568,10 +568,15 @@ sqlVdbeMemIntegerify(struct Mem *pMem) double d; if (sqlVdbeRealValue(pMem, &d) != 0) return -1; - if (d >= INT64_MAX || d < INT64_MIN) - return -1; - mem_set_int(pMem, d, d <= -1); - return 0; + if (d < INT64_MAX && d >= INT64_MIN) { + mem_set_int(pMem, d, d <= -1); + return 0; + } + if (d >= INT64_MAX && d < UINT64_MAX) { + mem_set_u64(pMem, d); + return 0; + } + return -1; } /* diff --git a/test/sql-tap/numcast.test.lua b/test/sql-tap/numcast.test.lua index 9f7248577c9cf7d4dda83706645a615f0793ae3d..07117d08eedfe640aa9ff121cddc5ae5dc511312 100755 --- a/test/sql-tap/numcast.test.lua +++ b/test/sql-tap/numcast.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool test = require("sqltester") -test:plan(11) +test:plan(20) --!./tcltestrunner.lua -- 2013 March 20 @@ -65,5 +65,86 @@ for _, enc in ipairs({"utf8"}) do end end +-- +-- gh-4526: make sure that DOUBLE values that more than +-- 9223372036854775296 and less than 18446744073709551616 can be +-- converted to INTEGER or UNSIGNED. +-- +test:do_execsql_test( + "cast-2.1", + [[ + SELECT CAST((9223372036854775297.01) AS INTEGER); + ]], { + 9223372036854775808ULL + }) + +test:do_execsql_test( + "cast-2.2", + [[ + SELECT CAST((18000000000000000000.) AS INTEGER); + ]], { + 18000000000000000000ULL + }) + +test:do_execsql_test( + "cast-2.3", + [[ + SELECT CAST((9223372036854775297.01) AS UNSIGNED); + ]], { + 9223372036854775808ULL + }) + +test:do_execsql_test( + "cast-2.4", + [[ + SELECT CAST((18000000000000000000.) AS UNSIGNED); + ]], { + 18000000000000000000ULL + }) + +test:do_catchsql_test( + "cast-2.5", + [[ + SELECT CAST((20000000000000000000.) AS UNSIGNED); + ]], { + 1,"Type mismatch: can not convert 2.0e+19 to unsigned" + }) + +test:do_execsql_test( + "cast-2.6", + [[ + CREATE TABLE t (i INTEGER PRIMARY KEY); + INSERT INTO t VALUES(9223372036854775297.01); + SELECT * FROM t; + ]], { + 9223372036854775808ULL + }) + +test:do_execsql_test( + "cast-2.7", + [[ + INSERT INTO t VALUES(18000000000000000000.01); + SELECT * FROM t; + ]], { + 9223372036854775808ULL,18000000000000000000ULL + }) + +test:do_catchsql_test( + "cast-2.8", + [[ + INSERT INTO t VALUES(20000000000000000000.01); + SELECT * FROM t; + ]], { + 1,"Tuple field 1 type does not match one required by operation: expected integer" + }) + +test:do_catchsql_test( + "cast-2.9", + [[ + INSERT INTO t VALUES(2.1); + SELECT * FROM t; + ]], { + 1,"Tuple field 1 type does not match one required by operation: expected integer" + }) test:finish_test() diff --git a/test/sql/integer-overflow.result b/test/sql/integer-overflow.result index 223ba023e3ef523aa98c2a24243d537354f67fb3..d6223ffdb9ad24b13c116ce48347ad94c0c553ef 100644 --- a/test/sql/integer-overflow.result +++ b/test/sql/integer-overflow.result @@ -110,15 +110,15 @@ box.execute('SELECT CAST(\'18446744073709551616\' AS INTEGER);') - 'Type mismatch: can not convert 18446744073709551616 to integer' ... -- Due to inexact represantation of large integers in terms of --- floating point numbers, numerics with value < INT64_MAX --- have INT64_MAX + 1 value in integer representation: --- float 9223372036854775800 -> int (9223372036854775808), --- with error due to conversion = 8. +-- floating point numbers, numerics with value < UINT64_MAX +-- have UINT64_MAX + 1 value in integer representation: +-- float 18446744073709551600 -> int (18446744073709551616), +-- with error due to conversion = 16. -- -box.execute('SELECT CAST(9223372036854775807.0 AS INTEGER);') +box.execute('SELECT CAST(18446744073709551600. AS INTEGER);') --- - null -- 'Type mismatch: can not convert 9.22337203685478e+18 to integer' +- 'Type mismatch: can not convert 1.84467440737096e+19 to integer' ... -- gh-3810: make sure that if space contains integers in range -- [INT64_MAX, UINT64_MAX], they are handled inside SQL in a diff --git a/test/sql/integer-overflow.test.lua b/test/sql/integer-overflow.test.lua index 1b3e8cec1dbfe8d48b2a1332fdd2179c7d3b69f8..17051f9a2e8a9a0c465fc3a35cc4def5226999dd 100644 --- a/test/sql/integer-overflow.test.lua +++ b/test/sql/integer-overflow.test.lua @@ -27,12 +27,12 @@ box.execute('SELECT 18446744073709551616;') box.execute('SELECT CAST(\'9223372036854775808\' AS INTEGER);') box.execute('SELECT CAST(\'18446744073709551616\' AS INTEGER);') -- Due to inexact represantation of large integers in terms of --- floating point numbers, numerics with value < INT64_MAX --- have INT64_MAX + 1 value in integer representation: --- float 9223372036854775800 -> int (9223372036854775808), --- with error due to conversion = 8. +-- floating point numbers, numerics with value < UINT64_MAX +-- have UINT64_MAX + 1 value in integer representation: +-- float 18446744073709551600 -> int (18446744073709551616), +-- with error due to conversion = 16. -- -box.execute('SELECT CAST(9223372036854775807.0 AS INTEGER);') +box.execute('SELECT CAST(18446744073709551600. AS INTEGER);') -- gh-3810: make sure that if space contains integers in range -- [INT64_MAX, UINT64_MAX], they are handled inside SQL in a -- proper way, which now means that an error is raised.