diff --git a/changelogs/unreleased/gh-6485-bug-of-decimal.md b/changelogs/unreleased/gh-6485-bug-of-decimal.md new file mode 100644 index 0000000000000000000000000000000000000000..bcf023b3165313902eacd55e2bedec94bf81ee9b --- /dev/null +++ b/changelogs/unreleased/gh-6485-bug-of-decimal.md @@ -0,0 +1,8 @@ +## bugfix/sql + +* Fixed truncation of DECIMAL during implicit cast to INTEGER in LIMIT and + OFFSET. +* Fixed truncation of DECIMAL during implicit cast to INTEGER when value is used + in an index. +* Fixed assert on cast of DECIMAL value that greater than -1.0 and less than 0.0 + to INTEGER (gh-6485). diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index bc6f9b35a94cf3849ca997cb40057d03c1fab8e2..7e94dc8bbbc71c9e400c0937a88c8db9886e4512 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -1338,7 +1338,7 @@ mem_to_int_precise(struct Mem *mem) if (mem->type == MEM_TYPE_DOUBLE) return double_to_int_precise(mem); if (mem->type == MEM_TYPE_DEC) - return dec_to_int(mem); + return dec_to_int_precise(mem); return -1; } 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 0b9b2ea0a53748c5bdb145fe13c3610587db57f1..fc2ac963d162c4d4c1f678740f1f139e7df82329 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(3) +test:plan(5) -- Make sure DECIMAL is not truncated when used in an index. test:do_execsql_test( @@ -35,4 +35,23 @@ test:do_execsql_test( ]], { }) +-- Make sure DECIMAL is not truncated when used in LIMIT and OFFSET. +test:do_catchsql_test( + "gh-6485-4", + [[ + SELECT 1 LIMIT CAST(1.5 AS DECIMAL); + ]], { + 1, [[Failed to execute SQL statement: ]].. + [[Only positive integers are allowed in the LIMIT clause]] + }) + +test:do_catchsql_test( + "gh-6485-5", + [[ + SELECT 1 LIMIT 1 OFFSET CAST(1.5 AS DECIMAL); + ]], { + 1, [[Failed to execute SQL statement: ]].. + [[Only positive integers are allowed in the OFFSET clause]] + }) + test:finish_test()