From 18a5ee8e1919e32233412fe18bb32303a8451fe5 Mon Sep 17 00:00:00 2001 From: Mergen Imeev <imeevma@gmail.com> Date: Sat, 25 Sep 2021 16:08:50 +0300 Subject: [PATCH] sql: fix truncation of DECIMAL in implicit cast 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 5e23c901c5..b0eba303e7 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 a6f03307f1..9d318a4569 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 0000000000..3f63f2b76c --- /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() -- GitLab