[Tarantool-patches] [PATCH v1 2/7] sql: re-introduce NUMBER and SCALAR meta-types

imeevma at tarantool.org imeevma at tarantool.org
Wed Aug 11 19:01:42 MSK 2021


This patch re-introduces the NUMBER and SCALAR meta-types. They were
introduced some time ago, however there were some problems with how they
worked and some properties of these meta-types were not implemented.

New properties of meta-type SCALAR in SQL:
1) typeof() with SCALAR argument returns "scalar";
2) Any value of any scalar type can be implicitly cast to SCALAR;
3) SCALAR values can be compared with any value of any other scalar
   type;
4) SCALAR values cannot be implicitly cast to any other scalar type;
5) SCALAR values cannot participate in arithmetic or bitwise
   operations;
6) SCALAR values cannot be concatenated with other values.

New properties of meta-type NUMBER in SQL:
1) typeof() with NUMBER argument returns "number";
2) Any value of a numeric type can be implicitly cast to NUMBER;
3) NUMBER values can be implicitly cast to SCALAR;
4) NUMBER cannot be implicitly cast to any other numeric type;
5) NUMBER values cannot participate in arithmetic or bitwise
   operations;

This patch only applies 1) and 2) for both meta-types. All other
properties will be presented in the next patches.

Part of #6221
---
 src/box/sql/func.c                           |   9 +-
 src/box/sql/mem.c                            | 135 +++++++++++++------
 src/box/sql/mem.h                            |   4 +
 src/box/sql/vdbe.c                           |   8 ++
 test/sql-tap/cast.test.lua                   |  14 +-
 test/sql-tap/default.test.lua                |   2 +-
 test/sql-tap/engine.cfg                      |   3 +
 test/sql-tap/in4.test.lua                    |   2 +-
 test/sql-tap/metatypes.test.lua              |  51 +++++++
 test/sql-tap/select7.test.lua                |   4 +-
 test/sql-tap/tkt-3998683a16.test.lua         |   1 +
 test/sql-tap/uuid.test.lua                   |   2 +-
 test/sql/boolean.result                      |  84 ++++++------
 test/sql/gh-4697-scalar-bool-sort-cmp.result |  12 +-
 test/sql/types.result                        |   8 +-
 15 files changed, 230 insertions(+), 109 deletions(-)
 create mode 100755 test/sql-tap/metatypes.test.lua

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 5ac5c5e56..ebb546a7f 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -162,6 +162,12 @@ typeofFunc(sql_context * context, int NotUsed, sql_value ** argv)
 {
 	const char *z = 0;
 	UNUSED_PARAMETER(NotUsed);
+	if (argv[0]->type == MEM_TYPE_NULL)
+		return mem_set_str0_static(context->pOut, "NULL");
+	if ((argv[0]->flags & MEM_Number) != 0)
+		return mem_set_str0_static(context->pOut, "number");
+	if ((argv[0]->flags & MEM_Scalar) != 0)
+		return mem_set_str0_static(context->pOut, "scalar");
 	switch (argv[0]->type) {
 	case MEM_TYPE_INT:
 	case MEM_TYPE_UINT:
@@ -181,9 +187,6 @@ typeofFunc(sql_context * context, int NotUsed, sql_value ** argv)
 	case MEM_TYPE_BOOL:
 		z = "boolean";
 		break;
-	case MEM_TYPE_NULL:
-		z = "NULL";
-		break;
 	case MEM_TYPE_UUID:
 		z = "uuid";
 		break;
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 773ef4d40..41aa40fdb 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -116,21 +116,22 @@ const char *
 mem_str(const struct Mem *mem)
 {
 	char buf[STR_VALUE_MAX_LEN];
+	const char *type = mem_type_to_str(mem);
 	switch (mem->type) {
 	case MEM_TYPE_NULL:
 		return "NULL";
 	case MEM_TYPE_STR:
 		if (mem->n <= STR_VALUE_MAX_LEN)
-			return tt_sprintf("string('%.*s')", mem->n, mem->z);
-		return tt_sprintf("string('%.*s...)", STR_VALUE_MAX_LEN,
+			return tt_sprintf("%s('%.*s')", type, mem->n, mem->z);
+		return tt_sprintf("%s('%.*s...)", type, STR_VALUE_MAX_LEN,
 				  mem->z);
 	case MEM_TYPE_INT:
-		return tt_sprintf("integer(%lld)", mem->u.i);
+		return tt_sprintf("%s(%lld)", type, mem->u.i);
 	case MEM_TYPE_UINT:
-		return tt_sprintf("integer(%llu)", mem->u.u);
+		return tt_sprintf("%s(%llu)", type, mem->u.u);
 	case MEM_TYPE_DOUBLE:
 		sql_snprintf(STR_VALUE_MAX_LEN, buf, "%!.15g", mem->u.r);
-		return tt_sprintf("double(%s)", buf);
+		return tt_sprintf("%s(%s)", type, buf);
 	case MEM_TYPE_BIN: {
 		int len = MIN(mem->n, STR_VALUE_MAX_LEN / 2);
 		for (int i = 0; i < len; ++i) {
@@ -140,13 +141,12 @@ mem_str(const struct Mem *mem)
 			buf[2 * i + 1] = n < 10 ? ('0' + n) : ('A' + n - 10);
 		}
 		if (mem->n > len)
-			return tt_sprintf("varbinary(x'%.*s...)", len * 2, buf);
-		return tt_sprintf("varbinary(x'%.*s')", len * 2, buf);
+			return tt_sprintf("%s(x'%.*s...)", type, len * 2, buf);
+		return tt_sprintf("%s(x'%.*s')", type, len * 2, buf);
 	}
 	case MEM_TYPE_MAP:
 	case MEM_TYPE_ARRAY: {
 		const char *str = mp_str(mem->z);
-		const char *type = mem_type_to_str(mem);
 		uint32_t len = strlen(str);
 		uint32_t minlen = MIN(STR_VALUE_MAX_LEN, len);
 		memcpy(buf, str, minlen);
@@ -156,9 +156,9 @@ mem_str(const struct Mem *mem)
 	}
 	case MEM_TYPE_UUID:
 		tt_uuid_to_string(&mem->u.uuid, buf);
-		return tt_sprintf("uuid('%s')", buf);
+		return tt_sprintf("%s('%s')", type, buf);
 	case MEM_TYPE_BOOL:
-		return mem->u.b ? "boolean(TRUE)" : "boolean(FALSE)";
+		return tt_sprintf("%s(%s)", type, mem->u.b ? "TRUE" : "FALSE");
 	default:
 		return "unknown";
 	}
@@ -626,7 +626,7 @@ int_to_double(struct Mem *mem)
 		d = (double)mem->u.i;
 	mem->u.r = d;
 	mem->type = MEM_TYPE_DOUBLE;
-	assert(mem->flags == 0);
+	mem->flags = 0;
 	return 0;
 }
 
@@ -640,6 +640,7 @@ int_to_double_precise(struct Mem *mem)
 		return -1;
 	mem->u.r = d;
 	mem->type = MEM_TYPE_DOUBLE;
+	mem->flags = 0;
 	return 0;
 }
 
@@ -656,6 +657,7 @@ int_to_double_forced(struct Mem *mem)
 	double d = (double)i;
 	mem->u.r = d;
 	mem->type = MEM_TYPE_DOUBLE;
+	mem->flags = 0;
 	return CMP_OLD_NEW(i, d, int64_t);
 }
 
@@ -668,6 +670,7 @@ uint_to_double_precise(struct Mem *mem)
 	if (mem->u.u != (uint64_t)d)
 		return -1;
 	mem->u.r = d;
+	mem->flags = 0;
 	mem->type = MEM_TYPE_DOUBLE;
 	return 0;
 }
@@ -684,6 +687,7 @@ uint_to_double_forced(struct Mem *mem)
 	uint64_t u = mem->u.u;
 	double d = (double)u;
 	mem->u.r = d;
+	mem->flags = 0;
 	mem->type = MEM_TYPE_DOUBLE;
 	return CMP_OLD_NEW(u, d, uint64_t);
 }
@@ -708,6 +712,7 @@ str_to_str0(struct Mem *mem)
 		return -1;
 	mem->z[mem->n] = '\0';
 	mem->flags |= MEM_Term;
+	mem->flags &= ~MEM_Scalar;
 	return 0;
 }
 
@@ -716,7 +721,7 @@ str_to_bin(struct Mem *mem)
 {
 	assert(mem->type == MEM_TYPE_STR);
 	mem->type = MEM_TYPE_BIN;
-	mem->flags &= ~MEM_Term;
+	mem->flags &= ~(MEM_Term | MEM_Scalar);
 	return 0;
 }
 
@@ -765,6 +770,7 @@ bin_to_str(struct Mem *mem)
 	if (ExpandBlob(mem) != 0)
 		return -1;
 	mem->type = MEM_TYPE_STR;
+	mem->flags &= ~MEM_Scalar;
 	return 0;
 }
 
@@ -840,13 +846,13 @@ double_to_int(struct Mem *mem)
 	if (d <= -1.0 && d >= (double)INT64_MIN) {
 		mem->u.i = (int64_t)d;
 		mem->type = MEM_TYPE_INT;
-		assert(mem->flags == 0);
+		mem->flags = 0;
 		return 0;
 	}
 	if (d > -1.0 && d < (double)UINT64_MAX) {
 		mem->u.u = (uint64_t)d;
 		mem->type = MEM_TYPE_UINT;
-		assert(mem->flags == 0);
+		mem->flags = 0;
 		return 0;
 	}
 	return -1;
