Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH v2 0/2] sql: introduce UUID
@ 2021-06-02  8:02 Mergen Imeev via Tarantool-patches
  2021-06-02  8:02 ` [Tarantool-patches] [PATCH v2 1/2] sql: introduce UUID field type Mergen Imeev via Tarantool-patches
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Mergen Imeev via Tarantool-patches @ 2021-06-02  8:02 UTC (permalink / raw)
  To: tsafin; +Cc: tarantool-patches

This patch-set introduces UUID to SQL. After this patch-set UUID field type will
be available in SQL and UUID values can be generated using UUID() function.

https://github.com/tarantool/tarantool/issues/5886
https://github.com/tarantool/tarantool/tree/imeevma/gh-5886-introduce-uuid-type-in-sql

Changes in v2:
 - Added SQL built-in function uuid().

Mergen Imeev (2):
  sql: introduce UUID field type
  sql: introduce SQL built-in function UUID()

 .../unreleased/introduce-uuid-to-sql.md       |    3 +
 extra/mkkeywordhash.c                         |    1 +
 src/box/bootstrap.snap                        |  Bin 5991 -> 6016 bytes
 src/box/lua/upgrade.lua                       |   19 +
 src/box/sql/func.c                            |   69 +-
 src/box/sql/mem.c                             |  203 ++-
 src/box/sql/mem.h                             |   29 +-
 src/box/sql/parse.y                           |    3 +-
 src/box/sql/vdbe.c                            |   15 +-
 test/box-py/bootstrap.result                  |    3 +-
 test/box/access.result                        |    2 +-
 test/box/access.test.lua                      |    2 +-
 test/box/access_bin.result                    |    2 +-
 test/box/access_bin.test.lua                  |    2 +-
 test/box/access_sysview.result                |    8 +-
 test/box/function1.result                     |    6 +-
 test/sql-tap/CMakeLists.txt                   |    1 +
 .../gh-5913-segfault-on-select-uuid.test.lua  |   42 +-
 .../sql-tap/gh-6024-funcs-return-bin.test.lua |    8 +-
 test/sql-tap/sql_uuid.c                       |   46 +
 test/sql-tap/uuid.test.lua                    | 1344 +++++++++++++++++
 test/wal_off/func_max.result                  |    8 +-
 22 files changed, 1731 insertions(+), 85 deletions(-)
 create mode 100644 changelogs/unreleased/introduce-uuid-to-sql.md
 create mode 100644 test/sql-tap/sql_uuid.c
 create mode 100755 test/sql-tap/uuid.test.lua

-- 
2.25.1


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

* [Tarantool-patches] [PATCH v2 1/2] sql: introduce UUID field type
  2021-06-02  8:02 [Tarantool-patches] [PATCH v2 0/2] sql: introduce UUID Mergen Imeev via Tarantool-patches
@ 2021-06-02  8:02 ` Mergen Imeev via Tarantool-patches
  2021-06-02 20:13   ` Timur Safin via Tarantool-patches
  2021-06-02  8:02 ` [Tarantool-patches] [PATCH v2 2/2] sql: introduce SQL built-in function UUID() Mergen Imeev via Tarantool-patches
  2021-06-02 20:13 ` [Tarantool-patches] [PATCH v2 0/2] sql: introduce UUID Timur Safin via Tarantool-patches
  2 siblings, 1 reply; 8+ messages in thread
From: Mergen Imeev via Tarantool-patches @ 2021-06-02  8:02 UTC (permalink / raw)
  To: tsafin; +Cc: tarantool-patches

This patch introduces UUID to SQL. UUID is now available as a new field
type.

Part of #5886

@TarantoolBot document
Title: Field type UUID is now available in SQL

The UUID field type is now available in SQL. This means that we can
create spaces and indexes with UUID, use it in SELECT, UPDATE and
DELETE. UUID can be accepted and returned by built-in functions and
user-defined functions.

According to the comparison rules, there will be no implicit casting in
the comparison. This rule also applies to UUID values: if a value is not
part of a SCALAR field, it cannot be compared to a value of any other
type. If the value is in a SCALAR field, it can be compared to any other
scalar value according to the comparison rules for a SCALAR field.

In case a UUID value is used in an operation that is not a comparison,
it can be implicitly converted to STRING or VARBINARY.

If a STRING or VARBINARY value is used in an operation that is not a
comparison, it can be implicitly converted to a UUID.

UUID value can always be explicitly converted to STRING or VARBINARY.

A STRING or VARBINARY value can be explicitly converted to a UUID if it
conforms to the UUID standard.
---
 extra/mkkeywordhash.c                         |    1 +
 src/box/sql/func.c                            |   30 +-
 src/box/sql/mem.c                             |  203 ++-
 src/box/sql/mem.h                             |   29 +-
 src/box/sql/parse.y                           |    1 +
 src/box/sql/vdbe.c                            |   15 +-
 test/sql-tap/CMakeLists.txt                   |    1 +
 .../gh-5913-segfault-on-select-uuid.test.lua  |   42 +-
 .../sql-tap/gh-6024-funcs-return-bin.test.lua |    8 +-
 test/sql-tap/sql_uuid.c                       |   46 +
 test/sql-tap/uuid.test.lua                    | 1294 +++++++++++++++++
 11 files changed, 1602 insertions(+), 68 deletions(-)
 create mode 100644 test/sql-tap/sql_uuid.c
 create mode 100755 test/sql-tap/uuid.test.lua

diff --git a/extra/mkkeywordhash.c b/extra/mkkeywordhash.c
index 7480c0211..0d998506c 100644
--- a/extra/mkkeywordhash.c
+++ b/extra/mkkeywordhash.c
@@ -172,6 +172,7 @@ static Keyword aKeywordTable[] = {
   { "UNSIGNED",               "TK_UNSIGNED",    true  },
   { "UPDATE",                 "TK_UPDATE",      true  },
   { "USING",                  "TK_USING",       true  },
+  { "UUID" ,                  "TK_UUID" ,       true  },
   { "VALUES",                 "TK_VALUES",      true  },
   { "VARBINARY",              "TK_VARBINARY",   true  },
   { "VIEW",                   "TK_VIEW",        true  },
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 90e8e152f..9c4480a92 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -58,7 +58,8 @@ static const void *
 mem_as_bin(struct Mem *mem)
 {
 	const char *s;
-	if (!mem_is_bytes(mem) && mem_to_str(mem) != 0)
+	if (mem_cast_implicit(mem, FIELD_TYPE_VARBINARY) != 0 &&
+	    mem_to_str(mem) != 0)
 		return NULL;
 	if (mem_get_bin(mem, &s) != 0)
 		return NULL;
@@ -142,26 +143,29 @@ typeofFunc(sql_context * context, int NotUsed, sql_value ** argv)
 				-1, SQL_STATIC);
 		return;
 	}
-	switch (sql_value_type(argv[0])) {
-	case MP_INT:
-	case MP_UINT:
+	switch (argv[0]->type) {
+	case MEM_TYPE_INT:
+	case MEM_TYPE_UINT:
 		z = "integer";
 		break;
-	case MP_STR:
+	case MEM_TYPE_STR:
 		z = "string";
 		break;
-	case MP_DOUBLE:
+	case MEM_TYPE_DOUBLE:
 		z = "double";
 		break;
-	case MP_BIN:
-	case MP_ARRAY:
-	case MP_MAP:
+	case MEM_TYPE_BIN:
+	case MEM_TYPE_ARRAY:
+	case MEM_TYPE_MAP:
 		z = "varbinary";
 		break;
-	case MP_BOOL:
-	case MP_NIL:
+	case MEM_TYPE_BOOL:
+	case MEM_TYPE_NULL:
 		z = "boolean";
 		break;
+	case MEM_TYPE_UUID:
+		z = "uuid";
+		break;
 	default:
 		unreachable();
 		break;
@@ -191,6 +195,7 @@ lengthFunc(sql_context * context, int argc, sql_value ** argv)
 			sql_result_uint(context, mem_len_unsafe(argv[0]));
 			break;
 		}
+	case MP_EXT:
 	case MP_STR:{
 			const unsigned char *z = mem_as_ustr(argv[0]);
 			if (z == 0)
@@ -235,6 +240,7 @@ absFunc(sql_context * context, int argc, sql_value ** argv)
 		}
 	case MP_BOOL:
 	case MP_BIN:
+	case MP_EXT:
 	case MP_ARRAY:
 	case MP_MAP: {
 		diag_set(ClientError, ER_INCONSISTENT_TYPES, "number",
@@ -1461,8 +1467,8 @@ trim_func_one_arg(struct sql_context *context, sql_value *arg)
 		default_trim = (const unsigned char *) "\0";
 	else
 		default_trim = (const unsigned char *) " ";
-	int input_str_sz = mem_len_unsafe(arg);
 	const unsigned char *input_str = mem_as_ustr(arg);
+	int input_str_sz = mem_len_unsafe(arg);
 	uint8_t trim_char_len[1] = { 1 };
 	trim_procedure(context, TRIM_BOTH, default_trim, trim_char_len, 1,
 		       input_str, input_str_sz);
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 9894c09af..f0dfce395 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -58,6 +58,16 @@ enum {
 	BUF_SIZE = 32,
 };
 
+bool
+mem_is_field_compatible(const struct Mem *mem, enum field_type type)
+{
+	if (mem->type == MEM_TYPE_UUID)
+		return (field_ext_type[type] & (1U << MP_UUID)) != 0;
+	enum mp_type mp_type = mem_mp_type(mem);
+	assert(mp_type != MP_EXT);
+	return field_mp_plain_type_is_compatible(type, mp_type, true);
+}
+
 const char *
 mem_str(const struct Mem *mem)
 {
@@ -81,6 +91,8 @@ mem_str(const struct Mem *mem)
 	case MEM_TYPE_MAP:
 	case MEM_TYPE_ARRAY:
 		return mp_str(mem->z);
+	case MEM_TYPE_UUID:
+		return tt_uuid_str(&mem->u.uuid);
 	case MEM_TYPE_BOOL:
 		return mem->u.b ? "TRUE" : "FALSE";
 	default:
@@ -190,6 +202,16 @@ mem_set_double(struct Mem *mem, double value)
 	mem->type = MEM_TYPE_DOUBLE;
 }
 
+void
+mem_set_uuid(struct Mem *mem, const struct tt_uuid *uuid)
+{
+	mem_clear(mem);
+	mem->field_type = FIELD_TYPE_UUID;
+	mem->u.uuid = *uuid;
+	mem->type = MEM_TYPE_UUID;
+	assert(mem->flags == 0);
+}
+
 static inline void
 set_str_const(struct Mem *mem, char *value, uint32_t len, int alloc_type)
 {
@@ -585,6 +607,18 @@ str_to_bin(struct Mem *mem)
 	return 0;
 }
 
+static inline int
+str_to_uuid(struct Mem *mem)
+{
+	assert(mem->type == MEM_TYPE_STR);
+	struct tt_uuid uuid;
+	if (mem->n != UUID_STR_LEN ||
+	    tt_uuid_from_string(tt_cstr(mem->z, mem->n), &uuid) != 0)
+		return -1;
+	mem_set_uuid(mem, &uuid);
+	return 0;
+}
+
 static inline int
 str_to_bool(struct Mem *mem)
 {
@@ -639,6 +673,19 @@ bin_to_str0(struct Mem *mem)
 	return 0;
 }
 
+static inline int
+bin_to_uuid(struct Mem *mem)
+{
+	assert(mem->type == MEM_TYPE_BIN);
+	if (ExpandBlob(mem) != 0)
+		return -1;
+	if (mem->n != UUID_LEN ||
+	    tt_uuid_validate((struct tt_uuid *)mem->z) != 0)
+		return -1;
+	mem_set_uuid(mem, (struct tt_uuid *)mem->z);
+	return 0;
+}
+
 static inline int
 bytes_to_int(struct Mem *mem)
 {
@@ -810,6 +857,22 @@ map_to_str0(struct Mem *mem)
 	return mem_copy_str0(mem, str);
 }
 
+static inline int
+uuid_to_str0(struct Mem *mem)
+{
+	assert(mem->type == MEM_TYPE_UUID);
+	char buf[UUID_STR_LEN + 1];
+	tt_uuid_to_string(&mem->u.uuid, &buf[0]);
+	return mem_copy_str0(mem, &buf[0]);
+}
+
+static inline int
+uuid_to_bin(struct Mem *mem)
+{
+	assert(mem->type == MEM_TYPE_UUID);
+	return mem_copy_bin(mem, (char *)&mem->u.uuid, UUID_LEN);
+}
+
 int
 mem_to_int(struct Mem *mem)
 {
@@ -889,6 +952,8 @@ mem_to_str0(struct Mem *mem)
 		return map_to_str0(mem);
 	case MEM_TYPE_ARRAY:
 		return array_to_str0(mem);
+	case MEM_TYPE_UUID:
+		return uuid_to_str0(mem);
 	default:
 		return -1;
 	}
@@ -914,6 +979,8 @@ mem_to_str(struct Mem *mem)
 		return map_to_str0(mem);
 	case MEM_TYPE_ARRAY:
 		return array_to_str0(mem);
+	case MEM_TYPE_UUID:
+		return uuid_to_str0(mem);
 	default:
 		return -1;
 	}
@@ -966,9 +1033,19 @@ mem_cast_explicit(struct Mem *mem, enum field_type type)
 			return str_to_bin(mem);
 		if (mem_is_bytes(mem))
 			return 0;
+		if (mem->type == MEM_TYPE_UUID)
+			return uuid_to_bin(mem);
 		return -1;
 	case FIELD_TYPE_NUMBER:
 		return mem_to_number(mem);
+	case FIELD_TYPE_UUID:
+		if (mem->type == MEM_TYPE_UUID)
+			return 0;
+		if (mem->type == MEM_TYPE_STR)
+			return str_to_uuid(mem);
+		if (mem->type == MEM_TYPE_BIN)
+			return bin_to_uuid(mem);
+		return -1;
 	case FIELD_TYPE_SCALAR:
 		if ((mem->type & (MEM_TYPE_MAP | MEM_TYPE_ARRAY)) != 0)
 			return -1;
@@ -996,6 +1073,8 @@ mem_cast_implicit(struct Mem *mem, enum field_type type)
 	case FIELD_TYPE_STRING:
 		if (mem->type == MEM_TYPE_STR)
 			return 0;
+		if (mem->type == MEM_TYPE_UUID)
+			return uuid_to_str0(mem);
 		return -1;
 	case FIELD_TYPE_DOUBLE:
 		if (mem->type == MEM_TYPE_DOUBLE)
@@ -1017,6 +1096,8 @@ mem_cast_implicit(struct Mem *mem, enum field_type type)
 		if ((mem->type & (MEM_TYPE_BIN | MEM_TYPE_MAP |
 				  MEM_TYPE_ARRAY)) != 0)
 			return 0;
+		if (mem->type == MEM_TYPE_UUID)
+			return uuid_to_bin(mem);
 		return -1;
 	case FIELD_TYPE_NUMBER:
 		if (mem_is_num(mem))
@@ -1034,6 +1115,14 @@ mem_cast_implicit(struct Mem *mem, enum field_type type)
 		if ((mem->type & (MEM_TYPE_MAP | MEM_TYPE_ARRAY)) != 0)
 			return -1;
 		return 0;
+	case FIELD_TYPE_UUID:
+		if (mem->type == MEM_TYPE_UUID)
+			return 0;
+		if (mem->type == MEM_TYPE_STR)
+			return str_to_uuid(mem);
+		if (mem->type == MEM_TYPE_BIN)
+			return bin_to_uuid(mem);
+		return -1;
 	case FIELD_TYPE_ANY:
 		return 0;
 	default:
@@ -1063,6 +1152,8 @@ mem_cast_implicit_old(struct Mem *mem, enum field_type type)
 			return int_to_str0(mem);
 		if (mem->type == MEM_TYPE_DOUBLE)
 			return double_to_str0(mem);
+		if (mem->type == MEM_TYPE_UUID)
+			return uuid_to_str0(mem);
 		return -1;
 	case FIELD_TYPE_DOUBLE:
 		if (mem->type == MEM_TYPE_DOUBLE)
@@ -1087,6 +1178,8 @@ mem_cast_implicit_old(struct Mem *mem, enum field_type type)
 	case FIELD_TYPE_VARBINARY:
 		if (mem->type == MEM_TYPE_BIN)
 			return 0;
+		if (mem->type == MEM_TYPE_UUID)
+			return uuid_to_bin(mem);
 		return -1;
 	case FIELD_TYPE_NUMBER:
 		if (mem_is_num(mem))
@@ -1106,6 +1199,14 @@ mem_cast_implicit_old(struct Mem *mem, enum field_type type)
 		if ((mem->type & (MEM_TYPE_MAP | MEM_TYPE_ARRAY)) != 0)
 			return -1;
 		return 0;
+	case FIELD_TYPE_UUID:
+		if (mem->type == MEM_TYPE_UUID)
+			return 0;
+		if (mem->type == MEM_TYPE_STR)
+			return str_to_uuid(mem);
+		if (mem->type == MEM_TYPE_BIN)
+			return bin_to_uuid(mem);
+		return -1;
 	default:
 		break;
 	}
@@ -1899,6 +2000,15 @@ mem_cmp_str(const struct Mem *left, const struct Mem *right, int *result,
 	return 0;
 }
 
+int
+mem_cmp_uuid(const struct Mem *a, const struct Mem *b, int *result)
+{
+	if ((a->type & b->type & MEM_TYPE_UUID) == 0)
+		return -1;
+	*result = memcmp(&a->u.uuid, &b->u.uuid, UUID_LEN);
+	return 0;
+}
+
 /*
  * Both *pMem1 and *pMem2 contain string values. Compare the two values
  * using the collation sequence pColl. As usual, return a negative , zero
@@ -1951,13 +2061,15 @@ mem_type_to_str(const struct Mem *p)
 		return "varbinary";
 	case MEM_TYPE_BOOL:
 		return "boolean";
+	case MEM_TYPE_UUID:
+		return "uuid";
 	default:
 		unreachable();
 	}
 }
 
 enum mp_type
-mem_mp_type(struct Mem *mem)
+mem_mp_type(const struct Mem *mem)
 {
 	assert(mem->type < MEM_TYPE_INVALID);
 	switch (mem->type) {
@@ -1979,6 +2091,8 @@ mem_mp_type(struct Mem *mem)
 		return MP_BOOL;
 	case MEM_TYPE_DOUBLE:
 		return MP_DOUBLE;
+	case MEM_TYPE_UUID:
+		return MP_EXT;
 	default:
 		unreachable();
 	}
@@ -2144,6 +2258,9 @@ memTracePrint(Mem *p)
 	case MEM_TYPE_BOOL:
 		printf(" bool:%s", SQL_TOKEN_BOOLEAN(p->u.b));
 		return;
+	case MEM_TYPE_UUID:
+		printf(" uuid:%s", tt_uuid_str(&p->u.uuid));
+		return;
 	default: {
 		char zBuf[200];
 		sqlVdbeMemPrettyPrint(p, zBuf);
@@ -2360,6 +2477,14 @@ sqlMemCompare(const Mem * pMem1, const Mem * pMem2, const struct coll * pColl)
 		return -1;
 	}
 
+	if (((type1 | type2) & MEM_TYPE_UUID) != 0) {
+		if (mem_cmp_uuid(pMem1, pMem2, &res) == 0)
+			return res;
+		if (type1 != MEM_TYPE_UUID)
+			return +1;
+		return -1;
+	}
+
 	/* At least one of the two values is a number
 	 */
 	if (((type1 | type2) &
@@ -2565,13 +2690,30 @@ sqlVdbeCompareMsgpack(const char **key1,
 			break;
 		}
 	case MP_ARRAY:
-	case MP_MAP:
-	case MP_EXT:{
+	case MP_MAP: {
 			mem1.z = (char *)aKey1;
 			mp_next(&aKey1);
 			mem1.n = aKey1 - (char *)mem1.z;
 			goto do_blob;
 		}
+	case MP_EXT: {
+		int8_t type;
+		const char *buf = aKey1;
+		uint32_t len = mp_decode_extl(&aKey1, &type);
+		if (type == MP_UUID) {
+			assert(len == UUID_LEN);
+			mem1.type = MEM_TYPE_UUID;
+			aKey1 = buf;
+			if (mp_decode_uuid(&aKey1, &mem1.u.uuid) == NULL ||
+			    mem_cmp_uuid(&mem1, pKey2, &rc) != 0)
+				rc = 1;
+			break;
+		}
+		aKey1 += len;
+		mem1.z = (char *)buf;
+		mem1.n = aKey1 - buf;
+		goto do_blob;
+	}
 	}
 	*key1 = aKey1;
 	return rc;
@@ -2625,9 +2767,25 @@ mem_from_mp_ephemeral(struct Mem *mem, const char *buf, uint32_t *len)
 		break;
 	}
 	case MP_EXT: {
-		mem->z = (char *)buf;
-		mp_next(&buf);
-		mem->n = buf - mem->z;
+		int8_t type;
+		const char *svp = buf;
+		uint32_t size = mp_decode_extl(&buf, &type);
+		if (type == MP_UUID) {
+			assert(size == UUID_LEN);
+			buf = svp;
+			if (mp_decode_uuid(&buf, &mem->u.uuid) == NULL) {
+				diag_set(ClientError, ER_INVALID_MSGPACK,
+					 "Invalid MP_UUID MsgPack format");
+				return -1;
+			}
+			mem->type = MEM_TYPE_UUID;
+			mem->flags = 0;
+			mem->field_type = FIELD_TYPE_UUID;
+			break;
+		}
+		buf += size;
+		mem->z = (char *)svp;
+		mem->n = buf - svp;
 		mem->type = MEM_TYPE_BIN;
 		mem->flags = MEM_Ephem;
 		mem->field_type = FIELD_TYPE_VARBINARY;
@@ -2764,6 +2922,9 @@ mpstream_encode_vdbe_mem(struct mpstream *stream, struct Mem *var)
 	case MEM_TYPE_BOOL:
 		mpstream_encode_bool(stream, var->u.b);
 		return;
+	case MEM_TYPE_UUID:
+		mpstream_encode_uuid(stream, &var->u.uuid);
+		return;
 	default:
 		unreachable();
 	}
@@ -2850,6 +3011,9 @@ port_vdbemem_dump_lua(struct port *base, struct lua_State *L, bool is_flat)
 		case MEM_TYPE_BOOL:
 			lua_pushboolean(L, mem->u.b);
 			break;
+		case MEM_TYPE_UUID:
+			*luaL_pushuuid(L) = mem->u.uuid;
+			break;
 		default:
 			unreachable();
 		}
@@ -2976,14 +3140,8 @@ port_lua_get_vdbemem(struct port *base, uint32_t *size)
 			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);
+				mem_set_uuid(&val[i], field.uuidval);
+				break;
 			} else {
 				size = mp_sizeof_decimal(field.decval);
 				buf = region_alloc(&fiber()->gc, size);
@@ -3087,7 +3245,22 @@ port_c_get_vdbemem(struct port *base, uint32_t *size)
 			break;
 		case MP_EXT:
 			str = data;
-			mp_next(&data);
+			int8_t type;
+			len = mp_decode_extl(&data, &type);
+			if (type == MP_UUID) {
+				assert(len == UUID_LEN);
+				struct tt_uuid *uuid = &val[i].u.uuid;
+				data = str;
+				if (mp_decode_uuid(&data, uuid) == NULL) {
+					diag_set(ClientError,
+						 ER_INVALID_MSGPACK, "Invalid "
+						 "MP_UUID MsgPack format");
+					goto error;
+				}
+				val[i].type = MEM_TYPE_UUID;
+				break;
+			}
+			data += len;
 			if (mem_copy_bin(&val[i], str, data - str) != 0)
 				goto error;
 			break;
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index 15d97da0e..b3cd5c545 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -30,6 +30,7 @@
  * SUCH DAMAGE.
  */
 #include "box/field_def.h"
+#include "uuid/tt_uuid.h"
 
 struct sql;
 struct Vdbe;
@@ -47,10 +48,11 @@ enum mem_type {
 	MEM_TYPE_MAP		= 1 << 6,
 	MEM_TYPE_BOOL		= 1 << 7,
 	MEM_TYPE_DOUBLE		= 1 << 8,
-	MEM_TYPE_INVALID	= 1 << 9,
-	MEM_TYPE_FRAME		= 1 << 10,
-	MEM_TYPE_PTR		= 1 << 11,
-	MEM_TYPE_AGG		= 1 << 12,
+	MEM_TYPE_UUID		= 1 << 9,
+	MEM_TYPE_INVALID	= 1 << 10,
+	MEM_TYPE_FRAME		= 1 << 11,
+	MEM_TYPE_PTR		= 1 << 12,
+	MEM_TYPE_AGG		= 1 << 13,
 };
 
 /*
@@ -72,6 +74,7 @@ struct Mem {
 		 */
 		struct func *func;
 		struct VdbeFrame *pFrame;	/* Used when flags==MEM_Frame */
+		struct tt_uuid uuid;
 	} u;
 	/** Type of the value this MEM contains. */
 	enum mem_type type;
@@ -255,6 +258,10 @@ mem_is_any_null(const struct Mem *mem1, const struct Mem *mem2)
 	return ((mem1->type| mem2->type) & MEM_TYPE_NULL) != 0;
 }
 
+/** Check if MEM is compatible with field type. */
+bool
+mem_is_field_compatible(const struct Mem *mem, enum field_type type);
+
 /**
  * Return a string that represent content of MEM. String is either allocated
  * using static_alloc() of just a static variable.
@@ -290,6 +297,10 @@ mem_set_bool(struct Mem *mem, bool value);
 void
 mem_set_double(struct Mem *mem, double value);
 
+/** Clear MEM and set it to UUID. */
+void
+mem_set_uuid(struct Mem *mem, const struct tt_uuid *uuid);
+
 /** Clear MEM and set it to STRING. The string belongs to another object. */
 void
 mem_set_str_ephemeral(struct Mem *mem, char *value, uint32_t len);
@@ -677,6 +688,14 @@ mem_cmp_str(const struct Mem *left, const struct Mem *right, int *result,
 int
 mem_cmp_num(const struct Mem *a, const struct Mem *b, int *result);
 
+/**
+ * Compare two MEMs and return the result of comparison. MEMs should be of
+ * UUID type or their values are converted to UUID according to
+ * implicit cast rules. Original MEMs are not changed.
+ */
+int
+mem_cmp_uuid(const struct Mem *left, const struct Mem *right, int *result);
+
 /**
  * Convert the given MEM to INTEGER. This function and the function below define
  * the rules that are used to convert values of all other types to INTEGER. In
@@ -898,7 +917,7 @@ mem_type_to_str(const struct Mem *p);
  * transparent memory cell.
  */
 enum mp_type
-mem_mp_type(struct Mem *mem);
+mem_mp_type(const struct Mem *mem);
 
 enum mp_type
 sql_value_type(struct Mem *);
diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y
index abc363951..4c9cf475e 100644
--- a/src/box/sql/parse.y
+++ b/src/box/sql/parse.y
@@ -1834,6 +1834,7 @@ typedef(A) ::= SCALAR . { A.type = FIELD_TYPE_SCALAR; }
 typedef(A) ::= BOOL . { A.type = FIELD_TYPE_BOOLEAN; }
 typedef(A) ::= BOOLEAN . { A.type = FIELD_TYPE_BOOLEAN; }
 typedef(A) ::= VARBINARY . { A.type = FIELD_TYPE_VARBINARY; }
+typedef(A) ::= UUID . { A.type = FIELD_TYPE_UUID; }
 
 /**
  * Time-like types are temporary disabled, until they are
diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index 12ec703a2..32d02d96e 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -1322,10 +1322,10 @@ case OP_FunctionByName: {
 	region_truncate(region, region_svp);
 	if (mem == NULL)
 		goto abort_due_to_error;
-	enum mp_type type = sql_value_type((sql_value *)pOut);
-	if (!field_mp_plain_type_is_compatible(returns, type, true)) {
+	if (!mem_is_field_compatible(pOut, returns)) {
 		diag_set(ClientError, ER_FUNC_INVALID_RETURN_TYPE, pOp->p4.z,
-			 field_type_strs[returns], mp_type_strs[type]);
+			 field_type_strs[returns],
+			 mp_type_strs[mem_mp_type(pOut)]);
 		goto abort_due_to_error;
 	}
 
@@ -1634,6 +1634,15 @@ case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
 				 "boolean");
 			goto abort_due_to_error;
 		}
+	} else if (((pIn3->type | pIn1->type) & MEM_TYPE_UUID) != 0) {
+		if (mem_cmp_uuid(pIn3, pIn1, &res) != 0) {
+			char *str = pIn3->type != MEM_TYPE_UUID ?
+				    mem_type_to_str(pIn3) :
+				    mem_type_to_str(pIn1);
+			diag_set(ClientError, ER_SQL_TYPE_MISMATCH, str,
+				 "uuid");
+			goto abort_due_to_error;
+		}
 	} else if (mem_is_bin(pIn3) || mem_is_bin(pIn1)) {
 		if (mem_cmp_bin(pIn3, pIn1, &res) != 0) {
 			char *str = !mem_is_bin(pIn3) ?
diff --git a/test/sql-tap/CMakeLists.txt b/test/sql-tap/CMakeLists.txt
index bf0c3a11d..bd2b9f33f 100644
--- a/test/sql-tap/CMakeLists.txt
+++ b/test/sql-tap/CMakeLists.txt
@@ -1,3 +1,4 @@
 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)
+build_module(sql_uuid sql_uuid.c)
diff --git a/test/sql-tap/gh-5913-segfault-on-select-uuid.test.lua b/test/sql-tap/gh-5913-segfault-on-select-uuid.test.lua
index 60978c2b5..7dcebe5d3 100755
--- a/test/sql-tap/gh-5913-segfault-on-select-uuid.test.lua
+++ b/test/sql-tap/gh-5913-segfault-on-select-uuid.test.lua
@@ -1,6 +1,6 @@
 #!/usr/bin/env tarantool
 local test = require("sqltester")
-test:plan(6)
+test:plan(4)
 
 local uuid = require("uuid").fromstr("11111111-1111-1111-1111-111111111111")
 local decimal = require("decimal").new(111.111)
@@ -13,16 +13,16 @@ box.space.T:insert({1, uuid, decimal})
 
 --
 -- Make sure that there is no segmentation fault on select from field that
--- contains UUID or DECIMAL. Currently SQL does not support UUID and DECIMAL,
--- so they treated as VARBINARY.
+-- contains UUID or DECIMAL. Currently SQL does not support DECIMAL, so it is
+-- treated as VARBINARY.
 --
 test:do_execsql_test(
     "gh-5913-1",
     [[
         SELECT i, u, d FROM t;
-        SELECT i from t;
+        SELECT i, u from t;
     ]], {
-        1
+        1, uuid
     })
 
 box.schema.create_space('T1')
@@ -32,19 +32,11 @@ box.space.T1:format({{name = "I", type = "integer"},
 box.space.T1:create_index("primary")
 
 --
--- Since SQL does not support UUID and DECIMAL and they treated as VARBINARY,
--- they cannot be inserted from SQL.
+-- Since SQL does not support DECIMAL and it is treated as VARBINARY, it cannot
+-- be inserted from SQL.
 --
 test:do_catchsql_test(
     "gh-5913-2",
-    [[
-        INSERT INTO t1 SELECT i, u, NULL FROM t;
-    ]], {
-        1, "Type mismatch: can not convert varbinary to uuid"
-    })
-
-test:do_catchsql_test(
-    "gh-5913-3",
     [[
         INSERT INTO t1 SELECT i, NULL, d FROM t;
     ]], {
@@ -52,11 +44,11 @@ test:do_catchsql_test(
     })
 
 --
--- Still, if UUID or DECIMAL fields does not selected directly, insert is
--- working properly.
+-- Still, if DECIMAL fields does not selected directly, insert is working
+-- properly in case the space which receives these values is empty.
 --
 test:do_execsql_test(
-    "gh-5913-4",
+    "gh-5913-3",
     [[
         INSERT INTO t1 SELECT * FROM t;
         SELECT count() FROM t1;
@@ -77,19 +69,11 @@ box.space.TD:create_index("primary")
 box.space.TD:insert({1, decimal})
 
 --
--- Update of UUID or VARBINARY also does not lead to segfault, however throws an
--- error since after changing value cannot be inserted into the field from SQL.
+-- Update of DECIMAL also does not lead to segfault, however throws an error
+-- since after changing value cannot be inserted into the field from SQL.
 --
 test:do_catchsql_test(
-    "gh-5913-5",
-    [[
-        UPDATE tu SET u = u;
-    ]], {
-        1, "Type mismatch: can not convert varbinary to uuid"
-    })
-
-test:do_catchsql_test(
-    "gh-5913-6",
+    "gh-5913-4",
     [[
         UPDATE td SET d = d;
     ]], {
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 ee3838fa9..79464afd1 100755
--- a/test/sql-tap/gh-6024-funcs-return-bin.test.lua
+++ b/test/sql-tap/gh-6024-funcs-return-bin.test.lua
@@ -23,7 +23,7 @@ test:do_execsql_test(
 box.schema.func.create("gh-6024-funcs-return-bin.ret_uuid", {
     language = "C",
     param_list = {},
-    returns = "varbinary",
+    returns = "uuid",
     exports = {"SQL"},
 })
 
@@ -32,7 +32,7 @@ test:do_execsql_test(
     [[
         SELECT typeof("gh-6024-funcs-return-bin.ret_uuid"());
     ]], {
-        "varbinary"
+        "uuid"
     })
 
 box.schema.func.create("gh-6024-funcs-return-bin.ret_decimal", {
@@ -53,7 +53,7 @@ test:do_execsql_test(
 box.schema.func.create("get_uuid", {
     language = "LUA",
     param_list = {},
-    returns = "varbinary",
+    returns = "uuid",
     body = "function(x) return require('uuid').fromstr('11111111-1111-1111-1111-111111111111') end",
     exports = {"SQL"},
 })
@@ -63,7 +63,7 @@ test:do_execsql_test(
     [[
         SELECT typeof("get_uuid"()), "get_uuid"() == "gh-6024-funcs-return-bin.ret_uuid"();
     ]], {
-        "varbinary", true
+        "uuid", true
     })
 
 box.schema.func.create("get_decimal", {
diff --git a/test/sql-tap/sql_uuid.c b/test/sql-tap/sql_uuid.c
new file mode 100644
index 000000000..4032bba5e
--- /dev/null
+++ b/test/sql-tap/sql_uuid.c
@@ -0,0 +1,46 @@
+#include "msgpuck.h"
+#include "module.h"
+#include "uuid/mp_uuid.h"
+#include "mp_extension_types.h"
+
+enum {
+	BUF_SIZE = 512,
+};
+
+int
+is_uuid(box_function_ctx_t *ctx, const char *args, const char *args_end)
+{
+	(void)args_end;
+	uint32_t arg_count = mp_decode_array(&args);
+	if (arg_count != 1) {
+		return box_error_set(__FILE__, __LINE__, ER_PROC_C,
+				     "invalid argument count");
+	}
+	bool is_uuid;
+	if (mp_typeof(*args) == MP_EXT) {
+		const char *str = args;
+		int8_t type;
+		mp_decode_extl(&str, &type);
+		is_uuid = type == MP_UUID;
+	} else {
+		is_uuid = false;
+	}
+
+	char res[BUF_SIZE];
+	char *end = mp_encode_bool(res, is_uuid);
+	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;
+}
diff --git a/test/sql-tap/uuid.test.lua b/test/sql-tap/uuid.test.lua
new file mode 100755
index 000000000..4287f0921
--- /dev/null
+++ b/test/sql-tap/uuid.test.lua
@@ -0,0 +1,1294 @@
+#!/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(139)
+
+local uuid = require("uuid")
+local uuid1 = uuid.fromstr("11111111-1111-1111-1111-111111111111")
+local uuid2 = uuid.fromstr("22222222-1111-1111-1111-111111111111")
+local uuid3 = uuid.fromstr("11111111-3333-1111-1111-111111111111")
+
+-- Check that it is possible to create spaces with UUID field.
+test:do_execsql_test(
+    "uuid-1",
+    [[
+        CREATE TABLE t1 (i INT PRIMARY KEY, u UUID);
+        CREATE TABLE t2 (u UUID PRIMARY KEY);
+    ]], {
+    })
+
+box.space.T1:insert({1, uuid1})
+box.space.T1:insert({2, uuid2})
+box.space.T1:insert({3, uuid3})
+box.space.T1:insert({4, uuid1})
+box.space.T1:insert({5, uuid1})
+box.space.T1:insert({6, uuid2})
+box.space.T2:insert({uuid1})
+box.space.T2:insert({uuid2})
+box.space.T2:insert({uuid3})
+
+-- Check that SELECT can work with UUID.
+test:do_execsql_test(
+    "uuid-2.1.1",
+    [[
+        SELECT * FROM t1;
+    ]], {
+        1, uuid1, 2, uuid2, 3, uuid3, 4, uuid1, 5, uuid1, 6, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-2.1.2",
+    [[
+        SELECT * FROM t2;
+    ]], {
+        uuid1, uuid3, uuid2
+    })
+
+-- Check that ORDER BY can work with UUID.
+test:do_execsql_test(
+    "uuid-2.2.1",
+    [[
+        SELECT * FROM t1 ORDER BY u;
+    ]], {
+        1, uuid1, 4, uuid1, 5, uuid1, 3, uuid3, 2, uuid2, 6, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-2.2.2",
+    [[
+        SELECT * FROM t1 ORDER BY u DESC;
+    ]], {
+        2, uuid2, 6, uuid2, 3, uuid3, 1, uuid1, 4, uuid1, 5, uuid1
+    })
+
+test:do_execsql_test(
+    "uuid-2.2.3",
+    [[
+        SELECT * FROM t2 ORDER BY u;
+    ]], {
+        uuid1, uuid3, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-2.2.4",
+    [[
+        SELECT * FROM t2 ORDER BY u DESC;
+    ]], {
+        uuid2, uuid3, uuid1
+    })
+
+-- Check that GROUP BY can work with UUID.
+test:do_execsql_test(
+    "uuid-2.3.1",
+    [[
+        SELECT count(*), u FROM t1 GROUP BY u;
+    ]], {
+        3, uuid1, 1, uuid3, 2, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-2.3.2",
+    [[
+        SELECT count(*), u FROM t2 GROUP BY u;
+    ]], {
+        1, uuid1, 1, uuid3, 1, uuid2
+    })
+
+-- Check that subselects can work with UUID.
+test:do_execsql_test(
+    "uuid-2.4",
+    [[
+        SELECT * FROM (SELECT * FROM (SELECT * FROM t2 LIMIT 2) LIMIT 2 OFFSET 1);
+    ]], {
+        uuid3
+    })
+
+-- Check that DISTINCT can work with UUID.
+test:do_execsql_test(
+    "uuid-2.5",
+    [[
+        SELECT DISTINCT u FROM t1;
+    ]], {
+        uuid1, uuid2, uuid3
+    })
+
+-- Check that VIEW can work with UUID.
+test:do_execsql_test(
+    "uuid-2.6",
+    [[
+        CREATE VIEW v AS SELECT u FROM t1;
+        SELECT * FROM v;
+    ]], {
+        uuid1, uuid2, uuid3, uuid1, uuid1, uuid2
+    })
+
+-- Check that LIMIT does not accept UUID as argument.
+test:do_catchsql_test(
+    "uuid-3.1",
+    [[
+        SELECT 1 LIMIT (SELECT u FROM t1 LIMIT 1);
+    ]], {
+        1, "Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"
+    })
+
+-- Check that OFFSET does not accept UUID as argument.
+test:do_catchsql_test(
+    "uuid-3.2",
+    [[
+        SELECT 1 LIMIT 1 OFFSET (SELECT u FROM t1 LIMIT 1);
+    ]], {
+        1, "Failed to execute SQL statement: Only positive integers are allowed in the OFFSET clause"
+    })
+
+-- Check that ephemeral space can work with UUID.
+test:do_execsql_test(
+    "uuid-4",
+    [[
+        EXPLAIN SELECT * from (VALUES(1)), t2;
+    ]], {
+        "/OpenTEphemeral/"
+    })
+
+test:execsql([[
+    CREATE TABLE t5f (u UUID PRIMARY KEY, f UUID REFERENCES t5f(u));
+    CREATE TABLE t5c (i INT PRIMARY KEY, f UUID, CONSTRAINT ck CHECK(CAST(f AS STRING) != '11111111-1111-1111-1111-111111111111'));
+    CREATE TABLE t5u (i INT PRIMARY KEY, f UUID UNIQUE);
+]])
+
+-- Check that FOREIGN KEY constraint can work with UUID.
+test:do_catchsql_test(
+    "uuid-5.1.1",
+    [[
+        INSERT INTO t5f SELECT (SELECT u from t2 LIMIT 1 OFFSET 1), (SELECT u from t2 LIMIT 1);
+    ]], {
+        1, "Failed to execute SQL statement: FOREIGN KEY constraint failed"
+    })
+
+test:do_execsql_test(
+    "uuid-5.1.2",
+    [[
+        INSERT INTO t5f SELECT u, u from t2 LIMIT 1;
+        SELECT * from t5f;
+    ]], {
+        uuid1, uuid1
+    })
+
+test:do_execsql_test(
+    "uuid-5.1.3",
+    [[
+        INSERT INTO t5f SELECT (SELECT u from t2 LIMIT 1 OFFSET 1), (SELECT u from t2 LIMIT 1);
+        SELECT * from t5f;
+    ]], {
+        uuid1, uuid1, uuid3, uuid1
+    })
+
+-- Check that CHECK constraint can work with UUID.
+test:do_catchsql_test(
+    "uuid-5.2.1",
+    [[
+        INSERT INTO t5c SELECT 1, u FROM t2 LIMIT 1;
+    ]], {
+        1, "Check constraint failed 'CK': CAST(f AS STRING) != '11111111-1111-1111-1111-111111111111'"
+    })
+
+test:do_execsql_test(
+    "uuid-5.2.2",
+    [[
+        INSERT INTO t5c SELECT 2, u FROM t2 LIMIT 1 OFFSET 1;
+        SELECT * from t5c;
+    ]], {
+        2, uuid3
+    })
+
+-- Check that UNIQUE constraint can work with UUID.
+test:do_execsql_test(
+    "uuid-5.3.1",
+    [[
+        INSERT INTO t5u SELECT 1, u FROM t2 LIMIT 1;
+        SELECT * from t5u;
+    ]], {
+        1, uuid1
+    })
+
+test:do_catchsql_test(
+    "uuid-5.3.2",
+    [[
+        INSERT INTO t5u SELECT 2, u FROM t2 LIMIT 1;
+    ]], {
+        1, 'Duplicate key exists in unique index "unique_unnamed_T5U_2" '..
+        'in space "T5U" with old tuple - '..
+        '[1, 11111111-1111-1111-1111-111111111111] and new tuple - '..
+        '[2, 11111111-1111-1111-1111-111111111111]'
+    })
+
+-- Check that built-in functions work with UUIDs as intended.
+test:do_catchsql_test(
+    "uuid-6.1.1",
+    [[
+        SELECT ABS(u) from t2;
+    ]], {
+        1, "Inconsistent types: expected number got uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-6.1.2",
+    [[
+        SELECT AVG(u) from t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to number"
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.3",
+    [[
+        SELECT CHAR(u) from t2;
+    ]], {
+        "\0", "\0", "\0"
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.4",
+    [[
+        SELECT CHARACTER_LENGTH(u) from t2;
+    ]], {
+        36, 36, 36
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.5",
+    [[
+        SELECT CHAR_LENGTH(u) from t2;
+    ]], {
+        36, 36, 36
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.6",
+    [[
+        SELECT COALESCE(NULL, u, NULL, NULL) from t2;
+    ]], {
+        uuid1, uuid3, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.7",
+    [[
+        SELECT COUNT(u) from t2;
+    ]], {
+        3
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.8",
+    [[
+        SELECT GREATEST((SELECT u FROM t2 LIMIT 1), (SELECT u FROM t2 LIMIT 1 OFFSET 1));
+    ]], {
+        uuid3
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.9",
+    [[
+        SELECT GROUP_CONCAT(u) from t2;
+    ]], {
+        "11111111-1111-1111-1111-111111111111,"..
+        "11111111-3333-1111-1111-111111111111,"..
+        "22222222-1111-1111-1111-111111111111"
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.10",
+    [[
+        SELECT HEX(u) from t2;
+    ]], {
+        "11111111111111111111111111111111",
+        "11111111333311111111111111111111",
+        "22222222111111111111111111111111"
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.11",
+    [[
+        SELECT IFNULL(u, NULL) from t2;
+    ]], {
+        uuid1, uuid3, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.12",
+    [[
+        SELECT LEAST((SELECT u FROM t2 LIMIT 1), (SELECT u FROM t2 LIMIT 1 OFFSET 1));
+    ]], {
+        uuid1
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.13",
+    [[
+        SELECT LENGTH(u) from t2;
+    ]], {
+        36, 36, 36
+    })
+
+test:do_catchsql_test(
+    "uuid-6.1.14",
+    [[
+        SELECT u LIKE 'a' from t2;
+    ]], {
+        1, "Inconsistent types: expected text got uuid"
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.15",
+    [[
+        SELECT LIKELIHOOD(u, 0.5) from t2;
+    ]], {
+        uuid1, uuid3, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.16",
+    [[
+        SELECT LIKELY(u) from t2;
+    ]], {
+        uuid1, uuid3, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.17",
+    [[
+        SELECT LOWER(u) from t2;
+    ]], {
+        "11111111-1111-1111-1111-111111111111",
+        "11111111-3333-1111-1111-111111111111",
+        "22222222-1111-1111-1111-111111111111"
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.18",
+    [[
+        SELECT MAX(u) from t2;
+    ]], {
+        uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.19",
+    [[
+        SELECT MIN(u) from t2;
+    ]], {
+        uuid1
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.20",
+    [[
+        SELECT NULLIF(u, 1) from t2;
+    ]], {
+        uuid1, uuid3, uuid2
+    })
+
+test:do_catchsql_test(
+    "uuid-6.1.21",
+    [[
+        SELECT POSITION(u, '1') from t2;
+    ]], {
+        1, "Inconsistent types: expected text or varbinary got uuid"
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.22",
+    [[
+        SELECT RANDOMBLOB(u) from t2;
+    ]], {
+        "", "", ""
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.23",
+    [[
+        SELECT REPLACE(u, '1', '2') from t2;
+    ]], {
+        "22222222-2222-2222-2222-222222222222",
+        "22222222-3333-2222-2222-222222222222",
+        "22222222-2222-2222-2222-222222222222"
+    })
+
+test:do_catchsql_test(
+    "uuid-6.1.24",
+    [[
+        SELECT ROUND(u) from t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to numeric"
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.25",
+    [[
+        SELECT SOUNDEX(u) from t2;
+    ]], {
+        "?000", "?000", "?000"
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.26",
+    [[
+        SELECT SUBSTR(u, 3, 3) from t2;
+    ]], {
+        "111", "111", "222"
+    })
+
+test:do_catchsql_test(
+    "uuid-6.1.27",
+    [[
+        SELECT SUM(u) from t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to number"
+    })
+
+test:do_catchsql_test(
+    "uuid-6.1.28",
+    [[
+        SELECT TOTAL(u) from t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to number"
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.29",
+    [[
+        SELECT TRIM(u) from t2;
+    ]], {
+        "11111111-1111-1111-1111-111111111111",
+        "11111111-3333-1111-1111-111111111111",
+        "22222222-1111-1111-1111-111111111111"
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.30",
+    [[
+        SELECT TYPEOF(u) from t2;
+    ]], {
+        "uuid", "uuid", "uuid"
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.31",
+    [[
+        SELECT UNICODE(u) from t2;
+    ]], {
+        49, 49, 50
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.32",
+    [[
+        SELECT UNLIKELY(u) from t2;
+    ]], {
+        uuid1, uuid3, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-6.1.33",
+    [[
+        SELECT UPPER(u) from t2;
+    ]], {
+        "11111111-1111-1111-1111-111111111111",
+        "11111111-3333-1111-1111-111111111111",
+        "22222222-1111-1111-1111-111111111111"
+    })
+
+test:do_catchsql_test(
+    "uuid-6.1.33",
+    [[
+        SELECT u || u from t2;
+    ]], {
+        1, "Inconsistent types: expected text or varbinary got uuid"
+    })
+
+local func = {language = 'Lua', body = 'function(x) return type(x) end',
+              returns = 'string', param_list = {'any'}, exports = {'SQL'}}
+box.schema.func.create('RETURN_TYPE', func);
+
+-- Check that Lua user-defined functions can accept UUID.
+test:do_execsql_test(
+    "uuid-6.2",
+    [[
+        SELECT RETURN_TYPE(u) FROM t2;
+    ]], {
+        "cdata", "cdata", "cdata"
+    })
+
+func = {language = 'Lua', returns = 'uuid', param_list = {}, exports = {'SQL'},
+        body = 'function(x) return require("uuid").fromstr("11111111-1111-1111-1111-111111111111") end'}
+box.schema.func.create('GET_UUID', func);
+
+-- Check that Lua user-defined functions can return UUID.
+test:do_execsql_test(
+    "uuid-6.3",
+    [[
+        SELECT GET_UUID();
+    ]], {
+        uuid1
+    })
+
+func = {language = 'C', returns = 'boolean', param_list = {'any'}, exports = {'SQL'}}
+box.schema.func.create("sql_uuid.is_uuid", func)
+
+-- Check that C user-defined functions can accept UUID.
+test:do_execsql_test(
+    "uuid-6.4",
+    [[
+        SELECT "sql_uuid.is_uuid"(i), "sql_uuid.is_uuid"(u) FROM t1 LIMIT 1;
+    ]], {
+        false, true
+    })
+
+func = {language = 'C', returns = 'uuid', param_list = {}, exports = {'SQL'}}
+box.schema.func.create("sql_uuid.ret_uuid", func)
+
+-- Check that C user-defined functions can return UUID.
+test:do_execsql_test(
+    "uuid-6.5",
+    [[
+        SELECT "sql_uuid.ret_uuid"();
+    ]], {
+        uuid1
+    })
+
+-- Check that explicit cast from UUID to another types works as intended.
+test:do_catchsql_test(
+    "uuid-7.1.1",
+    [[
+        SELECT cast(u AS UNSIGNED) FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to unsigned"
+    })
+
+test:do_execsql_test(
+    "uuid-7.1.2",
+    [[
+        SELECT cast(u AS STRING) FROM t2;
+    ]], {
+        "11111111-1111-1111-1111-111111111111",
+        "11111111-3333-1111-1111-111111111111",
+        "22222222-1111-1111-1111-111111111111"
+    })
+
+test:do_catchsql_test(
+    "uuid-7.1.3",
+    [[
+        SELECT cast(u AS NUMBER) FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to number"
+    })
+
+test:do_catchsql_test(
+    "uuid-7.1.4",
+    [[
+        SELECT cast(u AS DOUBLE) FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to double"
+    })
+
+test:do_catchsql_test(
+    "uuid-7.1.5",
+    [[
+        SELECT cast(u AS INTEGER) FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to integer"
+    })
+
+test:do_catchsql_test(
+    "uuid-7.1.6",
+    [[
+        SELECT cast(u AS BOOLEAN) FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to boolean"
+    })
+
+test:do_execsql_test(
+    "uuid-7.1.7",
+    [[
+        SELECT hex(cast(u AS VARBINARY)) FROM t2;
+    ]], {
+        "11111111111111111111111111111111",
+        "11111111333311111111111111111111",
+        "22222222111111111111111111111111"
+    })
+
+test:do_execsql_test(
+    "uuid-7.1.8",
+    [[
+        SELECT cast(u AS SCALAR) FROM t2;
+    ]], {
+        uuid1, uuid3, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-7.1.9",
+    [[
+        SELECT cast(u AS UUID) FROM t2;
+    ]], {
+        uuid1, uuid3, uuid2
+    })
+
+-- Check that explicit cast from another types to UUID works as intended.
+test:do_catchsql_test(
+    "uuid-7.2.1",
+    [[
+        SELECT cast(1 AS UUID);
+    ]], {
+        1, "Type mismatch: can not convert 1 to uuid"
+    })
+
+test:do_execsql_test(
+    "uuid-7.2.2",
+    [[
+        SELECT cast('11111111-1111-1111-1111-111111111111' AS UUID);
+    ]], {
+        uuid1
+    })
+
+test:do_catchsql_test(
+    "uuid-7.2.3",
+    [[
+        SELECT cast('1' AS UUID);
+    ]], {
+        1, "Type mismatch: can not convert 1 to uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-7.2.4",
+    [[
+        SELECT cast(1.5 AS UUID);
+    ]], {
+        1, "Type mismatch: can not convert 1.5 to uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-7.2.5",
+    [[
+        SELECT cast(-1 AS UUID);
+    ]], {
+        1, "Type mismatch: can not convert -1 to uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-7.2.6",
+    [[
+        SELECT cast(true AS UUID);
+    ]], {
+        1, "Type mismatch: can not convert TRUE to uuid"
+    })
+
+test:do_execsql_test(
+    "uuid-7.2.7",
+    [[
+        SELECT cast(x'11111111111111111111111111111111' AS UUID);
+    ]], {
+        uuid1
+    })
+
+test:do_catchsql_test(
+    "uuid-7.2.8",
+    [[
+        SELECT cast(randomblob(10) as UUID) FROM t2 LIMIT 1;
+    ]], {
+        1, "Type mismatch: can not convert varbinary to uuid"
+    })
+
+test:execsql([[
+    CREATE TABLE tu (id INT PRIMARY KEY AUTOINCREMENT, u UNSIGNED);
+    CREATE TABLE ts (id INT PRIMARY KEY AUTOINCREMENT, s STRING);
+    CREATE TABLE tn (id INT PRIMARY KEY AUTOINCREMENT, n NUMBER);
+    CREATE TABLE td (id INT PRIMARY KEY AUTOINCREMENT, d DOUBLE);
+    CREATE TABLE ti (id INT PRIMARY KEY AUTOINCREMENT, i INTEGER);
+    CREATE TABLE tb (id INT PRIMARY KEY AUTOINCREMENT, b BOOLEAN);
+    CREATE TABLE tv (id INT PRIMARY KEY AUTOINCREMENT, v VARBINARY);
+    CREATE TABLE tsc (id INT PRIMARY KEY AUTOINCREMENT, sc SCALAR);
+    CREATE TABLE tuu (id INT PRIMARY KEY AUTOINCREMENT, uu UUID);
+    CREATE TABLE tsu (s STRING PRIMARY KEY, u UUID);
+]])
+
+-- Check that implcit cast from UUID to another types works as intended.
+test:do_catchsql_test(
+    "uuid-8.1.1",
+    [[
+        INSERT INTO tu(u) SELECT u FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to unsigned"
+    })
+
+test:do_execsql_test(
+    "uuid-8.1.2",
+    [[
+        INSERT INTO ts(s) SELECT u FROM t2;
+        SELECT * FROM ts;
+    ]], {
+        1, "11111111-1111-1111-1111-111111111111",
+        2, "11111111-3333-1111-1111-111111111111",
+        3, "22222222-1111-1111-1111-111111111111"
+    })
+
+test:do_catchsql_test(
+    "uuid-8.1.3",
+    [[
+        INSERT INTO tn(n) SELECT u FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to number"
+    })
+
+test:do_catchsql_test(
+    "uuid-8.1.4",
+    [[
+        INSERT INTO td(d) SELECT u FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to double"
+    })
+
+test:do_catchsql_test(
+    "uuid-8.1.5",
+    [[
+        INSERT INTO ti(i) SELECT u FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to integer"
+    })
+
+test:do_catchsql_test(
+    "uuid-8.1.6",
+    [[
+        INSERT INTO tb(b) SELECT u FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to boolean"
+    })
+
+test:do_execsql_test(
+    "uuid-8.1.7",
+    [[
+        INSERT INTO tv(v) SELECT u FROM t2;
+        SELECT id, hex(v) FROM tv;
+    ]], {
+        1, "11111111111111111111111111111111",
+        2, "11111111333311111111111111111111",
+        3, "22222222111111111111111111111111"
+    })
+
+test:do_execsql_test(
+    "uuid-8.1.8",
+    [[
+        INSERT INTO tsc(sc) SELECT u FROM t2;
+        SELECT * FROM tsc;
+    ]], {
+        1, uuid1, 2, uuid3, 3, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-8.1.9",
+    [[
+        INSERT INTO tuu(uu) SELECT u FROM t2;
+        SELECT * FROM tuu;
+    ]], {
+        1, uuid1, 2, uuid3, 3, uuid2
+    })
+
+-- Check that implicit cast from another types to UUID works as intended.
+test:do_catchsql_test(
+    "uuid-8.2.1",
+    [[
+        INSERT INTO tsu VALUES ('1_unsigned', 1);
+    ]], {
+        1, "Type mismatch: can not convert 1 to uuid"
+    })
+
+test:do_execsql_test(
+    "uuid-8.2.2",
+    [[
+        INSERT INTO tsu VALUES ('2_string_right', '11111111-1111-1111-1111-111111111111');
+        SELECT * FROM tsu ORDER BY s DESC LIMIT 1;
+    ]], {
+        '2_string_right', uuid1
+    })
+
+test:do_catchsql_test(
+    "uuid-8.2.3",
+    [[
+        INSERT INTO tsu VALUES ('3_string_wrong', '1');
+    ]], {
+        1, "Type mismatch: can not convert 1 to uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-8.2.4",
+    [[
+        INSERT INTO tsu VALUES ('4_double', 1.5);
+    ]], {
+        1, "Type mismatch: can not convert 1.5 to uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-8.2.5",
+    [[
+        INSERT INTO tsu VALUES ('5_integer', -1);
+    ]], {
+        1, "Type mismatch: can not convert -1 to uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-8.2.6",
+    [[
+        INSERT INTO tsu VALUES ('6_boolean', true);
+    ]], {
+        1, "Type mismatch: can not convert TRUE to uuid"
+    })
+
+test:do_execsql_test(
+    "uuid-8.2.7",
+    [[
+        INSERT INTO tsu SELECT '7_varbinary', x'11111111111111111111111111111111' FROM t2 LIMIT 1;
+        SELECT * FROM tsu ORDER BY s DESC LIMIT 1;
+    ]], {
+        '7_varbinary', uuid1
+    })
+
+test:do_catchsql_test(
+    "uuid-8.2.8",
+    [[
+        INSERT INTO tsu VALUES ('8_varbinary', randomblob(10));
+    ]], {
+        1, "Type mismatch: can not convert varbinary to uuid"
+    })
+
+test:execsql([[
+    CREATE TABLE t9 (i INT PRIMARY KEY AUTOINCREMENT, u UUID);
+    CREATE TABLE t9t (u UUID PRIMARY KEY);
+    CREATE TRIGGER t AFTER INSERT ON t9 FOR EACH ROW BEGIN INSERT INTO t9t SELECT new.u; END;
+]])
+
+-- Check that trigger can work with UUID.
+test:do_execsql_test(
+    "uuid-9",
+    [[
+        INSERT INTO t9(u) SELECT * FROM t2;
+        SELECT * FROM t9t;
+    ]], {
+        uuid1, uuid3, uuid2
+    })
+
+test:execsql([[
+    CREATE TABLE t10 (i INT PRIMARY KEY AUTOINCREMENT, u UUID DEFAULT '11111111-1111-1111-1111-111111111111');
+]])
+
+-- Check that INSERT into UUID field works.
+test:do_execsql_test(
+    "uuid-10.1.1",
+    [[
+        INSERT INTO t10 VALUES (1, '22222222-1111-1111-1111-111111111111');
+        SELECT * FROM t10 WHERE i = 1;
+    ]], {
+        1, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-10.1.2",
+    [[
+        INSERT INTO t10 VALUES (2, x'22222222111111111111111111111111');
+        SELECT * FROM t10 WHERE i = 2;
+    ]], {
+        2, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-10.1.3",
+    [[
+        INSERT INTO t10(i) VALUES (3);
+        SELECT * FROM t10 WHERE i = 3;
+    ]], {
+        3, uuid1
+    })
+
+test:do_execsql_test(
+    "uuid-10.1.4",
+    [[
+        INSERT INTO t10 VALUES (4, NULL);
+        SELECT * FROM t10 WHERE i = 4;
+    ]], {
+        4, ''
+    })
+
+-- Check that UPDATE of UUID field works.
+test:do_execsql_test(
+    "uuid-10.2.1",
+    [[
+        UPDATE t10 SET u = '11111111-3333-1111-1111-111111111111' WHERE i = 1;
+        SELECT * FROM t10 WHERE i = 1;
+    ]], {
+        1, uuid3
+    })
+
+test:do_execsql_test(
+    "uuid-10.2.2",
+    [[
+        UPDATE t10 SET u = x'11111111333311111111111111111111' WHERE i = 2;
+        SELECT * FROM t10 WHERE i = 2;
+    ]], {
+        2, uuid3
+    })
+
+-- Check that JOIN by UUID field works.
+test:do_execsql_test(
+    "uuid-11.1",
+    [[
+        SELECT * FROM t1 JOIN t2 on t1.u = t2.u;
+    ]], {
+        1, uuid1, uuid1, 2, uuid2, uuid2, 3, uuid3, uuid3,
+        4, uuid1, uuid1, 5, uuid1, uuid1, 6, uuid2, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-11.2",
+    [[
+        SELECT * FROM t1 LEFT JOIN t2 on t1.u = t2.u;
+    ]], {
+        1, uuid1, uuid1, 2, uuid2, uuid2, 3, uuid3, uuid3,
+        4, uuid1, uuid1, 5, uuid1, uuid1, 6, uuid2, uuid2
+    })
+
+test:do_execsql_test(
+    "uuid-11.3",
+    [[
+        SELECT * FROM t1 INNER JOIN t2 on t1.u = t2.u;
+    ]], {
+        1, uuid1, uuid1, 2, uuid2, uuid2, 3, uuid3, uuid3,
+        4, uuid1, uuid1, 5, uuid1, uuid1, 6, uuid2, uuid2
+    })
+
+-- Check that arithmetic operations work with UUIDs as intended.
+test:do_catchsql_test(
+    "uuid-12.1.1",
+    [[
+        SELECT -u FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to numeric"
+    })
+
+test:do_catchsql_test(
+    "uuid-12.1.2",
+    [[
+        SELECT u + 1 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to numeric"
+    })
+
+test:do_catchsql_test(
+    "uuid-12.1.3",
+    [[
+        SELECT u - 1 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to numeric"
+    })
+
+test:do_catchsql_test(
+    "uuid-12.1.4",
+    [[
+        SELECT u * 1 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to numeric"
+    })
+
+test:do_catchsql_test(
+    "uuid-12.1.5",
+    [[
+        SELECT u / 1 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to numeric"
+    })
+
+test:do_catchsql_test(
+    "uuid-12.1.6",
+    [[
+        SELECT u % 1 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to numeric"
+    })
+
+-- Check that bitwise operations work with UUIDs as intended.
+test:do_catchsql_test(
+    "uuid-12.2.1",
+    [[
+        SELECT ~u FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to integer"
+    })
+
+test:do_catchsql_test(
+    "uuid-12.2.2",
+    [[
+        SELECT u >> 1 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to integer"
+    })
+
+test:do_catchsql_test(
+    "uuid-12.2.3",
+    [[
+        SELECT u << 1 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to integer"
+    })
+
+test:do_catchsql_test(
+    "uuid-12.2.4",
+    [[
+        SELECT u | 1 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to integer"
+    })
+
+test:do_catchsql_test(
+    "uuid-12.2.5",
+    [[
+        SELECT u & 1 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to integer"
+    })
+
+-- Check that logical operations work with UUIDs as intended.
+test:do_catchsql_test(
+    "uuid-12.3.1",
+    [[
+        SELECT NOT u FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to boolean"
+    })
+
+test:do_catchsql_test(
+    "uuid-12.3.2",
+    [[
+        SELECT u AND true FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to boolean"
+    })
+
+test:do_catchsql_test(
+    "uuid-12.3.3",
+    [[
+        SELECT u OR true FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to boolean"
+    })
+
+test:do_catchsql_test(
+    "uuid-12.3.4",
+    [[
+        SELECT true AND u FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to boolean"
+    })
+
+test:do_catchsql_test(
+    "uuid-12.3.5",
+    [[
+        SELECT true OR u FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert 11111111-1111-1111-1111-111111111111 to boolean"
+    })
+
+-- Check that comparison with UUID works as intended.
+test:do_catchsql_test(
+    "uuid-13.1.1",
+    [[
+        SELECT u > 1 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert unsigned to uuid"
+    })
+
+test:do_execsql_test(
+    "uuid-13.1.2",
+    [[
+        SELECT u > CAST('11111111-1111-1111-1111-111111111111' AS UUID) FROM t2;
+    ]], {
+        false, true, true
+    })
+
+test:do_catchsql_test(
+    "uuid-13.1.3",
+    [[
+        SELECT u > '1' FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert text to uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-13.1.4",
+    [[
+        SELECT u > 1.5 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert real to uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-13.1.5",
+    [[
+        SELECT u > -1 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert integer to uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-13.1.6",
+    [[
+        SELECT u > true FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert uuid to boolean"
+    })
+
+test:do_execsql_test(
+    "uuid-13.1.7",
+    [[
+        SELECT u > CAST(x'11111111111111111111111111111111' AS UUID) FROM t2;
+    ]], {
+        false, true, true
+    })
+
+test:do_catchsql_test(
+    "uuid-13.1.8",
+    [[
+        SELECT u > x'31' FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert varbinary to uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-13.2.1",
+    [[
+        SELECT u = 1 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert unsigned to uuid"
+    })
+
+test:do_execsql_test(
+    "uuid-13.2.2",
+    [[
+        SELECT u = CAST('11111111-1111-1111-1111-111111111111' AS UUID) FROM t2;
+    ]], {
+        true, false, false
+    })
+
+test:do_catchsql_test(
+    "uuid-13.2.3",
+    [[
+        SELECT u = '1' FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert text to uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-13.2.4",
+    [[
+        SELECT u = 1.5 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert real to uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-13.2.5",
+    [[
+        SELECT u = -1 FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert integer to uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-13.2.6",
+    [[
+        SELECT u = true FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert uuid to boolean"
+    })
+
+test:do_execsql_test(
+    "uuid-13.2.7",
+    [[
+        SELECT u = CAST(x'11111111111111111111111111111111' AS UUID) FROM t2;
+    ]], {
+        true, false, false
+    })
+
+test:do_catchsql_test(
+    "uuid-13.2.8",
+    [[
+        SELECT u = x'31' FROM t2;
+    ]], {
+        1, "Type mismatch: can not convert varbinary to uuid"
+    })
+
+test:execsql([[
+    CREATE TABLE t14 (s SCALAR PRIMARY KEY);
+]])
+
+-- Check that SCALAR field can contain UUID and use it in index.
+test:do_execsql_test(
+    "uuid-14",
+    [[
+        INSERT INTO t14 VALUES (1), (true), (1.5), (-1);
+        INSERT INTO t14 VALUES (x'11111111111111111111111111111111');
+        INSERT INTO t14 VALUES (CAST(x'11111111111111111111111111111111' AS UUID));
+        INSERT INTO t14 VALUES ('11111111-1111-1111-1111-111111111111');
+        SELECT typeof(s) FROM t14;
+    ]], {
+        "boolean", "integer", "integer", "double", "string", "varbinary", "uuid"
+    })
+
+local s = box.schema.space.create('T15', {format={{'I', 'integer'}, {'M', 'map'}, {'A', 'array'}}})
+s:create_index('i15')
+s:insert({1, {['1'] = 1, ['2'] = 2, ['3']= 3}, {1,2,3}})
+s:insert({2, {['1'] = 1, ['2'] = 2}, {1,2,3,4}})
+s:insert({3, {['1'] = 1}, {1,2,3,4,5}})
+
+-- Make sure that addition of UUID does not change behaviour of MAP and ARRAY.
+-- Currently it works wrong, but there should not segmentation faults.
+test:do_execsql_test(
+    "uuid-15.1",
+    [[
+        SELECT i FROM t15 ORDER BY m;
+    ]], {
+        3,2,1
+    })
+
+test:do_execsql_test(
+    "uuid-15.2",
+    [[
+        SELECT i FROM t15 ORDER BY a;
+    ]], {
+        3,2,1
+    })
+
+test:execsql([[
+    DROP TRIGGER t;
+    DROP VIEW v;
+    DROP TABLE t15;
+    DROP TABLE t14;
+    DROP TABLE t10;
+    DROP TABLE t9t;
+    DROP TABLE t9;
+    DROP TABLE tsu;
+    DROP TABLE tuu;
+    DROP TABLE tsc;
+    DROP TABLE tv;
+    DROP TABLE tb;
+    DROP TABLE ti;
+    DROP TABLE td;
+    DROP TABLE tn;
+    DROP TABLE ts;
+    DROP TABLE tu;
+    DROP TABLE t5u;
+    DROP TABLE t5c;
+    DROP TABLE t5f;
+    DROP TABLE t2;
+    DROP TABLE t1;
+]])
+
+test:finish_test()
-- 
2.25.1


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

* [Tarantool-patches] [PATCH v2 2/2] sql: introduce SQL built-in function UUID()
  2021-06-02  8:02 [Tarantool-patches] [PATCH v2 0/2] sql: introduce UUID Mergen Imeev via Tarantool-patches
  2021-06-02  8:02 ` [Tarantool-patches] [PATCH v2 1/2] sql: introduce UUID field type Mergen Imeev via Tarantool-patches
