From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 4520C4696C4 for ; Mon, 30 Dec 2019 16:01:27 +0300 (MSK) From: imeevma@tarantool.org Date: Mon, 30 Dec 2019 16:01:26 +0300 Message-Id: In-Reply-To: References: Subject: [Tarantool-patches] [PATCH v2 1/3] sql: allow conversion of numeric binary values to DOUBLE List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: korablev@tarantool.org Cc: tarantool-patches@dev.tarantool.org This patch allows to convert binary values consisting of numeric literals to numbers. Follow-uo #3812 --- src/box/sql/vdbemem.c | 2 +- test/sql-tap/numcast.test.lua | 25 ++++++++++++++++++++++++- test/sql-tap/tkt-80e031a00f.test.lua | 8 ++++---- test/sql/types.result | 7 +++++-- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c index df3f0d8..3c5e5fc 100644 --- a/src/box/sql/vdbemem.c +++ b/src/box/sql/vdbemem.c @@ -516,7 +516,7 @@ sqlVdbeRealValue(Mem * pMem, double *v) } else if ((pMem->flags & MEM_UInt) != 0) { *v = (double)pMem->u.u; return 0; - } else if (pMem->flags & MEM_Str) { + } else if ((pMem->flags & (MEM_Blob | MEM_Str)) != 0) { if (sqlAtoF(pMem->z, v, pMem->n)) return 0; } diff --git a/test/sql-tap/numcast.test.lua b/test/sql-tap/numcast.test.lua index 07117d0..a45daef 100755 --- a/test/sql-tap/numcast.test.lua +++ b/test/sql-tap/numcast.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool test = require("sqltester") -test:plan(20) +test:plan(22) --!./tcltestrunner.lua -- 2013 March 20 @@ -147,4 +147,27 @@ test:do_catchsql_test( 1,"Tuple field 1 type does not match one required by operation: expected integer" }) +-- +-- Allow to convert binary values consisting of numeric literals +-- to numbers. +-- +test:do_execsql_test( + "cast-3.1", + [[ + CREATE TABLE td (i DOUBLE PRIMARY KEY); + INSERT INTO td VALUES(X'3132332E35'); + INSERT INTO td VALUES(X'31323334'); + SELECT * FROM td; + ]], { + 123.5, 1234 + }) + +test:do_execsql_test( + "cast-3.2", + [[ + SELECT CAST(X'39' AS DOUBLE), CAST(X'39' AS DOUBLE) / 10; + ]], { + 9, 0.9 + }) + test:finish_test() diff --git a/test/sql-tap/tkt-80e031a00f.test.lua b/test/sql-tap/tkt-80e031a00f.test.lua index a0e6539..01f4265 100755 --- a/test/sql-tap/tkt-80e031a00f.test.lua +++ b/test/sql-tap/tkt-80e031a00f.test.lua @@ -380,23 +380,23 @@ test:do_execsql_test( -- }) -test:do_catchsql_test( +test:do_execsql_test( "tkt-80e031a00f.31", [[ SELECT x'303132' IN t1 ]], { -- - 1, 'Type mismatch: can not convert varbinary to integer' + false -- }) -test:do_catchsql_test( +test:do_execsql_test( "tkt-80e031a00f.32", [[ SELECT x'303132' NOT IN t1 ]], { -- - 1, 'Type mismatch: can not convert varbinary to integer' + true -- }) diff --git a/test/sql/types.result b/test/sql/types.result index 6d0aefd..8c0186d 100644 --- a/test/sql/types.result +++ b/test/sql/types.result @@ -1760,8 +1760,11 @@ box.execute("SELECT CAST(x'' AS DOUBLE);") ... box.execute("SELECT CAST(x'35' AS DOUBLE);") --- -- null -- 'Type mismatch: can not convert varbinary to double' +- metadata: + - name: CAST(x'35' AS DOUBLE) + type: double + rows: + - [5] ... box.execute("SELECT CAST(CAST(x'35' AS STRING) AS DOUBLE);") --- -- 2.7.4