Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH v1 1/1] sql: allow to convert big real values to integer
@ 2019-11-05 17:42 imeevma
  2019-11-21  1:19 ` Nikita Pettik
  0 siblings, 1 reply; 4+ messages in thread
From: imeevma @ 2019-11-05 17:42 UTC (permalink / raw)
  To: korablev; +Cc: tarantool-patches

This patch fixes a bug that prevented the conversion of real
values that are greater than INT64_MAX and less than UINT64_MAX to
INTEGER and UNSIGNED.

Closes #4526
---
https://github.com/tarantool/tarantool/issues/4526
https://github.com/tarantool/tarantool/tree/imeevma/gh-4526-big-float-to-int-conversation

 src/box/sql/vdbemem.c              | 14 ++++++++----
 test/sql-tap/numcast.test.lua      | 46 +++++++++++++++++++++++++++++++++++++-
 test/sql/integer-overflow.result   |  4 ++--
 test/sql/integer-overflow.test.lua |  2 +-
 4 files changed, 58 insertions(+), 8 deletions(-)

diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c
index 2d37b62..82daac7 100644
--- a/src/box/sql/vdbemem.c
+++ b/src/box/sql/vdbemem.c
@@ -564,10 +564,16 @@ sqlVdbeMemIntegerify(Mem * pMem, bool is_forced)
 		mem_set_int(pMem, i, is_neg);
 		return 0;
 	} else if ((pMem->flags & MEM_Real) != 0 && is_forced) {
-		if (pMem->u.r >= INT64_MAX || pMem->u.r < INT64_MIN)
-			return -1;
-		mem_set_int(pMem, pMem->u.r, pMem->u.r <= -1);
-		return 0;
+		if (pMem->u.r < INT64_MAX && pMem->u.r >= INT64_MIN) {
+			mem_set_int(pMem, pMem->u.r, pMem->u.r <= -1);
+			return 0;
+		}
+		if (pMem->u.r >= INT64_MAX && pMem->u.r < UINT64_MAX) {
+			uint64_t val = pMem->u.r;
+			mem_set_int(pMem, (int64_t)val, false);
+			return 0;
+		}
+		return -1;
 	}
 
 	double d;
diff --git a/test/sql-tap/numcast.test.lua b/test/sql-tap/numcast.test.lua
index 9f72485..1d0a96c 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(16)
 
 --!./tcltestrunner.lua
 -- 2013 March 20
@@ -65,5 +65,49 @@ 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:finish_test()
diff --git a/test/sql/integer-overflow.result b/test/sql/integer-overflow.result
index 223ba02..9e04304 100644
--- a/test/sql/integer-overflow.result
+++ b/test/sql/integer-overflow.result
@@ -115,10 +115,10 @@ box.execute('SELECT CAST(\'18446744073709551616\' AS INTEGER);')
 -- float 9223372036854775800 -> int (9223372036854775808),
 -- with error due to conversion = 8.
 --
-box.execute('SELECT CAST(9223372036854775807.0 AS INTEGER);')
+box.execute('SELECT CAST(18446744073709551616. 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 1b3e8ce..5260639 100644
--- a/test/sql/integer-overflow.test.lua
+++ b/test/sql/integer-overflow.test.lua
@@ -32,7 +32,7 @@ box.execute('SELECT CAST(\'18446744073709551616\' AS INTEGER);')
 -- float 9223372036854775800 -> int (9223372036854775808),
 -- with error due to conversion = 8.
 --
-box.execute('SELECT CAST(9223372036854775807.0 AS INTEGER);')
+box.execute('SELECT CAST(18446744073709551616. 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.
-- 
2.7.4

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-12-05 12:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-05 17:42 [Tarantool-patches] [PATCH v1 1/1] sql: allow to convert big real values to integer imeevma
2019-11-21  1:19 ` Nikita Pettik
2019-11-23 11:48   ` Mergen Imeev
2019-12-05 12:36     ` Nikita Pettik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox