[Tarantool-patches] [PATCH v1 8/8] sql: refactor LIKE() function
Mergen Imeev
imeevma at tarantool.org
Mon Nov 1 13:53:51 MSK 2021
I am sorry, I uncluded wrong diff and patch. Diff and patch below.
On Mon, Nov 01, 2021 at 01:48:51PM +0300, Mergen Imeev via Tarantool-patches wrote:
> Thank you for the review! I replaced ucnv_getNextUChar() by U8_NEXT(). Diff and
> new patch below.
>
> On Wed, Oct 20, 2021 at 08:19:38PM +0300, Mergen Imeev via Tarantool-patches wrote:
> <cut>
>
Diff:
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index b055c7515..868d51145 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -1301,11 +1301,11 @@ likeFunc(sql_context *context, int argc, struct Mem *argv)
* single UTF-8 character. Otherwise, return an
* error.
*/
- 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) {
+ const char *str = argv[2].z;
+ int pos = 0;
+ int end = argv[2].n;
+ U8_NEXT((uint8_t *)str, pos, end, escape);
+ if (pos != end || end == 0) {
diag_set(ClientError, ER_SQL_EXECUTE, "ESCAPE "\
"expression must be a single character");
context->is_aborted = true;
Patch:
commit 43d41680d9198a098f866ba7669739221952f446
Author: Mergen Imeev <imeevma at gmail.com>
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 7e71a757b..868d51145 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -1271,63 +1271,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) {
+ const char *str = argv[2].z;
+ int pos = 0;
+ int end = argv[2].n;
+ U8_NEXT((uint8_t *)str, pos, end, escape);
+ if (pos != end || end == 0) {
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;
@@ -1342,7 +1325,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);
}
/**
More information about the Tarantool-patches
mailing list