@@ -860,13 +866,13 @@ double_to_int_precise(struct Mem *mem)
 	if (d <= -1.0 && d >= (double)INT64_MIN && (double)(int64_t)d == d) {
 		mem->u.i = (int64_t)d;
 		mem->type = MEM_TYPE_INT;
-		assert(mem->flags == 0);
+		mem->flags = 0;
 		return 0;
 	}
 	if (d > -1.0 && d < (double)UINT64_MAX && (double)(uint64_t)d == d) {
 		mem->u.u = (uint64_t)d;
 		mem->type = MEM_TYPE_UINT;
-		assert(mem->flags == 0);
+		mem->flags = 0;
 		return 0;
 	}
 	return -1;
@@ -905,6 +911,7 @@ double_to_int_forced(struct Mem *mem)
 	}
 	mem->u.i = i;
 	mem->type = type;
+	mem->flags = 0;
 	return res;
 }
 
@@ -916,7 +923,7 @@ double_to_uint(struct Mem *mem)
 	if (d > -1.0 && d < (double)UINT64_MAX) {
 		mem->u.u = (uint64_t)d;
 		mem->type = MEM_TYPE_UINT;
-		assert(mem->flags == 0);
+		mem->flags = 0;
 		return 0;
 	}
 	return -1;
@@ -930,7 +937,7 @@ double_to_uint_precise(struct Mem *mem)
 	if (d > -1.0 && d < (double)UINT64_MAX && (double)(uint64_t)d == d) {
 		mem->u.u = (uint64_t)d;
 		mem->type = MEM_TYPE_UINT;
-		assert(mem->flags == 0);
+		mem->flags = 0;
 		return 0;
 	}
 	return -1;
@@ -960,6 +967,7 @@ double_to_uint_forced(struct Mem *mem)
 	}
 	mem->u.u = u;
 	mem->type = MEM_TYPE_UINT;
+	mem->flags = 0;
 	return res;
 }
 
@@ -1020,8 +1028,10 @@ int
 mem_to_int(struct Mem *mem)
 {
 	assert(mem->type < MEM_TYPE_INVALID);
-	if ((mem->type & (MEM_TYPE_INT | MEM_TYPE_UINT)) != 0)
+	if ((mem->type & (MEM_TYPE_INT | MEM_TYPE_UINT)) != 0) {
+		mem->flags = 0;
 		return 0;
+	}
 	if (mem->type == MEM_TYPE_STR)
 		return str_to_int(mem);
 	if (mem->type == MEM_TYPE_DOUBLE)
@@ -1033,8 +1043,10 @@ int
 mem_to_int_precise(struct Mem *mem)
 {
 	assert(mem->type < MEM_TYPE_INVALID);
-	if ((mem->type & (MEM_TYPE_INT | MEM_TYPE_UINT)) != 0)
+	if ((mem->type & (MEM_TYPE_INT | MEM_TYPE_UINT)) != 0) {
+		mem->flags = 0;
 		return 0;
+	}
 	if (mem->type == MEM_TYPE_STR)
 		return str_to_int(mem);
 	if (mem->type == MEM_TYPE_DOUBLE)
@@ -1046,8 +1058,10 @@ int
 mem_to_double(struct Mem *mem)
 {
 	assert(mem->type < MEM_TYPE_INVALID);
-	if (mem->type == MEM_TYPE_DOUBLE)
+	if (mem->type == MEM_TYPE_DOUBLE) {
+		mem->flags = 0;
 		return 0;
+	}
 	if ((mem->type & (MEM_TYPE_INT | MEM_TYPE_UINT)) != 0)
 		return int_to_double(mem);
 	if (mem->type == MEM_TYPE_STR)
@@ -1059,12 +1073,15 @@ int
 mem_to_number(struct Mem *mem)
 {
 	assert(mem->type < MEM_TYPE_INVALID);
-	if (mem_is_num(mem))
+	if (mem_is_num(mem)) {
+		mem->flags = MEM_Number;
 		return 0;
+	}
 	if (mem->type == MEM_TYPE_STR) {
-		if (str_to_int(mem) == 0)
-			return 0;
-		return str_to_double(mem);
+		if (str_to_int(mem) != 0 && str_to_double(mem) != 0)
+			return -1;
+		mem->flags = MEM_Number;
+		return 0;
 	}
 	return -1;
 }
@@ -1075,8 +1092,10 @@ mem_to_str0(struct Mem *mem)
 	assert(mem->type < MEM_TYPE_INVALID);
 	switch (mem->type) {
 	case MEM_TYPE_STR:
-		if ((mem->flags & MEM_Term) != 0)
+		if ((mem->flags & MEM_Term) != 0) {
+			mem->flags &= ~MEM_Scalar;
 			return 0;
+		}
 		return str_to_str0(mem);
 	case MEM_TYPE_INT:
 	case MEM_TYPE_UINT:
@@ -1104,6 +1123,7 @@ mem_to_str(struct Mem *mem)
 	assert(mem->type < MEM_TYPE_INVALID);
 	switch (mem->type) {
 	case MEM_TYPE_STR:
+		mem->flags &= ~MEM_Scalar;
 		return 0;
 	case MEM_TYPE_INT:
 	case MEM_TYPE_UINT:
@@ -1134,6 +1154,7 @@ mem_cast_explicit(struct Mem *mem, enum field_type type)
 	case FIELD_TYPE_UNSIGNED:
 		switch (mem->type) {
 		case MEM_TYPE_UINT:
+			mem->flags = 0;
 			return 0;
 		case MEM_TYPE_STR:
 			return str_to_uint(mem);
@@ -1151,6 +1172,7 @@ mem_cast_explicit(struct Mem *mem, enum field_type type)
 	case FIELD_TYPE_BOOLEAN:
 		switch (mem->type) {
 		case MEM_TYPE_BOOL:
+			mem->flags = 0;
 			return 0;
 		case MEM_TYPE_STR:
 			return str_to_bool(mem);
@@ -1160,16 +1182,20 @@ mem_cast_explicit(struct Mem *mem, enum field_type type)
 	case FIELD_TYPE_VARBINARY:
 		if (mem->type == MEM_TYPE_STR)
 			return str_to_bin(mem);
-		if (mem_is_bytes(mem))
+		if (mem_is_bytes(mem)) {
+			mem->flags &= ~MEM_Scalar;
 			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)
+		if (mem->type == MEM_TYPE_UUID) {
+			mem->flags = 0;
 			return 0;
+		}
 		if (mem->type == MEM_TYPE_STR)
 			return str_to_uuid(mem);
 		if (mem->type == MEM_TYPE_BIN)
@@ -1178,6 +1204,8 @@ mem_cast_explicit(struct Mem *mem, enum field_type type)
 	case FIELD_TYPE_SCALAR:
 		if ((mem->type & (MEM_TYPE_MAP | MEM_TYPE_ARRAY)) != 0)
 			return -1;
+		mem->flags |= MEM_Scalar;
+		mem->flags &= ~MEM_Number;
 		return 0;
 	default:
 		break;
@@ -1192,42 +1220,55 @@ mem_cast_implicit(struct Mem *mem, enum field_type type)
 		return 0;
 	switch (type) {
 	case FIELD_TYPE_UNSIGNED:
-		if (mem->type == MEM_TYPE_UINT)
+		if (mem->type == MEM_TYPE_UINT) {
+			mem->flags = 0;
 			return 0;
+		}
 		if (mem->type == MEM_TYPE_DOUBLE)
 			return double_to_uint_precise(mem);
 		return -1;
 	case FIELD_TYPE_STRING:
-		if (mem->type == MEM_TYPE_STR)
+		if (mem->type == MEM_TYPE_STR) {
+			mem->flags &= ~MEM_Scalar;
 			return 0;
+		}
 		return -1;
 	case FIELD_TYPE_DOUBLE:
-		if (mem->type == MEM_TYPE_DOUBLE)
+		if (mem->type == MEM_TYPE_DOUBLE) {
+			mem->flags = 0;
 			return 0;
+		}
 		if (mem->type == MEM_TYPE_INT)
 			return int_to_double_precise(mem);
 		if (mem->type == MEM_TYPE_UINT)
 			return uint_to_double_precise(mem);
 		return -1;
 	case FIELD_TYPE_INTEGER:
-		if ((mem->type & (MEM_TYPE_INT | MEM_TYPE_UINT)) != 0)
+		if ((mem->type & (MEM_TYPE_INT | MEM_TYPE_UINT)) != 0) {
+			mem->flags = 0;
 			return 0;
+		}
 		if (mem->type == MEM_TYPE_DOUBLE)
 			return double_to_int_precise(mem);
 		return -1;
 	case FIELD_TYPE_BOOLEAN:
-		if (mem->type == MEM_TYPE_BOOL)
+		if (mem->type == MEM_TYPE_BOOL) {
+			mem->flags = 0;
 			return 0;
+		}
 		return -1;
 	case FIELD_TYPE_VARBINARY:
 		if ((mem->type & (MEM_TYPE_BIN | MEM_TYPE_MAP |
-				  MEM_TYPE_ARRAY)) != 0)
+				  MEM_TYPE_ARRAY)) != 0) {
+			mem->flags &= ~MEM_Scalar;
 			return 0;
+		}
 		return -1;
 	case FIELD_TYPE_NUMBER:
-		if (mem_is_num(mem))
-			return 0;
-		return -1;
+		if (!mem_is_num(mem))
+			return -1;
+		mem->flags = MEM_Number;
+		return 0;
 	case FIELD_TYPE_MAP:
 		if (mem->type == MEM_TYPE_MAP)
 			return 0;
@@ -1239,11 +1280,14 @@ mem_cast_implicit(struct Mem *mem, enum field_type type)
 	case FIELD_TYPE_SCALAR:
 		if ((mem->type & (MEM_TYPE_MAP | MEM_TYPE_ARRAY)) != 0)
 			return -1;
+		mem->flags |= MEM_Scalar;
+		mem->flags &= ~MEM_Number;
 		return 0;
 	case FIELD_TYPE_UUID:
-		if (mem->type == MEM_TYPE_UUID)
-			return 0;
-		return -1;
+		if (mem->type != MEM_TYPE_UUID)
+			return -1;
+		mem->flags = 0;
+		return 0;
 	case FIELD_TYPE_ANY:
 		return 0;
 	default:
@@ -1260,9 +1304,11 @@ mem_cast_implicit_number(struct Mem *mem, enum field_type type)
 	case FIELD_TYPE_UNSIGNED:
 		switch (mem->type) {
 		case MEM_TYPE_UINT:
+			mem->flags = 0;
 			return 0;
 		case MEM_TYPE_INT:
 			mem->u.u = 0;
+			mem->flags = 0;
 			mem->type = MEM_TYPE_UINT;
 			return -1;
 		case MEM_TYPE_DOUBLE:
@@ -1278,6 +1324,7 @@ mem_cast_implicit_number(struct Mem *mem, enum field_type type)
 		case MEM_TYPE_UINT:
 			return uint_to_double_forced(mem);
 		case MEM_TYPE_DOUBLE:
+			mem->flags = 0;
 			return 0;
 		default:
 			unreachable();
@@ -1287,6 +1334,7 @@ mem_cast_implicit_number(struct Mem *mem, enum field_type type)
 		switch (mem->type) {
 		case MEM_TYPE_UINT:
 		case MEM_TYPE_INT:
+			mem->flags = 0;
 			return 0;
 		case MEM_TYPE_DOUBLE:
 			return double_to_int_forced(mem);
@@ -2070,15 +2118,18 @@ char *
 mem_type_to_str(const struct Mem *p)
 {
 	assert(p != NULL);
+	if ((p->flags & MEM_Scalar) != 0)
+		return "scalar";
+	if ((p->flags & MEM_Number) != 0)
+		return "number";
 	switch (p->type) {
 	case MEM_TYPE_NULL:
 		return "NULL";
 	case MEM_TYPE_STR:
 		return "string";
 	case MEM_TYPE_INT:
-		return "integer";
 	case MEM_TYPE_UINT:
-		return "unsigned";
+		return "integer";
 	case MEM_TYPE_DOUBLE:
 		return "double";
 	case MEM_TYPE_ARRAY:
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index 958907aaf..a5c9015cd 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -93,6 +93,10 @@ struct Mem {
 #endif
 };
 
+/** MEM is of NUMBER meta-type. */
+#define MEM_Number    0x0001
+/** MEM is of SCALAR meta-type. */
+#define MEM_Scalar    0x0002
 #define MEM_Cleared   0x0200	/* NULL set by OP_Null, not from data */
 
 /* Whenever Mem contains a valid string or blob representation, one of
diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index db5f96b62..79f2160d2 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -1982,6 +1982,14 @@ case OP_Column: {
 	    default_val_mem != NULL) {
 		mem_copy_as_ephemeral(pDest, default_val_mem);
 	}
+	enum field_type field_type = field_type_MAX;
+	/* Currently PSEUDO cursor does not have info about field types. */
+	if (pC->eCurType == CURTYPE_TARANTOOL)
+		field_type = pC->uc.pCursor->space->def->fields[p2].type;
+	if (field_type == FIELD_TYPE_SCALAR)
+		pDest->flags |= MEM_Scalar;
+	else if (field_type == FIELD_TYPE_NUMBER)
+		pDest->flags |= MEM_Number;
 op_column_out:
 	REGISTER_TRACE(p, pOp->p3, pDest);
 	break;
diff --git a/test/sql-tap/cast.test.lua b/test/sql-tap/cast.test.lua
index 4f2bff93e..e0295fd3b 100755
--- a/test/sql-tap/cast.test.lua
+++ b/test/sql-tap/cast.test.lua
@@ -90,7 +90,7 @@ test:do_execsql_test(
         SELECT typeof(CAST(x'616263' AS SCALAR))
     ]], {
         -- <cast-1.8>
-        "varbinary"
+        "scalar"
         -- </cast-1.8>
     })
 
@@ -280,7 +280,7 @@ test:do_execsql_test(
         SELECT typeof(CAST(123 AS SCALAR))
     ]], {
         -- <cast-1.28>
-        "integer"
+        "scalar"
         -- </cast-1.28>
     })
 
@@ -380,7 +380,7 @@ test:do_execsql_test(
         SELECT typeof(CAST(123.456 AS SCALAR))
     ]], {
         -- <cast-1.38>
-        "double"
+        "scalar"
         -- </cast-1.38>
     })
 
@@ -460,7 +460,7 @@ test:do_execsql_test(
         SELECT typeof(CAST('123abc' AS SCALAR))
     ]], {
         -- <cast-1.48>
-        "string"
+        "scalar"
         -- </cast-1.48>
     })
 
@@ -531,7 +531,7 @@ test:do_execsql_test(
         SELECT typeof(CAST(1 AS NUMBER))
     ]], {
         -- <case-1.63>
-        "integer"
+        "number"
         -- </case-1.63>
     })
 
