Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH v1 0/2] sql: VARBINARY result for user-defined functions
@ 2021-05-28 11:41 Mergen Imeev via Tarantool-patches
  2021-05-28 11:41 ` [Tarantool-patches] [PATCH v1 1/2] sql: VARBINARY result for C functions Mergen Imeev via Tarantool-patches
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Mergen Imeev via Tarantool-patches @ 2021-05-28 11:41 UTC (permalink / raw)
  To: v.shpilevoy; +Cc: tarantool-patches

This patch allows VARBINARY to be returned for user-defined functions. Also,
since there is currently no support for UUID and DECIMAL in SQL, so they are
also treated as VARBINARY.

Mergen Imeev (2):
  sql: VARBINARY result for C functions
  sql: VARBINARY result for LUA functions

 .../fix-error-on-return-bin-from-funcs.md     |  3 +
 src/box/sql/mem.c                             | 44 ++++++++++
 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 | 85 +++++++++++++++++++
 5 files changed, 179 insertions(+)
 create mode 100644 changelogs/unreleased/fix-error-on-return-bin-from-funcs.md
 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

-- 
2.25.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Tarantool-patches] [PATCH v1 1/2] sql: VARBINARY result for C functions
  2021-05-28 11:41 [Tarantool-patches] [PATCH v1 0/2] sql: VARBINARY result for user-defined functions Mergen Imeev via Tarantool-patches
@ 2021-05-28 11:41 ` Mergen Imeev via Tarantool-patches
  2021-05-28 21:05   ` Vladislav Shpilevoy via Tarantool-patches
  2021-05-28 11:41 ` [Tarantool-patches] [PATCH v1 2/2] sql: VARBINARY result for LUA functions Mergen Imeev via Tarantool-patches
  2021-06-01 21:44 ` [Tarantool-patches] [PATCH v1 0/2] sql: VARBINARY result for user-defined functions Vladislav Shpilevoy via Tarantool-patches
  2 siblings, 1 reply; 6+ messages in thread
From: Mergen Imeev via Tarantool-patches @ 2021-05-28 11:41 UTC (permalink / raw)
  To: v.shpilevoy; +Cc: tarantool-patches

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Tarantool-patches] [PATCH v1 2/2] sql: VARBINARY result for LUA functions
  2021-05-28 11:41 [Tarantool-patches] [PATCH v1 0/2] sql: VARBINARY result for user-defined functions Mergen Imeev via Tarantool-patches
  2021-05-28 11:41 ` [Tarantool-patches] [PATCH v1 1/2] sql: VARBINARY result for C functions Mergen Imeev via Tarantool-patches
@ 2021-05-28 11:41 ` Mergen Imeev via Tarantool-patches
  2021-06-01 21:44 ` [Tarantool-patches] [PATCH v1 0/2] sql: VARBINARY result for user-defined functions Vladislav Shpilevoy via Tarantool-patches
  2 siblings, 0 replies; 6+ messages in thread
From: Mergen Imeev via Tarantool-patches @ 2021-05-28 11:41 UTC (permalink / raw)
  To: v.shpilevoy; +Cc: tarantool-patches

This patch allows VARBINARY to be returned for user-defined LUA
functions. However, there are currently no values that can be
interpreted as VARBINARY by the serializer, so the only way to get a
VARBINARY result for user-defined LUA functions is to return a UUID or
DECIMAL. Both types are not supported by SQL and are treated as
VARBINARY.

Closes #6024
---
 .../fix-error-on-return-bin-from-funcs.md     |  3 ++
 src/box/sql/mem.c                             | 33 ++++++++++++++++++
 .../sql-tap/gh-6024-funcs-return-bin.test.lua | 34 ++++++++++++++++++-
 3 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 changelogs/unreleased/fix-error-on-return-bin-from-funcs.md

