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 215C06EC40; Fri, 13 Aug 2021 01:22:09 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 215C06EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628806929; bh=SFQQg9iZRus6JU8ONgFD2lqafBfAjwkoCLdYfGgHU1M=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Dfg8ZK3aV8rztDFfelnoTh++PcQhUsZErTDreUC+q8+C538kdvvG28dmxAzw9Obx6 h34kgHE1W68WtQgvNX6JXKOEJT6yDYvh0cwcplGA8kyAvy0SlwbWeXHIkIkYXgwwO7 ExVR+MqX7vADMIVfnS2TZHnwjCRxJHQD8XyoKwzg= 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 CBBB96EC40 for ; Fri, 13 Aug 2021 01:22:07 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CBBB96EC40 Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1mEJ5S-00078r-U0; Fri, 13 Aug 2021 01:22:07 +0300 Date: Fri, 13 Aug 2021 01:22:05 +0300 To: Vladislav Shpilevoy Cc: tarantool-patches@dev.tarantool.org Message-ID: <20210812222205.GA206310@tarantool.org> References: <2831e4d4b237ead1e70129010ddd10f9429afe72.1628697448.git.imeevma@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD972FF4A7D76DB5E242D14FEF1BD8BF4AC182A05F5380850409AF626F2FBA6CCFC4D4D692CBB443011A548512BE2FED7A9A90A71171AFCE3D5 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7A3295C83650092F9EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006374C960BD2D03BF0BDEA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F27CBDE26643D28782ADC69ACA7D51D61FCC7F00164DA146DAFE8445B8C89999728AA50765F7900637F6B57BC7E64490618DEB871D839B7333395957E7521B51C2DFABB839C843B9C08941B15DA834481F8AA50765F790063783E00425F71A4181389733CBF5DBD5E9B5C8C57E37DE458B9E9CE733340B9D5F3BBE47FD9DD3FB595F5C1EE8F4F765FC72CEEB2601E22B093A03B725D353964B0B7D0EA88DDEDAC722CA9DD8327EE4930A3850AC1BE2E735E4A630A5B664A4FFC4224003CC83647689D4C264860C145E X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B50524C51A17839ADAE50F6757B75AC0274F X-C1DE0DAB: 0D63561A33F958A54DFA3F9085B139CB193E6E1957436D171CA7A353723E1211D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA75907D60A27468CB01410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D340A58BF0C7EAC340F35A68D9FE9473EBFE4D07F02652FD2D4A244115B24E62929EE037B881978199D1D7E09C32AA3244C079D63A4AA429427DA52F0313EBD9803B038C9161EF167A1729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj0dLV0c3jbkwTWYESrdhS+Q== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DB629A298B944BFE648682D7D0FAC18F083D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: Re: [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: Mergen Imeev Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thank you for the review! My answers, diff and new patch below. On Thu, Aug 12, 2021 at 09:51:01PM +0300, Vladislav Shpilevoy wrote: > Thanks for the patch! > > > 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"); > > 1. This can be a part of the switch-case a few lines below. > Fixed. I did this because there were case when NULL could be with MEM_SCALAR flag set, however now it is fixed. > > + 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"); > > 2. Could you please remind me what was wrong with having type > MEM_TYPE_SCALAR and storing the actual type in another field? > Generally speaking, there was nothing. However, when I tried to implement such approach I found out that changes are quite big. Also, I started to think that for such "storage" element as MEM it does not look good to have more than one type with the same representation. Using flags I only need to make small adjustments to mem_set_*() functions, mem_cast_*() functions and couple more functions. Also, NUMBER and SCALAR types are only used in arithmetic operations, bitwise operations, concatenations and functions, so I thought that using flags to implement them should be enough. > > switch (argv[0]->type) { > > case MEM_TYPE_INT: > > case MEM_TYPE_UINT: Diff: diff --git a/src/box/sql/func.c b/src/box/sql/func.c index ebb546a7f..1551d3ef2 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -162,8 +162,6 @@ 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) @@ -187,6 +185,9 @@ 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/vdbe.c b/src/box/sql/vdbe.c index 79f2160d2..37a542fe8 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -1982,6 +1982,8 @@ 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) New patch: commit eeb009625c446c27f9f9f69479315f1a87873255 Author: Mergen Imeev Date: Wed Aug 11 10:00:38 2021 +0300 sql: re-introduce NUMBER and SCALAR meta-types 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 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)')