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 5BB3871052; Wed, 20 Oct 2021 20:02:52 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 5BB3871052 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1634749372; bh=eqhMEPUSWyffYmwf/Et0oS3/sU+vLLcWRwRqepC30ck=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Z6bcJxX4SQphrfG7f8DoZfz1oIzN28Z7LR+RbXJPbsc4m0CcT0kG5lnGV9xlgPZ3Q H04cSQ/Ft97da7Mzzi5ulA5A5JriCn9lZ1nqGt5tQEWQVl5SIdtL76mQunPIFIdwdQ WZyOD4G6z5MFAINMpfFfZ3AnBbp7dCXI/Dr6VpyQ= 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 3FEE871051 for ; Wed, 20 Oct 2021 20:02:51 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3FEE871051 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mdEzK-00022B-Cs; Wed, 20 Oct 2021 20:02:50 +0300 Date: Wed, 20 Oct 2021 20:02:49 +0300 To: v.shpilevoy@tarantool.org, tarantool-patches@dev.tarantool.org Message-ID: <20211020170249.GC203963@tarantool.org> References: <7ac6109c37c1bd073a3693010ef7eaecf14074a8.1633105483.git.imeevma@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <7ac6109c37c1bd073a3693010ef7eaecf14074a8.1633105483.git.imeevma@gmail.com> X-4EC0790: 10 X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD9C7814344C8C501C83238E3156CE19B78C95C7AD4390B7974182A05F538085040D113B89C6FEDB2C6BEFE1E587E4D98E32BD5D11D432131DEAAE633CAFEF74400 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE70CB15FA6C489297DEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637770CCD3A0ADFB7EEEA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F201CF327E8B2EE166666B5D46A5A7A91FCC7F00164DA146DAFE8445B8C89999728AA50765F7900637F6B57BC7E64490618DEB871D839B7333395957E7521B51C2DFABB839C843B9C08941B15DA834481F8AA50765F7900637F6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8B953A8A48A05D51F175ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: 0D63561A33F958A5BE3F8E393C68387A939E6510C30ABDDE48CF0113B0F2CDC5D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7557E988E9157162368E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D344A2E3AE0A8792E69055E3AA8161D4FB338F6583354184A425D1BD0C7DBF9B0F51B0371C1105706301D7E09C32AA3244C60817ECF3932535035E13FECC46357D8B018FE5BB746DCD1729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojMPcpz6kU2m+W1hSbUflEKw== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D5DB88B7C8E009BE6264919583F5E05D683D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v1 3/8] sql: refactor UPPER() and LOWER() functions 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: Mergen Imeev Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thank you for the review! I replaced check of MEM type by mem_is_*() functions. Diff below. On Fri, Oct 01, 2021 at 07:29:29PM +0300, Mergen Imeev via Tarantool-patches wrote: > Part of #4145 > --- > src/box/sql/func.c | 118 +++++++++++++++++++++------------------------ > 1 file changed, 55 insertions(+), 63 deletions(-) > > diff --git a/src/box/sql/func.c b/src/box/sql/func.c > index 2e53b32d8..565ebcabe 100644 > --- a/src/box/sql/func.c > +++ b/src/box/sql/func.c > @@ -295,6 +295,59 @@ func_char_length(struct sql_context *ctx, int argc, struct Mem *argv) > mem_set_uint(ctx->pOut, utf8_len_str(arg->z, arg->n)); > } > > +/** Implementation of the UPPER() and LOWER() functions. */ > +static void > +func_lower_upper(struct sql_context *ctx, int argc, struct Mem *argv) > +{ > + assert(argc == 1); > + (void)argc; > + struct Mem *arg = &argv[0]; > + if (arg->type == MEM_TYPE_NULL) > + return; > + assert(arg->type == MEM_TYPE_STR && arg->n >= 0); > + if (arg->n == 0) > + return mem_set_str0_static(ctx->pOut, ""); > + const char *str = arg->z; > + int32_t len = arg->n; > + struct sql *db = sql_get(); > + char *res = sqlDbMallocRawNN(db, len); > + if (res == NULL) { > + ctx->is_aborted = true; > + return; > + } > + int32_t size = sqlDbMallocSize(db, res); > + assert(size >= len); > + UErrorCode status = U_ZERO_ERROR; > + const char *locale = NULL; > + if (ctx->coll != NULL && ctx->coll->type == COLL_TYPE_ICU) { > + locale = ucol_getLocaleByType(ctx->coll->collator, > + ULOC_VALID_LOCALE, &status); > + } > + UCaseMap *cm = ucasemap_open(locale, 0, &status); > + assert(cm != NULL); > + assert(ctx->func->def->name[0] == 'U' || > + ctx->func->def->name[0] == 'L'); > + bool is_upper = ctx->func->def->name[0] == 'U'; > + int32_t new_len = > + is_upper ? > + ucasemap_utf8ToUpper(cm, res, size, str, len, &status) : > + ucasemap_utf8ToLower(cm, res, size, str, len, &status); > + if (new_len > size) { > + res = sqlDbRealloc(db, res, new_len); > + if (db->mallocFailed != 0) { > + ctx->is_aborted = true; > + return; > + } > + status = U_ZERO_ERROR; > + if (is_upper) > + ucasemap_utf8ToUpper(cm, res, size, str, len, &status); > + else > + ucasemap_utf8ToLower(cm, res, size, str, len, &status); > + } > + ucasemap_close(cm); > + mem_set_str_allocated(ctx->pOut, res, new_len); > +} > + > static const unsigned char * > mem_as_ustr(struct Mem *mem) > { > @@ -808,67 +861,6 @@ contextMalloc(struct sql_context *context, i64 nByte) > return z; > } > > -/* > - * Implementation of the upper() and lower() SQL functions. > - */ > - > -#define ICU_CASE_CONVERT(case_type) \ > -static void \ > -case_type##ICUFunc(sql_context *context, int argc, struct Mem *argv) \ > -{ \ > - char *z1; \ > - const char *z2; \ > - int n; \ > - UNUSED_PARAMETER(argc); \ > - if (mem_is_bin(&argv[0]) || mem_is_map(&argv[0]) || \ > - mem_is_array(&argv[0])) { \ > - diag_set(ClientError, ER_INCONSISTENT_TYPES, "string", \ > - mem_str(&argv[0])); \ > - context->is_aborted = true; \ > - return; \ > - } \ > - z2 = mem_as_str0(&argv[0]); \ > - n = mem_len_unsafe(&argv[0]); \ > - /* \ > - * Verify that the call to _bytes() \ > - * does not invalidate the _text() pointer. \ > - */ \ > - assert(z2 == mem_as_str0(&argv[0])); \ > - if (!z2) \ > - return; \ > - z1 = contextMalloc(context, ((i64) n) + 1); \ > - if (z1 == NULL) { \ > - context->is_aborted = true; \ > - return; \ > - } \ > - UErrorCode status = U_ZERO_ERROR; \ > - struct coll *coll = context->coll; \ > - const char *locale = NULL; \ > - if (coll != NULL && coll->type == COLL_TYPE_ICU) { \ > - locale = ucol_getLocaleByType(coll->collator, \ > - ULOC_VALID_LOCALE, &status); \ > - } \ > - UCaseMap *case_map = ucasemap_open(locale, 0, &status); \ > - assert(case_map != NULL); \ > - int len = ucasemap_utf8To##case_type(case_map, z1, n, z2, n, &status); \ > - if (len > n) { \ > - status = U_ZERO_ERROR; \ > - sql_free(z1); \ > - z1 = contextMalloc(context, ((i64) len) + 1); \ > - if (z1 == NULL) { \ > - context->is_aborted = true; \ > - return; \ > - } \ > - ucasemap_utf8To##case_type(case_map, z1, len, z2, n, &status); \ > - } \ > - ucasemap_close(case_map); \ > - sql_result_text(context, z1, len, sql_free); \ > -} \ > - > -ICU_CASE_CONVERT(Lower); > -ICU_CASE_CONVERT(Upper); > - > - > /* > * Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented > * as VDBE code so that unused argument values do not have to be computed. > @@ -2007,7 +1999,7 @@ static struct sql_func_definition definitions[] = { > FIELD_TYPE_BOOLEAN, sql_builtin_stub, NULL}, > {"LIKELY", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_BOOLEAN, sql_builtin_stub, > NULL}, > - {"LOWER", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, LowerICUFunc, > + {"LOWER", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, func_lower_upper, > NULL}, > > {"MAX", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, step_minmax, NULL}, > @@ -2082,7 +2074,7 @@ static struct sql_func_definition definitions[] = { > NULL}, > {"UNLIKELY", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_BOOLEAN, sql_builtin_stub, > NULL}, > - {"UPPER", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, UpperICUFunc, > + {"UPPER", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, func_lower_upper, > NULL}, > {"UUID", 0, {}, FIELD_TYPE_UUID, sql_func_uuid, NULL}, > {"UUID", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_UUID, sql_func_uuid, NULL}, > -- > 2.25.1 > Diff: diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 441bcb51d..d6c557c54 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -286,9 +286,9 @@ func_lower_upper(struct sql_context *ctx, int argc, struct Mem *argv) assert(argc == 1); (void)argc; struct Mem *arg = &argv[0]; - if (arg->type == MEM_TYPE_NULL) + if (mem_is_null(arg)) return; - assert(arg->type == MEM_TYPE_STR && arg->n >= 0); + assert(mem_is_str(arg) && arg->n >= 0); if (arg->n == 0) return mem_set_str0_static(ctx->pOut, ""); const char *str = arg->z;