From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org> To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Subject: [Tarantool-patches] [PATCH v1 16/21] sql: refactor REPLACE() function Date: Thu, 11 Nov 2021 13:49:17 +0300 [thread overview] Message-ID: <b39de8d4d6fce328074b185052c6da8075a26dcc.1636627580.git.imeevma@gmail.com> (raw) In-Reply-To: <cover.1636627579.git.imeevma@gmail.com> Part of #4145 --- src/box/sql/func.c | 59 +++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index ed4d681fc..3b1e62ebf 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1496,34 +1496,27 @@ replaceFunc(struct sql_context *context, int argc, struct Mem *argv) int i, j; /* Loop counters */ assert(argc == 3); - UNUSED_PARAMETER(argc); - zStr = mem_as_ustr(&argv[0]); - if (zStr == 0) - return; - nStr = mem_len_unsafe(&argv[0]); - assert(zStr == mem_as_ustr(&argv[0])); /* No encoding change */ - zPattern = mem_as_ustr(&argv[1]); - if (zPattern == 0) { - assert(mem_is_null(&argv[1]) - || sql_context_db_handle(context)->mallocFailed); + (void)argc; + if (mem_is_any_null(&argv[0], &argv[1]) || mem_is_null(&argv[2])) return; - } - nPattern = mem_len_unsafe(&argv[1]); + assert(mem_is_bytes(&argv[0]) && mem_is_bytes(&argv[1]) && + mem_is_bytes(&argv[2])); + zStr = (const unsigned char *)argv[0].z; + nStr = argv[0].n; + zPattern = (const unsigned char *)argv[1].z; + nPattern = argv[1].n; if (nPattern == 0) { - assert(!mem_is_null(&argv[1])); - sql_result_value(context, &argv[0]); + if (mem_copy(context->pOut, &argv[0]) != 0) + context->is_aborted = true; return; } - assert(zPattern == mem_as_ustr(&argv[1])); /* No encoding change */ - zRep = mem_as_ustr(&argv[2]); - if (zRep == 0) - return; - nRep = mem_len_unsafe(&argv[2]); - assert(zRep == mem_as_ustr(&argv[2])); + zRep = (const unsigned char *)argv[2].z; + nRep = argv[2].n; nOut = nStr + 1; - assert(nOut < SQL_MAX_LENGTH); - zOut = contextMalloc(context, (i64) nOut); - if (zOut == 0) { + struct sql *db = sql_get(); + zOut = sqlDbMallocRawNN(db, nOut); + if (zOut == NULL) { + context->is_aborted = true; return; } loopLimit = nStr - nPattern; @@ -1533,22 +1526,12 @@ replaceFunc(struct sql_context *context, int argc, struct Mem *argv) zOut[j++] = zStr[i]; } else { u8 *zOld; - sql *db = sql_context_db_handle(context); nOut += nRep - nPattern; - testcase(nOut - 1 == db->aLimit[SQL_LIMIT_LENGTH]); - testcase(nOut - 2 == db->aLimit[SQL_LIMIT_LENGTH]); - if (nOut - 1 > db->aLimit[SQL_LIMIT_LENGTH]) { - diag_set(ClientError, ER_SQL_EXECUTE, "string "\ - "or binary string is too big"); - context->is_aborted = true; - sql_free(zOut); - return; - } zOld = zOut; - zOut = sql_realloc64(zOut, (int)nOut); - if (zOut == 0) { + zOut = sqlDbRealloc(db, zOut, nOut); + if (zOut == NULL) { context->is_aborted = true; - sql_free(zOld); + sqlDbFree(db, zOld); return; } memcpy(&zOut[j], zRep, nRep); @@ -1562,9 +1545,9 @@ replaceFunc(struct sql_context *context, int argc, struct Mem *argv) assert(j <= nOut); zOut[j] = 0; if (context->func->def->returns == FIELD_TYPE_STRING) - mem_set_str_dynamic(context->pOut, (char *)zOut, j); + mem_set_str_allocated(context->pOut, (char *)zOut, j); else - mem_set_bin_dynamic(context->pOut, (char *)zOut, j); + mem_set_bin_allocated(context->pOut, (char *)zOut, j); } /* -- 2.25.1
next prev parent reply other threads:[~2021-11-11 10:58 UTC|newest] Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-11-11 10:48 [Tarantool-patches] [PATCH v1 00/21] Refactor non-standard and non-aggragate functions Mergen Imeev via Tarantool-patches 2021-11-11 10:48 ` [Tarantool-patches] [PATCH v1 01/21] sql: rework CHAR() function Mergen Imeev via Tarantool-patches 2021-11-11 10:48 ` [Tarantool-patches] [PATCH v1 02/21] sql: refactor GREATEST() and LEAST() functions Mergen Imeev via Tarantool-patches 2021-11-11 10:48 ` [Tarantool-patches] [PATCH v1 03/21] sql: refactor HEX() function Mergen Imeev via Tarantool-patches 2021-11-11 10:48 ` [Tarantool-patches] [PATCH v1 04/21] sql: refactor LENGTH() function Mergen Imeev via Tarantool-patches 2021-11-11 10:48 ` [Tarantool-patches] [PATCH v1 05/21] sql: refactor PRINTF() function Mergen Imeev via Tarantool-patches 2021-11-11 10:48 ` [Tarantool-patches] [PATCH v1 06/21] sql: refactor RANDOM() function Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 07/21] sql: rework RANDOMBLOB() function Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 08/21] sql: refactor ZEROBLOB() function Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 09/21] sql: refactor TYPEOF() function Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 10/21] sql: refactor ROUND() function Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 11/21] sql: refactor ROW_COUNT() function Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 12/21] sql: rework UUID() function Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 13/21] sql: refactor VERSION() function Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 14/21] sql: refactor UNICODE() function Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 15/21] sql: refactor SOUNDEX() function Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` Mergen Imeev via Tarantool-patches [this message] 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 17/21] sql: refactor QUOTE() function Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 18/21] sql: remove unused code Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 19/21] sql: remove MEM_Dyn flag Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 20/21] sql: remove MEM_Term flag Mergen Imeev via Tarantool-patches 2021-11-11 10:49 ` [Tarantool-patches] [PATCH v1 21/21] sql: make arguments to be const Mergen Imeev via Tarantool-patches 2021-11-11 11:00 ` [Tarantool-patches] [PATCH v1 00/21] Refactor non-standard and non-aggragate functions Kirill Yukhin via Tarantool-patches -- strict thread matches above, loose matches on Subject: below -- 2021-10-08 17:31 Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 16/21] sql: refactor REPLACE() function Mergen Imeev via Tarantool-patches 2021-10-14 22:45 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:45 ` Mergen Imeev via Tarantool-patches
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=b39de8d4d6fce328074b185052c6da8075a26dcc.1636627580.git.imeevma@gmail.com \ --to=tarantool-patches@dev.tarantool.org \ --cc=imeevma@tarantool.org \ --cc=kyukhin@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v1 16/21] sql: refactor REPLACE() function' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox