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 6C0166CE3F; Thu, 11 Nov 2021 13:53:35 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 6C0166CE3F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1636628015; bh=PACOxq5ona2uM9Cihe4d6eulEXKkoKfWbpkXHKURSIc=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=hniBOtccnOQY+XQA9H8o/syAfCeZfDaLb7wvefaJrTU3Wlcos7njO8exhsqcpZt6P wiufHoLtHAJkC/Eo8FpCjoarinGwJe35IRfNGDlbjMtwCrXD1zv89jfGa0xuWs909e uDXJJZsY9cMGypKfCp+2XShfVB8fUpKEeoIsiTgI= 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 5D8B771067 for ; Thu, 11 Nov 2021 13:49:02 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 5D8B771067 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1ml7dd-0004yV-If; Thu, 11 Nov 2021 13:49:01 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Thu, 11 Nov 2021 13:49:01 +0300 Message-Id: <070ab9eb0e286f92c72acc48e4ba885c6120bb2d.1636627579.git.imeevma@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9731B3922EC063979A442978D427656AE2233A9C8B25E6BF500894C459B0CD1B9FCBD0921BC466138C6E9D1ECBDC94D7DBD7B9CED87D25567C518DC9AAFF1970F X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE73C714006C69EB7BAEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006379347C0682FC030B08638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8F12AF38BADF25DE36C083008B836259F117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF1175FABE1C0F9B6A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18CB629EEF1311BF91D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-B7AD71C0: AC4F5C86D027EB78D6D0BB59C9AE7EFFE8F7B195E1C97831946E8758045FA9D6A3BF5431196D8D52 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C7E9FEBB9C11794A1D7886E5F6DCB3549409E260DA1DCB8E19C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFCDE59DDCCE53AF2B699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34E420FF71F2F0FE03727C76E4711FC521B97A48E662696D71B20E862BADAF88A7E38D9B76F2E592EA1D7E09C32AA3244CDE48597507C8D25E1B81794FB29D8E2D1DD47778AE04E04D729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj4t8MBgWr8bKMR8QZv/VVIQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DE37FD326222B4C00BF9A09A44DBF49AA83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 07/21] sql: rework RANDOMBLOB() function 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" This patch refactors RANDOMBLOB() function. Also, RANDOMBLOB(0) now returns empty string. part of #4145 --- src/box/sql/func.c | 57 +++++++++++++++++++------------------- test/sql-tap/func.test.lua | 4 +-- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 2d83d52f8..408c56e45 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -900,6 +900,33 @@ func_random(struct sql_context *ctx, int argc, struct Mem *argv) mem_set_int(ctx->pOut, r, r < 0); } +/** + * Implementation of the RANDOMBLOB() function. + * + * This function returns a random VARBINARY value. The size of this value is + * specified as an argument of the function. + */ +static void +func_randomblob(struct sql_context *ctx, int argc, struct Mem *argv) +{ + assert(argc == 1); + (void)argc; + struct Mem *arg = &argv[0]; + assert(mem_is_null(arg) || mem_is_int(arg)); + if (mem_is_null(arg) || !mem_is_uint(arg)) + return; + if (arg->u.u == 0) + return mem_set_bin_static(ctx->pOut, "", 0); + uint64_t len = arg->u.u; + char *res = sqlDbMallocRawNN(sql_get(), len); + if (res == NULL) { + ctx->is_aborted = true; + return; + } + sql_randomness(len, res); + mem_set_bin_allocated(ctx->pOut, res, len); +} + static const unsigned char * mem_as_ustr(struct Mem *mem) { @@ -1065,34 +1092,6 @@ contextMalloc(struct sql_context *context, i64 nByte) return z; } -/* - * Implementation of randomblob(N). Return a random blob - * that is N bytes long. - */ -static void -randomBlob(struct sql_context *context, int argc, struct Mem *argv) -{ - int64_t n; - unsigned char *p; - assert(argc == 1); - UNUSED_PARAMETER(argc); - if (mem_is_bin(&argv[0]) || mem_is_map(&argv[0]) || - mem_is_array(&argv[0])) { - diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - mem_str(&argv[0]), "number"); - context->is_aborted = true; - return; - } - n = mem_get_int_unsafe(&argv[0]); - if (n < 1) - return; - p = contextMalloc(context, n); - if (p) { - sql_randomness(n, p); - sql_result_blob(context, (char *)p, n, sql_free); - } -} - #define Utf8Read(s, e) \ ucnv_getNextUChar(icu_utf8_conv, &(s), (e), &status) @@ -1917,7 +1916,7 @@ static struct sql_func_definition definitions[] = { {"QUOTE", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_STRING, quoteFunc, NULL}, {"RANDOM", 0, {}, FIELD_TYPE_INTEGER, func_random, NULL}, {"RANDOMBLOB", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_VARBINARY, - randomBlob, NULL}, + func_randomblob, NULL}, {"REPLACE", 3, {FIELD_TYPE_STRING, FIELD_TYPE_STRING, FIELD_TYPE_STRING}, FIELD_TYPE_STRING, replaceFunc, NULL}, diff --git a/test/sql-tap/func.test.lua b/test/sql-tap/func.test.lua index b52d5bd4c..e2e905907 100755 --- a/test/sql-tap/func.test.lua +++ b/test/sql-tap/func.test.lua @@ -2545,8 +2545,8 @@ test:do_execsql_test( test:do_execsql_test( "func-36", - [[VALUES (LENGTH(RANDOMBLOB(0)))]], - {""}) + [[VALUES (RANDOMBLOB(0))]], + {''}) -- gh-3542 -- In SQL '\0' is NOT a end-of-string signal. Tests below ensures -- 2.25.1