[tarantool-patches] [PATCH 2/3] sql: allow CAST operation from REAL to BOOLEAN

Nikita Pettik korablev at tarantool.org
Tue May 21 13:34:55 MSK 2019


It was decided that all explicit casts for which we can come up with
meaningful semantics should work. If a user requests an explicit cast,
he/she most probably knows what they are doing.
CAST from REAL to BOOLEAN is disallowed by ANSI rules. However, we allow
CAST from INT to BOOLEAN which is also prohibited by ANSI. So, basically
it is possible to covert REAL to BOOLEAN in two steps:

SELECT CAST(CAST(1.123 AS INT) AS BOOLEAN);

For the reason mentioned above, now we allow straight CAST from REAL to
BOOLEAN. Anything different from 0.0 is evaluated to TRUE.

Part of #4229
---
 src/box/sql/vdbemem.c   |  4 ++++
 test/sql/types.result   | 22 +++++++++++++++++++++-
 test/sql/types.test.lua |  2 ++
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c
index f73ea0a71..da17bf601 100644
--- a/src/box/sql/vdbemem.c
+++ b/src/box/sql/vdbemem.c
@@ -664,6 +664,10 @@ sqlVdbeMemCast(Mem * pMem, enum field_type type)
 			mem_set_bool(pMem, pMem->u.i);
 			return 0;
 		}
+		if ((pMem->flags & MEM_Real) != 0) {
+			mem_set_bool(pMem, pMem->u.r);
+			return 0;
+		}
 		if ((pMem->flags & MEM_Str) != 0) {
 			bool value;
 			if (str_cast_to_boolean(pMem->z, &value) != 0)
diff --git a/test/sql/types.result b/test/sql/types.result
index bc4518c01..bde7a2b92 100644
--- a/test/sql/types.result
+++ b/test/sql/types.result
@@ -786,7 +786,27 @@ box.execute("SELECT CAST(1 AS BOOLEAN);")
 ...
 box.execute("SELECT CAST(1.123 AS BOOLEAN);")
 ---
-- error: 'Type mismatch: can not convert 1.123 to boolean'
+- metadata:
+  - name: CAST(1.123 AS BOOLEAN)
+    type: boolean
+  rows:
+  - [true]
+...
+box.execute("SELECT CAST(0.0 AS BOOLEAN);")
+---
+- metadata:
+  - name: CAST(0.0 AS BOOLEAN)
+    type: boolean
+  rows:
+  - [false]
+...
+box.execute("SELECT CAST(0.00000001 AS BOOLEAN);")
+---
+- metadata:
+  - name: CAST(0.00000001 AS BOOLEAN)
+    type: boolean
+  rows:
+  - [true]
 ...
 box.execute("SELECT CAST('abc' AS BOOLEAN);")
 ---
diff --git a/test/sql/types.test.lua b/test/sql/types.test.lua
index c51660cb9..8e1745e7c 100644
--- a/test/sql/types.test.lua
+++ b/test/sql/types.test.lua
@@ -186,6 +186,8 @@ box.execute("SELECT CAST(true AS FLOAT);")
 box.execute("SELECT CAST(true AS SCALAR);")
 box.execute("SELECT CAST(1 AS BOOLEAN);")
 box.execute("SELECT CAST(1.123 AS BOOLEAN);")
+box.execute("SELECT CAST(0.0 AS BOOLEAN);")
+box.execute("SELECT CAST(0.00000001 AS BOOLEAN);")
 box.execute("SELECT CAST('abc' AS BOOLEAN);")
 box.execute("SELECT CAST('  TrUe' AS BOOLEAN);")
 box.execute("SELECT CAST('  falsE    ' AS BOOLEAN);")
-- 
2.15.1





More information about the Tarantool-patches mailing list