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 2/3] sql: allow CAST operation from REAL to BOOLEAN
Date: Tue, 21 May 2019 13:34:55 +0300	[thread overview]
Message-ID: <8087599f1e9fd32120cabf20d56f878d8da1b9ff.1558378847.git.korablev@tarantool.org> (raw)
In-Reply-To: <cover.1558378847.git.korablev@tarantool.org>
In-Reply-To: <cover.1558378847.git.korablev@tarantool.org>

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

  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 ` Nikita Pettik [this message]
2019-05-21 10:34 ` [tarantool-patches] [PATCH 3/3] sql: allow CAST operation from quoted float to int Nikita Pettik
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=8087599f1e9fd32120cabf20d56f878d8da1b9ff.1558378847.git.korablev@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [tarantool-patches] [PATCH 2/3] sql: allow CAST operation from REAL to BOOLEAN' \
    /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