Tarantool development patches archive
 help / color / mirror / Atom feed
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 3/3] sql: do not truncate DECIMAL in LIMIT and OFFSET
Date: Mon,  4 Oct 2021 16:30:23 +0300	[thread overview]
Message-ID: <51f777fff4af90470d36f9e5af85970d0234efc4.1633352298.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1633352298.git.imeevma@gmail.com>

This patch removes the DECIMAL truncation in LIMIT and OFFSET, because
according to the implicit casting rules, DECIMAL with digits after the
decimal point cannot be implicitly cast to INTEGER.

Closes #6485
---
 .../unreleased/gh-6485-bug-of-decimal.md      |  8 +++++++
 src/box/sql/mem.c                             |  2 +-
 test/sql-tap/gh-6485-bugs-in-decimal.test.lua | 21 ++++++++++++++++++-
 3 files changed, 29 insertions(+), 2 deletions(-)
 create mode 100644 changelogs/unreleased/gh-6485-bug-of-decimal.md

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 000000000..bcf023b31
--- /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 bc6f9b35a..7e94dc8bb 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 0b9b2ea0a..fc2ac963d 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()
-- 
2.25.1


  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 ` [Tarantool-patches] [PATCH v1 2/3] sql: fix cast of small negative DECIMAL to INTEGER Mergen Imeev via Tarantool-patches
2021-10-04 13:30 ` Mergen Imeev via Tarantool-patches [this message]
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 3/3] sql: do not truncate DECIMAL in LIMIT and OFFSET 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=51f777fff4af90470d36f9e5af85970d0234efc4.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 3/3] sql: do not truncate DECIMAL in LIMIT and OFFSET' \
    /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