Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: kyukhin@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v1 1/3] sql: fix truncation of DECIMAL in implicit cast
Date: Tue, 19 Oct 2021 09:19:46 +0300	[thread overview]
Message-ID: <2c1af4cf637304b33edbb6560f88cb8d8b2c1be6.1634624290.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1634624290.git.imeevma@gmail.com>

In case the DECIMAL value is implicitly cast to INTEGER during a search
using an index, it was possible that DECIMAL would be truncated, which
is not correct according to the implicit cast rules. This patch removes
this truncation.

Part of #6485
---
 src/box/sql/mem.c                             |  5 +++--
 test/sql-tap/engine.cfg                       |  1 +
 test/sql-tap/gh-6485-bugs-in-decimal.test.lua | 16 ++++++++++++++++
 3 files changed, 20 insertions(+), 2 deletions(-)
 create mode 100755 test/sql-tap/gh-6485-bugs-in-decimal.test.lua

diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 5e23c901c..b0eba303e 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -1134,6 +1134,7 @@ static inline int
 dec_to_int_forced(struct Mem *mem)
 {
 	assert(mem->type == MEM_TYPE_DEC);
+	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;
@@ -1148,7 +1149,7 @@ dec_to_int_forced(struct Mem *mem)
 		 * Decimal is floored when cast to int, which means that after
 		 * cast it becomes bigger if it was not integer.
 		 */
-		return decimal_is_int(&mem->u.d) ? 0 : -1;
+		return is_dec_int ? 0 : -1;
 	}
 	uint64_t u;
 	mem->type = MEM_TYPE_UINT;
@@ -1162,7 +1163,7 @@ dec_to_int_forced(struct Mem *mem)
 	 * Decimal is floored when cast to uint, which means that after cast it
 	 * becomes less if it was not integer.
 	 */
-	return decimal_is_int(&mem->u.d) ? 0 : 1;
+	return is_dec_int ? 0 : 1;
 }
 
 static inline int
diff --git a/test/sql-tap/engine.cfg b/test/sql-tap/engine.cfg
index a6f03307f..9d318a456 100644
--- a/test/sql-tap/engine.cfg
+++ b/test/sql-tap/engine.cfg
@@ -39,6 +39,7 @@
     "gh-6376-wrong-double-to-dec-cmp.test.lua": {},
     "gh-4077-iproto-execute-no-bind.test.lua": {},
     "gh-6375-assert-on-unsupported-ext.test.lua": {},
+    "gh-6485-bugs-in-decimal.test.lua": {},
     "*": {
         "memtx": {"engine": "memtx"},
         "vinyl": {"engine": "vinyl"}
diff --git a/test/sql-tap/gh-6485-bugs-in-decimal.test.lua b/test/sql-tap/gh-6485-bugs-in-decimal.test.lua
new file mode 100755
index 000000000..3f63f2b76
--- /dev/null
+++ b/test/sql-tap/gh-6485-bugs-in-decimal.test.lua
@@ -0,0 +1,16 @@
+#!/usr/bin/env tarantool
+local test = require("sqltester")
+test:plan(1)
+
+-- Make sure DECIMAL is not truncated when used in an index.
+test:do_execsql_test(
+    "gh-6485-1",
+    [[
+        CREATE TABLE t(i INTEGER PRIMARY KEY);
+        INSERT INTO t VALUES(1), (2);
+        SELECT i FROM t WHERE i IN (CAST(1.1 AS DECIMAL), CAST(2.2 AS DECIMAL));
+        DROP TABLE t;
+    ]], {
+    })
+
+test:finish_test()
-- 
2.25.1


  reply	other threads:[~2021-10-19  6:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-19  6:19 [Tarantool-patches] [PATCH v1 0/3] Fix some bugs with DECIMAL Mergen Imeev via Tarantool-patches
2021-10-19  6:19 ` Mergen Imeev via Tarantool-patches [this message]
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
2021-10-19  6:19 ` [Tarantool-patches] [PATCH v1 3/3] sql: do not truncate DECIMAL in LIMIT and OFFSET Mergen Imeev via Tarantool-patches
  -- strict thread matches above, loose matches on Subject: below --
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

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=2c1af4cf637304b33edbb6560f88cb8d8b2c1be6.1634624290.git.imeevma@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=kyukhin@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v1 1/3] sql: fix truncation of DECIMAL in implicit cast' \
    /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