[tarantool-patches] [PATCH 3/3] sql: allow CAST operation from quoted float to int
Nikita Pettik
korablev at tarantool.org
Tue May 21 13:34:56 MSK 2019
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
More information about the Tarantool-patches
mailing list