Tarantool development patches archive
 help / color / mirror / Atom feed
From: Nikita Pettik <korablev@tarantool.org>
To: tarantool-patches@freelists.org
Cc: Nikita Pettik <korablev@tarantool.org>
Subject: [tarantool-patches] [PATCH 3/3] sql: allow CAST operation from quoted float to int
Date: Tue, 21 May 2019 13:34:56 +0300	[thread overview]
Message-ID: <ce8a85d739a97adf9f8a2d51311a83907016b98b.1558378847.git.korablev@tarantool.org> (raw)
In-Reply-To: <cover.1558378847.git.korablev@tarantool.org>
In-Reply-To: <cover.1558378847.git.korablev@tarantool.org>

As previous commit says, we've decided to allow all meaningful explicit
casts. One of these - conversion from string consisting from quoted
float literal to integer. Before this patch, mentioned operation was not
allowed:

SELECT CAST('1.123' AS INTEGER);
---
- error: 'Type mismatch: can not convert 1.123 to integer'
...

But anyway it can be done in two steps:

SELECT CAST(CAST('1.123' AS REAL) AS INTEGER);

So now this cast can be done in one CAST operation.

Closes #4229
---
 src/box/sql/vdbemem.c   |  8 +++++---
 test/sql/types.result   | 32 ++++++++++++++++++++++++++++++++
 test/sql/types.test.lua |  9 +++++++++
 3 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c
index da17bf601..08b649926 100644
--- a/src/box/sql/vdbemem.c
+++ b/src/box/sql/vdbemem.c
@@ -546,9 +546,11 @@ sqlVdbeMemIntegerify(Mem * pMem, bool is_forced)
 	}
 
 	double d;
-	if (sqlVdbeRealValue(pMem, &d) || (int64_t) d != d) {
-		return SQL_ERROR;
-	}
+	if (sqlVdbeRealValue(pMem, &d) != 0)
+		return -1;
+	if (d >= INT64_MAX || d < INT64_MIN)
+		return -1;
+
 	pMem->u.i = (int64_t) d;
 	MemSetTypeFlag(pMem, MEM_Int);
 	return 0;
diff --git a/test/sql/types.result b/test/sql/types.result
index bde7a2b92..200cf8201 100644
--- a/test/sql/types.result
+++ b/test/sql/types.result
@@ -247,6 +247,38 @@ box.execute("SELECT NULL LIKE s FROM t1;")
 box.space.T1:drop()
 ---
 ...
+-- gh-4229: allow explicit cast from string to integer for string
+-- values containing quoted floating point literals.
+--
+box.execute("SELECT CAST('1.123' AS INTEGER);")
+---
+- metadata:
+  - name: CAST('1.123' AS INTEGER)
+    type: integer
+  rows:
+  - [1]
+...
+box.execute("CREATE TABLE t1 (f TEXT PRIMARY KEY);")
+---
+- row_count: 1
+...
+box.execute("INSERT INTO t1 VALUES('0.0'), ('1.5'), ('3.9312453');")
+---
+- row_count: 3
+...
+box.execute("SELECT CAST(f AS INTEGER) FROM t1;")
+---
+- metadata:
+  - name: CAST(f AS INTEGER)
+    type: integer
+  rows:
+  - [0]
+  - [1]
+  - [3]
+...
+box.space.T1:drop()
+---
+...
 -- Test basic capabilities of boolean type.
 --
 box.execute("SELECT true;")
diff --git a/test/sql/types.test.lua b/test/sql/types.test.lua
index 8e1745e7c..39de18b82 100644
--- a/test/sql/types.test.lua
+++ b/test/sql/types.test.lua
@@ -71,6 +71,15 @@ box.execute("SELECT * FROM t1 WHERE 'int' LIKE 4;")
 box.execute("SELECT NULL LIKE s FROM t1;")
 box.space.T1:drop()
 
+-- gh-4229: allow explicit cast from string to integer for string
+-- values containing quoted floating point literals.
+--
+box.execute("SELECT CAST('1.123' AS INTEGER);")
+box.execute("CREATE TABLE t1 (f TEXT PRIMARY KEY);")
+box.execute("INSERT INTO t1 VALUES('0.0'), ('1.5'), ('3.9312453');")
+box.execute("SELECT CAST(f AS INTEGER) FROM t1;")
+box.space.T1:drop()
+
 -- Test basic capabilities of boolean type.
 --
 box.execute("SELECT true;")
-- 
2.15.1

  parent reply	other threads:[~2019-05-21 10:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-21 10:34 [tarantool-patches] [PATCH 0/3] Fix CAST operation Nikita Pettik
2019-05-21 10:34 ` [tarantool-patches] [PATCH 1/3] sql: remove redundant conversion from OP_AddImm Nikita Pettik
2019-05-21 10:34 ` [tarantool-patches] [PATCH 2/3] sql: allow CAST operation from REAL to BOOLEAN Nikita Pettik
2019-05-21 10:34 ` Nikita Pettik [this message]
2019-05-27 20:43 ` [tarantool-patches] Re: [PATCH 0/3] Fix CAST operation Vladislav Shpilevoy
2019-05-28  1:39 ` Kirill Yukhin

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=ce8a85d739a97adf9f8a2d51311a83907016b98b.1558378847.git.korablev@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [tarantool-patches] [PATCH 3/3] sql: allow CAST operation from quoted float to int' \
    /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