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 9BB646F3E5; Thu, 11 Nov 2021 13:46:57 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9BB646F3E5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1636627617; bh=kQx4bIUchBWUJbvXDfa273etDzGIfibE7e0i1p+cmb0=; 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=GRy7543JVKaNyazr7RoRcNjzNJP1LaStcWqmmF/3s+B9WbKZeZ22e61sY2FHxwzWV 8b6fanxL+HpgOAtndwJ+KY+CrOJwcmJvzema2yv9TlDl4A7ELVHzVaw6N7ag1nWMDY POzeDov6scC273O6NL4XmFlNa1yJiFI2aHpNk0gQ= 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 337C76DB24 for ; Thu, 11 Nov 2021 13:45:30 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 337C76DB24 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1ml7aD-0002x9-9d; Thu, 11 Nov 2021 13:45:29 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Thu, 11 Nov 2021 13:45:29 +0300 Message-Id: <7a644db9589ed8857f340b98eb249d84680dded5.1636627366.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: 4F1203BC0FB41BD9731B3922EC063979EC8F5BB1F79852547911E65F5173D5CB00894C459B0CD1B99F661F086E4FA9D68470601AD23CA7F4BD7B9CED87D2556797B3F93076A4A5D8 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE731D82F3F177D3BCDEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063706922F90966A37BA8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D890489F08D305FCDBC7EA7BC1346A1FEC117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF1175FABE1C0F9B6A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735209ECD01F8117BC8BEA471835C12D1D977C4224003CC8364762BB6847A3DEAEFB0F43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B68655334FD4449CB9ECD01F8117BC8BEAAAE862A0553A39223F8577A6DFFEA7C289736CE4F78F08343847C11F186F3C59DAA53EE0834AAEE X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C7E9FEBB9C11794A1FCFD631EEBE6A69F2B0BF4AD094CBA319C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF309DFB797F6729CB699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34BC3EEE75EF3BACCFADE81FD71497AEF05199E957040DC018DC446EB47B0904211226B2F4D9040BC71D7E09C32AA3244C5490A8BD2DD2CA5BD7A89EDAA79DF4AFD9ADFF0C0BDB8D1F729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj4t8MBgWr8bI1nN45wNh/Dw== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D7FC91A24DDF865395398F358C1F99F9183D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B 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 bc7a1fedd..88830fd78 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -278,6 +278,59 @@ func_char_length(struct sql_context *ctx, int argc, struct Mem *argv) mem_set_uint(ctx->pOut, len); } +/** 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 (mem_is_null(arg)) + return; + assert(mem_is_str(arg) && 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) { @@ -791,67 +844,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. @@ -1989,7 +1981,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}, @@ -2064,7 +2056,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