From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 275292F16C for ; Tue, 21 May 2019 06:35:02 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id SUNJv9eau3Zz for ; Tue, 21 May 2019 06:35:01 -0400 (EDT) Received: from smtp61.i.mail.ru (smtp61.i.mail.ru [217.69.128.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 839692EDE2 for ; Tue, 21 May 2019 06:35:01 -0400 (EDT) From: Nikita Pettik Subject: [tarantool-patches] [PATCH 2/3] sql: allow CAST operation from REAL to BOOLEAN Date: Tue, 21 May 2019 13:34:55 +0300 Message-Id: <8087599f1e9fd32120cabf20d56f878d8da1b9ff.1558378847.git.korablev@tarantool.org> In-Reply-To: References: In-Reply-To: References: Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-Help: List-Unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-Subscribe: List-Owner: List-post: List-Archive: To: tarantool-patches@freelists.org Cc: Nikita Pettik 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