diff --git a/changelogs/unreleased/fix-error-on-return-bin-from-funcs.md b/changelogs/unreleased/fix-error-on-return-bin-from-funcs.md
new file mode 100644
index 000000000..f82815892
--- /dev/null
+++ b/changelogs/unreleased/fix-error-on-return-bin-from-funcs.md
@@ -0,0 +1,3 @@
+## bugfix/sql
+
+* User-defined functions can now return VARBINARY as result (gh-6024).
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index f81f78e27..9894c09af 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -39,6 +39,8 @@
 #include "box/port.h"
 #include "lua/utils.h"
 #include "lua/msgpack.h"
+#include "uuid/mp_uuid.h"
+#include "mp_decimal.h"
 
 /*
  * Make sure pMem->z points to a writable allocation of at least
@@ -2967,6 +2969,37 @@ port_lua_get_vdbemem(struct port *base, uint32_t *size)
 					 field.sval.len) != 0)
 				goto error;
 			break;
+		case MP_EXT: {
+			assert(field.ext_type == MP_UUID ||
+			       field.ext_type == MP_DECIMAL);
+			char *buf;
+			uint32_t size;
+			uint32_t svp = region_used(&fiber()->gc);
+			if (field.ext_type == MP_UUID) {
+				size = mp_sizeof_uuid();
+				buf = region_alloc(&fiber()->gc, size);
+				if (buf == NULL) {
+					diag_set(OutOfMemory, size,
+						 "region_alloc", "buf");
+					goto error;
+				}
+				mp_encode_uuid(buf, field.uuidval);
+			} else {
+				size = mp_sizeof_decimal(field.decval);
+				buf = region_alloc(&fiber()->gc, size);
+				if (buf == NULL) {
+					diag_set(OutOfMemory, size,
+						 "region_alloc", "buf");
+					goto error;
+				}
+				mp_encode_decimal(buf, field.decval);
+			}
+			int rc = mem_copy_bin(&val[i], buf, size);
+			region_truncate(&fiber()->gc, svp);
+			if (rc != 0)
+				goto error;
+			break;
+		}
 		case MP_NIL:
 			break;
 		default:
diff --git a/test/sql-tap/gh-6024-funcs-return-bin.test.lua b/test/sql-tap/gh-6024-funcs-return-bin.test.lua
index 90d09b497..bf406ab7b 100755
--- a/test/sql-tap/gh-6024-funcs-return-bin.test.lua
+++ b/test/sql-tap/gh-6024-funcs-return-bin.test.lua
@@ -3,7 +3,7 @@ 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)
+test:plan(5)
 
 box.schema.func.create("gh-6024-funcs-return-bin.ret_bin", {
     language = "C",
@@ -50,4 +50,36 @@ test:do_execsql_test(
         "varbinary"
     })
 
+box.schema.func.create("get_uuid", {
+    language = "LUA",
+    param_list = {},
+    returns = "varbinary",
+    body = "function(x) return require('uuid').fromstr('11111111-1111-1111-1111-111111111111') end",
+    exports = {"SQL"},
+})
+
+test:do_execsql_test(
+    "gh-6024-4",
+    [[
+        SELECT typeof("get_uuid"()), "get_uuid"() == "gh-6024-funcs-return-bin.ret_uuid"();
+    ]], {
+        "varbinary", true
+    })
+
+box.schema.func.create("get_decimal", {
+    language = "LUA",
+    param_list = {},
+    returns = "varbinary",
+    body = "function(x) return require('decimal').new('9999999999999999999.9999999999999999999') end",
+    exports = {"SQL"},
+})
+
+test:do_execsql_test(
+    "gh-6024-5",
+    [[
+        SELECT typeof("get_decimal"()), "get_decimal"() == "gh-6024-funcs-return-bin.ret_decimal"();
+    ]], {
+        "varbinary", true
+    })
+
 test:finish_test()
-- 
2.25.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Tarantool-patches] [PATCH v1 1/2] sql: VARBINARY result for C functions
  2021-05-28 11:41 ` [Tarantool-patches] [PATCH v1 1/2] sql: VARBINARY result for C functions Mergen Imeev via Tarantool-patches
@ 2021-05-28 21:05   ` Vladislav Shpilevoy via Tarantool-patches
  2021-05-31 10:18     ` Mergen Imeev via Tarantool-patches
  0 siblings, 1 reply; 6+ messages in thread
From: Vladislav Shpilevoy via Tarantool-patches @ 2021-05-28 21:05 UTC (permalink / raw)
  To: imeevma; +Cc: tarantool-patches

Hi! Thanks for the patch!

> 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

Would be good to do a cleanup and remove the functions before the
test ends. The same in the next commit.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Tarantool-patches] [PATCH v1 1/2] sql: VARBINARY result for C functions
  2021-05-28 21:05   ` Vladislav Shpilevoy via Tarantool-patches
@ 2021-05-31 10:18     ` Mergen Imeev via Tarantool-patches
  0 siblings, 0 replies; 6+ messages in thread
From: Mergen Imeev via Tarantool-patches @ 2021-05-31 10:18 UTC (permalink / raw)
  To: Vladislav Shpilevoy; +Cc: tarantool-patches

Hi! Thank you for the review. My answer and diffs below.

On Fri, May 28, 2021 at 11:05:05PM +0200, Vladislav Shpilevoy wrote:
> Hi! Thanks for the patch!
> 
> > 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
> 
> Would be good to do a cleanup and remove the functions before the
> test ends. The same in the next commit.
Fixed.

First diff:

diff --git a/test/sql-tap/gh-6024-funcs-return-bin.test.lua b/test/sql-tap/gh-6024-funcs-return-bin.test.lua
index 90d09b497..7a748c0eb 100755
--- a/test/sql-tap/gh-6024-funcs-return-bin.test.lua
+++ b/test/sql-tap/gh-6024-funcs-return-bin.test.lua
@@ -50,4 +50,8 @@ test:do_execsql_test(
         "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()


Second diff:

diff --git a/test/sql-tap/gh-6024-funcs-return-bin.test.lua b/test/sql-tap/gh-6024-funcs-return-bin.test.lua
index f0c009679..ee3838fa9 100755
--- a/test/sql-tap/gh-6024-funcs-return-bin.test.lua
+++ b/test/sql-tap/gh-6024-funcs-return-bin.test.lua
@@ -85,5 +85,7 @@ test:do_execsql_test(
 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")
+box.schema.func.drop("get_uuid")
+box.schema.func.drop("get_decimal")
 
 test:finish_test()


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Tarantool-patches] [PATCH v1 0/2] sql: VARBINARY result for user-defined functions
  2021-05-28 11:41 [Tarantool-patches] [PATCH v1 0/2] sql: VARBINARY result for user-defined functions Mergen Imeev via Tarantool-patches
  2021-05-28 11:41 ` [Tarantool-patches] [PATCH v1 1/2] sql: VARBINARY result for C functions Mergen Imeev via Tarantool-patches
  2021-05-28 11:41 ` [Tarantool-patches] [PATCH v1 2/2] sql: VARBINARY result for LUA functions Mergen Imeev via Tarantool-patches
@ 2021-06-01 21:44 ` Vladislav Shpilevoy via Tarantool-patches
  2 siblings, 0 replies; 6+ messages in thread
From: Vladislav Shpilevoy via Tarantool-patches @ 2021-06-01 21:44 UTC (permalink / raw)
  To: imeevma; +Cc: tarantool-patches

Hi! Thanks for the patchset!

LGTM.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-06-01 21:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-28 11:41 [Tarantool-patches] [PATCH v1 0/2] sql: VARBINARY result for user-defined functions Mergen Imeev via Tarantool-patches
2021-05-28 11:41 ` [Tarantool-patches] [PATCH v1 1/2] sql: VARBINARY result for C functions Mergen Imeev via Tarantool-patches
2021-05-28 21:05   ` Vladislav Shpilevoy via Tarantool-patches
2021-05-31 10:18     ` Mergen Imeev via Tarantool-patches
2021-05-28 11:41 ` [Tarantool-patches] [PATCH v1 2/2] sql: VARBINARY result for LUA functions Mergen Imeev via Tarantool-patches
2021-06-01 21:44 ` [Tarantool-patches] [PATCH v1 0/2] sql: VARBINARY result for user-defined functions Vladislav Shpilevoy via Tarantool-patches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox