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
Hi! Thanks for the patch!
On 21.07.2021 17:05, imeevma@tarantool.org wrote:
> 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
possiblity -> possibility
After that LGTM.
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
LGTM, thanks! : From: imeevma@tarantool.org <imeevma@tarantool.org> : Subject: [PATCH v1 1/1] sql: disallow cast of negative DOUBLE to UNSIGNED : : 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
Hello,
On 27 июл 10:41, Mergen Imeev via Tarantool-patches wrote:
> 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
I've checked your patch into 2.7, 2.8 and master.
--
Regards, Kirill Yukhin