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 8987E6FC8B; Fri, 1 Oct 2021 19:30:54 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 8987E6FC8B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1633105854; bh=ALk7eLJaQJ43IPSpJXNypN6eyreG7PGVNbuFXiVZlfw=; 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=Zvo7kUMYcaa6HhrLt7TiXNgy9tNVxX4MLkCMb/8NIBPSSr2a9ATO9rtp0OuvonqDY ss+lojOlYRTkXccG+4r9613nR76tEcVDA26FkQZAS7gTLdHKObaz0ivJ6I1o+1vnUD C1q/fZMT9ETbfe9vyQAibT8cfuFMS3P7rZjh3irU= 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 EE76574144 for ; Fri, 1 Oct 2021 19:29:29 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org EE76574144 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mWLPd-0004IW-9S; Fri, 01 Oct 2021 19:29:29 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 1 Oct 2021 19:29:29 +0300 Message-Id: <7ac6109c37c1bd073a3693010ef7eaecf14074a8.1633105483.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: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD96A58C36AA2E9964976B67DC8394C8DAB0EDC1900E6C7EF8D182A05F5380850404F0DE588047D8352C365DBFC1837ABDA5DDF3B3392D04493382772D2E0011298 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE727FD6E7FC3A8F857EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637CDA089757FB31C668638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8D8200BE056328210F934BDDF202C2770117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BAA867293B0326636D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CBBB17C150BCA67939F9BC7263BA37CC5BCE253CD9BC97D439C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF783E2B6F79C23BED699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3473688ED311681BF0FA8EF0E7BEABB2DEEE1997AB9EA1FB4A2B1F3BC601CF7B2CCDFF1F4987343A071D7E09C32AA3244C6FE0F902A867BBA1A571B1D78EC72DC9FE8DA44ABE2443F7729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj/2kGGGWRGj5hTWd5w0IqdA== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D4E21150E7E4B00512EB01FFAB46137E683D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [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: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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