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 3C4E471051; Wed, 20 Oct 2021 19:58:48 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3C4E471051 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1634749128; bh=d54P8+3gmOBZyRP/i07M1KEgIcoTWtDI4EQIY8t2q2o=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=IHfOxvpb+e2KYlc3BBfu3y7zyeaVBrrIuMLFsoRze6he4lUxSlU/dUmoggHHzK93u CpHLSrBUw3ZS/hE0vHaNXg1c02GKeEvSOSFZusRth9DJaNrRs1OIoSJTFkpcX98gBn Ui7kNoyR/+UKNfWCBsygl2zUikr178g492mL+XRQ= 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 1E40071050 for ; Wed, 20 Oct 2021 19:58:46 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 1E40071050 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mdEvN-00008i-8Y; Wed, 20 Oct 2021 19:58:45 +0300 Date: Wed, 20 Oct 2021 19:58:44 +0300 To: Vladislav Shpilevoy Cc: tarantool-patches@dev.tarantool.org Message-ID: <20211020165844.GB203963@tarantool.org> References: <9cc35ba4625d4e3017725c35fbc4a7ed90341917.1633105483.git.imeevma@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9C7814344C8C501C8A4649CC2A06649B6A97F49A0BDC630D4182A05F5380850404E502B1735B2D5CD1D512060B23262D4A814042F8912FCF1A3CE4D5B42FCE7B0 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7705F446BE41E38A1EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006379A70878BADDF00B98638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8DA880DA8EDD2B1E36EE6345188BF41A9117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BAA867293B0326636D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B62CFFCC7B69C47339089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A53A1A1E58ECABDD8068ADAB7604AEE2DD5D7DC34D7512F43AD59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7557E988E9157162368E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D346B222596F62B8FA9415C380B36608D2712E720BA8F4B986A4DB9AA29617558E7A9C96FD199F4D4F61D7E09C32AA3244C2CD9F2C908FC897ABF223C14A13644F89CA7333006C390A0729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojMPcpz6kU2m/WOfiHNyUk2Q== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DCD0D1E57E09F435BA88CACBF6CDE77BE83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v1 2/8] sql: refactor CHAR_LENGTH() 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: Mergen Imeev Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thank you for the review! My answer, diff and new patch below. On Fri, Oct 08, 2021 at 11:56:59PM +0200, Vladislav Shpilevoy wrote: > Thanks for the patch! > > On 01.10.2021 18:29, imeevma@tarantool.org wrote: > > Part of #4145 > > --- > > src/box/sql/func.c | 38 +++++++++++++++++++++++++++++++++++--- > > 1 file changed, 35 insertions(+), 3 deletions(-) > > > > diff --git a/src/box/sql/func.c b/src/box/sql/func.c > > index 54b03f359..2e53b32d8 100644 > > --- a/src/box/sql/func.c > > +++ b/src/box/sql/func.c > > @@ -263,6 +263,38 @@ func_abs_double(struct sql_context *ctx, int argc, struct Mem *argv) > > mem_set_double(ctx->pOut, arg->u.r < 0 ? -arg->u.r : arg->u.r); > > } > > > > +/** Implementation of the CHAR_LENGTH() function. */ > > +static inline uint8_t > > +utf8_len_char(char c) > > +{ > > + uint8_t u = (uint8_t)c; > > + return 1 + (u >= 0xc2) + (u >= 0xe0) + (u >= 0xf0); > > It is not that simple really. Consider either using the old > lengthFunc() and other sqlite utf8 helpers or use the approach > similar to utf8_len() in utf8.c. It uses ICU macro U8_NEXT() > and has handling for special symbols like U_SENTINEL. > > Otherwise you are making already third version of functions to > work with utf8. > > I would even prefer to refactor lengthFunc() to stop using sqlite > legacy and drop sqlite utf8 entirely, but I suspect it might be > not so trivial to do and should be done later. I was able to use ucnv_getNextUChar() here. In fact, I was able to use this functions in all the places in this patch-set where we had to work with my or SQLite functions that work with UTF8 characters. I think I can remove sql/utf.c in the next patchset, since I refactor the LENGTH() and UNICODE() functions there. Diff: diff --git a/src/box/sql/func.c b/src/box/sql/func.c index e7d1988e0..faef0eef3 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -259,35 +259,24 @@ func_abs_double(struct sql_context *ctx, int argc, struct Mem *argv) } /** Implementation of the CHAR_LENGTH() function. */ -static inline uint8_t -utf8_len_char(char c) -{ - uint8_t u = (uint8_t)c; - return 1 + (u >= 0xc2) + (u >= 0xe0) + (u >= 0xf0); -} - -static inline uint32_t -utf8_len_str(const char *str, uint32_t size) -{ - uint32_t len = 0; - uint32_t pos = 0; - while (pos < size) { - pos += utf8_len_char(str[pos]); - ++len; - } - return len; -} - static void func_char_length(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); - mem_set_uint(ctx->pOut, utf8_len_str(arg->z, arg->n)); + assert(mem_is_str(arg) && arg->n >= 0); + uint32_t len = 0; + UErrorCode err = U_ZERO_ERROR; + const char *pos = arg->z; + const char *end = arg->z + arg->n; + while (pos < end) { + ucnv_getNextUChar(icu_utf8_conv, &pos, end, &err); + ++len; + } + mem_set_uint(ctx->pOut, len); } static const unsigned char * diff --git a/test/sql-tap/badutf1.test.lua b/test/sql-tap/badutf1.test.lua index b25436186..27f17168b 100755 --- a/test/sql-tap/badutf1.test.lua +++ b/test/sql-tap/badutf1.test.lua @@ -287,7 +287,7 @@ test:do_test( return test:execsql2("SELECT length('\x80\x80\x80\x80\x80\xf0\x80\x80\x80\x80') AS x") end, { -- - "X", 7 + "X", 10 -- }) @@ -297,7 +297,7 @@ test:do_test( return test:execsql2("SELECT length('\x80\x80\x80\x80\x80\xf0\x80\x80\x80\xff') AS x") end, { -- - "X", 7 + "X", 10 -- }) diff --git a/test/sql-tap/built-in-functions.test.lua b/test/sql-tap/built-in-functions.test.lua index 6fae811dc..7fe987abc 100755 --- a/test/sql-tap/built-in-functions.test.lua +++ b/test/sql-tap/built-in-functions.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool local test = require("sqltester") -test:plan(52) +test:plan(54) -- -- Make sure that number of arguments check is checked properly for SQL built-in @@ -545,4 +545,28 @@ test:do_test( {name = "COLUMN_2", type = "scalar"}, }) +-- gh-4145: Make sure the character is now checked when calculating its length. + +-- Character with UTF-8 code F0808080 does not exist. +test:do_execsql_test( + "builtins-4.1", + [[ + SELECT CHAR_LENGTH(CAST(x'f0808080' AS STRING)); + ]], + { + 4 + } +) + +-- Character with UTF-8 code F0908080 is '𐀀'. +test:do_execsql_test( + "builtins-4.2", + [[ + SELECT CHAR_LENGTH(CAST(x'f0908080' AS STRING)); + ]], + { + 1 + } +) + test:finish_test() diff --git a/test/sql-tap/func3.test.lua b/test/sql-tap/func3.test.lua index 7f1d8d33c..6999fea67 100755 --- a/test/sql-tap/func3.test.lua +++ b/test/sql-tap/func3.test.lua @@ -297,8 +297,8 @@ suits[5] = {str = '\x61\xc0', len = 2} suits[6] = {str = '\x61\xc0\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80', len = 12} suits[7] = {str = '\xc0\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80', len = 11} suits[8] = {str = '\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80', len = 10} -suits[9] = {str = '\x80\x80\x80\x80\x80\xf0\x80\x80\x80\x80', len = 7} -suits[10] = {str = '\x80\x80\x80\x80\x80\xf0\x80\x80\x80\xff', len = 7} +suits[9] = {str = '\x80\x80\x80\x80\x80\xf0\x90\x80\x80\x80', len = 7} +suits[10] = {str = '\x80\x80\x80\x80\x80\xf0\x90\x80\x80\xff', len = 7} for k,v in pairs(suits) do test:do_execsql_test( New patch: commit 080a23b1d84ce7749d887819d328e988ee7fe766 Author: Mergen Imeev Date: Fri Oct 1 11:12:39 2021 +0300 sql: rework CHAR_LENGTH() function The CHAR_LENGTH() and CHARACTER_LENGTH() functions now use ICU functions to determine the length of a string. This gives us a better pricision when determining the length. Part of #4145 diff --git a/src/box/sql/func.c b/src/box/sql/func.c index dbeb38bee..faef0eef3 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -258,6 +258,27 @@ func_abs_double(struct sql_context *ctx, int argc, struct Mem *argv) mem_set_double(ctx->pOut, arg->u.r < 0 ? -arg->u.r : arg->u.r); } +/** Implementation of the CHAR_LENGTH() function. */ +static void +func_char_length(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); + uint32_t len = 0; + UErrorCode err = U_ZERO_ERROR; + const char *pos = arg->z; + const char *end = arg->z + arg->n; + while (pos < end) { + ucnv_getNextUChar(icu_utf8_conv, &pos, end, &err); + ++len; + } + mem_set_uint(ctx->pOut, len); +} + static const unsigned char * mem_as_ustr(struct Mem *mem) { @@ -1912,8 +1933,8 @@ static struct sql_func_definition definitions[] = { {"AVG", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, step_avg, fin_avg}, {"AVG", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_avg, fin_avg}, {"CHAR", -1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_STRING, charFunc, NULL}, - {"CHAR_LENGTH", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_INTEGER, lengthFunc, - NULL}, + {"CHAR_LENGTH", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_INTEGER, + func_char_length, NULL}, {"COALESCE", -1, {FIELD_TYPE_ANY}, FIELD_TYPE_SCALAR, sql_builtin_stub, NULL}, {"COUNT", 0, {}, FIELD_TYPE_INTEGER, step_count, fin_count}, @@ -1957,7 +1978,7 @@ static struct sql_func_definition definitions[] = { {"LEAST", -1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, minmaxFunc, NULL}, {"LEAST", -1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, minmaxFunc, NULL}, - {"LENGTH", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_INTEGER, lengthFunc, + {"LENGTH", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_INTEGER, func_char_length, NULL}, {"LENGTH", 1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_INTEGER, lengthFunc, NULL}, diff --git a/test/sql-tap/badutf1.test.lua b/test/sql-tap/badutf1.test.lua index b25436186..27f17168b 100755 --- a/test/sql-tap/badutf1.test.lua +++ b/test/sql-tap/badutf1.test.lua @@ -287,7 +287,7 @@ test:do_test( return test:execsql2("SELECT length('\x80\x80\x80\x80\x80\xf0\x80\x80\x80\x80') AS x") end, { -- - "X", 7 + "X", 10 -- }) @@ -297,7 +297,7 @@ test:do_test( return test:execsql2("SELECT length('\x80\x80\x80\x80\x80\xf0\x80\x80\x80\xff') AS x") end, { -- - "X", 7 + "X", 10 -- }) diff --git a/test/sql-tap/built-in-functions.test.lua b/test/sql-tap/built-in-functions.test.lua index 6fae811dc..7fe987abc 100755 --- a/test/sql-tap/built-in-functions.test.lua +++ b/test/sql-tap/built-in-functions.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool local test = require("sqltester") -test:plan(52) +test:plan(54) -- -- Make sure that number of arguments check is checked properly for SQL built-in @@ -545,4 +545,28 @@ test:do_test( {name = "COLUMN_2", type = "scalar"}, }) +-- gh-4145: Make sure the character is now checked when calculating its length. + +-- Character with UTF-8 code F0808080 does not exist. +test:do_execsql_test( + "builtins-4.1", + [[ + SELECT CHAR_LENGTH(CAST(x'f0808080' AS STRING)); + ]], + { + 4 + } +) + +-- Character with UTF-8 code F0908080 is '𐀀'. +test:do_execsql_test( + "builtins-4.2", + [[ + SELECT CHAR_LENGTH(CAST(x'f0908080' AS STRING)); + ]], + { + 1 + } +) + test:finish_test() diff --git a/test/sql-tap/func3.test.lua b/test/sql-tap/func3.test.lua index 7f1d8d33c..6999fea67 100755 --- a/test/sql-tap/func3.test.lua +++ b/test/sql-tap/func3.test.lua @@ -297,8 +297,8 @@ suits[5] = {str = '\x61\xc0', len = 2} suits[6] = {str = '\x61\xc0\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80', len = 12} suits[7] = {str = '\xc0\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80', len = 11} suits[8] = {str = '\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80', len = 10} -suits[9] = {str = '\x80\x80\x80\x80\x80\xf0\x80\x80\x80\x80', len = 7} -suits[10] = {str = '\x80\x80\x80\x80\x80\xf0\x80\x80\x80\xff', len = 7} +suits[9] = {str = '\x80\x80\x80\x80\x80\xf0\x90\x80\x80\x80', len = 7} +suits[10] = {str = '\x80\x80\x80\x80\x80\xf0\x90\x80\x80\xff', len = 7} for k,v in pairs(suits) do test:do_execsql_test(