From b5a78eadb1600ac4549fecfd3edac4efddf0b12e Mon Sep 17 00:00:00 2001 From: Mergen Imeev <imeevma@gmail.com> Date: Mon, 26 Apr 2021 15:33:29 +0300 Subject: [PATCH] sql: VARBINARY result for C functions This patch allows VARBINARY to be returned for user-defined C functions. There is currently no support for UUID and DECIMAL in SQL, so they are also treated as VARBINARY. Part of #6024 --- src/box/sql/mem.c | 11 ++++ test/sql-tap/CMakeLists.txt | 1 + test/sql-tap/gh-6024-funcs-return-bin.c | 46 +++++++++++++++ .../sql-tap/gh-6024-funcs-return-bin.test.lua | 57 +++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 test/sql-tap/gh-6024-funcs-return-bin.c create mode 100755 test/sql-tap/gh-6024-funcs-return-bin.test.lua diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 03fbffc7f3..f81f78e27b 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -3047,6 +3047,17 @@ port_c_get_vdbemem(struct port *base, uint32_t *size) if (mem_copy_str(&val[i], str, len) != 0) goto error; break; + case MP_BIN: + str = mp_decode_bin(&data, &len); + if (mem_copy_bin(&val[i], str, len) != 0) + goto error; + break; + case MP_EXT: + str = data; + mp_next(&data); + if (mem_copy_bin(&val[i], str, data - str) != 0) + goto error; + break; case MP_NIL: break; default: diff --git a/test/sql-tap/CMakeLists.txt b/test/sql-tap/CMakeLists.txt index 6e2eae2ff1..bf0c3a11d1 100644 --- a/test/sql-tap/CMakeLists.txt +++ b/test/sql-tap/CMakeLists.txt @@ -1,2 +1,3 @@ include_directories(${MSGPUCK_INCLUDE_DIRS}) build_module(gh-5938-wrong-string-length gh-5938-wrong-string-length.c) +build_module(gh-6024-funcs-return-bin gh-6024-funcs-return-bin.c) diff --git a/test/sql-tap/gh-6024-funcs-return-bin.c b/test/sql-tap/gh-6024-funcs-return-bin.c new file mode 100644 index 0000000000..ed90700066 --- /dev/null +++ b/test/sql-tap/gh-6024-funcs-return-bin.c @@ -0,0 +1,46 @@ +#include "msgpuck.h" +#include "module.h" +#include "uuid/mp_uuid.h" +#include "mp_decimal.h" + +enum { + BUF_SIZE = 512, +}; + +int +ret_bin(box_function_ctx_t *ctx, const char *args, const char *args_end) +{ + (void)args; + (void)args_end; + const char bin[] = "some varbinary string"; + char res[BUF_SIZE]; + char *end = mp_encode_bin(res, bin, sizeof(bin)); + box_return_mp(ctx, res, end); + return 0; +} + +int +ret_uuid(box_function_ctx_t *ctx, const char *args, const char *args_end) +{ + (void)args; + (void)args_end; + struct tt_uuid uuid; + memset(&uuid, 0x11, sizeof(uuid)); + char res[BUF_SIZE]; + char *end = mp_encode_uuid(res, &uuid); + box_return_mp(ctx, res, end); + return 0; +} + +int +ret_decimal(box_function_ctx_t *ctx, const char *args, const char *args_end) +{ + (void)args; + (void)args_end; + decimal_t dec; + decimal_from_string(&dec, "9999999999999999999.9999999999999999999"); + char res[BUF_SIZE]; + char *end = mp_encode_decimal(res, &dec); + box_return_mp(ctx, res, end); + return 0; +} diff --git a/test/sql-tap/gh-6024-funcs-return-bin.test.lua b/test/sql-tap/gh-6024-funcs-return-bin.test.lua new file mode 100755 index 0000000000..7a748c0eb6 --- /dev/null +++ b/test/sql-tap/gh-6024-funcs-return-bin.test.lua @@ -0,0 +1,57 @@ +#!/usr/bin/env tarantool +local build_path = os.getenv("BUILDDIR") +package.cpath = build_path..'/test/sql-tap/?.so;'..build_path..'/test/sql-tap/?.dylib;'..package.cpath + +local test = require("sqltester") +test:plan(3) + +box.schema.func.create("gh-6024-funcs-return-bin.ret_bin", { + language = "C", + param_list = {}, + returns = "varbinary", + exports = {"SQL"}, +}) + +test:do_execsql_test( + "gh-6024-1", + [[ + SELECT typeof("gh-6024-funcs-return-bin.ret_bin"()); + ]], { + "varbinary" + }) + +box.schema.func.create("gh-6024-funcs-return-bin.ret_uuid", { + language = "C", + param_list = {}, + returns = "varbinary", + exports = {"SQL"}, +}) + +test:do_execsql_test( + "gh-6024-2", + [[ + SELECT typeof("gh-6024-funcs-return-bin.ret_uuid"()); + ]], { + "varbinary" + }) + +box.schema.func.create("gh-6024-funcs-return-bin.ret_decimal", { + language = "C", + param_list = {}, + returns = "varbinary", + exports = {"SQL"}, +}) + +test:do_execsql_test( + "gh-6024-3", + [[ + SELECT typeof("gh-6024-funcs-return-bin.ret_decimal"()); + ]], { + "varbinary" + }) + +box.schema.func.drop("gh-6024-funcs-return-bin.ret_bin") +box.schema.func.drop("gh-6024-funcs-return-bin.ret_uuid") +box.schema.func.drop("gh-6024-funcs-return-bin.ret_decimal") + +test:finish_test() -- GitLab