After addition of UUID to SQL a few new problems appeared. This patch fixes such problems. https://github.com/tarantool/tarantool/issues/6164 https://github.com/tarantool/tarantool/tree/imeevma/gh-6164-uuid-follow-ups Mergen Imeev (3): sql: introduce uuid to quote() sql: fix uuid behaviour in least() and greatest() sql: allow to bind uuid values src/box/bind.c | 3 ++ src/box/bind.h | 5 ++ src/box/lua/execute.c | 5 ++ src/box/sql/func.c | 24 +++++---- src/box/sql/mem.c | 4 +- src/box/sql/sqlInt.h | 5 ++ src/box/sql/vdbeapi.c | 11 +++++ test/sql-tap/gh-6164-uuid-follow-ups.test.lua | 49 +++++++++++++++++++ 8 files changed, 95 insertions(+), 11 deletions(-) create mode 100755 test/sql-tap/gh-6164-uuid-follow-ups.test.lua -- 2.25.1
Prior to this patch, built-in SQL function quote() could not work with uuid. It now returns a string representation of the received uuid. Part of #6164 --- src/box/sql/func.c | 24 ++++++++++++------- test/sql-tap/gh-6164-uuid-follow-ups.test.lua | 16 +++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) create mode 100755 test/sql-tap/gh-6164-uuid-follow-ups.test.lua diff --git a/src/box/sql/func.c b/src/box/sql/func.c index f93ae867d..aa565277c 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1098,8 +1098,8 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv) { assert(argc == 1); UNUSED_PARAMETER(argc); - switch (sql_value_type(argv[0])) { - case MP_DOUBLE:{ + switch (argv[0]->type) { + case MEM_TYPE_DOUBLE:{ double r1, r2; char zBuf[50]; r1 = mem_get_double_unsafe(argv[0]); @@ -1113,14 +1113,20 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv) SQL_TRANSIENT); break; } - case MP_UINT: - case MP_INT:{ + case MEM_TYPE_UUID: { + char buf[UUID_STR_LEN + 1]; + tt_uuid_to_string(&argv[0]->u.uuid, &buf[0]); + sql_result_text(context, buf, UUID_STR_LEN, SQL_TRANSIENT); + break; + } + case MEM_TYPE_UINT: + case MEM_TYPE_INT: { sql_result_value(context, argv[0]); break; } - case MP_BIN: - case MP_ARRAY: - case MP_MAP: { + case MEM_TYPE_BIN: + case MEM_TYPE_ARRAY: + case MEM_TYPE_MAP: { char *zText = 0; char const *zBlob = mem_as_bin(argv[0]); int nBlob = mem_len_unsafe(argv[0]); @@ -1146,7 +1152,7 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv) } break; } - case MP_STR:{ + case MEM_TYPE_STR: { int i, j; u64 n; const unsigned char *zArg = mem_as_ustr(argv[0]); @@ -1174,7 +1180,7 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv) } break; } - case MP_BOOL: { + case MEM_TYPE_BOOL: { sql_result_text(context, SQL_TOKEN_BOOLEAN(mem_get_bool_unsafe(argv[0])), -1, SQL_TRANSIENT); diff --git a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua new file mode 100755 index 000000000..d8fa700ea --- /dev/null +++ b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua @@ -0,0 +1,16 @@ +#!/usr/bin/env tarantool +local test = require("sqltester") +test:plan(1) + +box.execute([[select quote(cast('11111111-1111-1111-1111-111111111111' as uuid));]]) + +-- Make sure that function quote() can work with uuid. +test:do_execsql_test( + "gh-6164-1", + [[ + SELECT quote(cast('11111111-1111-1111-1111-111111111111' as uuid)); + ]], { + '11111111-1111-1111-1111-111111111111' + }) + +test:finish_test() -- 2.25.1
Prior to this patch, the built-in SQL functions greatest() and least() could return incorrect results if their arguments contained at least one uuid and at least one value of a different type. These values are now sorted according to the rules of SCALAR comparison, where UUID values are larger than any other scalar values. Part of #6164 --- src/box/sql/mem.c | 4 ++-- test/sql-tap/gh-6164-uuid-follow-ups.test.lua | 21 ++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 6f3bf52e5..330ddfd8e 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -2480,8 +2480,8 @@ sqlMemCompare(const Mem * pMem1, const Mem * pMem2, const struct coll * pColl) if (mem_cmp_uuid(pMem1, pMem2, &res) == 0) return res; if (type1 != MEM_TYPE_UUID) - return +1; - return -1; + return -1; + return +1; } /* At least one of the two values is a number diff --git a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua index d8fa700ea..8872f9f23 100755 --- a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua +++ b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua @@ -1,6 +1,8 @@ #!/usr/bin/env tarantool local test = require("sqltester") -test:plan(1) +test:plan(3) + +local uuid = require('uuid').fromstr('11111111-1111-1111-1111-111111111111') box.execute([[select quote(cast('11111111-1111-1111-1111-111111111111' as uuid));]]) @@ -13,4 +15,21 @@ test:do_execsql_test( '11111111-1111-1111-1111-111111111111' }) +-- Make sure that functions greatest() and least() can properly work with uuid. +test:do_execsql_test( + "gh-6164-2", + [[ + SELECT GREATEST(true, 1, x'33', cast('11111111-1111-1111-1111-111111111111' as uuid), 1e10); + ]], { + uuid + }) + +test:do_execsql_test( + "gh-6164-3", + [[ + SELECT LEAST(true, 1, x'33', cast('11111111-1111-1111-1111-111111111111' as uuid), 1e10); + ]], { + true + }) + test:finish_test() -- 2.25.1
After this patch, uuid values can be binded like any other supported by SQL values. Closes #6164 --- src/box/bind.c | 3 +++ src/box/bind.h | 5 +++++ src/box/lua/execute.c | 5 +++++ src/box/sql/sqlInt.h | 5 +++++ src/box/sql/vdbeapi.c | 11 +++++++++++ test/sql-tap/gh-6164-uuid-follow-ups.test.lua | 16 +++++++++++++++- 6 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/box/bind.c b/src/box/bind.c index d45a0f9a7..734f65186 100644 --- a/src/box/bind.c +++ b/src/box/bind.c @@ -191,6 +191,9 @@ sql_bind_column(struct sql_stmt *stmt, const struct sql_bind *p, case MP_BIN: return sql_bind_blob64(stmt, pos, (const void *) p->s, p->bytes, SQL_STATIC); + case MP_EXT: + assert(p->ext_type == MP_UUID); + return sql_bind_uuid(stmt, pos, &p->uuid); default: unreachable(); } diff --git a/src/box/bind.h b/src/box/bind.h index 568c558f3..20f3e7942 100644 --- a/src/box/bind.h +++ b/src/box/bind.h @@ -40,6 +40,8 @@ extern "C" { #include <stdlib.h> #include "msgpuck.h" +#include "uuid/tt_uuid.h" +#include "lib/core/mp_extension_types.h" struct sql_stmt; @@ -59,6 +61,8 @@ struct sql_bind { uint32_t bytes; /** MessagePack type of the value. */ enum mp_type type; + /** Subtype of MP_EXT type. */ + enum mp_extension_type ext_type; /** Bind value. */ union { bool b; @@ -67,6 +71,7 @@ struct sql_bind { uint64_t u64; /** For string or blob. */ const char *s; + struct tt_uuid uuid; }; }; diff --git a/src/box/lua/execute.c b/src/box/lua/execute.c index 926a0a61c..3d1352fc6 100644 --- a/src/box/lua/execute.c +++ b/src/box/lua/execute.c @@ -370,6 +370,10 @@ lua_sql_bind_decode(struct lua_State *L, struct sql_bind *bind, int idx, int i) bind->s = mp_decode_bin(&field.sval.data, &bind->bytes); break; case MP_EXT: + if (field.ext_type == MP_UUID) { + bind->uuid = *field.uuidval; + break; + } diag_set(ClientError, ER_SQL_BIND_TYPE, "USERDATA", sql_bind_name(bind)); return -1; @@ -385,6 +389,7 @@ lua_sql_bind_decode(struct lua_State *L, struct sql_bind *bind, int idx, int i) unreachable(); } bind->type = field.type; + bind->ext_type = field.ext_type; lua_pop(L, lua_gettop(L) - idx); return 0; } diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index ef8dcd693..115c52f96 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -326,6 +326,8 @@ struct sql_vfs { #define SQL_LIMIT_LIKE_PATTERN_LENGTH 8 #define SQL_LIMIT_TRIGGER_DEPTH 9 +struct tt_uuid; + enum sql_ret_code { /** sql_step() has another row ready. */ SQL_ROW = 1, @@ -634,6 +636,9 @@ int sql_bind_zeroblob64(sql_stmt *, int, sql_uint64); +int +sql_bind_uuid(struct sql_stmt *stmt, int i, const struct tt_uuid *uuid); + /** * Return the number of wildcards that should be bound to. */ diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index aaae12e41..a9700350d 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -840,6 +840,17 @@ sql_bind_zeroblob64(sql_stmt * pStmt, int i, sql_uint64 n) return sql_bind_zeroblob(pStmt, i, n); } +int +sql_bind_uuid(struct sql_stmt *stmt, int i, const struct tt_uuid *uuid) +{ + struct Vdbe *p = (struct Vdbe *)stmt; + if (vdbeUnbind(p, i) != 0) + return -1; + int rc = sql_bind_type(p, i, "uuid"); + mem_set_uuid(&p->aVar[i - 1], uuid); + return rc; +} + int sql_bind_parameter_count(const struct sql_stmt *stmt) { diff --git a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua index 8872f9f23..426717972 100755 --- a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua +++ b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool local test = require("sqltester") -test:plan(3) +test:plan(4) local uuid = require('uuid').fromstr('11111111-1111-1111-1111-111111111111') @@ -32,4 +32,18 @@ test:do_execsql_test( true }) +-- Make sure that uuid value can be binded. +box.execute('CREATE TABLE t(i INT PRIMARY KEY, a UUID);') +box.execute('INSERT INTO t VALUES(1, ?);', {uuid}); + +test:do_execsql_test( + "gh-6164-4", + [[ + SELECT * FROM t; + ]], { + 1, uuid + }) + +box.execute([[DROP TABLE t;]]) + test:finish_test() -- 2.25.1
Hi! Thanks for the patch! > diff --git a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua > new file mode 100755 > index 000000000..d8fa700ea > --- /dev/null > +++ b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua 1. The test runs 2 times with 2 engines. Please, make it work only once. It does not depend on an engine anyway. > @@ -0,0 +1,16 @@ > +#!/usr/bin/env tarantool > +local test = require("sqltester") > +test:plan(1) > + > +box.execute([[select quote(cast('11111111-1111-1111-1111-111111111111' as uuid));]]) 2. Why do you need this select? Isn't it exactly the same as the SELECT below? > + > +-- Make sure that function quote() can work with uuid. > +test:do_execsql_test( > + "gh-6164-1", > + [[ > + SELECT quote(cast('11111111-1111-1111-1111-111111111111' as uuid)); > + ]], { > + '11111111-1111-1111-1111-111111111111' > + }) > + > +test:finish_test()
Thanks for the patch! > diff --git a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua > index d8fa700ea..8872f9f23 100755 > --- a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua > +++ b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua > @@ -13,4 +15,21 @@ test:do_execsql_test( > '11111111-1111-1111-1111-111111111111' > }) > > +-- Make sure that functions greatest() and least() can properly work with uuid. > +test:do_execsql_test( > + "gh-6164-2", > + [[ > + SELECT GREATEST(true, 1, x'33', cast('11111111-1111-1111-1111-111111111111' as uuid), 1e10); Please, try to remain inside 80 symbols line width limit. The same below. > + ]], { > + uuid > + }) > + > +test:do_execsql_test( > + "gh-6164-3", > + [[ > + SELECT LEAST(true, 1, x'33', cast('11111111-1111-1111-1111-111111111111' as uuid), 1e10); > + ]], { > + true > + }) > + > test:finish_test() >
Thanks for the patch! See 4 comments below. On 05.07.2021 17:06, Mergen Imeev via Tarantool-patches wrote: > After this patch, uuid values can be binded like any other supported by 1. binded -> bound. > diff --git a/src/box/bind.h b/src/box/bind.h > index 568c558f3..20f3e7942 100644 > --- a/src/box/bind.h > +++ b/src/box/bind.h > @@ -40,6 +40,8 @@ extern "C" { > #include <stdlib.h> > > #include "msgpuck.h" > +#include "uuid/tt_uuid.h" > +#include "lib/core/mp_extension_types.h" 2. lib/core is already in the paths. You can omit it. > diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c > index aaae12e41..a9700350d 100644 > --- a/src/box/sql/vdbeapi.c > +++ b/src/box/sql/vdbeapi.c > @@ -840,6 +840,17 @@ sql_bind_zeroblob64(sql_stmt * pStmt, int i, sql_uint64 n) > return sql_bind_zeroblob(pStmt, i, n); > } > > +int > +sql_bind_uuid(struct sql_stmt *stmt, int i, const struct tt_uuid *uuid) > +{ > + struct Vdbe *p = (struct Vdbe *)stmt; > + if (vdbeUnbind(p, i) != 0) > + return -1; > + int rc = sql_bind_type(p, i, "uuid"); > + mem_set_uuid(&p->aVar[i - 1], uuid); 3. Why do you set UUID even if bind_type() failed? > diff --git a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua > index 8872f9f23..426717972 100755 > --- a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua > +++ b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua > @@ -32,4 +32,18 @@ test:do_execsql_test( > true > }) > > +-- Make sure that uuid value can be binded. > +box.execute('CREATE TABLE t(i INT PRIMARY KEY, a UUID);') > +box.execute('INSERT INTO t VALUES(1, ?);', {uuid}); 4. Do you need a table for that? Can you just make 'SELECT ?' with the uuid argument? > + > +test:do_execsql_test( > + "gh-6164-4", > + [[ > + SELECT * FROM t; > + ]], { > + 1, uuid > + }) > + > +box.execute([[DROP TABLE t;]]) > + > test:finish_test() >
Thank you for the review! I abandoned this patch because I realized that
a more
global solution was needed.
On 08.07.2021 00:42, Vladislav Shpilevoy wrote:
> Thanks for the patch!
>
>> diff --git a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua
>> index d8fa700ea..8872f9f23 100755
>> --- a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua
>> +++ b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua
>> @@ -13,4 +15,21 @@ test:do_execsql_test(
>> '11111111-1111-1111-1111-111111111111'
>> })
>>
>> +-- Make sure that functions greatest() and least() can properly work with uuid.
>> +test:do_execsql_test(
>> + "gh-6164-2",
>> + [[
>> + SELECT GREATEST(true, 1, x'33', cast('11111111-1111-1111-1111-111111111111' as uuid), 1e10);
> Please, try to remain inside 80 symbols line width limit. The same below.
>
>> + ]], {
>> + uuid
>> + })
>> +
>> +test:do_execsql_test(
>> + "gh-6164-3",
>> + [[
>> + SELECT LEAST(true, 1, x'33', cast('11111111-1111-1111-1111-111111111111' as uuid), 1e10);
>> + ]], {
>> + true
>> + })
>> +
>> test:finish_test()
>>