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 535382EDE2 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 92kIue7hinpF for ; Tue, 21 May 2019 06:35:02 -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 DBF582EEA8 for ; Tue, 21 May 2019 06:35:01 -0400 (EDT) From: Nikita Pettik Subject: [tarantool-patches] [PATCH 3/3] sql: allow CAST operation from quoted float to int Date: Tue, 21 May 2019 13:34:56 +0300 Message-Id: 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 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