@@ -551,7 +551,7 @@ test:do_execsql_test(
         SELECT typeof(CAST('1' AS NUMBER))
     ]], {
         -- <case-1.65>
-        "integer"
+        "number"
         -- </case-1.65>
     })
 
@@ -972,7 +972,7 @@ test:do_catchsql_test(
     [[
         SELECT CAST(CAST(1 AS NUMBER) AS BOOLEAN);
     ]], {
-        1, "Type mismatch: can not convert integer(1) to boolean"
+        1, "Type mismatch: can not convert number(1) to boolean"
     })
 
 -- Make sure that explicit cast from VARBINARY to numeric types throws an error.
diff --git a/test/sql-tap/default.test.lua b/test/sql-tap/default.test.lua
index 59dc2a2a1..9c15220eb 100755
--- a/test/sql-tap/default.test.lua
+++ b/test/sql-tap/default.test.lua
@@ -115,7 +115,7 @@ test:do_execsql_test(
 	g, typeof(g) FROM t3;
 	]], {
 	-- <default-3.1>
-	1, "integer", 5, "integer", "row1", "string", 5.25, "double", 8.67, "double", "321", "string", 432, "integer"
+	1, "integer", 5, "integer", "row1", "string", 5.25, "number", 8.67, "number", "321", "string", 432, "integer"
 	-- </default-3.1>
 })
 
diff --git a/test/sql-tap/engine.cfg b/test/sql-tap/engine.cfg
index 94b0bb1f6..820c72b00 100644
--- a/test/sql-tap/engine.cfg
+++ b/test/sql-tap/engine.cfg
@@ -23,6 +23,9 @@
     "gh-6164-uuid-follow-ups.test.lua": {
         "memtx": {"engine": "memtx"}
     },
+    "metatypes.test.lua": {
+        "memtx": {"engine": "memtx"}
+    },
     "gh-4077-iproto-execute-no-bind.test.lua": {},
     "*": {
         "memtx": {"engine": "memtx"},
diff --git a/test/sql-tap/in4.test.lua b/test/sql-tap/in4.test.lua
index aa6483697..9e11fac4f 100755
--- a/test/sql-tap/in4.test.lua
+++ b/test/sql-tap/in4.test.lua
@@ -153,7 +153,7 @@ test:do_catchsql_test(
         SELECT b FROM t2 WHERE a IN ('', '0.0.0', '2')
     ]], {
         -- <in4-2.8>
-        1, "Type mismatch: can not convert string('') to integer"
+        1, "Type mismatch: can not convert scalar('') to integer"
         -- </in4-2.8>
     })
 
diff --git a/test/sql-tap/metatypes.test.lua b/test/sql-tap/metatypes.test.lua
new file mode 100755
index 000000000..89802ec09
--- /dev/null
+++ b/test/sql-tap/metatypes.test.lua
@@ -0,0 +1,51 @@
+#!/usr/bin/env tarantool
+local test = require("sqltester")
+test:plan(3)
+
+-- Check that SCALAR and NUMBER meta-types works as intended.
+box.execute([[CREATE TABLE t (i INT PRIMARY KEY, s SCALAR, n NUMBER);]])
+
+--
+-- Check that implicit cast from numeric types to NUMBER and from scalar types
+-- to SCALAR works properly.
+--
+local uuid = [[CAST('11111111-1111-1111-1111-111111111111' AS UUID)]]
+test:do_execsql_test(
+    "metatypes-1.1",
+    [[
+        INSERT INTO t VALUES(1, 1, 1);
+        INSERT INTO t VALUES(2, 2.0, 2.0);
+        INSERT INTO t(i, s) VALUES(3, '3');
+        INSERT INTO t(i, s) VALUES(4, true);
+        INSERT INTO t(i, s) VALUES(5, x'35');
+        INSERT INTO t(i, s) VALUES(6, ]]..uuid..[[);
+        SELECT * FROM t;
+    ]], {
+        1,1,1,
+        2,2,2,
+        3,"3","",
+        4,true,"",
+        5,"5","",
+        6,require('uuid').fromstr('11111111-1111-1111-1111-111111111111'),""
+    })
+
+-- Check that typeof() returns right result.
+test:do_execsql_test(
+    "metatypes-1.2",
+    [[
+        SELECT typeof(s) FROM t;
+    ]], {
+        "scalar","scalar","scalar","scalar","scalar","scalar"
+    })
+
+test:do_execsql_test(
+    "metatypes-1.3",
+    [[
+        SELECT typeof(n) FROM t;
+    ]], {
+        "number","number","NULL","NULL","NULL","NULL"
+    })
+
+box.execute([[DROP TABLE t;]])
+
+test:finish_test()
diff --git a/test/sql-tap/select7.test.lua b/test/sql-tap/select7.test.lua
index 47e7240ed..815f9110b 100755
--- a/test/sql-tap/select7.test.lua
+++ b/test/sql-tap/select7.test.lua
@@ -235,7 +235,7 @@ test:do_execsql_test(
         SELECT a=0, typeof(a) FROM t4
     ]], {
         -- <select7-7.5>
-        false, "double", false, "double"
+        false, "number", false, "number"
         -- </select7-7.5>
     })
 
