Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH v1 1/1] sql: disallow cast of negative DOUBLE to UNSIGNED
@ 2021-07-21 15:05 Mergen Imeev via Tarantool-patches
  2021-07-26 19:41 ` Vladislav Shpilevoy via Tarantool-patches
  0 siblings, 1 reply; 5+ messages in thread
From: Mergen Imeev via Tarantool-patches @ 2021-07-21 15:05 UTC (permalink / raw)
  To: v.shpilevoy; +Cc: tarantool-patches

Prior to this patch it was possible to use CAST(<double> AS UNSIGNED)
and get negative result. For example, result of CAST(-1.5 AS UNSIGNED)
was -1. This patch removes this possiblity. Now such cast can only
return UNSIGNED value.

Closes #6010
---
https://github.com/tarantool/tarantool/issues/6010
https://github.com/tarantool/tarantool/tree/imeevma/gh-6010-disallow-double-to-int-cast

 ...-6010-disallow-negative-double-to-unsigned-cast |  4 ++++
 src/box/sql/mem.c                                  |  2 +-
 test/sql-tap/numcast.test.lua                      | 14 +++++++++++++-
 test/sql/types.result                              |  7 ++-----
 4 files changed, 20 insertions(+), 7 deletions(-)
 create mode 100644 changelogs/unreleased/gh-6010-disallow-negative-double-to-unsigned-cast

diff --git a/changelogs/unreleased/gh-6010-disallow-negative-double-to-unsigned-cast b/changelogs/unreleased/gh-6010-disallow-negative-double-to-unsigned-cast
new file mode 100644
index 000000000..566be594f
--- /dev/null
+++ b/changelogs/unreleased/gh-6010-disallow-negative-double-to-unsigned-cast
@@ -0,0 +1,4 @@
+## bugfix/sql
+
+* Fixed explicit cast of negative DOUBLE to UNSIGNED, which could return
+negative result (gh-6010).
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index fa80f6d5a..e4ce233e0 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -1061,7 +1061,7 @@ mem_cast_explicit(struct Mem *mem, enum field_type type)
 		case MEM_TYPE_BIN:
 			return bytes_to_uint(mem);
 		case MEM_TYPE_DOUBLE:
-			return double_to_int(mem);
+			return double_to_uint(mem);
 		case MEM_TYPE_BOOL:
 			return bool_to_int(mem);
 		default:
diff --git a/test/sql-tap/numcast.test.lua b/test/sql-tap/numcast.test.lua
index b3aa64e76..20aea3c4b 100755
--- a/test/sql-tap/numcast.test.lua
+++ b/test/sql-tap/numcast.test.lua
@@ -1,6 +1,6 @@
 #!/usr/bin/env tarantool
 local test = require("sqltester")
-test:plan(31)
+test:plan(32)
 
 --!./tcltestrunner.lua
 -- 2013 March 20
@@ -249,4 +249,16 @@ test:do_execsql_test(
         0 , 0.33333333333333, 0.33333333333333
 })
 
+--
+-- gh-6010: Make sure that a negative DOUBLE value cannot be explicitly cast to
+-- UNSIGNED.
+--
+test:do_catchsql_test(
+    "numcast-4",
+    [[
+        SELECT CAST(-2.5 AS UNSIGNED);
+    ]], {
+        1, "Type mismatch: can not convert double(-2.5) to unsigned"
+})
+
 test:finish_test()
diff --git a/test/sql/types.result b/test/sql/types.result
index a0b8668be..c1e1a8ef1 100644
--- a/test/sql/types.result
+++ b/test/sql/types.result
@@ -1097,11 +1097,8 @@ box.execute("SELECT CAST(1.5 AS UNSIGNED);")
 ...
 box.execute("SELECT CAST(-1.5 AS UNSIGNED);")
 ---
-- metadata:
-  - name: COLUMN_1
-    type: unsigned
-  rows:
-  - [-1]
+- null
+- 'Type mismatch: can not convert double(-1.5) to unsigned'
 ...
 box.execute("SELECT CAST(true AS UNSIGNED);")
 ---
-- 
2.25.1


^ permalink raw reply	[flat|nested] 5+ messages in thread
* [Tarantool-patches] [PATCH v1 1/1] sql: disallow cast of negative DOUBLE to UNSIGNED
@ 2021-07-27  7:41 Mergen Imeev via Tarantool-patches
  2021-07-28  8:56 ` Timur Safin via Tarantool-patches
  2021-07-28 16:24 ` Kirill Yukhin via Tarantool-patches
  0 siblings, 2 replies; 5+ messages in thread
From: Mergen Imeev via Tarantool-patches @ 2021-07-27  7:41 UTC (permalink / raw)
  To: tsafin; +Cc: tarantool-patches

Prior to this patch it was possible to use CAST(<double> AS UNSIGNED)
and get negative result. For example, result of CAST(-1.5 AS UNSIGNED)
was -1. This patch removes this possibility. Now such cast can only
return UNSIGNED value.

Closes #6010
---
https://github.com/tarantool/tarantool/issues/6010
https://github.com/tarantool/tarantool/tree/imeevma/gh-6010-disallow-double-to-int-cast

 ...-6010-disallow-negative-double-to-unsigned-cast |  4 ++++
 src/box/sql/mem.c                                  |  2 +-
 test/sql-tap/numcast.test.lua                      | 14 +++++++++++++-
 test/sql/types.result                              |  7 ++-----
 4 files changed, 20 insertions(+), 7 deletions(-)
 create mode 100644 changelogs/unreleased/gh-6010-disallow-negative-double-to-unsigned-cast

diff --git a/changelogs/unreleased/gh-6010-disallow-negative-double-to-unsigned-cast b/changelogs/unreleased/gh-6010-disallow-negative-double-to-unsigned-cast
new file mode 100644
index 000000000..566be594f
--- /dev/null
+++ b/changelogs/unreleased/gh-6010-disallow-negative-double-to-unsigned-cast
@@ -0,0 +1,4 @@
+## bugfix/sql
+
+* Fixed explicit cast of negative DOUBLE to UNSIGNED, which could return
+negative result (gh-6010).
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index fa80f6d5a..e4ce233e0 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -1061,7 +1061,7 @@ mem_cast_explicit(struct Mem *mem, enum field_type type)
 		case MEM_TYPE_BIN:
 			return bytes_to_uint(mem);
 		case MEM_TYPE_DOUBLE:
-			return double_to_int(mem);
+			return double_to_uint(mem);
 		case MEM_TYPE_BOOL:
 			return bool_to_int(mem);
 		default:
diff --git a/test/sql-tap/numcast.test.lua b/test/sql-tap/numcast.test.lua
index b3aa64e76..20aea3c4b 100755
--- a/test/sql-tap/numcast.test.lua
+++ b/test/sql-tap/numcast.test.lua
@@ -1,6 +1,6 @@
 #!/usr/bin/env tarantool
 local test = require("sqltester")
-test:plan(31)
+test:plan(32)
 
 --!./tcltestrunner.lua
 -- 2013 March 20
@@ -249,4 +249,16 @@ test:do_execsql_test(
         0 , 0.33333333333333, 0.33333333333333
 })
 
+--
+-- gh-6010: Make sure that a negative DOUBLE value cannot be explicitly cast to
+-- UNSIGNED.
+--
+test:do_catchsql_test(
+    "numcast-4",
+    [[
+        SELECT CAST(-2.5 AS UNSIGNED);
+    ]], {
+        1, "Type mismatch: can not convert double(-2.5) to unsigned"
+})
+
 test:finish_test()
diff --git a/test/sql/types.result b/test/sql/types.result
index a0b8668be..c1e1a8ef1 100644
--- a/test/sql/types.result
+++ b/test/sql/types.result
@@ -1097,11 +1097,8 @@ box.execute("SELECT CAST(1.5 AS UNSIGNED);")
 ...
 box.execute("SELECT CAST(-1.5 AS UNSIGNED);")
 ---
-- metadata:
-  - name: COLUMN_1
-    type: unsigned
-  rows:
-  - [-1]
+- null
+- 'Type mismatch: can not convert double(-1.5) to unsigned'
 ...
 box.execute("SELECT CAST(true AS UNSIGNED);")
 ---
-- 
2.25.1


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

end of thread, other threads:[~2021-07-28 16:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-21 15:05 [Tarantool-patches] [PATCH v1 1/1] sql: disallow cast of negative DOUBLE to UNSIGNED Mergen Imeev via Tarantool-patches
2021-07-26 19:41 ` Vladislav Shpilevoy via Tarantool-patches
2021-07-27  7:41 Mergen Imeev via Tarantool-patches
2021-07-28  8:56 ` Timur Safin via Tarantool-patches
2021-07-28 16:24 ` Kirill Yukhin via Tarantool-patches

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