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 88CEB741E4; Fri, 6 Aug 2021 02:43:23 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 88CEB741E4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628207003; bh=oRJbBSDYlAimoZPQUuopxRdnuxHV9fY1Munfwn6jjoI=; 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=goOKUoZH5roGbpxGqH1axTzxRUANcHCuk+FeID0NIvG6L0TpR344NF0WH+VWden11 f/HageVxlpihrPQJl2f6odGlYtxLdXVKz4O4MnE/CHLs0oiFvwl2rdisc4vIryS6IL JYC4W5wyXstd+sXJ+V2+j/anX4iphWyMrU6A/t0M= Received: from smtp40.i.mail.ru (smtp40.i.mail.ru [94.100.177.100]) (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 17F19741E1 for ; Fri, 6 Aug 2021 02:43:21 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 17F19741E1 Received: by smtp40.i.mail.ru with esmtpa (envelope-from ) id 1mBn1E-0000KE-AM; Fri, 06 Aug 2021 02:43:20 +0300 Date: Fri, 6 Aug 2021 02:43:19 +0300 To: Vladislav Shpilevoy Cc: tarantool-patches@dev.tarantool.org Message-ID: <20210805234319.GE81912@tarantool.org> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD94BB7C0677F3ED9D05EB08EAFC9298EA6182A05F538085040BB33BF6F0D3E58FB6857FE070718817A313029FB305CD27C032F66DD63BFA6FC X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE78981306C6E927004EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637D8FE4B420C2EEC248638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D80A70460766EB73A7F9BDC377DBCDD620117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BF1175FABE1C0F9B6A471835C12D1D977C4224003CC836476EB9C4185024447017B076A6E789B0E975F5C1EE8F4F765FCCA3E976C47030043D81D268191BDAD3DBD4B6F7A4D31EC0BEA7A3FFF5B025636D81D268191BDAD3D78DA827A17800CE7020E156585EF7189EC76A7562686271EEC990983EF5C03292E808ACE2090B5E14AD6D5ED66289B5259CC434672EE63711DD303D21008E298D5E8D9A59859A8B6B372FE9A2E580EFC725E5C173C3A84C309A7649CC036878F35872C767BF85DA2F004C90652538430E4A6367B16DE6309 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B5050F7413C9D46EB4D713AD012DD1956226 X-C1DE0DAB: 0D63561A33F958A54A84403FB2D3FC254C6BD46EBA75C9C8C03D1508CE846723D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7501A9DF589746230F410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D348B532EA2091F4FF6D6155AAD9F57D1589AD2417B5E2B8AA4F184C1854179684896ED180E79A979011D7E09C32AA3244CF82AEE340A1AFCB16CD0A8117240D593853296C06374E602729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojFhlvmGwdUwT1efil4OE1FQ== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED7B16A1AD67BD5DDE6857FE070718817A009DB4CE3002C7DB5105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v1 5/7] sql: remove implicit cast from OP_MakeRecord 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 and new patch below. On Thu, Aug 05, 2021 at 12:27:20AM +0200, Vladislav Shpilevoy wrote: > Thanks for the patch! > > On 28.07.2021 22:51, imeevma@tarantool.org wrote: > > This patch removes deprecated implicit cast from OP_MakeRecord opcode. > > Not there will be no implicit cast in OP_MakeRecord opcode. > > 1. Not -> Note? The commit seems strange. You basically said the same > thing 3 times: 1 in th title and 2 in the message. > "Not" was "Now", but I rewrite all commite-message. > > diff --git a/changelogs/unreleased/gh-4230-implicit-cast-for-comparison.md b/changelogs/unreleased/gh-4230-implicit-cast-for-comparison.md > > new file mode 100644 > > index 000000000..9f523d3ed > > --- /dev/null > > +++ b/changelogs/unreleased/gh-4230-implicit-cast-for-comparison.md > > @@ -0,0 +1,3 @@ > > +## feature/sql > > + > > +* Implicit cast for comparison now works according to defined rules (gh-4230). > > 2. Please, try to be more specific here. Users won't understand anything from > this sentence. Fixed. New patch: commit 71f945038ee7aca90e854dad29875312adf80a1a Author: Mergen Imeev Date: Tue Jul 27 23:31:45 2021 +0300 sql: remove implicit cast from OP_MakeRecord This patch removes deprecated implicit cast from OP_MakeRecord opcode, which were used in some rare cases, for example during IN operation with subselect as right-value. Closes #4230 Part of #4470 diff --git a/changelogs/unreleased/gh-4230-implicit-cast-for-comparison.md b/changelogs/unreleased/gh-4230-implicit-cast-for-comparison.md new file mode 100644 index 000000000..66103f461 --- /dev/null +++ b/changelogs/unreleased/gh-4230-implicit-cast-for-comparison.md @@ -0,0 +1,6 @@ +## feature/sql + +* Now any number can be compared to any other number, and values of any scalar + type can be compared to any other value of the same type. A value of a + non-numeric scalar type cannot be compared with a value of any other scalar + type (gh-4230). \ No newline at end of file diff --git a/src/box/sql/analyze.c b/src/box/sql/analyze.c index b87f69512..e97fefb3a 100644 --- a/src/box/sql/analyze.c +++ b/src/box/sql/analyze.c @@ -968,12 +968,7 @@ vdbe_emit_analyze_space(struct Parse *parse, struct space *space) sqlVdbeAddOp2(v, OP_Next, idx_cursor, next_row_addr); /* Add the entry to the stat1 table. */ callStatGet(v, stat4_reg, STAT_GET_STAT1, stat1_reg); - enum field_type types[4] = { FIELD_TYPE_STRING, - FIELD_TYPE_STRING, - FIELD_TYPE_STRING, - field_type_MAX }; - sqlVdbeAddOp4(v, OP_MakeRecord, tab_name_reg, 4, tmp_reg, - (char *)types, sizeof(types)); + sqlVdbeAddOp3(v, OP_MakeRecord, tab_name_reg, 4, tmp_reg); sqlVdbeAddOp4(v, OP_IdxInsert, tmp_reg, 0, 0, (char *)stat1, P4_SPACEPTR); /* Add the entries to the stat4 table. */ diff --git a/src/box/sql/delete.c b/src/box/sql/delete.c index 62a726fdd..5226dd6ea 100644 --- a/src/box/sql/delete.c +++ b/src/box/sql/delete.c @@ -328,12 +328,8 @@ sql_table_delete_from(struct Parse *parse, struct SrcList *tab_list, * key. */ key_len = 0; - struct index *pk = space_index(space, 0); - enum field_type *types = is_view ? NULL : - sql_index_type_str(parse->db, - pk->def); - sqlVdbeAddOp4(v, OP_MakeRecord, reg_pk, pk_len, - reg_key, (char *)types, P4_DYNAMIC); + sqlVdbeAddOp3(v, OP_MakeRecord, reg_pk, pk_len, + reg_key); /* Set flag to save memory allocating one * by malloc. */ diff --git a/src/box/sql/expr.c b/src/box/sql/expr.c index b9fc5bfc5..83766b3fb 100644 --- a/src/box/sql/expr.c +++ b/src/box/sql/expr.c @@ -2829,8 +2829,6 @@ sqlCodeSubselect(Parse * pParse, /* Parsing context */ struct ExprList_item *pItem; int r1, r2, r3; - enum field_type lhs_type = - sql_expr_type(pLeft); bool unused; struct coll *unused_coll; if (sql_expr_coll(pParse, pExpr->pLeft, &unused, @@ -2856,11 +2854,8 @@ sqlCodeSubselect(Parse * pParse, /* Parsing context */ jmpIfDynamic = -1; } r3 = sqlExprCodeTarget(pParse, pE2, r1); - enum field_type types[2] = - { lhs_type, field_type_MAX }; - sqlVdbeAddOp4(v, OP_MakeRecord, r3, - 1, r2, (char *)types, - sizeof(types)); + sqlVdbeAddOp3(v, OP_MakeRecord, r3, 1, + r2); sql_expr_type_cache_change(pParse, r3, 1); sqlVdbeAddOp2(v, OP_IdxInsert, r2, diff --git a/src/box/sql/fk_constraint.c b/src/box/sql/fk_constraint.c index 0dd10c420..2a9399d78 100644 --- a/src/box/sql/fk_constraint.c +++ b/src/box/sql/fk_constraint.c @@ -262,13 +262,8 @@ fk_constraint_lookup_parent(struct Parse *parse_context, struct space *parent, link->child_field + 1 + reg_data, temp_regs + i); } - struct index *idx = space_index(parent, referenced_idx); - assert(idx != NULL); - sqlVdbeAddOp4(v, OP_MakeRecord, temp_regs, field_count, - rec_reg, - (char *) sql_index_type_str(parse_context->db, - idx->def), - P4_DYNAMIC); + sqlVdbeAddOp3(v, OP_MakeRecord, temp_regs, field_count, + rec_reg); sqlVdbeAddOp4Int(v, OP_Found, cursor, ok_label, rec_reg, 0); sqlReleaseTempReg(parse_context, rec_reg); sqlReleaseTempRange(parse_context, temp_regs, field_count); diff --git a/src/box/sql/insert.c b/src/box/sql/insert.c index 02e9f9673..21b4f2407 100644 --- a/src/box/sql/insert.c +++ b/src/box/sql/insert.c @@ -42,20 +42,6 @@ #include "box/box.h" #include "box/schema.h" -enum field_type * -sql_index_type_str(struct sql *db, const struct index_def *idx_def) -{ - uint32_t column_count = idx_def->key_def->part_count; - uint32_t sz = (column_count + 1) * sizeof(enum field_type); - enum field_type *types = (enum field_type *) sqlDbMallocRaw(db, sz); - if (types == NULL) - return NULL; - for (uint32_t i = 0; i < column_count; i++) - types[i] = idx_def->key_def->parts[i].type; - types[column_count] = field_type_MAX; - return types; -} - void sql_emit_table_types(struct Vdbe *v, struct space_def *def, int reg) { diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index 6b3444cf3..adac80a64 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -3771,10 +3771,6 @@ int sqlVarintLen(u64 v); #define getVarint sqlGetVarint #define putVarint sqlPutVarint -/** Return string consisting of fields types of given index. */ -enum field_type * -sql_index_type_str(struct sql *db, const struct index_def *idx_def); - /** * Code an OP_ApplyType opcode that will force types * for given range of register starting from @reg. diff --git a/src/box/sql/update.c b/src/box/sql/update.c index 24c7cfa27..22f82390c 100644 --- a/src/box/sql/update.c +++ b/src/box/sql/update.c @@ -251,11 +251,7 @@ sqlUpdate(Parse * pParse, /* The parser context */ nKey = pk_part_count; regKey = iPk; } else { - enum field_type *types = is_view ? NULL : - sql_index_type_str(pParse->db, - pPk->def); - sqlVdbeAddOp4(v, OP_MakeRecord, iPk, pk_part_count, - regKey, (char *) types, P4_DYNAMIC); + sqlVdbeAddOp3(v, OP_MakeRecord, iPk, pk_part_count, regKey); /* * Set flag to save memory allocating one by * malloc. @@ -420,12 +416,8 @@ sqlUpdate(Parse * pParse, /* The parser context */ int key_reg; if (okOnePass) { key_reg = sqlGetTempReg(pParse); - enum field_type *types = - sql_index_type_str(pParse->db, - pPk->def); - sqlVdbeAddOp4(v, OP_MakeRecord, iPk, - pk_part_count, key_reg, - (char *) types, P4_DYNAMIC); + sqlVdbeAddOp3(v, OP_MakeRecord, iPk, + pk_part_count, key_reg); } else { assert(nKey == 0); key_reg = regKey; diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index aedff8b09..fcea9eefe 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -2049,24 +2049,17 @@ case OP_ApplyType: { break; } -/* Opcode: MakeRecord P1 P2 P3 P4 P5 +/* Opcode: MakeRecord P1 P2 P3 * P5 * Synopsis: r[P3]=mkrec(r[P1@P2]) * * Convert P2 registers beginning with P1 into the [record format] * use as a data record in a database table or as a key * in an index. The OP_Column opcode can decode the record later. * - * P4 may be a string that is P2 characters long. The nth character of the - * string indicates the column type that should be used for the nth - * field of the index key. - * - * If P4 is NULL then all index fields have type SCALAR. - * * If P5 is not NULL then record under construction is intended to be inserted * into ephemeral space. Thus, sort of memory optimization can be performed. */ case OP_MakeRecord: { - Mem *pRec; /* The new record */ Mem *pData0; /* First field to be combined into the record */ Mem MAYBE_UNUSED *pLast; /* Last field of the record */ int nField; /* Number of fields in the record */ @@ -2087,7 +2080,6 @@ case OP_MakeRecord: { * is the offset from the beginning of the record to data0. */ nField = pOp->p1; - enum field_type *types = pOp->p4.types; bIsEphemeral = pOp->p5; assert(nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1); pData0 = &aMem[nField]; @@ -2098,15 +2090,6 @@ case OP_MakeRecord: { assert(pOp->p3p1 || pOp->p3>=pOp->p1+pOp->p2); pOut = vdbe_prepare_null_out(p, pOp->p3); - /* Apply the requested types to all inputs */ - assert(pData0<=pLast); - if (types != NULL) { - pRec = pData0; - do { - mem_cast_implicit_old(pRec++, *(types++)); - } while(types[0] != field_type_MAX); - } - struct region *region = &fiber()->gc; size_t used = region_used(region); uint32_t tuple_size; diff --git a/src/box/sql/wherecode.c b/src/box/sql/wherecode.c index 92d374200..df6cc92e1 100644 --- a/src/box/sql/wherecode.c +++ b/src/box/sql/wherecode.c @@ -602,9 +602,6 @@ codeAllEqualityTerms(Parse * pParse, /* Parsing context */ nReg = pLoop->nEq + nExtraReg; pParse->nMem += nReg; - enum field_type *type = sql_index_type_str(pParse->db, idx_def); - assert(type != NULL || pParse->db->mallocFailed); - if (nSkip) { int iIdxCur = pLevel->iIdxCur; sqlVdbeAddOp1(v, (bRev ? OP_Last : OP_Rewind), iIdxCur); @@ -647,17 +644,7 @@ codeAllEqualityTerms(Parse * pParse, /* Parsing context */ sqlVdbeAddOp2(v, OP_SCopy, r1, regBase + j); } } - if (pTerm->eOperator & WO_IN) { - if (pTerm->pExpr->flags & EP_xIsSelect) { - /* No type ever needs to be (or should be) applied to a value - * from the RHS of an "? IN (SELECT ...)" expression. The - * sqlFindInIndex() routine has already ensured that the - * type of the comparison has been applied to the value. - */ - if (type != NULL) - type[j] = FIELD_TYPE_SCALAR; - } - } else if ((pTerm->eOperator & WO_ISNULL) == 0) { + if ((pTerm->eOperator & (WO_IN | WO_ISNULL)) == 0) { Expr *pRight = pTerm->pExpr->pRight; if (sqlExprCanBeNull(pRight)) { sqlVdbeAddOp2(v, OP_IsNull, regBase + j, diff --git a/test/sql-tap/cast.test.lua b/test/sql-tap/cast.test.lua index b570fc878..927114772 100755 --- a/test/sql-tap/cast.test.lua +++ b/test/sql-tap/cast.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool local test = require("sqltester") -test:plan(108) +test:plan(109) --!./tcltestrunner.lua -- 2005 June 25 @@ -1156,4 +1156,13 @@ test:do_execsql_test( 10000000000000000 }) +-- Make sure that there is no unnecessary implicit casts in IN operator. +test:do_catchsql_test( + "cast-12", + [[ + SELECT 1 IN (SELECT '1'); + ]], { + 1, "Type mismatch: can not convert integer(1) to string" + }) + test:finish_test() diff --git a/test/sql-tap/in3.test.lua b/test/sql-tap/in3.test.lua index 5f3f543af..4536fb0d3 100755 --- a/test/sql-tap/in3.test.lua +++ b/test/sql-tap/in3.test.lua @@ -342,7 +342,7 @@ test:do_test( return exec_neph(" SELECT y IN (SELECT a FROM t1) FROM t2 ") end, { -- - 1, true + 1, false -- }) @@ -366,7 +366,7 @@ test:do_test( return exec_neph(" SELECT y IN (SELECT c FROM t1) FROM t2 ") end, { -- - 1, true + 1, false -- })