[Tarantool-patches] [PATCH v1 07/21] sql: rework RANDOMBLOB() function
imeevma at tarantool.org
imeevma at tarantool.org
Fri Oct 8 20:31:44 MSK 2021
This patch refactors RANDOMBLOB() function. Also, RANDOMBLOB(0) now
returns empty string.
part of #4145
---
src/box/sql/func.c | 52 +++++++++++++++++---------------------
test/sql-tap/func.test.lua | 4 +--
2 files changed, 25 insertions(+), 31 deletions(-)
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 8caf389ab..51a43f271 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -891,6 +891,28 @@ func_random(struct sql_context *ctx, int argc, struct Mem *argv)
mem_set_int(ctx->pOut, r, r < 0);
}
+/** Implementation of the RANDOMBLOB() function. */
+static void
+func_randomblob(struct sql_context *ctx, int argc, struct Mem *argv)
+{
+ assert(argc == 1);
+ (void)argc;
+ struct Mem *arg = &argv[0];
+ assert(mem_is_null(arg) || mem_is_int(arg));
+ if (mem_is_null(arg) || !mem_is_uint(arg))
+ return;
+ if (arg->u.u == 0)
+ return mem_set_bin_static(ctx->pOut, "", 0);
+ uint64_t len = arg->u.u;
+ char *res = sqlDbMallocRawNN(sql_get(), len);
+ if (res == NULL) {
+ ctx->is_aborted = true;
+ return;
+ }
+ sql_randomness(len, res);
+ mem_set_bin_allocated(ctx->pOut, res, len);
+}
+
static const unsigned char *
mem_as_ustr(struct Mem *mem)
{
@@ -1056,34 +1078,6 @@ contextMalloc(struct sql_context *context, i64 nByte)
return z;
}
-/*
- * Implementation of randomblob(N). Return a random blob
- * that is N bytes long.
- */
-static void
-randomBlob(struct sql_context *context, int argc, struct Mem *argv)
-{
- int64_t n;
- unsigned char *p;
- assert(argc == 1);
- UNUSED_PARAMETER(argc);
- if (mem_is_bin(&argv[0]) || mem_is_map(&argv[0]) ||
- mem_is_array(&argv[0])) {
- diag_set(ClientError, ER_SQL_TYPE_MISMATCH,
- mem_str(&argv[0]), "number");
- context->is_aborted = true;
- return;
- }
- n = mem_get_int_unsafe(&argv[0]);
- if (n < 1)
- return;
- p = contextMalloc(context, n);
- if (p) {
- sql_randomness(n, p);
- sql_result_blob(context, (char *)p, n, sql_free);
- }
-}
-
#define Utf8Read(s, e) \
ucnv_getNextUChar(icu_utf8_conv, &(s), (e), &status)
@@ -1908,7 +1902,7 @@ static struct sql_func_definition definitions[] = {
{"QUOTE", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_STRING, quoteFunc, NULL},
{"RANDOM", 0, {}, FIELD_TYPE_INTEGER, func_random, NULL},
{"RANDOMBLOB", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_VARBINARY,
- randomBlob, NULL},
+ func_randomblob, NULL},
{"REPLACE", 3,
{FIELD_TYPE_STRING, FIELD_TYPE_STRING, FIELD_TYPE_STRING},
FIELD_TYPE_STRING, replaceFunc, NULL},
diff --git a/test/sql-tap/func.test.lua b/test/sql-tap/func.test.lua
index dc4dfdc0e..2ce77c6e1 100755
--- a/test/sql-tap/func.test.lua
+++ b/test/sql-tap/func.test.lua
@@ -2545,8 +2545,8 @@ test:do_execsql_test(
test:do_execsql_test(
"func-36",
- [[VALUES (LENGTH(RANDOMBLOB(0)))]],
- {""})
+ [[VALUES (RANDOMBLOB(0))]],
+ {''})
-- gh-3542
-- In SQL '\0' is NOT a end-of-string signal. Tests below ensures
--
2.25.1
More information about the Tarantool-patches
mailing list