From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 6D8B9261A7 for ; Wed, 30 Jan 2019 03:59:21 -0500 (EST) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id I2guF-Ifn0bg for ; Wed, 30 Jan 2019 03:59:21 -0500 (EST) Received: from smtp54.i.mail.ru (smtp54.i.mail.ru [217.69.128.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 185462619E for ; Wed, 30 Jan 2019 03:59:21 -0500 (EST) From: Kirill Shcherbatov Subject: [tarantool-patches] [PATCH v2 6/9] sql: disallow use of TYPEOF in Check Date: Wed, 30 Jan 2019 11:59:13 +0300 Message-Id: <1ca353f2acec30ae27d59684a08005f25fbd5228.1548838034.git.kshcherbatov@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: tarantool-patches@freelists.org, korablev@tarantool.org Cc: Kirill Shcherbatov Due to the fact that we are going to perform CHECKs validations on the server side, checks are performed on the fields with affinity already applied that may differ with type of original data. After the introduction of static types, the need for type checks based on the CHECKs disappeared. Needed for #3691 --- src/box/ck_constraint.c | 24 +++++++++++++-- src/box/sql/resolve.c | 3 ++ src/box/sql/sqliteInt.h | 3 ++ test/sql-tap/check.test.lua | 60 ++----------------------------------- 4 files changed, 31 insertions(+), 59 deletions(-) diff --git a/src/box/ck_constraint.c b/src/box/ck_constraint.c index 044416a4f..9d268d98b 100644 --- a/src/box/ck_constraint.c +++ b/src/box/ck_constraint.c @@ -58,8 +58,22 @@ ck_constraint_resolve_column_reference(struct Expr *expr, memset(&dummy_table, 0, sizeof(dummy_table)); dummy_table.def = (struct space_def *)space_def; - sql_resolve_self_reference(&parser, &dummy_table, NC_IsCheck, - expr, NULL); + /* Fake SrcList for parser->pNewTable */ + struct SrcList sSrc; + /* Name context for parser->pNewTable */ + struct NameContext sNC; + + memset(&sNC, 0, sizeof(sNC)); + memset(&sSrc, 0, sizeof(sSrc)); + sSrc.nSrc = 1; + sSrc.a[0].zName = (char *)space_def->name; + sSrc.a[0].pTab = &dummy_table; + sSrc.a[0].iCursor = -1; + sNC.pParse = &parser; + sNC.pSrcList = &sSrc; + sNC.ncFlags = NC_IsCheck; + sqlite3ResolveExprNames(&sNC, expr); + int rc = 0; if (parser.rc != SQLITE_OK) { /* Tarantool error may be already set with diag. */ @@ -69,6 +83,12 @@ ck_constraint_resolve_column_reference(struct Expr *expr, } rc = -1; } + if (sNC.ncFlags & NC_HasTypeofFunction) { + diag_set(ClientError, ER_CREATE_CK_CONSTRAINT, + ck_constraint_name, + "TYPEOF is forbidden in check constraint"); + rc = -1; + } sql_parser_destroy(&parser); return rc; } diff --git a/src/box/sql/resolve.c b/src/box/sql/resolve.c index 6462467bc..d728f1523 100644 --- a/src/box/sql/resolve.c +++ b/src/box/sql/resolve.c @@ -727,6 +727,9 @@ resolveExprStep(Walker * pWalker, Expr * pExpr) nId, zId); pNC->nErr++; } + if (pDef != NULL && + (pDef->funcFlags & SQLITE_FUNC_TYPEOF) != 0) + pNC->ncFlags |= NC_HasTypeofFunction; if (is_agg) pNC->ncFlags &= ~NC_AllowAgg; sqlite3WalkExprList(pWalker, pList); diff --git a/src/box/sql/sqliteInt.h b/src/box/sql/sqliteInt.h index c6678e884..68640c728 100644 --- a/src/box/sql/sqliteInt.h +++ b/src/box/sql/sqliteInt.h @@ -2456,6 +2456,9 @@ struct NameContext { #define NC_MinMaxAgg 0x1000 /* min/max aggregates seen. See note above */ /** One or more identifiers are out of aggregate function. */ #define NC_HasUnaggregatedId 0x2000 +/** One or more identifiers are in TYPEOF function. */ +#define NC_HasTypeofFunction 0x4000 + /* * An instance of the following structure contains all information * needed to generate code for a single SELECT statement. diff --git a/test/sql-tap/check.test.lua b/test/sql-tap/check.test.lua index c419e535d..31dd033a2 100755 --- a/test/sql-tap/check.test.lua +++ b/test/sql-tap/check.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool test = require("sqltester") -test:plan(58) +test:plan(53) --!./tcltestrunner.lua -- 2005 November 2 @@ -201,7 +201,7 @@ test:do_execsql_test( -- }) -test:do_execsql_test( +test:do_catchsql_test( "check-2.1", [[ CREATE TABLE t2( @@ -212,64 +212,10 @@ test:do_execsql_test( ); ]], { -- - + 1, "Failed to create check constraint 'THREE': TYPEOF is forbidden in check constraint" -- }) -test:do_execsql_test( - "check-2.2", - [[ - INSERT INTO t2 VALUES(1, 1,2.2,'three'); - SELECT x, y, z FROM t2; - ]], { - -- - 1, 2.2, "three" - -- - }) - ---db("close") ---sqlite3("db", "test.db") -test:do_execsql_test( - "check-2.3", - [[ - INSERT INTO t2 VALUES(2, NULL, NULL, NULL); - SELECT x, y, z FROM t2; - ]], { - -- - 1, 2.2, "three", "", "", "" - -- - }) - -test:do_catchsql_test( - "check-2.4", - [[ - INSERT INTO t2 VALUES(3, 1.1, NULL, NULL); - ]], { - -- - 1, "Check constraint failed: ONE" - -- - }) - -test:do_catchsql_test( - "check-2.5", - [[ - INSERT INTO t2 VALUES(4, NULL, 5, NULL); - ]], { - -- - 1, "Check constraint failed: TWO" - -- - }) - -test:do_catchsql_test( - "check-2.6", - [[ - INSERT INTO t2 VALUES(5, NULL, NULL, 3.14159); - ]], { - -- - 1, "Check constraint failed: THREE" - -- - }) - -- gh-3504: Check the CONSTRAINT name clause can't follow a constraint. test:do_catchsql_test( -- 2.19.2