[Tarantool-patches] [PATCH v1 2/2] sql: VARBINARY result for LUA functions

imeevma at tarantool.org imeevma at tarantool.org
Fri May 28 14:41:27 MSK 2021


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



More information about the Tarantool-patches mailing list