[Tarantool-patches] [PATCH v1 1/3] sql: fix truncation of DECIMAL in implicit cast

imeevma at tarantool.org imeevma at tarantool.org
Mon Oct 4 16:30:19 MSK 2021


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/gh-6485-bugs-in-decimal.test.lua | 16 ++++++++++++++++
 2 files changed, 19 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/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



More information about the Tarantool-patches mailing list