@@ -245,7 +245,7 @@ test:do_execsql_test(
         SELECT a=0, typeof(a) FROM t4 GROUP BY a
     ]], {
         -- <select7-7.6>
-        false, "double", false, "double"
+        false, "number", false, "number"
         -- </select7-7.6>
     })
 
diff --git a/test/sql-tap/tkt-3998683a16.test.lua b/test/sql-tap/tkt-3998683a16.test.lua
index 81c7806ad..44ce7087a 100755
--- a/test/sql-tap/tkt-3998683a16.test.lua
+++ b/test/sql-tap/tkt-3998683a16.test.lua
@@ -36,6 +36,7 @@ test:do_test(
         ]]
     end, {
         -- <tkt-3998683a16.1>
+        1, 2, 3, 4, 5, 6
         -- </tkt-3998683a16.1>
     })
 
diff --git a/test/sql-tap/uuid.test.lua b/test/sql-tap/uuid.test.lua
index bbe912c72..32203e9fb 100755
--- a/test/sql-tap/uuid.test.lua
+++ b/test/sql-tap/uuid.test.lua
@@ -1225,7 +1225,7 @@ test:do_execsql_test(
         INSERT INTO t14 VALUES ('11111111-1111-1111-1111-111111111111');
         SELECT typeof(s) FROM t14;
     ]], {
-        "boolean", "integer", "integer", "double", "string", "varbinary", "uuid"
+        "scalar", "scalar", "scalar", "scalar", "scalar", "scalar", "scalar"
     })
 
 local s = box.schema.space.create('T15', {format={{'I', 'integer'}, {'M', 'map'}, {'A', 'array'}}})
diff --git a/test/sql/boolean.result b/test/sql/boolean.result
index d027a56e3..b325ea458 100644
--- a/test/sql/boolean.result
+++ b/test/sql/boolean.result
@@ -4023,7 +4023,7 @@ SELECT a2, 2.3 OR a2 FROM t6
 SELECT c, true AND c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, false AND c FROM t8;
  | ---
@@ -4038,17 +4038,17 @@ SELECT c, false AND c FROM t8;
 SELECT c, true OR c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, false OR c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, c AND true FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, c AND false FROM t8;
  | ---
@@ -4063,53 +4063,53 @@ SELECT c, c AND false FROM t8;
 SELECT c, c OR true FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, c OR false FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 
 SELECT a1, c, a1 AND c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a1, c, a1 OR c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a1, c, c AND a1 FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a1, c, c OR a1 FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a2, c, a2 AND c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a2, c, a2 OR c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a2, c, c AND a2 FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a2, c, c OR a2 FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 
 SELECT true + 2.3;
@@ -4357,12 +4357,12 @@ SELECT c, false / c FROM t8;
 SELECT c, true % c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to integer'
+ | - 'Type mismatch: can not convert number(4.56) to integer'
  | ...
 SELECT c, false % c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to integer'
+ | - 'Type mismatch: can not convert number(4.56) to integer'
  | ...
 SELECT c, c + true FROM t8;
  | ---
@@ -4438,7 +4438,7 @@ SELECT a1, c, a1 / c FROM t6, t8;
 SELECT a1, c, a1 % c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to integer'
+ | - 'Type mismatch: can not convert number(4.56) to integer'
  | ...
 SELECT a1, c, c + a1 FROM t6, t8;
  | ---
@@ -4488,7 +4488,7 @@ SELECT a2, c, a2 / c FROM t6, t8;
 SELECT a2, c, a2 % c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to integer'
+ | - 'Type mismatch: can not convert number(4.56) to integer'
  | ...
 SELECT a2, c, c + a2 FROM t6, t8;
  | ---
@@ -4601,22 +4601,22 @@ SELECT a2, 2.3 < a2 FROM t6
 SELECT c, true > c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, false > c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, true < c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, false < c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, c > true FROM t8;
  | ---
@@ -4642,12 +4642,12 @@ SELECT c, c < false FROM t8;
 SELECT a1, c, a1 > c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a1, c, a1 < c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a1, c, c > a1 FROM t6, t8;
  | ---
@@ -4662,12 +4662,12 @@ SELECT a1, c, c < a1 FROM t6, t8;
 SELECT a2, c, a2 > c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a2, c, a2 < c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a2, c, c > a2 FROM t6, t8;
  | ---
@@ -4765,22 +4765,22 @@ SELECT a2, 2.3 <= a2 FROM t6
 SELECT c, true >= c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, false >= c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, true <= c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, false <= c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, c >= true FROM t8;
  | ---
@@ -4806,12 +4806,12 @@ SELECT c, c <= false FROM t8;
 SELECT a1, c, a1 >= c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a1, c, a1 <= c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a1, c, c >= a1 FROM t6, t8;
  | ---
@@ -4826,12 +4826,12 @@ SELECT a1, c, c <= a1 FROM t6, t8;
 SELECT a2, c, a2 >= c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a2, c, a2 <= c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a2, c, c >= a2 FROM t6, t8;
  | ---
@@ -4929,22 +4929,22 @@ SELECT a2, 2.3 != a2 FROM t6
 SELECT c, true == c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, false == c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, true != c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, false != c FROM t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT c, c == true FROM t8;
  | ---
@@ -4970,12 +4970,12 @@ SELECT c, c != false FROM t8;
 SELECT a1, c, a1 == c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a1, c, a1 != c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a1, c, c == a1 FROM t6, t8;
  | ---
@@ -4990,12 +4990,12 @@ SELECT a1, c, c != a1 FROM t6, t8;
 SELECT a2, c, a2 == c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a2, c, a2 != c FROM t6, t8;
  | ---
  | - null
- | - 'Type mismatch: can not convert double(4.56) to boolean'
+ | - 'Type mismatch: can not convert number(4.56) to boolean'
  | ...
 SELECT a2, c, c == a2 FROM t6, t8;
  | ---
diff --git a/test/sql/gh-4697-scalar-bool-sort-cmp.result b/test/sql/gh-4697-scalar-bool-sort-cmp.result
index e69c625bd..7f054b438 100644
--- a/test/sql/gh-4697-scalar-bool-sort-cmp.result
+++ b/test/sql/gh-4697-scalar-bool-sort-cmp.result
@@ -34,9 +34,9 @@ SELECT s2, typeof(s2) FROM test ORDER BY s2;
  |     type: string
  |   rows:
  |   - [null, 'NULL']
- |   - [true, 'boolean']
- |   - [1, 'integer']
- |   - [1.1, 'double']
+ |   - [true, 'scalar']
+ |   - [1, 'scalar']
+ |   - [1.1, 'scalar']
  | ...
 SELECT s3, typeof(s3) FROM test ORDER BY s3;
  | ---
@@ -47,7 +47,7 @@ SELECT s3, typeof(s3) FROM test ORDER BY s3;
  |     type: string
  |   rows:
  |   - [null, 'NULL']
- |   - [true, 'boolean']
- |   - [1, 'integer']
- |   - [1.1, 'double']
+ |   - [true, 'scalar']
+ |   - [1, 'scalar']
+ |   - [1.1, 'scalar']
  | ...
diff --git a/test/sql/types.result b/test/sql/types.result
index e8ebdae84..592b1aa19 100644
--- a/test/sql/types.result
+++ b/test/sql/types.result
@@ -215,12 +215,12 @@ box.execute("INSERT INTO t1 VALUES (randomblob(5));")
 box.execute("SELECT * FROM t1 WHERE s LIKE 'blob';")
 ---
 - null
-- 'Inconsistent types: expected string got varbinary(x''91A0FEE366'')'
+- 'Inconsistent types: expected string got scalar(x''91A0FEE366'')'
 ...
 box.execute("SELECT * FROM t1 WHERE 'blob' LIKE s;")
 ---
 - null
-- 'Inconsistent types: expected string got varbinary(x''91A0FEE366'')'
+- 'Inconsistent types: expected string got scalar(x''91A0FEE366'')'
 ...
 box.execute("SELECT * FROM t1 WHERE 'blob' LIKE x'0000';")
 ---
@@ -246,7 +246,7 @@ box.execute("INSERT INTO t1 VALUES (1);")
 box.execute("SELECT * FROM t1 WHERE s LIKE 'int';")
 ---
 - null
-- 'Inconsistent types: expected string got integer(1)'
+- 'Inconsistent types: expected string got scalar(1)'
 ...
 box.execute("SELECT * FROM t1 WHERE 'int' LIKE 4;")
 ---
@@ -1480,7 +1480,7 @@ box.execute("SELECT typeof(a), typeof(s) FROM t;")
   - name: COLUMN_2
     type: string
   rows:
-  - ['integer', 'integer']
+  - ['integer', 'scalar']
   - ['NULL', 'NULL']
 ...
 box.execute('CREATE TABLE t1 (id INTEGER PRIMARY KEY, a INTEGER, b INTEGER)')
-- 
2.25.1



More information about the Tarantool-patches mailing list