From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id B04806EC41; Fri, 13 Aug 2021 06:13:54 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B04806EC41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628824434; bh=nnxu/unaqJdaxI1lZpwZImNHfB/i+n04ovG2PSS3VAo=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=qt2UvtwAVXeGSTRLhvX4M3J7U6RhchkiIQddo48rg8fCrXBXi7N/5kx+ROaWLUr3D oOZnKB+b6L4W2XLjwPRwZwfgWJlzCXEQsb0Hi8HnWvlitoFbi9NvciihiTX0VB2pXv K8x2Wjjo02Vd5aRLNNJO6VmPfaX9KhdCv4l8EEMs= Received: from smtpng2.i.mail.ru (smtpng2.i.mail.ru [94.100.179.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 2CCE46EC44 for ; Fri, 13 Aug 2021 06:12:59 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2CCE46EC44 Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1mENcv-0007zz-W1; Fri, 13 Aug 2021 06:12:58 +0300 To: vdavydov@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 13 Aug 2021 06:12:57 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD910164DC12A5633065676A9727AC27C74182A05F5380850402B3AA7651B92735BECEEC4E1F1CB746F6EAD48A39B34F2C72B0BC0E548D3A701 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7BF6702EC5472AA0FEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637A005C90FB6EB35FF8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8F2836B390298FD8CFFC625CF2CB71266117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF1175FABE1C0F9B6A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18CB629EEF1311BF91D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C69415AB31670C86C8337AC0B9A0D4089EF0BF80CF9A114BC9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFE37876E7723AB534DC48ACC2A39D04F89CDFB48F4795C241BDAD6C7F3747799A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34738A88CA0C70A55802FF865D86DC9891C8561E492F65EC6902A22D504214D16F301C1E2B2ABD37341D7E09C32AA3244C2F2569B1FC1ECCA1E045638AD4BA40A833C9DC155518937F729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj0dLV0c3jbkxqKKazM/2VZw== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D164E8544C313D017262C35166E7A369383D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 2/7] sql: re-introduce NUMBER and SCALAR meta-types X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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 | 4 + src/box/sql/mem.c | 135 +++++++++++++------ src/box/sql/mem.h | 4 + src/box/sql/vdbe.c | 10 ++ 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(+), 106 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..1551d3ef2 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -162,6 +162,10 @@ typeofFunc(sql_context * context, int NotUsed, sql_value ** argv) { const char *z = 0; UNUSED_PARAMETER(NotUsed); + 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: 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..37a542fe8 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -1982,6 +1982,16 @@ case OP_Column: { default_val_mem != NULL) { mem_copy_as_ephemeral(pDest, default_val_mem); } + if (pDest->type == MEM_TYPE_NULL) + goto op_column_out; + 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)) ]], { -- - "varbinary" + "scalar" -- }) @@ -280,7 +280,7 @@ test:do_execsql_test( SELECT typeof(CAST(123 AS SCALAR)) ]], { -- - "integer" + "scalar" -- }) @@ -380,7 +380,7 @@ test:do_execsql_test( SELECT typeof(CAST(123.456 AS SCALAR)) ]], { -- - "double" + "scalar" -- }) @@ -460,7 +460,7 @@ test:do_execsql_test( SELECT typeof(CAST('123abc' AS SCALAR)) ]], { -- - "string" + "scalar" -- }) @@ -531,7 +531,7 @@ test:do_execsql_test( SELECT typeof(CAST(1 AS NUMBER)) ]], { -- - "integer" + "number" -- }) @@ -551,7 +551,7 @@ test:do_execsql_test( SELECT typeof(CAST('1' AS NUMBER)) ]], { -- - "integer" + "number" -- }) @@ -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; ]], { -- - 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" -- }) 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') ]], { -- - 1, "Type mismatch: can not convert string('') to integer" + 1, "Type mismatch: can not convert scalar('') to integer" -- }) 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 ]], { -- - false, "double", false, "double" + false, "number", false, "number" -- }) @@ -245,7 +245,7 @@ test:do_execsql_test( SELECT a=0, typeof(a) FROM t4 GROUP BY a ]], { -- - false, "double", false, "double" + false, "number", false, "number" -- }) 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, { -- + 1, 2, 3, 4, 5, 6 -- }) 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