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 24A376F3F2; Fri, 8 Oct 2021 20:35:11 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 24A376F3F2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1633714511; bh=8QCZ3e53Acx7kXe4BETxk7n6tgw7elRivwKyzVfgYQ0=; 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=Np8h1J6mtl3kWMIenmCX80Jo9zCjU//nO+zKborYV8B0zV3Com+34ky/d4aoeGWrL bJFnAqvdTAxiKT+agoRGQBkeyx/DC+2zlBGhpXkjPIPpOZxYreFKINWtOVMBZBAcOf iUCxahvMj0jWz6Mu8KywNo1lSNZvMsPwnhYmMznk= 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 BCD596F3F2 for ; Fri, 8 Oct 2021 20:31:45 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org BCD596F3F2 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mYtij-00082Q-5H; Fri, 08 Oct 2021 20:31:45 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 8 Oct 2021 20:31:44 +0300 Message-Id: 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: 4F1203BC0FB41BD9A6D4E3B1981C4C7D436932E0370A3249AEA3E77A50CFBF89182A05F5380850403442579A5E7EACA078F1FD561678C5C6BB51BE07E52214CBE4E0814B06CA0BDD X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE72847AA60176ABEF3EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637B43005D9966BC2348638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D83405C295F71D75FD7A949E06419565BC117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF1175FABE1C0F9B6A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735209ECD01F8117BC8BEA471835C12D1D977C4224003CC8364762BB6847A3DEAEFB0F43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B68655334FD4449CB9ECD01F8117BC8BEAAAE862A0553A39223F8577A6DFFEA7CD1D040B6C1ECEA3F43847C11F186F3C59DAA53EE0834AAEE X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C650BD2383FF25EF55DA7F59905C1155E01B8E859821D4C579C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF520746E264B47742699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34A08F09726D78E07BADA777DC8B74E132EDA85E28F1BB6A66E3095FC7815E69179996167771DBF5571D7E09C32AA3244CE82D8E072B32EFA361734C5B7DB4DCD03A92A9747B6CC886729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojMZ06aokA6bqHwHZSSJUlFA== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D7AFA6CE142094D5ACB0C11CF06C4700783D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B 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 | 52 +++++++++++++++++--------------------- test/sql-tap/func.test.lua | 4 +-- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 8caf389ab..51a43f271 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -891,6 +891,28 @@ func_random(struct sql_context *ctx, int argc, struct Mem *argv) mem_set_int(ctx->pOut, r, r < 0); } +/** Implementation of the RANDOMBLOB() 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) { @@ -1056,34 +1078,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) @@ -1908,7 +1902,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 dc4dfdc0e..2ce77c6e1 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