From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 3308F6ECC0; Wed, 15 Dec 2021 13:57:41 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3308F6ECC0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1639565861; bh=TUPryW48HBGgdtHb/nUolMqah+02RIukFHJe6kyDT5Q=; h=To:Cc:Date:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=C5OEzHWj2Xdk1FMNi2FS/M2ukyZrd8hb9veXLa5josrEzZWnOlutj6oFiNYMak8lZ 1hmdLQwNxIYleRdpinOlYIltE41qiDGN3rKPjJvwBOVq12jryQZTP47a5DI13cGHc0 C3OjIViv/vjwBRooPPKlt6Ww62q9B7rzhj1YvVFs= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [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 A856E6ECC0 for ; Wed, 15 Dec 2021 13:57:39 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A856E6ECC0 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mxRyc-00085a-NE; Wed, 15 Dec 2021 13:57:39 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Wed, 15 Dec 2021 13:57:38 +0300 Message-Id: <25ad6aca5c65286f69b537fd4241759bb966d8be.1639565791.git.imeevma@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9FFF274446F725B74506C72DDA7EBE5A636C5C70934101B1A182A05F538085040A7B61C0F86C9FBF9511E78ECB99D95541983E3CAE35144EE3158CC9453E61254 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE752E71F0C64B7C834EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063736901D4B68B3D4FF8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D80FDE271E254452D8A2A09715E7B5C847117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BF1175FABE1C0F9B6A471835C12D1D977C4224003CC8364762BB6847A3DEAEFB0F43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B68655334FD4449CB9ECD01F8117BC8BEAAAE862A0553A39223F8577A6DFFEA7C468D16C903838CAB43847C11F186F3C59DAA53EE0834AAEE X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C302CFC0D3F89A23BD81F3C26A6EFD41DD6965B15A909C7059C2B6934AE262D3EE7EAB7254005DCED7ECBB8B029842A501E0A4E2319210D9B64D260DF9561598F01A9E91200F654B02F433CA60753AEF28E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34C53D1911AD7E4199F109C48EE72EB886D81AC10F6230E036A431B12156D2223AFEA2FB1E14F7AF0A1D7E09C32AA3244C9590A5DCD6F3864A13C0C0CC818BBB4BF522A1CF68F4BE05729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojqIoINCajonCTVpgHtXey8Q== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DAA74D53648943E0EEE6B6AB2FF8C6AC383D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E3365FEEDEB644C299C0ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 1/1] sql: introduce binding for ARRAY X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" After this patch, ARRAY values can be used as bind variables. However, due to the current syntax for binding in Lua, the only possible way is to use ARRAY values as the named bind variable. Part of #4762 --- https://github.com/tarantool/tarantool/issues/4762 https://github.com/tarantool/tarantool/tree/imeevma/gh-4762-bindings-for-array src/box/bind.c | 7 +++---- src/box/lua/execute.c | 32 +++++++++++++++++++++++++++----- src/box/sql/sqlInt.h | 3 +++ src/box/sql/vdbeapi.c | 8 ++++++++ test/sql-tap/array.test.lua | 25 ++++++++++++++++++++++++- test/sql/bind.result | 7 ------- test/sql/bind.test.lua | 2 -- 7 files changed, 65 insertions(+), 19 deletions(-) diff --git a/src/box/bind.c b/src/box/bind.c index e75e36283..af9f9eac5 100644 --- a/src/box/bind.c +++ b/src/box/bind.c @@ -99,15 +99,12 @@ sql_bind_decode(struct sql_bind *bind, int i, const char **packet) case MP_BIN: bind->s = mp_decode_bin(packet, &bind->bytes); break; + case MP_ARRAY: case MP_EXT: bind->s = *packet; mp_next(packet); bind->bytes = *packet - bind->s; break; - case MP_ARRAY: - diag_set(ClientError, ER_SQL_BIND_TYPE, "ARRAY", - sql_bind_name(bind)); - return -1; case MP_MAP: diag_set(ClientError, ER_SQL_BIND_TYPE, "MAP", sql_bind_name(bind)); @@ -190,6 +187,8 @@ sql_bind_column(struct sql_stmt *stmt, const struct sql_bind *p, return sql_bind_null(stmt, pos); case MP_BIN: return sql_bind_bin_static(stmt, pos, p->s, p->bytes); + case MP_ARRAY: + return sql_bind_array_static(stmt, pos, p->s, p->bytes); case MP_EXT: assert(p->ext_type == MP_UUID || p->ext_type == MP_DECIMAL); if (p->ext_type == MP_UUID) diff --git a/src/box/lua/execute.c b/src/box/lua/execute.c index 18a45a5d5..71d4d7fae 100644 --- a/src/box/lua/execute.c +++ b/src/box/lua/execute.c @@ -8,6 +8,8 @@ #include "box/bind.h" #include "box/sql_stmt_cache.h" #include "box/schema.h" +#include "mpstream/mpstream.h" +#include "box/sql/vdbeInt.h" /** * Serialize a description of the prepared statement. @@ -331,6 +333,8 @@ lua_sql_bind_decode(struct lua_State *L, struct sql_bind *bind, int idx, int i) } if (luaL_tofield(L, luaL_msgpack_default, -1, &field) < 0) return -1; + bind->type = field.type; + bind->ext_type = field.ext_type; switch (field.type) { case MP_UINT: bind->u64 = field.ival; @@ -382,10 +386,30 @@ lua_sql_bind_decode(struct lua_State *L, struct sql_bind *bind, int idx, int i) diag_set(ClientError, ER_SQL_BIND_TYPE, "USERDATA", sql_bind_name(bind)); return -1; - case MP_ARRAY: - diag_set(ClientError, ER_SQL_BIND_TYPE, "ARRAY", - sql_bind_name(bind)); + case MP_ARRAY: { + size_t used = region_used(region); + struct mpstream stream; + bool is_error = false; + mpstream_init(&stream, region, region_reserve_cb, + region_alloc_cb, set_encode_error, &is_error); + lua_pushvalue(L, -1); + luamp_encode_r(L, luaL_msgpack_default, &stream, &field, 0); + lua_pop(L, 1); + mpstream_flush(&stream); + if (is_error) { + region_truncate(region, used); + diag_set(OutOfMemory, stream.pos - stream.buf, + "mpstream_flush", "stream"); + return -1; + } + bind->bytes = region_used(region) - used; + bind->s = region_join(region, bind->bytes); + if (bind->s != NULL) + break; + region_truncate(region, used); + diag_set(OutOfMemory, bind->bytes, "region_join", "bind->s"); return -1; + } case MP_MAP: diag_set(ClientError, ER_SQL_BIND_TYPE, "MAP", sql_bind_name(bind)); @@ -393,8 +417,6 @@ lua_sql_bind_decode(struct lua_State *L, struct sql_bind *bind, int idx, int i) default: unreachable(); } - bind->type = field.type; - bind->ext_type = field.ext_type; lua_pop(L, lua_gettop(L) - idx); return 0; } diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index dcd71e5bd..716110edc 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -556,6 +556,9 @@ sql_bind_str_static(sql_stmt *stmt, int i, const char *str, uint32_t len); int sql_bind_bin_static(sql_stmt *stmt, int i, const char *str, uint32_t size); +int +sql_bind_array_static(sql_stmt *stmt, int i, const char *str, uint32_t size); + int sql_bind_uuid(struct sql_stmt *stmt, int i, const struct tt_uuid *uuid); diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 4ce5feeae..3ea155d17 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -524,6 +524,14 @@ sql_bind_bin_static(sql_stmt *stmt, int i, const char *str, uint32_t size) return sql_bind_type(vdbe, i, "text"); } +int +sql_bind_array_static(sql_stmt *stmt, int i, const char *str, uint32_t size) +{ + struct Vdbe *vdbe = (struct Vdbe *)stmt; + mem_set_array_static(&vdbe->aVar[i - 1], (char *)str, size); + return sql_bind_type(vdbe, i, "array"); +} + int sql_bind_uuid(struct sql_stmt *stmt, int i, const struct tt_uuid *uuid) { diff --git a/test/sql-tap/array.test.lua b/test/sql-tap/array.test.lua index 79a1c831d..3387742bf 100755 --- a/test/sql-tap/array.test.lua +++ b/test/sql-tap/array.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool local test = require("sqltester") -test:plan(115) +test:plan(117) box.schema.func.create('A1', { language = 'Lua', @@ -1024,6 +1024,29 @@ test:do_execsql_test( "array" }) +-- Make sure that ARRAY values can be used as bound variable. +test:do_test( + "builtins-14.1", + function() + local res = box.execute([[SELECT #a;]], {{['#a'] = {1, 2, 3}}}) + return {res.rows[1][1]} + end, { + {1, 2, 3} + }) + +local remote = require('net.box') +box.cfg{listen = os.getenv('LISTEN')} +local cn = remote.connect(box.cfg.listen) +test:do_test( + "builtins-14.2", + function() + local res = cn:execute([[SELECT #a;]], {{['#a'] = {1, 2, 3}}}) + return {res.rows[1][1]} + end, { + {1, 2, 3} + }) +cn:close() + box.execute([[DROP TABLE t1;]]) box.execute([[DROP TABLE t;]]) diff --git a/test/sql/bind.result b/test/sql/bind.result index cb0302885..f269056e2 100644 --- a/test/sql/bind.result +++ b/test/sql/bind.result @@ -249,13 +249,6 @@ execute('SELECT ? AS big_uint', {0xefffffffffffffff}) - [17293822569102704640] ... -- Bind incorrect parameters. -ok, err = pcall(execute, 'SELECT ?', { {1, 2, 3} }) ---- -... -ok ---- -- false -... parameters = {} --- ... diff --git a/test/sql/bind.test.lua b/test/sql/bind.test.lua index 2ced7775a..4ad227d95 100644 --- a/test/sql/bind.test.lua +++ b/test/sql/bind.test.lua @@ -82,8 +82,6 @@ execute(sql, parameters) -- suitable method in its bind API. execute('SELECT ? AS big_uint', {0xefffffffffffffff}) -- Bind incorrect parameters. -ok, err = pcall(execute, 'SELECT ?', { {1, 2, 3} }) -ok parameters = {} parameters[1] = {} parameters[1][100] = 200 -- 2.25.1