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 54C0B71054; Wed, 20 Oct 2021 20:19:42 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 54C0B71054 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1634750382; bh=eKMZ1bxZ2BZMHWgPNK4y3vGm5vKwaA+lsvbZEoVzOD4=; 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=JxXMy2oRBYXDpUkU3dh0TuMGWXHQbfRkIkddpejXRtjrLzLJBgzWWhHW3dbp3y1cN dewD8jWnGP865HmCBGau0l4HjRLz96j09saAQdhXTsxhd/o3oJmNKU9tGSECflACX2 1UyUYzyAuT9zYRoQAUB/LwXqLiARnxqs7gk1Q5LU= 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 388F371054 for ; Wed, 20 Oct 2021 20:19:40 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 388F371054 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mdFFb-0000MC-Hg; Wed, 20 Oct 2021 20:19:39 +0300 Date: Wed, 20 Oct 2021 20:19:38 +0300 To: Vladislav Shpilevoy Cc: tarantool-patches@dev.tarantool.org Message-ID: <20211020171938.GG203963@tarantool.org> References: <873f9256f524e841ae7489ca21933ce16a9a34e0.1633105483.git.imeevma@gmail.com> <762d321c-004d-4ef9-7d8c-1314c19fc673@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <762d321c-004d-4ef9-7d8c-1314c19fc673@tarantool.org> X-4EC0790: 10 X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD9C7814344C8C501C81DF2D982FCC3642ABC592EA95DC3FE9F182A05F5380850404E3CBC9BCC512236A1ABF488C382DBD093288041F3A634FDFC6E481CB7DE7397 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE78E8764B5BC580342EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637D82ED515D6052E03EA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F22D1C0B51BD17FDE4C256D4F1A683F587CC7F00164DA146DAFE8445B8C89999728AA50765F7900637F6B57BC7E64490618DEB871D839B7333395957E7521B51C2DFABB839C843B9C08941B15DA834481F8AA50765F7900637F6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8B953A8A48A05D51F175ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B5059571F82A808FD66BCC44297AA3A2320F X-C1DE0DAB: 0D63561A33F958A5938E4A100C35F4ADCEC3F5F6704CDC85558D6E8C0F61527DD59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7557E988E9157162368E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3498EF79680EE3725CA5A4E7D47287D2053C2E2F8BE9CF3FFFADDC1DA4AF2DBA4A3E064B865B9F8D831D7E09C32AA3244CE4CCA27F594B43E975B3513324C9F43A7C0C08F7987826B9729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojMPcpz6kU2m80U+J3hCmNpA== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DF9263098FB06CC64D9759043AC4F3D8983D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v1 8/8] sql: refactor LIKE() 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. Also, I replaced self-created and SQLite3 functions to work with UTF8 characters by iucnv_getNextUChar(). And I replaced sql_result_bool() by mem_set_bool(), so I will be able to drop sql_result_bool() later. On Sat, Oct 09, 2021 at 12:02:35AM +0200, Vladislav Shpilevoy wrote: > Thanks for the patch! > > > diff --git a/src/box/sql/func.c b/src/box/sql/func.c > > index 1b4d52225..27106ced6 100644 > > --- a/src/box/sql/func.c > > +++ b/src/box/sql/func.c > > @@ -1281,46 +1281,28 @@ likeFunc(sql_context *context, int argc, struct Mem *argv) > > { > > u32 escape = SQL_END_OF_STRING; > > int nPat; > > - if (argc != 2 && argc != 3) { > > - diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, > > - "LIKE", "2 or 3", argc); > > - context->is_aborted = true; > > - return; > > - } > > - sql *db = sql_context_db_handle(context); > > - int rhs_type = sql_value_type(&argv[0]); > > - int lhs_type = sql_value_type(&argv[1]); > > - > > - if (lhs_type != MP_STR || rhs_type != MP_STR) { > > - if (lhs_type == MP_NIL || rhs_type == MP_NIL) > > - return; > > - const char *str = rhs_type != MP_STR ? > > - mem_str(&argv[0]) : mem_str(&argv[1]); > > - diag_set(ClientError, ER_INCONSISTENT_TYPES, "string", str); > > - context->is_aborted = true; > > + assert(argc == 2 || argc == 3); > > + struct sql *db = sql_context_db_handle(context); > > You can get the db after the is_null check below. It is not > used when the check passes. > Fixed. Dropped this line and used sql_get() instead. > > + if (mem_is_any_null(&argv[0], &argv[1])) > > return; Diff: diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 90417c892..afe34f7f0 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1261,7 +1261,6 @@ likeFunc(sql_context *context, int argc, struct Mem *argv) u32 escape = SQL_END_OF_STRING; int nPat; assert(argc == 2 || argc == 3); - struct sql *db = sql_context_db_handle(context); if (mem_is_any_null(&argv[0], &argv[1])) return; assert(mem_is_str(&argv[0]) && mem_is_str(&argv[1])); @@ -1276,7 +1275,7 @@ likeFunc(sql_context *context, int argc, struct Mem *argv) * sql_utf8_pattern_compare(). */ nPat = argv[0].n; - if (nPat > db->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH]) { + if (nPat > sql_get()->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH]) { diag_set(ClientError, ER_SQL_EXECUTE, "LIKE pattern is too "\ "complex"); context->is_aborted = true; @@ -1284,21 +1283,23 @@ likeFunc(sql_context *context, int argc, struct Mem *argv) } if (argc == 3) { + if (mem_is_null(&argv[2])) + return; /* * The escape character string must consist of a * single UTF-8 character. Otherwise, return an * error. */ - const unsigned char *zEsc = (const unsigned char *)argv[2].z; - if (zEsc == 0) - return; - if (sql_utf8_char_count(zEsc, argv[2].n) != 1) { + UErrorCode err = U_ZERO_ERROR; + const char *pos = argv[2].z; + const char *end = argv[2].z + argv[2].n; + escape = ucnv_getNextUChar(icu_utf8_conv, &pos, end, &err); + if (pos != end || err != U_ZERO_ERROR) { diag_set(ClientError, ER_SQL_EXECUTE, "ESCAPE "\ "expression must be a single character"); context->is_aborted = true; return; } - escape = sqlUtf8Read(&zEsc); } if (!zA || !zB) return; @@ -1313,7 +1314,7 @@ likeFunc(sql_context *context, int argc, struct Mem *argv) context->is_aborted = true; return; } - sql_result_bool(context, res == MATCH); + mem_set_bool(context->pOut, res == MATCH); } /** New patch: commit a739926ff3178d499650cff67c8597ff21e18b3c Author: Mergen Imeev Date: Mon Sep 27 19:29:28 2021 +0300 sql: refactor LIKE() function Part of #4145 diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 65bf03250..afe34f7f0 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1260,63 +1260,46 @@ likeFunc(sql_context *context, int argc, struct Mem *argv) { u32 escape = SQL_END_OF_STRING; int nPat; - if (argc != 2 && argc != 3) { - diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, - "LIKE", "2 or 3", argc); - context->is_aborted = true; - return; - } - sql *db = sql_context_db_handle(context); - int rhs_type = sql_value_type(&argv[0]); - int lhs_type = sql_value_type(&argv[1]); - - if (lhs_type != MP_STR || rhs_type != MP_STR) { - if (lhs_type == MP_NIL || rhs_type == MP_NIL) - return; - const char *str = rhs_type != MP_STR ? - mem_str(&argv[0]) : mem_str(&argv[1]); - diag_set(ClientError, ER_INCONSISTENT_TYPES, "string", str); - context->is_aborted = true; + assert(argc == 2 || argc == 3); + if (mem_is_any_null(&argv[0], &argv[1])) return; - } - const char *zB = mem_as_str0(&argv[0]); - const char *zA = mem_as_str0(&argv[1]); - const char *zB_end = zB + mem_len_unsafe(&argv[0]); - const char *zA_end = zA + mem_len_unsafe(&argv[1]); + assert(mem_is_str(&argv[0]) && mem_is_str(&argv[1])); + const char *zB = argv[0].z; + const char *zA = argv[1].z; + const char *zB_end = zB + argv[0].n; + const char *zA_end = zA + argv[1].n; /* * Limit the length of the LIKE pattern to avoid problems * of deep recursion and N*N behavior in * sql_utf8_pattern_compare(). */ - nPat = mem_len_unsafe(&argv[0]); - testcase(nPat == db->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH]); - testcase(nPat == db->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH] + 1); - if (nPat > db->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH]) { + nPat = argv[0].n; + if (nPat > sql_get()->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH]) { diag_set(ClientError, ER_SQL_EXECUTE, "LIKE pattern is too "\ "complex"); context->is_aborted = true; return; } - /* Encoding did not change */ - assert(zB == mem_as_str0(&argv[0])); if (argc == 3) { + if (mem_is_null(&argv[2])) + return; /* * The escape character string must consist of a * single UTF-8 character. Otherwise, return an * error. */ - const unsigned char *zEsc = mem_as_ustr(&argv[2]); - if (zEsc == 0) - return; - if (sql_utf8_char_count(zEsc, mem_len_unsafe(&argv[2])) != 1) { + UErrorCode err = U_ZERO_ERROR; + const char *pos = argv[2].z; + const char *end = argv[2].z + argv[2].n; + escape = ucnv_getNextUChar(icu_utf8_conv, &pos, end, &err); + if (pos != end || err != U_ZERO_ERROR) { diag_set(ClientError, ER_SQL_EXECUTE, "ESCAPE "\ "expression must be a single character"); context->is_aborted = true; return; } - escape = sqlUtf8Read(&zEsc); } if (!zA || !zB) return; @@ -1331,7 +1314,7 @@ likeFunc(sql_context *context, int argc, struct Mem *argv) context->is_aborted = true; return; } - sql_result_bool(context, res == MATCH); + mem_set_bool(context->pOut, res == MATCH); } /**