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 0CC256EC55; Sat, 31 Jul 2021 00:55:10 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 0CC256EC55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1627682110; bh=QpeHtKtxoaVAGVSFgU03xkPbUNRxMEzI94ydLcVcybk=; h=To:Cc:References:Date:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=GncgNpjeL/C5zSYcnsAGD5g9hpnyE1XyUgAmmvzV1Cxd+yrxsyY/ZEl685Js79Kuk Aj6Rhkwwo92Lu6Ie9Ulp8Ikaf4EAVZwUTyr+ECVURlxROZPmY9SI7rgvoxWwDqQ2UU 4FjIk/fy+t2oXHkkLuWSLet7XG4tWVEdIzwJYFzY= Received: from smtpng3.i.mail.ru (smtpng3.i.mail.ru [94.100.177.149]) (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 1641E6EC55 for ; Sat, 31 Jul 2021 00:55:07 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 1641E6EC55 Received: by smtpng3.m.smailru.net with esmtpa (envelope-from ) id 1m9aTC-0001FA-3M; Sat, 31 Jul 2021 00:55:06 +0300 To: imeevma@tarantool.org Cc: tarantool-patches@dev.tarantool.org References: Message-ID: <8a2c3321-0251-56b5-cc47-475a407c722f@tarantool.org> Date: Fri, 30 Jul 2021 23:55:05 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD941C43E597735A9C30A5AB0699C09BB51E5FD76225F0C99C3182A05F538085040AA27BA6BA9542E8C91B1A016F0DAA81DA109F33588BD7C3A76885EDCDB1C2CC7 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE704FFE27C31EF363AEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006377548186386978BE68638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D828D1E629955DD07E9F21E713A4DEAF82117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC8C7ADC89C2F0B2A5A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18BDFBBEFFF4125B51D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B613439FA09F3DCB32089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B5058F89CB161ADD3D9C9B4F976604943C85 X-C1DE0DAB: 0D63561A33F958A5FB3DB1A48158A3CBF6E8BCCB8BD66D750EF2C189705B16BAD59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA751B940EDA0DFB0535410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3454E98140CCACB5B309A02AEEB59D145B87FC9D738788A73E7E35018C985B72AE0385208ECAEE8D3E1D7E09C32AA3244CBFEED1B4DF0E5AFA5407DBF6DC226C6D853296C06374E602729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWBddABnKmoKSvb2tqdE0ZA== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D166DD6769BA337739113BBCAD6C5D33B3841015FED1DE5223CC9A89AB576DD93FB559BB5D741EB963CF37A108A312F5C27E8A8C3839CE0E267EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v1 1/7] sql: rework implicit cast fo assignment 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: Vladislav Shpilevoy via Tarantool-patches Reply-To: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thanks for the patch! I will return later to continue the review of the next commits. Sending first comments so as I could switch to other patches. See 10 comments below. > diff --git a/changelogs/unreleased/gh-4470-implicit-cast-for-assignment.md b/changelogs/unreleased/gh-4470-implicit-cast-for-assignment.md > new file mode 100644 > index 000000000..c758494eb > --- /dev/null > +++ b/changelogs/unreleased/gh-4470-implicit-cast-for-assignment.md > @@ -0,0 +1,3 @@ > +## feature/sql > + > +* Implicit cast for assignment now works according to defined rules (gh-4470). 1. Please, be more specific about what exactly has changed. > diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c > index 351d80b76..e804dba67 100644 > --- a/src/box/sql/mem.c > +++ b/src/box/sql/mem.c > @@ -45,6 +45,8 @@ > #include "uuid/mp_uuid.h" > #include "mp_decimal.h" > > +#define CMP_OLD_NEW(a, b, type) ((int)(a > (type)b) - (int)(a < (type)b)) 2. Please, wrap 'a' and 'b' into parentheses. Otherwise the macro might work in unexpected ways if these are expressions. > @@ -900,6 +902,92 @@ uuid_to_bin(struct Mem *mem) > return mem_copy_bin(mem, (char *)&mem->u.uuid, UUID_LEN); > } > > +static inline int > +forced_int_to_double(struct Mem *mem) > +{ > + assert(mem->type == MEM_TYPE_INT || mem->type == MEM_TYPE_UINT); > + double d; > + int res; > + if (mem->type == MEM_TYPE_INT) { > + d = (double)mem->u.i; > + res = CMP_OLD_NEW(mem->u.i, d, int64_t); > + } else { > + d = (double)mem->u.u; > + res = CMP_OLD_NEW(mem->u.u, d, uint64_t); > + } > + mem->u.r = d; > + mem->type = MEM_TYPE_DOUBLE; > + mem->field_type = FIELD_TYPE_DOUBLE; 3. Why do you need not to fail in case of imprecise conversion? It don't see -1 and 1 values used. Only != 0 in the end where the implicit casts are applied. > + return res; > +} > + > +static inline int > +forced_int_to_uint(struct Mem *mem) > +{ > + assert(mem->type == MEM_TYPE_INT || mem->type == MEM_TYPE_UINT); 4. It is never used with MEM_TYPE_UINT from what I see. > + if (mem->type == MEM_TYPE_UINT) > + return 0; > + mem->u.u = 0; > + mem->type = MEM_TYPE_UINT; > + mem->field_type = FIELD_TYPE_UNSIGNED; > + return -1; > +} > @@ -1228,6 +1316,53 @@ mem_cast_implicit_old(struct Mem *mem, enum field_type type) > return -1; > } > > +int > +mem_cast_implicit_number(struct Mem *mem, enum field_type type) > +{ > + assert(mem_is_num(mem) && sql_type_is_numeric(type)); > + switch (type) { > + case FIELD_TYPE_UNSIGNED: > + switch (mem->type) { > + case MEM_TYPE_UINT: > + mem->field_type = FIELD_TYPE_UNSIGNED; > + return 0; > + case MEM_TYPE_INT: > + return forced_int_to_uint(mem); > + case MEM_TYPE_DOUBLE: > + return forced_double_to_uint(mem); > + default: > + unreachable(); > + } > + break; > + case FIELD_TYPE_DOUBLE: > + switch (mem->type) { > + case MEM_TYPE_INT: > + case MEM_TYPE_UINT: > + return forced_int_to_double(mem); 5. You already have the switch here and yet you have a branch by MEM_TYPE_UINT vs MEM_TYPE_INT inside forced_int_to_double. Maybe split it 2 for INT and UINT separately and use them right under each 'case'? > diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h > index 645d0ee27..9766bb836 100644 > --- a/src/box/sql/mem.h > +++ b/src/box/sql/mem.h > @@ -779,6 +779,16 @@ mem_cast_implicit(struct Mem *mem, enum field_type type); > int > mem_cast_implicit_old(struct Mem *mem, enum field_type type); > > +/** > + * Convert the given MEM that contains numeric value to given numeric type > + * according to implicit cast rules. This function cannot fail. Returns: > + * -1 if before conversion value was more that after conversion; 6. 'more' -> 'greater', 'that' -> 'than'. > + * +1 if before conversion value was more that after conversion; > + * 0 if conversion is precise. > + */ > +int > +mem_cast_implicit_number(struct Mem *mem, enum field_type type); > diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c > index 9e763ed85..d143ce364 100644 > --- a/src/box/sql/vdbe.c > +++ b/src/box/sql/vdbe.c > @@ -2157,11 +2157,24 @@ case OP_ApplyType: { > while((type = *(types++)) != field_type_MAX) { > assert(pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)]); > assert(memIsValid(pIn1)); > - if (mem_cast_implicit(pIn1, type) != 0) { > + if (mem_is_field_compatible(pIn1, type)) { > + pIn1++; > + continue; > + } > + if (!mem_is_num(pIn1) || !sql_type_is_numeric(type)) { > diag_set(ClientError, ER_SQL_TYPE_MISMATCH, > mem_str(pIn1), field_type_strs[type]); > goto abort_due_to_error; > } > + struct Mem mem; > + mem.type = pIn1->type; > + mem.u = pIn1->u; > + mem.flags = 0; 7. The mem is only used for the error message. Please, move it under the 'if' right before diag_set so as not to create it for success path. And why can't you use pIn1 in mem_str() below? > + if (mem_cast_implicit_number(pIn1, type) != 0) { > + diag_set(ClientError, ER_SQL_TYPE_MISMATCH, > + mem_str(&mem), field_type_strs[type]); > + goto abort_due_to_error; > + } > pIn1++; > } > break; > diff --git a/test/sql-tap/numcast.test.lua b/test/sql-tap/numcast.test.lua > index 802fe712c..be1366260 100755 > --- a/test/sql-tap/numcast.test.lua > +++ b/test/sql-tap/numcast.test.lua > @@ -136,13 +136,13 @@ test:do_catchsql_test( > 1,"Type mismatch: can not convert double(2.0e+19) to integer" > }) > > -test:do_execsql_test( > +test:do_catchsql_test( > "cast-2.9", > [[ > INSERT INTO t VALUES(2.1); > SELECT * FROM t; 8. The select is not needed now. > ]], { > - 2, 9223372036854775808ULL, 18000000000000000000ULL > + 1, "Type mismatch: can not convert double(2.1) to integer" > }) > > -- > diff --git a/test/sql-tap/uuid.test.lua b/test/sql-tap/uuid.test.lua > index 70683a4fd..613f4c865 100755 > --- a/test/sql-tap/uuid.test.lua > +++ b/test/sql-tap/uuid.test.lua > @@ -844,13 +837,13 @@ test:do_catchsql_test( > 1, "Type mismatch: can not convert boolean(TRUE) to uuid" > }) > > -test:do_execsql_test( > +test:do_catchsql_test( > "uuid-8.2.7", > [[ > INSERT INTO tsu SELECT '7_varbinary', x'11111111111111111111111111111111' FROM t2 LIMIT 1; > SELECT * FROM tsu ORDER BY s DESC LIMIT 1; 9. The select is unreachable. > ]], { > - '7_varbinary', uuid1 > + 1, "Type mismatch: can not convert varbinary(x'11111111111111111111111111111111') to uuid" > }) > @@ -882,59 +875,48 @@ test:execsql([[ <...> > -test:do_execsql_test( > +test:do_catchsql_test( > "uuid-10.1.3", > [[ > INSERT INTO t10(i) VALUES (3); > - SELECT * FROM t10 WHERE i = 3; > ]], { > - 3, uuid1 > + 1, "Type mismatch: can not convert string('11111111-1111-1111-1111-111111111111') to uuid" 10. I suspect this test was about checking if UUID DEFAULT works. Maybe better fix its DEFAULT in the table definition so as not to use a string. Is it possible to use CAST in DEFAULT?