From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: v.shpilevoy@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v1 2/3] sql: fix cast of small negative DECIMAL to INTEGER
Date: Mon, 4 Oct 2021 16:30:21 +0300 [thread overview]
Message-ID: <fb8295f4ce4a3e8295d13f9c6a8133b0510faaf1.1633352298.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1633352298.git.imeevma@gmail.com>
This patch fixes an assertion when casting DECIMAL value less than 0 and
greater than -1 to INTEGER.
Part of #6485
---
src/box/sql/mem.c | 9 +++----
test/sql-tap/gh-6485-bugs-in-decimal.test.lua | 24 ++++++++++++++++++-
2 files changed, 28 insertions(+), 5 deletions(-)
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index b0eba303e..bc6f9b35a 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -1106,9 +1106,9 @@ dec_to_int(struct Mem *mem)
int64_t i;
if (decimal_to_int64(&mem->u.d, &i) == NULL)
return -1;
- assert(i < 0);
+ assert(i <= 0);
mem->u.i = i;
- mem->type = MEM_TYPE_INT;
+ mem->type = i == 0 ? MEM_TYPE_UINT : MEM_TYPE_INT;
mem->flags = 0;
return 0;
}
@@ -1137,14 +1137,15 @@ dec_to_int_forced(struct Mem *mem)
bool is_dec_int = decimal_is_int(&mem->u.d);
if (decimal_is_neg(&mem->u.d)) {
int64_t i;
- mem->type = MEM_TYPE_INT;
mem->flags = 0;
if (decimal_to_int64(&mem->u.d, &i) == NULL) {
mem->u.i = INT64_MIN;
+ mem->type = MEM_TYPE_INT;
return -1;
}
- assert(i < 0);
+ assert(i <= 0);
mem->u.i = i;
+ mem->type = i == 0 ? MEM_TYPE_UINT : MEM_TYPE_INT;
/*
* Decimal is floored when cast to int, which means that after
* cast it becomes bigger if it was not integer.
diff --git a/test/sql-tap/gh-6485-bugs-in-decimal.test.lua b/test/sql-tap/gh-6485-bugs-in-decimal.test.lua
index 3f63f2b76..0b9b2ea0a 100755
--- a/test/sql-tap/gh-6485-bugs-in-decimal.test.lua
+++ b/test/sql-tap/gh-6485-bugs-in-decimal.test.lua
@@ -1,6 +1,6 @@
#!/usr/bin/env tarantool
local test = require("sqltester")
-test:plan(1)
+test:plan(3)
-- Make sure DECIMAL is not truncated when used in an index.
test:do_execsql_test(
@@ -13,4 +13,26 @@ test:do_execsql_test(
]], {
})
+--
+-- Make sure that DECIMAL greater than -1 and less than 0 are correctly cast to
+-- INTEGER.
+--
+test:do_execsql_test(
+ "gh-6485-2",
+ [[
+ SELECT CAST(CAST(-0.5 AS DECIMAL) AS INTEGER);
+ ]], {
+ 0
+ })
+
+test:do_execsql_test(
+ "gh-6485-3",
+ [[
+ CREATE TABLE t(i INTEGER PRIMARY KEY);
+ INSERT INTO t VALUES(1);
+ SELECT i FROM t WHERE i IN (CAST(-0.1 AS DECIMAL), CAST(2 AS DECIMAL));
+ DROP TABLE t;
+ ]], {
+ })
+
test:finish_test()
--
2.25.1
next prev parent reply other threads:[~2021-10-04 13:31 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-04 13:30 [Tarantool-patches] [PATCH v1 0/3] Fix some bugs with DECIMAL Mergen Imeev via Tarantool-patches
2021-10-04 13:30 ` [Tarantool-patches] [PATCH v1 1/3] sql: fix truncation of DECIMAL in implicit cast Mergen Imeev via Tarantool-patches
2021-10-05 21:56 ` Vladislav Shpilevoy via Tarantool-patches
2021-10-06 10:50 ` Mergen Imeev via Tarantool-patches
2021-10-04 13:30 ` Mergen Imeev via Tarantool-patches [this message]
2021-10-04 13:30 ` [Tarantool-patches] [PATCH v1 3/3] sql: do not truncate DECIMAL in LIMIT and OFFSET Mergen Imeev via Tarantool-patches
2021-10-11 21:58 ` [Tarantool-patches] [PATCH v1 0/3] Fix some bugs with DECIMAL Vladislav Shpilevoy via Tarantool-patches
2021-10-19 6:19 Mergen Imeev via Tarantool-patches
2021-10-19 6:19 ` [Tarantool-patches] [PATCH v1 2/3] sql: fix cast of small negative DECIMAL to INTEGER Mergen Imeev via Tarantool-patches
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=fb8295f4ce4a3e8295d13f9c6a8133b0510faaf1.1633352298.git.imeevma@gmail.com \
--to=tarantool-patches@dev.tarantool.org \
--cc=imeevma@tarantool.org \
--cc=v.shpilevoy@tarantool.org \
--subject='Re: [Tarantool-patches] [PATCH v1 2/3] sql: fix cast of small negative DECIMAL to INTEGER' \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox