Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: v.shpilevoy@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v1 2/2] sql: VARBINARY result for LUA functions
Date: Fri, 28 May 2021 14:41:27 +0300	[thread overview]
Message-ID: <43a3a45ce4f75288641b5c63299eca9fda69e64c.1622201982.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1622201982.git.imeevma@gmail.com>

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


  parent reply	other threads:[~2021-05-28 11:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Mergen Imeev via Tarantool-patches [this message]
2021-06-01 21:44 ` [Tarantool-patches] [PATCH v1 0/2] sql: VARBINARY result for user-defined functions Vladislav Shpilevoy via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=43a3a45ce4f75288641b5c63299eca9fda69e64c.1622201982.git.imeevma@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v1 2/2] sql: VARBINARY result for LUA functions' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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