@ 2021-06-02  8:02 ` Mergen Imeev via Tarantool-patches
  2021-06-02 20:13   ` Timur Safin via Tarantool-patches
  2021-06-02 20:13 ` [Tarantool-patches] [PATCH v2 0/2] sql: introduce UUID Timur Safin via Tarantool-patches
  2 siblings, 1 reply; 8+ messages in thread
From: Mergen Imeev via Tarantool-patches @ 2021-06-02  8:02 UTC (permalink / raw)
  To: tsafin; +Cc: tarantool-patches

This patch introduces a new SQL built-in function UUID().

Closes #5886

@TarantoolBot document
Title: SQL built-in function UUID()

SQL built-in function UUID() takes zero or one argument. If no argument
is specified, a UUID v4 is generated. If the version of the UUID to
generate is specified as an argument, the function returns the new UUID
of the given version. Currently only version 4 of UUID is supported.
---
 .../unreleased/introduce-uuid-to-sql.md       |   3 +
 src/box/bootstrap.snap                        | Bin 5991 -> 6016 bytes
 src/box/lua/upgrade.lua                       |  19 +++++++
 src/box/sql/func.c                            |  39 +++++++++++++
 src/box/sql/parse.y                           |   2 +-
 test/box-py/bootstrap.result                  |   3 +-
 test/box/access.result                        |   2 +-
 test/box/access.test.lua                      |   2 +-
 test/box/access_bin.result                    |   2 +-
 test/box/access_bin.test.lua                  |   2 +-
 test/box/access_sysview.result                |   8 +--
 test/box/function1.result                     |   6 +-
 test/sql-tap/uuid.test.lua                    |  52 +++++++++++++++++-
 test/wal_off/func_max.result                  |   8 +--
 14 files changed, 130 insertions(+), 18 deletions(-)
 create mode 100644 changelogs/unreleased/introduce-uuid-to-sql.md

diff --git a/changelogs/unreleased/introduce-uuid-to-sql.md b/changelogs/unreleased/introduce-uuid-to-sql.md
new file mode 100644
index 000000000..642ac94fb
--- /dev/null
+++ b/changelogs/unreleased/introduce-uuid-to-sql.md
@@ -0,0 +1,3 @@
+## feature/core
+
+ * Field type UUID is now available in SQL, and new UUID can be generated using new SQL built-in function uuid() (gh-5886).
diff --git a/src/box/bootstrap.snap b/src/box/bootstrap.snap
index c4a70297aad138d426a24ee4447af485e3597536..57374decc0f9de140772d9809a227e3ba4ce61eb 100644
GIT binary patch
delta 5952
zcmbtzRag@a+%*kSqf?OX7`4$k7$qej1F4NJadeJmgfL3!Zj^3N1O_NL5L7_AQ$R$R
zLA?L(dA{5C>OIf7InO!g_q#g#QZdpPW|Wc?P*NTw>F(+RR01g~AY>>F`~m`<{SdCo
zJc=#|xQm;cf}|Tr?!OdR0U-&Og)2zPxPX94@*pq*E(fNx*713Y@K)xDh@zzY+E3Mw
zV<jh;R$zG#sv-D)5b2V>A+WjZA@G`eCv8CIZyN;s2ppxB`^aNgm7XA-m8Xb0CORVO
zBN`K<N*x3H-?o%D8Vqx?G%0D37U#tfP3r*(-Ib_fNdORFfHJ}G0!ED%=1vUw#OA-@
zz4)X&w#E4oA(_X$NJ(Ho)w{<ks>;g(KXeh>d)~c!O}GU*LXhLA^F@q;W26TGQRMq%
zFIuN$Qnq)r4PRPyf;}Xh6pK-n9JECi<Gk~&u;NKihlb)k)_}s|@u6t^Sx7E^`DqVT
z2&dr63bWs)Q1^J=?s$;CeDk;h&A;Y|-_72IAyJPv9TqnFn{9iWA1d@VH#j^l?*|1m
zDiCVEo_%#mK=P@Ws9esSfLMR`QY7!OFagmk@j`<6OKO7SCHeS0<`+t+3(61qVTmn2
z9w%8(K;5kC@J91K9)S7HKXJn{Z=C2-S$$5&TTQr_2I8mdxCY{(?k4+!d!ojE<c9*j
z(#82)fs`HbPW~#r@i=Ld;SCyN;NmYqHlHQGJ2{0L$BvuzUn7vupKHd)(hC3K3&k&j
z?_TfSnt$<Dv+MWRtPFaAlC(w4&K=cAytW>hJF<3!%`K&7&s#NntX9KG4@x@4G|!67
z3BxZbeoYH-JOs@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 6fba260bd..97afc0b4d 100644
--- a/src/box/lua/upgrade.lua
+++ b/src/box/lua/upgrade.lua
@@ -1000,6 +1000,24 @@ local function upgrade_to_2_7_1()
     function_access()
 end
 
+--------------------------------------------------------------------------------
+-- Tarantool 2.9.1
+--------------------------------------------------------------------------------
+local function sql_builtin_function_uuid()
+    local _func = box.space._func
+    local _priv = box.space._priv
+    local datetime = os.date("%Y-%m-%d %H:%M:%S")
+    local t = _func:auto_increment({ADMIN, 'UUID', 1, 'SQL_BUILTIN', '',
+                                    'function', {}, 'any', 'none', 'none',
+                                    false, false, true, {}, setmap({}), '',
+                                    datetime, datetime})
+    _priv:replace{ADMIN, PUBLIC, 'function', t.id, box.priv.X}
+end
+
+local function upgrade_to_2_9_1()
+    sql_builtin_function_uuid()
+end
+
 --------------------------------------------------------------------------------
 
 local handlers = {
@@ -1015,6 +1033,7 @@ local handlers = {
     {version = mkversion(2, 3, 0), func = upgrade_to_2_3_0, auto = true},
     {version = mkversion(2, 3, 1), func = upgrade_to_2_3_1, auto = true},
     {version = mkversion(2, 7, 1), func = upgrade_to_2_7_1, auto = true},
+    {version = mkversion(2, 9, 1), func = upgrade_to_2_9_1, auto = true},
 }
 
 -- Schema version of the snapshot.
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 9c4480a92..f93ae867d 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -66,6 +66,35 @@ mem_as_bin(struct Mem *mem)
 	return s;
 }
 
+static void
+sql_func_uuid(struct sql_context *ctx, int argc, struct Mem **argv)
+{
+	if (argc > 1) {
+		diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, "UUID",
+			 "one or zero", argc);
+		ctx->is_aborted = true;
+		return;
+	}
+	if (argc == 1) {
+		uint64_t version;
+		if (mem_get_uint(argv[0], &version) != 0) {
+			diag_set(ClientError, ER_SQL_TYPE_MISMATCH,
+				 mem_str(argv[0]), "integer");
+			ctx->is_aborted = true;
+			return;
+		}
+		if (version != 4) {
+			diag_set(ClientError, ER_UNSUPPORTED, "Function UUID",
+				 "versions other than 4");
+			ctx->is_aborted = true;
+			return;
+		}
+	}
+	struct tt_uuid uuid;
+	tt_uuid_create(&uuid);
+	mem_set_uuid(ctx->pOut, &uuid);
+}
+
 /*
  * Return the collating function associated with a function.
  */
@@ -2544,6 +2573,16 @@ static struct {
 	 .call = UpperICUFunc,
 	 .finalize = NULL,
 	 .export_to_sql = true,
+	}, {
+	 .name = "UUID",
+	 .param_count = -1,
+	 .returns = FIELD_TYPE_UUID,
+	 .aggregate = FUNC_AGGREGATE_NONE,
+	 .is_deterministic = false,
+	 .flags = 0,
+	 .call = sql_func_uuid,
+	 .finalize = NULL,
+	 .export_to_sql = true,
 	}, {
 	 .name = "VERSION",
 	 .param_count = 0,
diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y
index 4c9cf475e..bd041e862 100644
--- a/src/box/sql/parse.y
+++ b/src/box/sql/parse.y
@@ -268,7 +268,7 @@ columnlist ::= tcons.
   CONFLICT DEFERRED END ENGINE FAIL
   IGNORE INITIALLY INSTEAD NO MATCH PLAN
   QUERY KEY OFFSET RAISE RELEASE REPLACE RESTRICT
-  RENAME CTIME_KW IF ENABLE DISABLE
+  RENAME CTIME_KW IF ENABLE DISABLE UUID
   .
 %wildcard ANY.
 
diff --git a/test/box-py/bootstrap.result b/test/box-py/bootstrap.result
index ed7accea3..b2328487c 100644
--- a/test/box-py/bootstrap.result
+++ b/test/box-py/bootstrap.result
@@ -4,7 +4,7 @@ box.internal.bootstrap()
 box.space._schema:select{}
 ---
 - - ['max_id', 511]
-  - ['version', 2, 7, 1]
+  - ['version', 2, 9, 1]
 ...
 box.space._cluster:select{}
 ---
@@ -242,6 +242,7 @@ box.space._priv:select{}
   - [1, 2, 'function', 65, 4]
   - [1, 2, 'function', 66, 4]
   - [1, 2, 'function', 67, 4]
+  - [1, 2, 'function', 68, 4]
   - [1, 2, 'space', 276, 2]
   - [1, 2, 'space', 277, 1]
   - [1, 2, 'space', 281, 1]
diff --git a/test/box/access.result b/test/box/access.result
index 27e636122..1a8730f1a 100644
--- a/test/box/access.result
+++ b/test/box/access.result
@@ -703,7 +703,7 @@ box.schema.func.exists(1)
 ---
 - true
 ...
-box.schema.func.exists(68)
+box.schema.func.exists(69)
 ---
 - false
 ...
diff --git a/test/box/access.test.lua b/test/box/access.test.lua
index a62f87ad8..2bf772b7b 100644
--- a/test/box/access.test.lua
+++ b/test/box/access.test.lua
@@ -282,7 +282,7 @@ box.schema.user.exists{}
 box.schema.func.exists('nosuchfunc')
 box.schema.func.exists('guest')
 box.schema.func.exists(1)
-box.schema.func.exists(68)
+box.schema.func.exists(69)
 box.schema.func.exists('box.schema.user.info')
 box.schema.func.exists()
 box.schema.func.exists(nil)
diff --git a/test/box/access_bin.result b/test/box/access_bin.result
index c58f331d3..aeb8b3bd8 100644
--- a/test/box/access_bin.result
+++ b/test/box/access_bin.result
@@ -298,7 +298,7 @@ box.schema.user.grant('guest', 'execute', 'universe')
 function f1() return box.space._func:get(1)[4] end
 ---
 ...
-function f2() return box.space._func:get(68)[4] end
+function f2() return box.space._func:get(69)[4] end
 ---
 ...
 box.schema.func.create('f1')
diff --git a/test/box/access_bin.test.lua b/test/box/access_bin.test.lua
index 41d5f4245..954266858 100644
--- a/test/box/access_bin.test.lua
+++ b/test/box/access_bin.test.lua
@@ -112,7 +112,7 @@ test:drop()
 -- notice that guest can execute stuff, but can't read space _func
 box.schema.user.grant('guest', 'execute', 'universe')
 function f1() return box.space._func:get(1)[4] end
-function f2() return box.space._func:get(68)[4] end
+function f2() return box.space._func:get(69)[4] end
 box.schema.func.create('f1')
 box.schema.func.create('f2',{setuid=true})
 c = net.connect(box.cfg.listen)
diff --git a/test/box/access_sysview.result b/test/box/access_sysview.result
index 6468a0709..d7a7b7534 100644
--- a/test/box/access_sysview.result
+++ b/test/box/access_sysview.result
@@ -258,11 +258,11 @@ box.session.su('guest')
 ...
 #box.space._vpriv:select{}
 ---
-- 82
+- 83
 ...
 #box.space._vfunc:select{}
 ---
-- 67
+- 68
 ...
 #box.space._vcollation:select{}
 ---
@@ -290,11 +290,11 @@ box.session.su('guest')
 ...
 #box.space._vpriv:select{}
 ---
-- 82
+- 83
 ...
 #box.space._vfunc:select{}
 ---
-- 67
+- 68
 ...
 #box.space._vsequence:select{}
 ---
diff --git a/test/box/function1.result b/test/box/function1.result
index 928cd5758..0166c828f 100644
--- a/test/box/function1.result
+++ b/test/box/function1.result
@@ -97,7 +97,7 @@ box.func["function1.args"]
   exports:
     lua: true
     sql: false
-  id: 68
+  id: 69
   setuid: false
   is_multikey: false
   is_deterministic: false
@@ -593,7 +593,7 @@ func
   exports:
     lua: true
     sql: false
-  id: 68
+  id: 69
   setuid: false
   is_multikey: false
   is_deterministic: false
@@ -665,7 +665,7 @@ func
   exports:
     lua: true
     sql: false
-  id: 68
+  id: 69
   setuid: false
   is_multikey: false
   is_deterministic: false
diff --git a/test/sql-tap/uuid.test.lua b/test/sql-tap/uuid.test.lua
index 4287f0921..83fcc3d0e 100755
--- a/test/sql-tap/uuid.test.lua
+++ b/test/sql-tap/uuid.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(139)
+test:plan(145)
 
 local uuid = require("uuid")
 local uuid1 = uuid.fromstr("11111111-1111-1111-1111-111111111111")
@@ -1266,6 +1266,56 @@ test:do_execsql_test(
         3,2,1
     })
 
+-- Check function uuid().
+test:do_execsql_test(
+    "uuid-16.1",
+    [[
+        SELECT typeof(uuid());
+    ]], {
+        "uuid"
+    })
+
+test:do_execsql_test(
+    "uuid-16.2",
+    [[
+        SELECT typeof(uuid(4));
+    ]], {
+        "uuid"
+    })
+
+test:do_catchsql_test(
+    "uuid-16.3",
+    [[
+        SELECT uuid(1);
+    ]], {
+        1, "Function UUID does not support versions other than 4"
+    })
+
+test:do_catchsql_test(
+    "uuid-16.4",
+    [[
+        SELECT uuid('asd');
+    ]], {
+        1, "Type mismatch: can not convert asd to integer"
+    })
+
+test:do_catchsql_test(
+    "uuid-16.5",
+    [[
+        SELECT uuid(4, 5);
+    ]], {
+        1, "Wrong number of arguments is passed to UUID(): expected one or zero, got 2"
+    })
+
+-- Make sure the uuid() function generates a new UUID each time when called.
+test:do_execsql_test(
+    "uuid-16.6",
+    [[
+        SELECT uuid() != uuid();
+    ]], {
+        true
+    })
+
 test:execsql([[
     DROP TRIGGER t;
     DROP VIEW v;
diff --git a/test/wal_off/func_max.result b/test/wal_off/func_max.result
index 78db38d6b..cc5bcc141 100644
--- a/test/wal_off/func_max.result
+++ b/test/wal_off/func_max.result
@@ -42,11 +42,11 @@ test_run:cmd("setopt delimiter ''");
 ...
 func_limit()
 ---
-- error: 'Failed to create function ''func31934'': function id is too big'
+- error: 'Failed to create function ''func31933'': function id is too big'
 ...
 drop_limit_func()
 ---
-- error: Function 'func31934' does not exist
+- error: Function 'func31933' does not exist
 ...
 box.schema.user.create('testuser')
 ---
@@ -62,11 +62,11 @@ session.su('testuser')
 ...
 func_limit()
 ---
-- error: 'Failed to create function ''func31934'': function id is too big'
+- error: 'Failed to create function ''func31933'': function id is too big'
 ...
 drop_limit_func()
 ---
-- error: Function 'func31934' does not exist
+- error: Function 'func31933' does not exist
 ...
 session.su('admin')
 ---
-- 
2.25.1


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

* Re: [Tarantool-patches] [PATCH v2 1/2] sql: introduce UUID field type
  2021-06-02  8:02 ` [Tarantool-patches] [PATCH v2 1/2] sql: introduce UUID field type Mergen Imeev via Tarantool-patches
@ 2021-06-02 20:13   ` Timur Safin via Tarantool-patches
  2021-06-03  9:04     ` Mergen Imeev via Tarantool-patches
  0 siblings, 1 reply; 8+ messages in thread
From: Timur Safin via Tarantool-patches @ 2021-06-02 20:13 UTC (permalink / raw)
  To: imeevma; +Cc: tarantool-patches

There are few minor complains...
: -----Original Message-----
: From: imeevma@tarantool.org <imeevma@tarantool.org>
: Sent: Wednesday, June 2, 2021 11:03 AM
: To: tsafin@tarantool.org
: Cc: tarantool-patches@dev.tarantool.org
: Subject: [PATCH v2 1/2] sql: introduce UUID field type
: 
: This patch introduces UUID to SQL. UUID is now available as a new field
: type.
: 
: Part of #5886
: 
: @TarantoolBot document
: Title: Field type UUID is now available in SQL
: 
: The UUID field type is now available in SQL. This means that we can
: create spaces and indexes with UUID, use it in SELECT, UPDATE and
: DELETE. UUID can be accepted and returned by built-in functions and
: user-defined functions.
: 
: According to the comparison rules, there will be no implicit casting in
: the comparison. This rule also applies to UUID values: if a value is not
: part of a SCALAR field, it cannot be compared to a value of any other
: type. If the value is in a SCALAR field, it can be compared to any other
: scalar value according to the comparison rules for a SCALAR field.
: 
: In case a UUID value is used in an operation that is not a comparison,
: it can be implicitly converted to STRING or VARBINARY.
: 
: If a STRING or VARBINARY value is used in an operation that is not a
: comparison, it can be implicitly converted to a UUID.
: 
: UUID value can always be explicitly converted to STRING or VARBINARY.
: 
: A STRING or VARBINARY value can be explicitly converted to a UUID if it
: conforms to the UUID standard.
: ---
:  extra/mkkeywordhash.c                         |    1 +
:  src/box/sql/func.c                            |   30 +-
:  src/box/sql/mem.c                             |  203 ++-
:  src/box/sql/mem.h                             |   29 +-
:  src/box/sql/parse.y                           |    1 +
:  src/box/sql/vdbe.c                            |   15 +-
:  test/sql-tap/CMakeLists.txt                   |    1 +
:  .../gh-5913-segfault-on-select-uuid.test.lua  |   42 +-
:  .../sql-tap/gh-6024-funcs-return-bin.test.lua |    8 +-
:  test/sql-tap/sql_uuid.c                       |   46 +
:  test/sql-tap/uuid.test.lua                    | 1294 +++++++++++++++++
:  11 files changed, 1602 insertions(+), 68 deletions(-)
:  create mode 100644 test/sql-tap/sql_uuid.c
:  create mode 100755 test/sql-tap/uuid.test.lua
: 
: diff --git a/extra/mkkeywordhash.c b/extra/mkkeywordhash.c
: index 7480c0211..0d998506c 100644
: --- a/extra/mkkeywordhash.c
: +++ b/extra/mkkeywordhash.c
: @@ -172,6 +172,7 @@ static Keyword aKeywordTable[] = {
:    { "UNSIGNED",               "TK_UNSIGNED",    true  },
:    { "UPDATE",                 "TK_UPDATE",      true  },
:    { "USING",                  "TK_USING",       true  },
: +  { "UUID" ,                  "TK_UUID" ,       true  },
:    { "VALUES",                 "TK_VALUES",      true  },
:    { "VARBINARY",              "TK_VARBINARY",   true  },
:    { "VIEW",                   "TK_VIEW",        true  },
: diff --git a/src/box/sql/func.c b/src/box/sql/func.c
: index 90e8e152f..9c4480a92 100644
: --- a/src/box/sql/func.c
: +++ b/src/box/sql/func.c
: @@ -58,7 +58,8 @@ static const void *
:  mem_as_bin(struct Mem *mem)
:  {
:  	const char *s;
: -	if (!mem_is_bytes(mem) && mem_to_str(mem) != 0)
: +	if (mem_cast_implicit(mem, FIELD_TYPE_VARBINARY) != 0 &&
: +	    mem_to_str(mem) != 0)
:  		return NULL;
:  	if (mem_get_bin(mem, &s) != 0)
:  		return NULL;
: @@ -142,26 +143,29 @@ typeofFunc(sql_context * context, int NotUsed,
: sql_value ** argv)
:  				-1, SQL_STATIC);
:  		return;
:  	}
: -	switch (sql_value_type(argv[0])) {
: -	case MP_INT:
: -	case MP_UINT:
: +	switch (argv[0]->type) {
: +	case MEM_TYPE_INT:
: +	case MEM_TYPE_UINT:
:  		z = "integer";
:  		break;
: -	case MP_STR:
: +	case MEM_TYPE_STR:
:  		z = "string";
:  		break;
: -	case MP_DOUBLE:
: +	case MEM_TYPE_DOUBLE:
:  		z = "double";
:  		break;
: -	case MP_BIN:
: -	case MP_ARRAY:
: -	case MP_MAP:
: +	case MEM_TYPE_BIN:
: +	case MEM_TYPE_ARRAY:
: +	case MEM_TYPE_MAP:
:  		z = "varbinary";
:  		break;
: -	case MP_BOOL:
: -	case MP_NIL:
: +	case MEM_TYPE_BOOL:
: +	case MEM_TYPE_NULL:
:  		z = "boolean";
:  		break;
: +	case MEM_TYPE_UUID:
: +		z = "uuid";
: +		break;
:  	default:
:  		unreachable();
:  		break;
: @@ -191,6 +195,7 @@ lengthFunc(sql_context * context, int argc, sql_value **
: argv)
:  			sql_result_uint(context, mem_len_unsafe(argv[0]));
:  			break;
:  		}
: +	case MP_EXT:
:  	case MP_STR:{
:  			const unsigned char *z = mem_as_ustr(argv[0]);
:  			if (z == 0)
: @@ -235,6 +240,7 @@ absFunc(sql_context * context, int argc, sql_value **
: argv)
:  		}
:  	case MP_BOOL:
:  	case MP_BIN:
: +	case MP_EXT:
:  	case MP_ARRAY:
:  	case MP_MAP: {
:  		diag_set(ClientError, ER_INCONSISTENT_TYPES, "number",
: @@ -1461,8 +1467,8 @@ trim_func_one_arg(struct sql_context *context,
: sql_value *arg)
:  		default_trim = (const unsigned char *) "\0";
:  	else
:  		default_trim = (const unsigned char *) " ";
: -	int input_str_sz = mem_len_unsafe(arg);
:  	const unsigned char *input_str = mem_as_ustr(arg);
: +	int input_str_sz = mem_len_unsafe(arg);
:  	uint8_t trim_char_len[1] = { 1 };
:  	trim_procedure(context, TRIM_BOTH, default_trim, trim_char_len, 1,
:  		       input_str, input_str_sz);
: diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
: index 9894c09af..f0dfce395 100644
: --- a/src/box/sql/mem.c
: +++ b/src/box/sql/mem.c
: @@ -58,6 +58,16 @@ enum {
:  	BUF_SIZE = 32,
:  };
: 
: +bool
: +mem_is_field_compatible(const struct Mem *mem, enum field_type type)
: +{
: +	if (mem->type == MEM_TYPE_UUID)
: +		return (field_ext_type[type] & (1U << MP_UUID)) != 0;
: +	enum mp_type mp_type = mem_mp_type(mem);
: +	assert(mp_type != MP_EXT);
: +	return field_mp_plain_type_is_compatible(type, mp_type, true);
: +}
: +
:  const char *
:  mem_str(const struct Mem *mem)
:  {
: @@ -81,6 +91,8 @@ mem_str(const struct Mem *mem)
:  	case MEM_TYPE_MAP:
:  	case MEM_TYPE_ARRAY:
:  		return mp_str(mem->z);
: +	case MEM_TYPE_UUID:
: +		return tt_uuid_str(&mem->u.uuid);
:  	case MEM_TYPE_BOOL:
:  		return mem->u.b ? "TRUE" : "FALSE";
:  	default:
: @@ -190,6 +202,16 @@ mem_set_double(struct Mem *mem, double value)
:  	mem->type = MEM_TYPE_DOUBLE;
:  }
: 
: +void
: +mem_set_uuid(struct Mem *mem, const struct tt_uuid *uuid)
: +{
: +	mem_clear(mem);
: +	mem->field_type = FIELD_TYPE_UUID;
: +	mem->u.uuid = *uuid;
: +	mem->type = MEM_TYPE_UUID;
: +	assert(mem->flags == 0);

What's the point of this assertion? It looks redundant, because flags 
supposed to be nulled at the end of mem_clear()?
(Not critical problem, but simply confusing)


: +}
: +
:  static inline void
:  set_str_const(struct Mem *mem, char *value, uint32_t len, int alloc_type)
:  {
: @@ -585,6 +607,18 @@ str_to_bin(struct Mem *mem)
:  	return 0;
:  }
: 
: +static inline int
: +str_to_uuid(struct Mem *mem)
: +{
: +	assert(mem->type == MEM_TYPE_STR);
: +	struct tt_uuid uuid;
: +	if (mem->n != UUID_STR_LEN ||
: +	    tt_uuid_from_string(tt_cstr(mem->z, mem->n), &uuid) != 0)

Eventually, when we would be able to parse more relaxed formats in tt_uuid_from_string()
(e.g. with dashes omitted or braces around uuid) we would have to remove this check for 
particular length UUID_STR_LEN, offloading any correctness check entirely to the function.
I'm not insisting that this check should be modified right away. Though, from future 
Compatible point of view it would be ideal... But not required. Just saying.

BTW, while we are here - could we avoid this static_alloc() call in tt_cstr() as a 
prerequisite to tt_uuid_from_string(), and pass view of this data with mem->z, mem->n
directly there?

: +		return -1;
: +	mem_set_uuid(mem, &uuid);
: +	return 0;
: +}
: +
:  static inline int
:  str_to_bool(struct Mem *mem)
:  {
: @@ -639,6 +673,19 @@ bin_to_str0(struct Mem *mem)
:  	return 0;
:  }
: 
: +static inline int
: +bin_to_uuid(struct Mem *mem)
: +{
: +	assert(mem->type == MEM_TYPE_BIN);
: +	if (ExpandBlob(mem) != 0)
: +		return -1;
: +	if (mem->n != UUID_LEN ||
: +	    tt_uuid_validate((struct tt_uuid *)mem->z) != 0)
: +		return -1;
: +	mem_set_uuid(mem, (struct tt_uuid *)mem->z);
: +	return 0;
: +}
: +
:  static inline int
:  bytes_to_int(struct Mem *mem)
:  {
: @@ -810,6 +857,22 @@ map_to_str0(struct Mem *mem)
:  	return mem_copy_str0(mem, str);
:  }
: 
: +static inline int
: +uuid_to_str0(struct Mem *mem)
: +{
: +	assert(mem->type == MEM_TYPE_UUID);
: +	char buf[UUID_STR_LEN + 1];
: +	tt_uuid_to_string(&mem->u.uuid, &buf[0]);
: +	return mem_copy_str0(mem, &buf[0]);
: +}
: +
: +static inline int
: +uuid_to_bin(struct Mem *mem)
: +{
: +	assert(mem->type == MEM_TYPE_UUID);
: +	return mem_copy_bin(mem, (char *)&mem->u.uuid, UUID_LEN);
: +}
: +
:  int
:  mem_to_int(struct Mem *mem)
:  {
: @@ -889,6 +952,8 @@ mem_to_str0(struct Mem *mem)
:  		return map_to_str0(mem);
:  	case MEM_TYPE_ARRAY:
:  		return array_to_str0(mem);
: +	case MEM_TYPE_UUID:
: +		return uuid_to_str0(mem);
:  	default:
:  		return -1;
:  	}
: @@ -914,6 +979,8 @@ mem_to_str(struct Mem *mem)
:  		return map_to_str0(mem);
:  	case MEM_TYPE_ARRAY:
:  		return array_to_str0(mem);
: +	case MEM_TYPE_UUID:
: +		return uuid_to_str0(mem);
:  	default:
:  		return -1;
:  	}
: @@ -966,9 +1033,19 @@ mem_cast_explicit(struct Mem *mem, enum field_type
: type)
:  			return str_to_bin(mem);
:  		if (mem_is_bytes(mem))
:  			return 0;
: +		if (mem->type == MEM_TYPE_UUID)
: +			return uuid_to_bin(mem);
:  		return -1;
:  	case FIELD_TYPE_NUMBER:
:  		return mem_to_number(mem);
: +	case FIELD_TYPE_UUID:
: +		if (mem->type == MEM_TYPE_UUID)
: +			return 0;
: +		if (mem->type == MEM_TYPE_STR)
: +			return str_to_uuid(mem);
: +		if (mem->type == MEM_TYPE_BIN)
: +			return bin_to_uuid(mem);
: +		return -1;
:  	case FIELD_TYPE_SCALAR:
:  		if ((mem->type & (MEM_TYPE_MAP | MEM_TYPE_ARRAY)) != 0)
:  			return -1;
: @@ -996,6 +1073,8 @@ mem_cast_implicit(struct Mem *mem, enum field_type
: type)
:  	case FIELD_TYPE_STRING:
:  		if (mem->type == MEM_TYPE_STR)
:  			return 0;
: +		if (mem->type == MEM_TYPE_UUID)
: +			return uuid_to_str0(mem);
:  		return -1;
:  	case FIELD_TYPE_DOUBLE:
:  		if (mem->type == MEM_TYPE_DOUBLE)
: @@ -1017,6 +1096,8 @@ mem_cast_implicit(struct Mem *mem, enum field_type
: type)
:  		if ((mem->type & (MEM_TYPE_BIN | MEM_TYPE_MAP |
:  				  MEM_TYPE_ARRAY)) != 0)
:  			return 0;
: +		if (mem->type == MEM_TYPE_UUID)
: +			return uuid_to_bin(mem);
:  		return -1;
:  	case FIELD_TYPE_NUMBER:
:  		if (mem_is_num(mem))
: @@ -1034,6 +1115,14 @@ mem_cast_implicit(struct Mem *mem, enum field_type
: type)
:  		if ((mem->type & (MEM_TYPE_MAP | MEM_TYPE_ARRAY)) != 0)
:  			return -1;
:  		return 0;
: +	case FIELD_TYPE_UUID:
: +		if (mem->type == MEM_TYPE_UUID)
: +			return 0;
: +		if (mem->type == MEM_TYPE_STR)
: +			return str_to_uuid(mem);
: +		if (mem->type == MEM_TYPE_BIN)
: +			return bin_to_uuid(mem);
: +		return -1;
:  	case FIELD_TYPE_ANY:
:  		return 0;
:  	default:
: @@ -1063,6 +1152,8 @@ mem_cast_implicit_old(struct Mem *mem, enum field_type
: type)
:  			return int_to_str0(mem);
:  		if (mem->type == MEM_TYPE_DOUBLE)
:  			return double_to_str0(mem);
: +		if (mem->type == MEM_TYPE_UUID)
: +			return uuid_to_str0(mem);
:  		return -1;
:  	case FIELD_TYPE_DOUBLE:
:  		if (mem->type == MEM_TYPE_DOUBLE)
: @@ -1087,6 +1178,8 @@ mem_cast_implicit_old(struct Mem *mem, enum field_type
: type)
:  	case FIELD_TYPE_VARBINARY:
:  		if (mem->type == MEM_TYPE_BIN)
:  			return 0;
: +		if (mem->type == MEM_TYPE_UUID)
: +			return uuid_to_bin(mem);
:  		return -1;
:  	case FIELD_TYPE_NUMBER:
:  		if (mem_is_num(mem))
: @@ -1106,6 +1199,14 @@ mem_cast_implicit_old(struct Mem *mem, enum
: field_type type)
:  		if ((mem->type & (MEM_TYPE_MAP | MEM_TYPE_ARRAY)) != 0)
:  			return -1;
:  		return 0;
: +	case FIELD_TYPE_UUID:
: +		if (mem->type == MEM_TYPE_UUID)
: +			return 0;
: +		if (mem->type == MEM_TYPE_STR)
: +			return str_to_uuid(mem);
: +		if (mem->type == MEM_TYPE_BIN)
: +			return bin_to_uuid(mem);
: +		return -1;
:  	default:
:  		break;
:  	}

Uh-oh, those changes in mem_cast_implicit_old() will conflict with my
cleanup here.But I'll handle it.
(OTOH, could we not touch mem_cast_implicit_old as it will be deleted soon?)

: @@ -1899,6 +2000,15 @@ mem_cmp_str(const struct Mem *left, const struct Mem
: *right, int *result,
:  	return 0;
:  }
: 
: +int
: +mem_cmp_uuid(const struct Mem *a, const struct Mem *b, int *result)
: +{
: +	if ((a->type & b->type & MEM_TYPE_UUID) == 0)
: +		return -1;
: +	*result = memcmp(&a->u.uuid, &b->u.uuid, UUID_LEN);
: +	return 0;
: +}
: +
:  /*
:   * Both *pMem1 and *pMem2 contain string values. Compare the two values
:   * using the collation sequence pColl. As usual, return a negative , zero
: @@ -1951,13 +2061,15 @@ mem_type_to_str(const struct Mem *p)
:  		return "varbinary";
:  	case MEM_TYPE_BOOL:
:  		return "boolean";
: +	case MEM_TYPE_UUID:
: +		return "uuid";
:  	default:
:  		unreachable();
:  	}
:  }
: 
:  enum mp_type
: -mem_mp_type(struct Mem *mem)
: +mem_mp_type(const struct Mem *mem)
:  {
:  	assert(mem->type < MEM_TYPE_INVALID);
:  	switch (mem->type) {
: @@ -1979,6 +2091,8 @@ mem_mp_type(struct Mem *mem)
:  		return MP_BOOL;
:  	case MEM_TYPE_DOUBLE:
:  		return MP_DOUBLE;
: +	case MEM_TYPE_UUID:
: +		return MP_EXT;
:  	default:
:  		unreachable();
:  	}
: @@ -2144,6 +2258,9 @@ memTracePrint(Mem *p)
:  	case MEM_TYPE_BOOL:
:  		printf(" bool:%s", SQL_TOKEN_BOOLEAN(p->u.b));
:  		return;
: +	case MEM_TYPE_UUID:
: +		printf(" uuid:%s", tt_uuid_str(&p->u.uuid));
: +		return;
:  	default: {
:  		char zBuf[200];
:  		sqlVdbeMemPrettyPrint(p, zBuf);
: @@ -2360,6 +2477,14 @@ sqlMemCompare(const Mem * pMem1, const Mem * pMem2,
: const struct coll * pColl)
:  		return -1;
:  	}
: 
: +	if (((type1 | type2) & MEM_TYPE_UUID) != 0) {
: +		if (mem_cmp_uuid(pMem1, pMem2, &res) == 0)
: +			return res;
: +		if (type1 != MEM_TYPE_UUID)
: +			return +1;
: +		return -1;
: +	}
: +
:  	/* At least one of the two values is a number
:  	 */
:  	if (((type1 | type2) &
: @@ -2565,13 +2690,30 @@ sqlVdbeCompareMsgpack(const char **key1,
:  			break;
:  		}
:  	case MP_ARRAY:
: -	case MP_MAP:
: -	case MP_EXT:{
: +	case MP_MAP: {
:  			mem1.z = (char *)aKey1;
:  			mp_next(&aKey1);
:  			mem1.n = aKey1 - (char *)mem1.z;
:  			goto do_blob;
:  		}
: +	case MP_EXT: {
: +		int8_t type;
: +		const char *buf = aKey1;
: +		uint32_t len = mp_decode_extl(&aKey1, &type);
: +		if (type == MP_UUID) {
: +			assert(len == UUID_LEN);
: +			mem1.type = MEM_TYPE_UUID;
: +			aKey1 = buf;
: +			if (mp_decode_uuid(&aKey1, &mem1.u.uuid) == NULL ||
: +			    mem_cmp_uuid(&mem1, pKey2, &rc) != 0)
: +				rc = 1;
: +			break;
: +		}
: +		aKey1 += len;
: +		mem1.z = (char *)buf;
: +		mem1.n = aKey1 - buf;
: +		goto do_blob;
: +	}
:  	}
:  	*key1 = aKey1;
:  	return rc;
: @@ -2625,9 +2767,25 @@ mem_from_mp_ephemeral(struct Mem *mem, const char
: *buf, uint32_t *len)
:  		break;
:  	}
:  	case MP_EXT: {
: -		mem->z = (char *)buf;
: -		mp_next(&buf);
: -		mem->n = buf - mem->z;
: +		int8_t type;
: +		const char *svp = buf;
: +		uint32_t size = mp_decode_extl(&buf, &type);
: +		if (type == MP_UUID) {
: +			assert(size == UUID_LEN);
: +			buf = svp;
: +			if (mp_decode_uuid(&buf, &mem->u.uuid) == NULL) {
: +				diag_set(ClientError, ER_INVALID_MSGPACK,
: +					 "Invalid MP_UUID MsgPack format");
: +				return -1;
: +			}
: +			mem->type = MEM_TYPE_UUID;
: +			mem->flags = 0;
: +			mem->field_type = FIELD_TYPE_UUID;
: +			break;
: +		}
: +		buf += size;
: +		mem->z = (char *)svp;
: +		mem->n = buf - svp;
:  		mem->type = MEM_TYPE_BIN;
:  		mem->flags = MEM_Ephem;
:  		mem->field_type = FIELD_TYPE_VARBINARY;
: @@ -2764,6 +2922,9 @@ mpstream_encode_vdbe_mem(struct mpstream *stream,
: struct Mem *var)
:  	case MEM_TYPE_BOOL:
:  		mpstream_encode_bool(stream, var->u.b);
:  		return;
: +	case MEM_TYPE_UUID:
: +		mpstream_encode_uuid(stream, &var->u.uuid);
: +		return;
:  	default:
:  		unreachable();
:  	}
: @@ -2850,6 +3011,9 @@ port_vdbemem_dump_lua(struct port *base, struct
: lua_State *L, bool is_flat)
:  		case MEM_TYPE_BOOL:
:  			lua_pushboolean(L, mem->u.b);
:  			break;
: +		case MEM_TYPE_UUID:
: +			*luaL_pushuuid(L) = mem->u.uuid;
: +			break;
:  		default:
:  			unreachable();
:  		}
: @@ -2976,14 +3140,8 @@ port_lua_get_vdbemem(struct port *base, uint32_t
: *size)
:  			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);
: +				mem_set_uuid(&val[i], field.uuidval);
: +				break;
:  			} else {
:  				size = mp_sizeof_decimal(field.decval);
:  				buf = region_alloc(&fiber()->gc, size);
: @@ -3087,7 +3245,22 @@ port_c_get_vdbemem(struct port *base, uint32_t *size)
:  			break;
:  		case MP_EXT:
:  			str = data;
: -			mp_next(&data);
: +			int8_t type;
: +			len = mp_decode_extl(&data, &type);
: +			if (type == MP_UUID) {
: +				assert(len == UUID_LEN);
: +				struct tt_uuid *uuid = &val[i].u.uuid;
: +				data = str;
: +				if (mp_decode_uuid(&data, uuid) == NULL) {
: +					diag_set(ClientError,
: +						 ER_INVALID_MSGPACK, "Invalid "
: +						 "MP_UUID MsgPack format");
: +					goto error;
: +				}
: +				val[i].type = MEM_TYPE_UUID;
: +				break;
: +			}
: +			data += len;
:  			if (mem_copy_bin(&val[i], str, data - str) != 0)
:  				goto error;
:  			break;
: diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
: index 15d97da0e..b3cd5c545 100644
: --- a/src/box/sql/mem.h
: +++ b/src/box/sql/mem.h
: @@ -30,6 +30,7 @@
:   * SUCH DAMAGE.
:   */
:  #include "box/field_def.h"
: +#include "uuid/tt_uuid.h"
: 
:  struct sql;
:  struct Vdbe;
: @@ -47,10 +48,11 @@ enum mem_type {
:  	MEM_TYPE_MAP		= 1 << 6,
:  	MEM_TYPE_BOOL		= 1 << 7,
:  	MEM_TYPE_DOUBLE		= 1 << 8,
: -	MEM_TYPE_INVALID	= 1 << 9,
: -	MEM_TYPE_FRAME		= 1 << 10,
: -	MEM_TYPE_PTR		= 1 << 11,
: -	MEM_TYPE_AGG		= 1 << 12,
: +	MEM_TYPE_UUID		= 1 << 9,
: +	MEM_TYPE_INVALID	= 1 << 10,
: +	MEM_TYPE_FRAME		= 1 << 11,
: +	MEM_TYPE_PTR		= 1 << 12,
: +	MEM_TYPE_AGG		= 1 << 13,
:  };
: 
:  /*
: @@ -72,6 +74,7 @@ struct Mem {
:  		 */
:  		struct func *func;
:  		struct VdbeFrame *pFrame;	/* Used when flags==MEM_Frame */
: +		struct tt_uuid uuid;
:  	} u;
:  	/** Type of the value this MEM contains. */
:  	enum mem_type type;
: @@ -255,6 +258,10 @@ mem_is_any_null(const struct Mem *mem1, const struct
: Mem *mem2)
:  	return ((mem1->type| mem2->type) & MEM_TYPE_NULL) != 0;
:  }
: 
: +/** Check if MEM is compatible with field type. */
: +bool
: +mem_is_field_compatible(const struct Mem *mem, enum field_type type);
: +
:  /**
:   * Return a string that represent content of MEM. String is either
: allocated
:   * using static_alloc() of just a static variable.
: @@ -290,6 +297,10 @@ mem_set_bool(struct Mem *mem, bool value);
:  void
:  mem_set_double(struct Mem *mem, double value);
: 
: +/** Clear MEM and set it to UUID. */
: +void
: +mem_set_uuid(struct Mem *mem, const struct tt_uuid *uuid);
: +
:  /** Clear MEM and set it to STRING. The string belongs to another object.
: */
:  void
:  mem_set_str_ephemeral(struct Mem *mem, char *value, uint32_t len);
: @@ -677,6 +688,14 @@ mem_cmp_str(const struct Mem *left, const struct Mem
: *right, int *result,
:  int
:  mem_cmp_num(const struct Mem *a, const struct Mem *b, int *result);
: 
: +/**
: + * Compare two MEMs and return the result of comparison. MEMs should be of
: + * UUID type or their values are converted to UUID according to
: + * implicit cast rules. Original MEMs are not changed.
: + */
: +int
: +mem_cmp_uuid(const struct Mem *left, const struct Mem *right, int *result);
: +
:  /**
:   * Convert the given MEM to INTEGER. This function and the function below
: define
:   * the rules that are used to convert values of all other types to INTEGER.
: In
: @@ -898,7 +917,7 @@ mem_type_to_str(const struct Mem *p);
:   * transparent memory cell.
:   */
:  enum mp_type
: -mem_mp_type(struct Mem *mem);
: +mem_mp_type(const struct Mem *mem);
: 
:  enum mp_type
:  sql_value_type(struct Mem *);
: diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y
: index abc363951..4c9cf475e 100644
: --- a/src/box/sql/parse.y
: +++ b/src/box/sql/parse.y
: @@ -1834,6 +1834,7 @@ typedef(A) ::= SCALAR . { A.type = FIELD_TYPE_SCALAR;
: }
:  typedef(A) ::= BOOL . { A.type = FIELD_TYPE_BOOLEAN; }
:  typedef(A) ::= BOOLEAN . { A.type = FIELD_TYPE_BOOLEAN; }
:  typedef(A) ::= VARBINARY . { A.type = FIELD_TYPE_VARBINARY; }
: +typedef(A) ::= UUID . { A.type = FIELD_TYPE_UUID; }
: 
:  /**
:   * Time-like types are temporary disabled, until they are
: diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
: index 12ec703a2..32d02d96e 100644
: --- a/src/box/sql/vdbe.c
: +++ b/src/box/sql/vdbe.c
: @@ -1322,10 +1322,10 @@ case OP_FunctionByName: {
:  	region_truncate(region, region_svp);
:  	if (mem == NULL)
:  		goto abort_due_to_error;
: -	enum mp_type type = sql_value_type((sql_value *)pOut);
: -	if (!field_mp_plain_type_is_compatible(returns, type, true)) {
: +	if (!mem_is_field_compatible(pOut, returns)) {
:  		diag_set(ClientError, ER_FUNC_INVALID_RETURN_TYPE, pOp->p4.z,
: -			 field_type_strs[returns], mp_type_strs[type]);
: +			 field_type_strs[returns],
: +			 mp_type_strs[mem_mp_type(pOut)]);
:  		goto abort_due_to_error;
:  	}
: 
: @@ -1634,6 +1634,15 @@ case OP_Ge: {             /* same as TK_GE, jump,
: in1, in3 */
:  				 "boolean");
:  			goto abort_due_to_error;
:  		}
: +	} else if (((pIn3->type | pIn1->type) & MEM_TYPE_UUID) != 0) {
: +		if (mem_cmp_uuid(pIn3, pIn1, &res) != 0) {
: +			char *str = pIn3->type != MEM_TYPE_UUID ?
: +				    mem_type_to_str(pIn3) :
: +				    mem_type_to_str(pIn1);
: +			diag_set(ClientError, ER_SQL_TYPE_MISMATCH, str,
: +				 "uuid");
: +			goto abort_due_to_error;
: +		}
:  	} else if (mem_is_bin(pIn3) || mem_is_bin(pIn1)) {
:  		if (mem_cmp_bin(pIn3, pIn1, &res) != 0) {
:  			char *str = !mem_is_bin(pIn3) ?

In general, despite minor complains, it all looks good to me, and ok to commit.
Those complains we will address later.

LGTM

Timur


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

* Re: [Tarantool-patches] [PATCH v2 2/2] sql: introduce SQL built-in function UUID()
  2021-06-02  8:02 ` [Tarantool-patches] [PATCH v2 2/2] sql: introduce SQL built-in function UUID() Mergen Imeev via Tarantool-patches
@ 2021-06-02 20:13   ` Timur Safin via Tarantool-patches
  0 siblings, 0 replies; 8+ messages in thread
From: Timur Safin via Tarantool-patches @ 2021-06-02 20:13 UTC (permalink / raw)
  To: imeevma; +Cc: tarantool-patches

LGTM

: From: imeevma@tarantool.org <imeevma@tarantool.org>
: Sent: Wednesday, June 2, 2021 11:03 AM
: To: tsafin@tarantool.org
: Cc: tarantool-patches@dev.tarantool.org
: Subject: [PATCH v2 2/2] sql: introduce SQL built-in function UUID()
: 
: This patch introduces a new SQL built-in function UUID().
: 
: Closes #5886
: 
: @TarantoolBot document
: Title: SQL built-in function UUID()
: 
: SQL built-in function UUID() takes zero or one argument. If no argument
: is specified, a UUID v4 is generated. If the version of the UUID to
: generate is specified as an argument, the function returns the new UUID
: of the given version. Currently only version 4 of UUID is supported.
: ---
:  .../unreleased/introduce-uuid-to-sql.md       |   3 +
:  src/box/bootstrap.snap                        | Bin 5991 -> 6016 bytes
:  src/box/lua/upgrade.lua                       |  19 +++++++
:  src/box/sql/func.c                            |  39 +++++++++++++
:  src/box/sql/parse.y                           |   2 +-
:  test/box-py/bootstrap.result                  |   3 +-
:  test/box/access.result                        |   2 +-
:  test/box/access.test.lua                      |   2 +-
:  test/box/access_bin.result                    |   2 +-
:  test/box/access_bin.test.lua                  |   2 +-
:  test/box/access_sysview.result                |   8 +--
:  test/box/function1.result                     |   6 +-
:  test/sql-tap/uuid.test.lua                    |  52 +++++++++++++++++-
:  test/wal_off/func_max.result                  |   8 +--
:  14 files changed, 130 insertions(+), 18 deletions(-)
:  create mode 100644 changelogs/unreleased/introduce-uuid-to-sql.md
: 
: diff --git a/changelogs/unreleased/introduce-uuid-to-sql.md
: b/changelogs/unreleased/introduce-uuid-to-sql.md
: new file mode 100644
: index 000000000..642ac94fb
: --- /dev/null
: +++ b/changelogs/unreleased/introduce-uuid-to-sql.md
: @@ -0,0 +1,3 @@
: +## feature/core
: +
: + * Field type UUID is now available in SQL, and new UUID can be generated
: using new SQL built-in function uuid() (gh-5886).
: 
...
: diff --git a/src/box/lua/upgrade.lua b/src/box/lua/upgrade.lua
: index 6fba260bd..97afc0b4d 100644
: --- a/src/box/lua/upgrade.lua
: +++ b/src/box/lua/upgrade.lua
: @@ -1000,6 +1000,24 @@ local function upgrade_to_2_7_1()
:      function_access()
:  end
: 
: +---------------------------------------------------------------------------
: -----
: +-- Tarantool 2.9.1
: +---------------------------------------------------------------------------
: -----
: +local function sql_builtin_function_uuid()
: +    local _func = box.space._func
: +    local _priv = box.space._priv
: +    local datetime = os.date("%Y-%m-%d %H:%M:%S")
: +    local t = _func:auto_increment({ADMIN, 'UUID', 1, 'SQL_BUILTIN', '',
: +                                    'function', {}, 'any', 'none', 'none',
: +                                    false, false, true, {}, setmap({}), '',
: +                                    datetime, datetime})
: +    _priv:replace{ADMIN, PUBLIC, 'function', t.id, box.priv.X}
: +end
: +
: +local function upgrade_to_2_9_1()
: +    sql_builtin_function_uuid()
: +end
: +
:  ---------------------------------------------------------------------------
: -----
: 
:  local handlers = {
: @@ -1015,6 +1033,7 @@ local handlers = {
:      {version = mkversion(2, 3, 0), func = upgrade_to_2_3_0, auto = true},
:      {version = mkversion(2, 3, 1), func = upgrade_to_2_3_1, auto = true},
:      {version = mkversion(2, 7, 1), func = upgrade_to_2_7_1, auto = true},
: +    {version = mkversion(2, 9, 1), func = upgrade_to_2_9_1, auto = true},
:  }
: 
:  -- Schema version of the snapshot.
: diff --git a/src/box/sql/func.c b/src/box/sql/func.c
: index 9c4480a92..f93ae867d 100644
: --- a/src/box/sql/func.c
: +++ b/src/box/sql/func.c
: @@ -66,6 +66,35 @@ mem_as_bin(struct Mem *mem)
:  	return s;
:  }
: 
: +static void
: +sql_func_uuid(struct sql_context *ctx, int argc, struct Mem **argv)
: +{
: +	if (argc > 1) {
: +		diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, "UUID",
: +			 "one or zero", argc);
: +		ctx->is_aborted = true;
: +		return;
: +	}
: +	if (argc == 1) {
: +		uint64_t version;
: +		if (mem_get_uint(argv[0], &version) != 0) {
: +			diag_set(ClientError, ER_SQL_TYPE_MISMATCH,
: +				 mem_str(argv[0]), "integer");
: +			ctx->is_aborted = true;
: +			return;
: +		}
: +		if (version != 4) {
: +			diag_set(ClientError, ER_UNSUPPORTED, "Function UUID",
: +				 "versions other than 4");
: +			ctx->is_aborted = true;
: +			return;
: +		}
: +	}
: +	struct tt_uuid uuid;
: +	tt_uuid_create(&uuid);
: +	mem_set_uuid(ctx->pOut, &uuid);
: +}
: +
:  /*
:   * Return the collating function associated with a function.
:   */
: @@ -2544,6 +2573,16 @@ static struct {
:  	 .call = UpperICUFunc,
:  	 .finalize = NULL,
:  	 .export_to_sql = true,
: +	}, {
: +	 .name = "UUID",
: +	 .param_count = -1,
: +	 .returns = FIELD_TYPE_UUID,
: +	 .aggregate = FUNC_AGGREGATE_NONE,
: +	 .is_deterministic = false,
: +	 .flags = 0,
: +	 .call = sql_func_uuid,
: +	 .finalize = NULL,
: +	 .export_to_sql = true,
:  	}, {
:  	 .name = "VERSION",
:  	 .param_count = 0,
: diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y
: index 4c9cf475e..bd041e862 100644
: --- a/src/box/sql/parse.y
: +++ b/src/box/sql/parse.y
: @@ -268,7 +268,7 @@ columnlist ::= tcons.
:    CONFLICT DEFERRED END ENGINE FAIL
:    IGNORE INITIALLY INSTEAD NO MATCH PLAN
:    QUERY KEY OFFSET RAISE RELEASE REPLACE RESTRICT
: -  RENAME CTIME_KW IF ENABLE DISABLE
: +  RENAME CTIME_KW IF ENABLE DISABLE UUID
:    .
:  %wildcard ANY.
: 
: diff --git a/test/box-py/bootstrap.result b/test/box-py/bootstrap.result
: index ed7accea3..b2328487c 100644
: --- a/test/box-py/bootstrap.result
: +++ b/test/box-py/bootstrap.result
: @@ -4,7 +4,7 @@ box.internal.bootstrap()
:  box.space._schema:select{}
:  ---
:  - - ['max_id', 511]
: -  - ['version', 2, 7, 1]
: +  - ['version', 2, 9, 1]
:  ...
:  box.space._cluster:select{}
:  ---
: @@ -242,6 +242,7 @@ box.space._priv:select{}
:    - [1, 2, 'function', 65, 4]
:    - [1, 2, 'function', 66, 4]
:    - [1, 2, 'function', 67, 4]
: +  - [1, 2, 'function', 68, 4]
:    - [1, 2, 'space', 276, 2]
:    - [1, 2, 'space', 277, 1]
:    - [1, 2, 'space', 281, 1]
: diff --git a/test/box/access.result b/test/box/access.result
: index 27e636122..1a8730f1a 100644
: --- a/test/box/access.result
: +++ b/test/box/access.result
: @@ -703,7 +703,7 @@ box.schema.func.exists(1)
:  ---
:  - true
:  ...
: -box.schema.func.exists(68)
: +box.schema.func.exists(69)
:  ---
:  - false
:  ...
: diff --git a/test/box/access.test.lua b/test/box/access.test.lua
: index a62f87ad8..2bf772b7b 100644
: --- a/test/box/access.test.lua
: +++ b/test/box/access.test.lua
: @@ -282,7 +282,7 @@ box.schema.user.exists{}
:  box.schema.func.exists('nosuchfunc')
:  box.schema.func.exists('guest')
:  box.schema.func.exists(1)
: -box.schema.func.exists(68)
: +box.schema.func.exists(69)
:  box.schema.func.exists('box.schema.user.info')
:  box.schema.func.exists()
:  box.schema.func.exists(nil)
: diff --git a/test/box/access_bin.result b/test/box/access_bin.result
: index c58f331d3..aeb8b3bd8 100644
: --- a/test/box/access_bin.result
: +++ b/test/box/access_bin.result
: @@ -298,7 +298,7 @@ box.schema.user.grant('guest', 'execute', 'universe')
:  function f1() return box.space._func:get(1)[4] end
:  ---
:  ...
: -function f2() return box.space._func:get(68)[4] end
: +function f2() return box.space._func:get(69)[4] end
:  ---
:  ...
:  box.schema.func.create('f1')
: diff --git a/test/box/access_bin.test.lua b/test/box/access_bin.test.lua
: index 41d5f4245..954266858 100644
: --- a/test/box/access_bin.test.lua
: +++ b/test/box/access_bin.test.lua
: @@ -112,7 +112,7 @@ test:drop()
:  -- notice that guest can execute stuff, but can't read space _func
:  box.schema.user.grant('guest', 'execute', 'universe')
:  function f1() return box.space._func:get(1)[4] end
: -function f2() return box.space._func:get(68)[4] end
: +function f2() return box.space._func:get(69)[4] end
:  box.schema.func.create('f1')
:  box.schema.func.create('f2',{setuid=true})
:  c = net.connect(box.cfg.listen)
: diff --git a/test/box/access_sysview.result b/test/box/access_sysview.result
: index 6468a0709..d7a7b7534 100644
: --- a/test/box/access_sysview.result
: +++ b/test/box/access_sysview.result
: @@ -258,11 +258,11 @@ box.session.su('guest')
:  ...
:  #box.space._vpriv:select{}
:  ---
: -- 82
: +- 83
:  ...
:  #box.space._vfunc:select{}
:  ---
: -- 67
: +- 68
:  ...
:  #box.space._vcollation:select{}
:  ---
: @@ -290,11 +290,11 @@ box.session.su('guest')
:  ...
:  #box.space._vpriv:select{}
:  ---
: -- 82
: +- 83
:  ...
:  #box.space._vfunc:select{}
:  ---
: -- 67
: +- 68
:  ...
:  #box.space._vsequence:select{}
:  ---
: diff --git a/test/box/function1.result b/test/box/function1.result
: index 928cd5758..0166c828f 100644
: --- a/test/box/function1.result
: +++ b/test/box/function1.result
: @@ -97,7 +97,7 @@ box.func["function1.args"]
:    exports:
:      lua: true
:      sql: false
: -  id: 68
: +  id: 69
:    setuid: false
:    is_multikey: false
:    is_deterministic: false
: @@ -593,7 +593,7 @@ func
:    exports:
:      lua: true
:      sql: false
: -  id: 68
: +  id: 69
:    setuid: false
:    is_multikey: false
:    is_deterministic: false
: @@ -665,7 +665,7 @@ func
:    exports:
:      lua: true
:      sql: false
: -  id: 68
: +  id: 69
:    setuid: false
:    is_multikey: false
:    is_deterministic: false
: diff --git a/test/sql-tap/uuid.test.lua b/test/sql-tap/uuid.test.lua
: index 4287f0921..83fcc3d0e 100755
: --- a/test/sql-tap/uuid.test.lua
: +++ b/test/sql-tap/uuid.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(139)
: +test:plan(145)
: 
:  local uuid = require("uuid")
:  local uuid1 = uuid.fromstr("11111111-1111-1111-1111-111111111111")
: @@ -1266,6 +1266,56 @@ test:do_execsql_test(
:          3,2,1
:      })
: 
: +-- Check function uuid().
: +test:do_execsql_test(
: +    "uuid-16.1",
: +    [[
: +        SELECT typeof(uuid());
: +    ]], {
: +        "uuid"
: +    })
: +
: +test:do_execsql_test(
: +    "uuid-16.2",
: +    [[
: +        SELECT typeof(uuid(4));
: +    ]], {
: +        "uuid"
: +    })
: +
: +test:do_catchsql_test(
: +    "uuid-16.3",
: +    [[
: +        SELECT uuid(1);
: +    ]], {
: +        1, "Function UUID does not support versions other than 4"
: +    })
: +
: +test:do_catchsql_test(
: +    "uuid-16.4",
: +    [[
: +        SELECT uuid('asd');
: +    ]], {
: +        1, "Type mismatch: can not convert asd to integer"
: +    })
: +
: +test:do_catchsql_test(
: +    "uuid-16.5",
: +    [[
: +        SELECT uuid(4, 5);
: +    ]], {
: +        1, "Wrong number of arguments is passed to UUID(): expected one or
: zero, got 2"
: +    })
: +
: +-- Make sure the uuid() function generates a new UUID each time when
: called.
: +test:do_execsql_test(
: +    "uuid-16.6",
: +    [[
: +        SELECT uuid() != uuid();
: +    ]], {
: +        true
: +    })
: +
:  test:execsql([[
:      DROP TRIGGER t;
:      DROP VIEW v;
: diff --git a/test/wal_off/func_max.result b/test/wal_off/func_max.result
: index 78db38d6b..cc5bcc141 100644
: --- a/test/wal_off/func_max.result
: +++ b/test/wal_off/func_max.result
: @@ -42,11 +42,11 @@ test_run:cmd("setopt delimiter ''");
:  ...
:  func_limit()
:  ---
: -- error: 'Failed to create function ''func31934'': function id is too big'
: +- error: 'Failed to create function ''func31933'': function id is too big'
:  ...
:  drop_limit_func()
:  ---
: -- error: Function 'func31934' does not exist
: +- error: Function 'func31933' does not exist
:  ...
:  box.schema.user.create('testuser')
:  ---
: @@ -62,11 +62,11 @@ session.su('testuser')
:  ...
:  func_limit()
:  ---
: -- error: 'Failed to create function ''func31934'': function id is too big'
: +- error: 'Failed to create function ''func31933'': function id is too big'
:  ...
:  drop_limit_func()
:  ---
: -- error: Function 'func31934' does not exist
: +- error: Function 'func31933' does not exist
:  ...
:  session.su('admin')
:  ---
: --
: 2.25.1



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

* Re: [Tarantool-patches] [PATCH v2 0/2] sql: introduce UUID
  2021-06-02  8:02 [Tarantool-patches] [PATCH v2 0/2] sql: introduce UUID Mergen Imeev via Tarantool-patches
  2021-06-02  8:02 ` [Tarantool-patches] [PATCH v2 1/2] sql: introduce UUID field type Mergen Imeev via Tarantool-patches
  2021-06-02  8:02 ` [Tarantool-patches] [PATCH v2 2/2] sql: introduce SQL built-in function UUID() Mergen Imeev via Tarantool-patches
@ 2021-06-02 20:13 ` Timur Safin via Tarantool-patches
  2 siblings, 0 replies; 8+ messages in thread
From: Timur Safin via Tarantool-patches @ 2021-06-02 20:13 UTC (permalink / raw)
  To: imeevma; +Cc: tarantool-patches

Thanks for these additions since previous version I've seen - it's now much
closer to the "Consistent SQL/Lua types" RFC we have agreed upon before. 

LGTM to whole patchset (with few minor, not blocking complains here and there)

Timur

: -----Original Message-----
: From: imeevma@tarantool.org <imeevma@tarantool.org>
: Sent: Wednesday, June 2, 2021 11:03 AM
: To: tsafin@tarantool.org
: Cc: tarantool-patches@dev.tarantool.org
: Subject: [PATCH v2 0/2] sql: introduce UUID
: 
: This patch-set introduces UUID to SQL. After this patch-set UUID field type
: will
: be available in SQL and UUID values can be generated using UUID() function.
: 
: https://github.com/tarantool/tarantool/issues/5886
: https://github.com/tarantool/tarantool/tree/imeevma/gh-5886-introduce-uuid-
: type-in-sql
: 
: Changes in v2:
:  - Added SQL built-in function uuid().
: 
: Mergen Imeev (2):
:   sql: introduce UUID field type
:   sql: introduce SQL built-in function UUID()
: 
:  .../unreleased/introduce-uuid-to-sql.md       |    3 +
:  extra/mkkeywordhash.c                         |    1 +
:  src/box/bootstrap.snap                        |  Bin 5991 -> 6016 bytes
:  src/box/lua/upgrade.lua                       |   19 +
:  src/box/sql/func.c                            |   69 +-
:  src/box/sql/mem.c                             |  203 ++-
:  src/box/sql/mem.h                             |   29 +-
:  src/box/sql/parse.y                           |    3 +-
:  src/box/sql/vdbe.c                            |   15 +-
:  test/box-py/bootstrap.result                  |    3 +-
:  test/box/access.result                        |    2 +-
:  test/box/access.test.lua                      |    2 +-
:  test/box/access_bin.result                    |    2 +-
:  test/box/access_bin.test.lua                  |    2 +-
:  test/box/access_sysview.result                |    8 +-
:  test/box/function1.result                     |    6 +-
:  test/sql-tap/CMakeLists.txt                   |    1 +
:  .../gh-5913-segfault-on-select-uuid.test.lua  |   42 +-
:  .../sql-tap/gh-6024-funcs-return-bin.test.lua |    8 +-
:  test/sql-tap/sql_uuid.c                       |   46 +
:  test/sql-tap/uuid.test.lua                    | 1344 +++++++++++++++++
:  test/wal_off/func_max.result                  |    8 +-
:  22 files changed, 1731 insertions(+), 85 deletions(-)
:  create mode 100644 changelogs/unreleased/introduce-uuid-to-sql.md
:  create mode 100644 test/sql-tap/sql_uuid.c
:  create mode 100755 test/sql-tap/uuid.test.lua
: 
: --
: 2.25.1



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

* Re: [Tarantool-patches] [PATCH v2 1/2] sql: introduce UUID field type
  2021-06-02 20:13   ` Timur Safin via Tarantool-patches
@ 2021-06-03  9:04     ` Mergen Imeev via Tarantool-patches
  2021-06-03  9:40       ` Timur Safin via Tarantool-patches
  0 siblings, 1 reply; 8+ messages in thread
From: Mergen Imeev via Tarantool-patches @ 2021-06-03  9:04 UTC (permalink / raw)
  To: Timur Safin; +Cc: tarantool-patches

Hi! Thank you for the review! My answers and diff below.

On Wed, Jun 02, 2021 at 11:13:00PM +0300, Timur Safin wrote:
> There are few minor complains...
> : -----Original Message-----
> : From: imeevma@tarantool.org <imeevma@tarantool.org>
> : Sent: Wednesday, June 2, 2021 11:03 AM
> : To: tsafin@tarantool.org
> : Cc: tarantool-patches@dev.tarantool.org
> : Subject: [PATCH v2 1/2] sql: introduce UUID field type
> : 
> : This patch introduces UUID to SQL. UUID is now available as a new field
> : type.
> : 
> : Part of #5886
> : 
> : @TarantoolBot document
> : Title: Field type UUID is now available in SQL
> : 
> : The UUID field type is now available in SQL. This means that we can
> : create spaces and indexes with UUID, use it in SELECT, UPDATE and
> : DELETE. UUID can be accepted and returned by built-in functions and
> : user-defined functions.
> : 
> : According to the comparison rules, there will be no implicit casting in
> : the comparison. This rule also applies to UUID values: if a value is not
> : part of a SCALAR field, it cannot be compared to a value of any other
> : type. If the value is in a SCALAR field, it can be compared to any other
> : scalar value according to the comparison rules for a SCALAR field.
> : 
> : In case a UUID value is used in an operation that is not a comparison,
> : it can be implicitly converted to STRING or VARBINARY.
> : 
> : If a STRING or VARBINARY value is used in an operation that is not a
> : comparison, it can be implicitly converted to a UUID.
> : 
> : UUID value can always be explicitly converted to STRING or VARBINARY.
> : 
> : A STRING or VARBINARY value can be explicitly converted to a UUID if it
> : conforms to the UUID standard.
> : ---
> :  extra/mkkeywordhash.c                         |    1 +
> :  src/box/sql/func.c                            |   30 +-
> :  src/box/sql/mem.c                             |  203 ++-
> :  src/box/sql/mem.h                             |   29 +-
> :  src/box/sql/parse.y                           |    1 +
> :  src/box/sql/vdbe.c                            |   15 +-
> :  test/sql-tap/CMakeLists.txt                   |    1 +
> :  .../gh-5913-segfault-on-select-uuid.test.lua  |   42 +-
> :  .../sql-tap/gh-6024-funcs-return-bin.test.lua |    8 +-
> :  test/sql-tap/sql_uuid.c                       |   46 +
> :  test/sql-tap/uuid.test.lua                    | 1294 +++++++++++++++++
> :  11 files changed, 1602 insertions(+), 68 deletions(-)
> :  create mode 100644 test/sql-tap/sql_uuid.c
> :  create mode 100755 test/sql-tap/uuid.test.lua
> : 
> : diff --git a/extra/mkkeywordhash.c b/extra/mkkeywordhash.c
> : index 7480c0211..0d998506c 100644
> : --- a/extra/mkkeywordhash.c
> : +++ b/extra/mkkeywordhash.c
> : @@ -172,6 +172,7 @@ static Keyword aKeywordTable[] = {
> :    { "UNSIGNED",               "TK_UNSIGNED",    true  },
> :    { "UPDATE",                 "TK_UPDATE",      true  },
> :    { "USING",                  "TK_USING",       true  },
> : +  { "UUID" ,                  "TK_UUID" ,       true  },
> :    { "VALUES",                 "TK_VALUES",      true  },
> :    { "VARBINARY",              "TK_VARBINARY",   true  },
> :    { "VIEW",                   "TK_VIEW",        true  },
> : diff --git a/src/box/sql/func.c b/src/box/sql/func.c
> : index 90e8e152f..9c4480a92 100644
> : --- a/src/box/sql/func.c
> : +++ b/src/box/sql/func.c
> : @@ -58,7 +58,8 @@ static const void *
> :  mem_as_bin(struct Mem *mem)
> :  {
> :  	const char *s;
> : -	if (!mem_is_bytes(mem) && mem_to_str(mem) != 0)
> : +	if (mem_cast_implicit(mem, FIELD_TYPE_VARBINARY) != 0 &&
> : +	    mem_to_str(mem) != 0)
> :  		return NULL;
> :  	if (mem_get_bin(mem, &s) != 0)
> :  		return NULL;
> : @@ -142,26 +143,29 @@ typeofFunc(sql_context * context, int NotUsed,
> : sql_value ** argv)
> :  				-1, SQL_STATIC);
> :  		return;
> :  	}
> : -	switch (sql_value_type(argv[0])) {
> : -	case MP_INT:
> : -	case MP_UINT:
> : +	switch (argv[0]->type) {
> : +	case MEM_TYPE_INT:
> : +	case MEM_TYPE_UINT:
> :  		z = "integer";
> :  		break;
> : -	case MP_STR:
> : +	case MEM_TYPE_STR:
> :  		z = "string";
> :  		break;
> : -	case MP_DOUBLE:
> : +	case MEM_TYPE_DOUBLE:
> :  		z = "double";
> :  		break;
> : -	case MP_BIN:
> : -	case MP_ARRAY:
> : -	case MP_MAP:
> : +	case MEM_TYPE_BIN:
> : +	case MEM_TYPE_ARRAY:
> : +	case MEM_TYPE_MAP:
> :  		z = "varbinary";
> :  		break;
> : -	case MP_BOOL:
> : -	case MP_NIL:
> : +	case MEM_TYPE_BOOL:
> : +	case MEM_TYPE_NULL:
> :  		z = "boolean";
> :  		break;
> : +	case MEM_TYPE_UUID:
> : +		z = "uuid";
> : +		break;
> :  	default:
> :  		unreachable();
> :  		break;
> : @@ -191,6 +195,7 @@ lengthFunc(sql_context * context, int argc, sql_value **
> : argv)
> :  			sql_result_uint(context, mem_len_unsafe(argv[0]));
> :  			break;
> :  		}
> : +	case MP_EXT:
> :  	case MP_STR:{
> :  			const unsigned char *z = mem_as_ustr(argv[0]);
> :  			if (z == 0)
> : @@ -235,6 +240,7 @@ absFunc(sql_context * context, int argc, sql_value **
> : argv)
> :  		}
> :  	case MP_BOOL:
> :  	case MP_BIN:
> : +	case MP_EXT:
> :  	case MP_ARRAY:
> :  	case MP_MAP: {
> :  		diag_set(ClientError, ER_INCONSISTENT_TYPES, "number",
> : @@ -1461,8 +1467,8 @@ trim_func_one_arg(struct sql_context *context,
> : sql_value *arg)
> :  		default_trim = (const unsigned char *) "\0";
> :  	else
> :  		default_trim = (const unsigned char *) " ";
> : -	int input_str_sz = mem_len_unsafe(arg);
> :  	const unsigned char *input_str = mem_as_ustr(arg);
> : +	int input_str_sz = mem_len_unsafe(arg);
> :  	uint8_t trim_char_len[1] = { 1 };
> :  	trim_procedure(context, TRIM_BOTH, default_trim, trim_char_len, 1,
> :  		       input_str, input_str_sz);
> : diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
> : index 9894c09af..f0dfce395 100644
> : --- a/src/box/sql/mem.c
> : +++ b/src/box/sql/mem.c
> : @@ -58,6 +58,16 @@ enum {
> :  	BUF_SIZE = 32,
> :  };
> : 
> : +bool
> : +mem_is_field_compatible(const struct Mem *mem, enum field_type type)
> : +{
> : +	if (mem->type == MEM_TYPE_UUID)
> : +		return (field_ext_type[type] & (1U << MP_UUID)) != 0;
> : +	enum mp_type mp_type = mem_mp_type(mem);
> : +	assert(mp_type != MP_EXT);
> : +	return field_mp_plain_type_is_compatible(type, mp_type, true);
> : +}
> : +
> :  const char *
> :  mem_str(const struct Mem *mem)
> :  {
> : @@ -81,6 +91,8 @@ mem_str(const struct Mem *mem)
> :  	case MEM_TYPE_MAP:
> :  	case MEM_TYPE_ARRAY:
> :  		return mp_str(mem->z);
> : +	case MEM_TYPE_UUID:
> : +		return tt_uuid_str(&mem->u.uuid);
> :  	case MEM_TYPE_BOOL:
> :  		return mem->u.b ? "TRUE" : "FALSE";
> :  	default:
> : @@ -190,6 +202,16 @@ mem_set_double(struct Mem *mem, double value)
> :  	mem->type = MEM_TYPE_DOUBLE;
> :  }
> : 
> : +void
> : +mem_set_uuid(struct Mem *mem, const struct tt_uuid *uuid)
> : +{
> : +	mem_clear(mem);
> : +	mem->field_type = FIELD_TYPE_UUID;
> : +	mem->u.uuid = *uuid;
> : +	mem->type = MEM_TYPE_UUID;
> : +	assert(mem->flags == 0);
> 
> What's the point of this assertion? It looks redundant, because flags 
> supposed to be nulled at the end of mem_clear()?
> (Not critical problem, but simply confusing)
> 
For me it is something that can be used as comment that says that flags should
be 0, also it checks flag in debug.

> 
> : +}
> : +
> :  static inline void
> :  set_str_const(struct Mem *mem, char *value, uint32_t len, int alloc_type)
> :  {
> : @@ -585,6 +607,18 @@ str_to_bin(struct Mem *mem)
> :  	return 0;
> :  }
> : 
> : +static inline int
> : +str_to_uuid(struct Mem *mem)
> : +{
> : +	assert(mem->type == MEM_TYPE_STR);
> : +	struct tt_uuid uuid;
> : +	if (mem->n != UUID_STR_LEN ||
> : +	    tt_uuid_from_string(tt_cstr(mem->z, mem->n), &uuid) != 0)
> 
> Eventually, when we would be able to parse more relaxed formats in tt_uuid_from_string()
> (e.g. with dashes omitted or braces around uuid) we would have to remove this check for 
> particular length UUID_STR_LEN, offloading any correctness check entirely to the function.
> I'm not insisting that this check should be modified right away. Though, from future 
> Compatible point of view it would be ideal... But not required. Just saying.
> 
> BTW, while we are here - could we avoid this static_alloc() call in tt_cstr() as a 
> prerequisite to tt_uuid_from_string(), and pass view of this data with mem->z, mem->n
> directly there?
> 
Actually, I can and I did. Fixed. Diff below.

> : +		return -1;
> : +	mem_set_uuid(mem, &uuid);
> : +	return 0;
> : +}
> : +
> :  static inline int
> :  str_to_bool(struct Mem *mem)
> :  {
> : @@ -639,6 +673,19 @@ bin_to_str0(struct Mem *mem)
> :  	return 0;
> :  }
> : 
> : +static inline int
> : +bin_to_uuid(struct Mem *mem)
> : +{
> : +	assert(mem->type == MEM_TYPE_BIN);
> : +	if (ExpandBlob(mem) != 0)
> : +		return -1;
> : +	if (mem->n != UUID_LEN ||
> : +	    tt_uuid_validate((struct tt_uuid *)mem->z) != 0)
> : +		return -1;
> : +	mem_set_uuid(mem, (struct tt_uuid *)mem->z);
> : +	return 0;
> : +}
> : +
> :  static inline int
> :  bytes_to_int(struct Mem *mem)
> :  {
> : @@ -810,6 +857,22 @@ map_to_str0(struct Mem *mem)
> :  	return mem_copy_str0(mem, str);
> :  }
> : 
> : +static inline int
> : +uuid_to_str0(struct Mem *mem)
> : +{
> : +	assert(mem->type == MEM_TYPE_UUID);
> : +	char buf[UUID_STR_LEN + 1];
> : +	tt_uuid_to_string(&mem->u.uuid, &buf[0]);
> : +	return mem_copy_str0(mem, &buf[0]);
> : +}
> : +
> : +static inline int
> : +uuid_to_bin(struct Mem *mem)
> : +{
> : +	assert(mem->type == MEM_TYPE_UUID);
> : +	return mem_copy_bin(mem, (char *)&mem->u.uuid, UUID_LEN);
> : +}
> : +
> :  int
> :  mem_to_int(struct Mem *mem)
> :  {
> : @@ -889,6 +952,8 @@ mem_to_str0(struct Mem *mem)
> :  		return map_to_str0(mem);
> :  	case MEM_TYPE_ARRAY:
> :  		return array_to_str0(mem);
> : +	case MEM_TYPE_UUID:
> : +		return uuid_to_str0(mem);
> :  	default:
> :  		return -1;
> :  	}
> : @@ -914,6 +979,8 @@ mem_to_str(struct Mem *mem)
> :  		return map_to_str0(mem);
> :  	case MEM_TYPE_ARRAY:
> :  		return array_to_str0(mem);
> : +	case MEM_TYPE_UUID:
> : +		return uuid_to_str0(mem);
> :  	default:
> :  		return -1;
> :  	}
> : @@ -966,9 +1033,19 @@ mem_cast_explicit(struct Mem *mem, enum field_type
> : type)
> :  			return str_to_bin(mem);
> :  		if (mem_is_bytes(mem))
> :  			return 0;
> : +		if (mem->type == MEM_TYPE_UUID)
> : +			return uuid_to_bin(mem);
> :  		return -1;
> :  	case FIELD_TYPE_NUMBER:
> :  		return mem_to_number(mem);
> : +	case FIELD_TYPE_UUID:
> : +		if (mem->type == MEM_TYPE_UUID)
> : +			return 0;
> : +		if (mem->type == MEM_TYPE_STR)
> : +			return str_to_uuid(mem);
> : +		if (mem->type == MEM_TYPE_BIN)
> : +			return bin_to_uuid(mem);
> : +		return -1;
> :  	case FIELD_TYPE_SCALAR:
> :  		if ((mem->type & (MEM_TYPE_MAP | MEM_TYPE_ARRAY)) != 0)
> :  			return -1;
> : @@ -996,6 +1073,8 @@ mem_cast_implicit(struct Mem *mem, enum field_type
> : type)
> :  	case FIELD_TYPE_STRING:
> :  		if (mem->type == MEM_TYPE_STR)
> :  			return 0;
> : +		if (mem->type == MEM_TYPE_UUID)
> : +			return uuid_to_str0(mem);
> :  		return -1;
> :  	case FIELD_TYPE_DOUBLE:
> :  		if (mem->type == MEM_TYPE_DOUBLE)
> : @@ -1017,6 +1096,8 @@ mem_cast_implicit(struct Mem *mem, enum field_type
> : type)
> :  		if ((mem->type & (MEM_TYPE_BIN | MEM_TYPE_MAP |
> :  				  MEM_TYPE_ARRAY)) != 0)
> :  			return 0;
> : +		if (mem->type == MEM_TYPE_UUID)
> : +			return uuid_to_bin(mem);
> :  		return -1;
> :  	case FIELD_TYPE_NUMBER:
> :  		if (mem_is_num(mem))
> : @@ -1034,6 +1115,14 @@ mem_cast_implicit(struct Mem *mem, enum field_type
> : type)
> :  		if ((mem->type & (MEM_TYPE_MAP | MEM_TYPE_ARRAY)) != 0)
> :  			return -1;
> :  		return 0;
> : +	case FIELD_TYPE_UUID:
> : +		if (mem->type == MEM_TYPE_UUID)
> : +			return 0;
> : +		if (mem->type == MEM_TYPE_STR)
> : +			return str_to_uuid(mem);
> : +		if (mem->type == MEM_TYPE_BIN)
> : +			return bin_to_uuid(mem);
> : +		return -1;
> :  	case FIELD_TYPE_ANY:
> :  		return 0;
> :  	default:
> : @@ -1063,6 +1152,8 @@ mem_cast_implicit_old(struct Mem *mem, enum field_type
> : type)
> :  			return int_to_str0(mem);
> :  		if (mem->type == MEM_TYPE_DOUBLE)
> :  			return double_to_str0(mem);
> : +		if (mem->type == MEM_TYPE_UUID)
> : +			return uuid_to_str0(mem);
> :  		return -1;
> :  	case FIELD_TYPE_DOUBLE:
> :  		if (mem->type == MEM_TYPE_DOUBLE)
> : @@ -1087,6 +1178,8 @@ mem_cast_implicit_old(struct Mem *mem, enum field_type
> : type)
> :  	case FIELD_TYPE_VARBINARY:
> :  		if (mem->type == MEM_TYPE_BIN)
> :  			return 0;
> : +		if (mem->type == MEM_TYPE_UUID)
> : +			return uuid_to_bin(mem);
> :  		return -1;
> :  	case FIELD_TYPE_NUMBER:
> :  		if (mem_is_num(mem))
> : @@ -1106,6 +1199,14 @@ mem_cast_implicit_old(struct Mem *mem, enum
> : field_type type)
> :  		if ((mem->type & (MEM_TYPE_MAP | MEM_TYPE_ARRAY)) != 0)
> :  			return -1;
> :  		return 0;
> : +	case FIELD_TYPE_UUID:
> : +		if (mem->type == MEM_TYPE_UUID)
> : +			return 0;
> : +		if (mem->type == MEM_TYPE_STR)
> : +			return str_to_uuid(mem);
> : +		if (mem->type == MEM_TYPE_BIN)
> : +			return bin_to_uuid(mem);
> : +		return -1;
> :  	default:
> :  		break;
> :  	}
> 
> Uh-oh, those changes in mem_cast_implicit_old() will conflict with my
> cleanup here.But I'll handle it.
> (OTOH, could we not touch mem_cast_implicit_old as it will be deleted soon?)
> 
I believe we can't. It is easier to remove function during merge than to fix
possible inconsistency.

> : @@ -1899,6 +2000,15 @@ mem_cmp_str(const struct Mem *left, const struct Mem
> : *right, int *result,
> :  	return 0;
> :  }
> : 
> : +int
> : +mem_cmp_uuid(const struct Mem *a, const struct Mem *b, int *result)
> : +{
> : +	if ((a->type & b->type & MEM_TYPE_UUID) == 0)
> : +		return -1;
> : +	*result = memcmp(&a->u.uuid, &b->u.uuid, UUID_LEN);
> : +	return 0;
> : +}
> : +
> :  /*
> :   * Both *pMem1 and *pMem2 contain string values. Compare the two values
> :   * using the collation sequence pColl. As usual, return a negative , zero
> : @@ -1951,13 +2061,15 @@ mem_type_to_str(const struct Mem *p)
> :  		return "varbinary";
> :  	case MEM_TYPE_BOOL:
> :  		return "boolean";
> : +	case MEM_TYPE_UUID:
> : +		return "uuid";
> :  	default:
> :  		unreachable();
> :  	}
> :  }
> : 
> :  enum mp_type
> : -mem_mp_type(struct Mem *mem)
> : +mem_mp_type(const struct Mem *mem)
> :  {
> :  	assert(mem->type < MEM_TYPE_INVALID);
> :  	switch (mem->type) {
> : @@ -1979,6 +2091,8 @@ mem_mp_type(struct Mem *mem)
> :  		return MP_BOOL;
> :  	case MEM_TYPE_DOUBLE:
> :  		return MP_DOUBLE;
> : +	case MEM_TYPE_UUID:
> : +		return MP_EXT;
> :  	default:
> :  		unreachable();
> :  	}
> : @@ -2144,6 +2258,9 @@ memTracePrint(Mem *p)
> :  	case MEM_TYPE_BOOL:
> :  		printf(" bool:%s", SQL_TOKEN_BOOLEAN(p->u.b));
> :  		return;
> : +	case MEM_TYPE_UUID:
> : +		printf(" uuid:%s", tt_uuid_str(&p->u.uuid));
> : +		return;
> :  	default: {
> :  		char zBuf[200];
> :  		sqlVdbeMemPrettyPrint(p, zBuf);
> : @@ -2360,6 +2477,14 @@ sqlMemCompare(const Mem * pMem1, const Mem * pMem2,
> : const struct coll * pColl)
> :  		return -1;
> :  	}
> : 
> : +	if (((type1 | type2) & MEM_TYPE_UUID) != 0) {
> : +		if (mem_cmp_uuid(pMem1, pMem2, &res) == 0)
> : +			return res;
> : +		if (type1 != MEM_TYPE_UUID)
> : +			return +1;
> : +		return -1;
> : +	}
> : +
> :  	/* At least one of the two values is a number
> :  	 */
> :  	if (((type1 | type2) &
> : @@ -2565,13 +2690,30 @@ sqlVdbeCompareMsgpack(const char **key1,
> :  			break;
> :  		}
> :  	case MP_ARRAY:
> : -	case MP_MAP:
> : -	case MP_EXT:{
> : +	case MP_MAP: {
> :  			mem1.z = (char *)aKey1;
> :  			mp_next(&aKey1);
> :  			mem1.n = aKey1 - (char *)mem1.z;
> :  			goto do_blob;
> :  		}
> : +	case MP_EXT: {
> : +		int8_t type;
> : +		const char *buf = aKey1;
> : +		uint32_t len = mp_decode_extl(&aKey1, &type);
> : +		if (type == MP_UUID) {
> : +			assert(len == UUID_LEN);
> : +			mem1.type = MEM_TYPE_UUID;
> : +			aKey1 = buf;
> : +			if (mp_decode_uuid(&aKey1, &mem1.u.uuid) == NULL ||
> : +			    mem_cmp_uuid(&mem1, pKey2, &rc) != 0)
> : +				rc = 1;
> : +			break;
> : +		}
> : +		aKey1 += len;
> : +		mem1.z = (char *)buf;
> : +		mem1.n = aKey1 - buf;
> : +		goto do_blob;
> : +	}
> :  	}
> :  	*key1 = aKey1;
> :  	return rc;
> : @@ -2625,9 +2767,25 @@ mem_from_mp_ephemeral(struct Mem *mem, const char
> : *buf, uint32_t *len)
> :  		break;
> :  	}
> :  	case MP_EXT: {
> : -		mem->z = (char *)buf;
> : -		mp_next(&buf);
> : -		mem->n = buf - mem->z;
> : +		int8_t type;
> : +		const char *svp = buf;
> : +		uint32_t size = mp_decode_extl(&buf, &type);
> : +		if (type == MP_UUID) {
> : +			assert(size == UUID_LEN);
> : +			buf = svp;
> : +			if (mp_decode_uuid(&buf, &mem->u.uuid) == NULL) {
> : +				diag_set(ClientError, ER_INVALID_MSGPACK,
> : +					 "Invalid MP_UUID MsgPack format");
> : +				return -1;
> : +			}
> : +			mem->type = MEM_TYPE_UUID;
> : +			mem->flags = 0;
> : +			mem->field_type = FIELD_TYPE_UUID;
> : +			break;
> : +		}
> : +		buf += size;
> : +		mem->z = (char *)svp;
> : +		mem->n = buf - svp;
> :  		mem->type = MEM_TYPE_BIN;
> :  		mem->flags = MEM_Ephem;
> :  		mem->field_type = FIELD_TYPE_VARBINARY;
> : @@ -2764,6 +2922,9 @@ mpstream_encode_vdbe_mem(struct mpstream *stream,
> : struct Mem *var)
> :  	case MEM_TYPE_BOOL:
> :  		mpstream_encode_bool(stream, var->u.b);
> :  		return;
> : +	case MEM_TYPE_UUID:
> : +		mpstream_encode_uuid(stream, &var->u.uuid);
> : +		return;
> :  	default:
> :  		unreachable();
> :  	}
> : @@ -2850,6 +3011,9 @@ port_vdbemem_dump_lua(struct port *base, struct
> : lua_State *L, bool is_flat)
> :  		case MEM_TYPE_BOOL:
> :  			lua_pushboolean(L, mem->u.b);
> :  			break;
> : +		case MEM_TYPE_UUID:
> : +			*luaL_pushuuid(L) = mem->u.uuid;
> : +			break;
> :  		default:
> :  			unreachable();
> :  		}
> : @@ -2976,14 +3140,8 @@ port_lua_get_vdbemem(struct port *base, uint32_t
> : *size)
> :  			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);
> : +				mem_set_uuid(&val[i], field.uuidval);
> : +				break;
> :  			} else {
> :  				size = mp_sizeof_decimal(field.decval);
> :  				buf = region_alloc(&fiber()->gc, size);
> : @@ -3087,7 +3245,22 @@ port_c_get_vdbemem(struct port *base, uint32_t *size)
> :  			break;
> :  		case MP_EXT:
> :  			str = data;
> : -			mp_next(&data);
> : +			int8_t type;
> : +			len = mp_decode_extl(&data, &type);
> : +			if (type == MP_UUID) {
> : +				assert(len == UUID_LEN);
> : +				struct tt_uuid *uuid = &val[i].u.uuid;
> : +				data = str;
> : +				if (mp_decode_uuid(&data, uuid) == NULL) {
> : +					diag_set(ClientError,
> : +						 ER_INVALID_MSGPACK, "Invalid "
> : +						 "MP_UUID MsgPack format");
> : +					goto error;
> : +				}
> : +				val[i].type = MEM_TYPE_UUID;
> : +				break;
> : +			}
> : +			data += len;
> :  			if (mem_copy_bin(&val[i], str, data - str) != 0)
> :  				goto error;
> :  			break;
> : diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
> : index 15d97da0e..b3cd5c545 100644
> : --- a/src/box/sql/mem.h
> : +++ b/src/box/sql/mem.h
> : @@ -30,6 +30,7 @@
> :   * SUCH DAMAGE.
> :   */
> :  #include "box/field_def.h"
> : +#include "uuid/tt_uuid.h"
> : 
> :  struct sql;
> :  struct Vdbe;
> : @@ -47,10 +48,11 @@ enum mem_type {
> :  	MEM_TYPE_MAP		= 1 << 6,
> :  	MEM_TYPE_BOOL		= 1 << 7,
> :  	MEM_TYPE_DOUBLE		= 1 << 8,
> : -	MEM_TYPE_INVALID	= 1 << 9,
> : -	MEM_TYPE_FRAME		= 1 << 10,
> : -	MEM_TYPE_PTR		= 1 << 11,
> : -	MEM_TYPE_AGG		= 1 << 12,
> : +	MEM_TYPE_UUID		= 1 << 9,
> : +	MEM_TYPE_INVALID	= 1 << 10,
> : +	MEM_TYPE_FRAME		= 1 << 11,
> : +	MEM_TYPE_PTR		= 1 << 12,
> : +	MEM_TYPE_AGG		= 1 << 13,
> :  };
> : 
> :  /*
> : @@ -72,6 +74,7 @@ struct Mem {
> :  		 */
> :  		struct func *func;
> :  		struct VdbeFrame *pFrame;	/* Used when flags==MEM_Frame */
> : +		struct tt_uuid uuid;
> :  	} u;
> :  	/** Type of the value this MEM contains. */
> :  	enum mem_type type;
> : @@ -255,6 +258,10 @@ mem_is_any_null(const struct Mem *mem1, const struct
> : Mem *mem2)
> :  	return ((mem1->type| mem2->type) & MEM_TYPE_NULL) != 0;
> :  }
> : 
> : +/** Check if MEM is compatible with field type. */
> : +bool
> : +mem_is_field_compatible(const struct Mem *mem, enum field_type type);
> : +
> :  /**
> :   * Return a string that represent content of MEM. String is either
> : allocated
> :   * using static_alloc() of just a static variable.
> : @@ -290,6 +297,10 @@ mem_set_bool(struct Mem *mem, bool value);
> :  void
> :  mem_set_double(struct Mem *mem, double value);
> : 
> : +/** Clear MEM and set it to UUID. */
> : +void
> : +mem_set_uuid(struct Mem *mem, const struct tt_uuid *uuid);
> : +
> :  /** Clear MEM and set it to STRING. The string belongs to another object.
> : */
> :  void
> :  mem_set_str_ephemeral(struct Mem *mem, char *value, uint32_t len);
> : @@ -677,6 +688,14 @@ mem_cmp_str(const struct Mem *left, const struct Mem
> : *right, int *result,
> :  int
> :  mem_cmp_num(const struct Mem *a, const struct Mem *b, int *result);
> : 
> : +/**
> : + * Compare two MEMs and return the result of comparison. MEMs should be of
> : + * UUID type or their values are converted to UUID according to
> : + * implicit cast rules. Original MEMs are not changed.
> : + */
> : +int
> : +mem_cmp_uuid(const struct Mem *left, const struct Mem *right, int *result);
> : +
> :  /**
> :   * Convert the given MEM to INTEGER. This function and the function below
> : define
> :   * the rules that are used to convert values of all other types to INTEGER.
> : In
> : @@ -898,7 +917,7 @@ mem_type_to_str(const struct Mem *p);
> :   * transparent memory cell.
> :   */
> :  enum mp_type
> : -mem_mp_type(struct Mem *mem);
> : +mem_mp_type(const struct Mem *mem);
> : 
> :  enum mp_type
> :  sql_value_type(struct Mem *);
> : diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y
> : index abc363951..4c9cf475e 100644
> : --- a/src/box/sql/parse.y
> : +++ b/src/box/sql/parse.y
> : @@ -1834,6 +1834,7 @@ typedef(A) ::= SCALAR . { A.type = FIELD_TYPE_SCALAR;
> : }
> :  typedef(A) ::= BOOL . { A.type = FIELD_TYPE_BOOLEAN; }
> :  typedef(A) ::= BOOLEAN . { A.type = FIELD_TYPE_BOOLEAN; }
> :  typedef(A) ::= VARBINARY . { A.type = FIELD_TYPE_VARBINARY; }
> : +typedef(A) ::= UUID . { A.type = FIELD_TYPE_UUID; }
> : 
> :  /**
> :   * Time-like types are temporary disabled, until they are
> : diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
> : index 12ec703a2..32d02d96e 100644
> : --- a/src/box/sql/vdbe.c
> : +++ b/src/box/sql/vdbe.c
> : @@ -1322,10 +1322,10 @@ case OP_FunctionByName: {
> :  	region_truncate(region, region_svp);
> :  	if (mem == NULL)
> :  		goto abort_due_to_error;
> : -	enum mp_type type = sql_value_type((sql_value *)pOut);
> : -	if (!field_mp_plain_type_is_compatible(returns, type, true)) {
> : +	if (!mem_is_field_compatible(pOut, returns)) {
> :  		diag_set(ClientError, ER_FUNC_INVALID_RETURN_TYPE, pOp->p4.z,
> : -			 field_type_strs[returns], mp_type_strs[type]);
> : +			 field_type_strs[returns],
> : +			 mp_type_strs[mem_mp_type(pOut)]);
> :  		goto abort_due_to_error;
> :  	}
> : 
> : @@ -1634,6 +1634,15 @@ case OP_Ge: {             /* same as TK_GE, jump,
> : in1, in3 */
> :  				 "boolean");
> :  			goto abort_due_to_error;
> :  		}
> : +	} else if (((pIn3->type | pIn1->type) & MEM_TYPE_UUID) != 0) {
> : +		if (mem_cmp_uuid(pIn3, pIn1, &res) != 0) {
> : +			char *str = pIn3->type != MEM_TYPE_UUID ?
> : +				    mem_type_to_str(pIn3) :
> : +				    mem_type_to_str(pIn1);
> : +			diag_set(ClientError, ER_SQL_TYPE_MISMATCH, str,
> : +				 "uuid");
> : +			goto abort_due_to_error;
> : +		}
> :  	} else if (mem_is_bin(pIn3) || mem_is_bin(pIn1)) {
> :  		if (mem_cmp_bin(pIn3, pIn1, &res) != 0) {
> :  			char *str = !mem_is_bin(pIn3) ?
> 
> In general, despite minor complains, it all looks good to me, and ok to commit.
> Those complains we will address later.
> 
> LGTM
> 
> Timur
> 

Diff:


diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index f0dfce395..6f3bf52e5 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -612,8 +612,7 @@ str_to_uuid(struct Mem *mem)
 {
 	assert(mem->type == MEM_TYPE_STR);
 	struct tt_uuid uuid;
-	if (mem->n != UUID_STR_LEN ||
-	    tt_uuid_from_string(tt_cstr(mem->z, mem->n), &uuid) != 0)
+	if (tt_uuid_from_strl(mem->z, mem->n, &uuid) != 0)
 		return -1;
 	mem_set_uuid(mem, &uuid);
 	return 0;

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

* Re: [Tarantool-patches] [PATCH v2 1/2] sql: introduce UUID field type
  2021-06-03  9:04     ` Mergen Imeev via Tarantool-patches
@ 2021-06-03  9:40       ` Timur Safin via Tarantool-patches
  0 siblings, 0 replies; 8+ messages in thread
From: Timur Safin via Tarantool-patches @ 2021-06-03  9:40 UTC (permalink / raw)
  To: 'Mergen Imeev'; +Cc: tarantool-patches

: From: Mergen Imeev <imeevma@tarantool.org>
: Sent: Thursday, June 3, 2021 12:05 PM
: To: Timur Safin <tsafin@tarantool.org>
: Cc: tarantool-patches@dev.tarantool.org
: Subject: Re: [PATCH v2 1/2] sql: introduce UUID field type
: 
: Hi! Thank you for the review! My answers and diff below.
: 
...
: 
: Diff:
: 
: 
: diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
: index f0dfce395..6f3bf52e5 100644
: --- a/src/box/sql/mem.c
: +++ b/src/box/sql/mem.c
: @@ -612,8 +612,7 @@ str_to_uuid(struct Mem *mem)
:  {
:  	assert(mem->type == MEM_TYPE_STR);
:  	struct tt_uuid uuid;
: -	if (mem->n != UUID_STR_LEN ||
: -	    tt_uuid_from_string(tt_cstr(mem->z, mem->n), &uuid) != 0)
: +	if (tt_uuid_from_strl(mem->z, mem->n, &uuid) != 0)
:  		return -1;
:  	mem_set_uuid(mem, &uuid);
:  	return 0;

Did not know we do have tt_uuid_from_strl() which operates
with view. That's good! And although it's still using unnecessary
buffer copy (implemented in very naïve manner) it's still a good 
abstraction here, and we could avoid calls to static_alloc() :)


LGTM

Timur


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

end of thread, other threads:[~2021-06-03  9:41 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-02  8:02 [Tarantool-patches] [PATCH v2 0/2] sql: introduce UUID Mergen Imeev via Tarantool-patches
2021-06-02  8:02 ` [Tarantool-patches] [PATCH v2 1/2] sql: introduce UUID field type Mergen Imeev via Tarantool-patches
2021-06-02 20:13   ` Timur Safin via Tarantool-patches
2021-06-03  9:04     ` Mergen Imeev via Tarantool-patches
2021-06-03  9:40       ` Timur Safin via Tarantool-patches
2021-06-02  8:02 ` [Tarantool-patches] [PATCH v2 2/2] sql: introduce SQL built-in function UUID() Mergen Imeev via Tarantool-patches
2021-06-02 20:13   ` Timur Safin via Tarantool-patches
2021-06-02 20:13 ` [Tarantool-patches] [PATCH v2 0/2] sql: introduce UUID Timur Safin 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