[Tarantool-patches] [PATCH v1 1/2] sql: VARBINARY result for C functions
imeevma at tarantool.org
imeevma at tarantool.org
Fri May 28 14:41:25 MSK 2021
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 | 53 +++++++++++++++++++
4 files changed, 111 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 03fbffc7f..f81f78e27 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 6e2eae2ff..bf0c3a11d 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 000000000..ed9070006
--- /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 000000000..90d09b497
--- /dev/null
+++ b/test/sql-tap/gh-6024-funcs-return-bin.test.lua
@@ -0,0 +1,53 @@
+#!/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"
+ })
+
+test:finish_test()
--
2.25.1
More information about the Tarantool-patches
mailing list