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 745C42C510 for ; Thu, 29 Nov 2018 08:10:03 -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 NHZkVuc08kYa for ; Thu, 29 Nov 2018 08:10:03 -0500 (EST) Received: from smtp52.i.mail.ru (smtp52.i.mail.ru [94.100.177.112]) (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 D19102C1D9 for ; Thu, 29 Nov 2018 08:09:56 -0500 (EST) From: Kirill Shcherbatov Subject: [tarantool-patches] [PATCH v1 2/6] sql: disallow use of TYPEOF in Check Date: Thu, 29 Nov 2018 16:09:46 +0300 Message-Id: <20eef78e5670691bd9932463f752daf11db96966.1543496928.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, v.shpilevoy@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. Need for #3691 --- src/box/sql/build.c | 23 ++++++++++ test/sql-tap/check.test.lua | 89 ++----------------------------------- 2 files changed, 26 insertions(+), 86 deletions(-) diff --git a/src/box/sql/build.c b/src/box/sql/build.c index 7d32f3874..4598087ae 100644 --- a/src/box/sql/build.c +++ b/src/box/sql/build.c @@ -852,12 +852,35 @@ primary_key_exit: return; } +static int +sql_check_expr_test_callback(struct Walker *walker, struct Expr *expr) +{ + if (expr->op == TK_FUNCTION && strcmp(expr->u.zToken, "TYPEOF") == 0) { + diag_set(ClientError, ER_SQL, "TYPEOF function is forbidden " + "for usage in Check constraint"); + walker->eCode = 1; + return WRC_Abort; + } + return WRC_Continue; +} + void sql_add_check_constraint(struct Parse *parser, struct ExprSpan *span) { struct Expr *expr = span->pExpr; struct Table *table = parser->pNewTable; if (table != NULL) { + /* Test if Check expression valid. */ + struct Walker w; + memset(&w, 0, sizeof(w)); + w.xExprCallback = sql_check_expr_test_callback; + sqlite3WalkExpr(&w, expr); + if (w.eCode != 0) { + parser->rc = SQL_TARANTOOL_ERROR; + parser->nErr++; + goto release_expr; + } + expr->u.zToken = sqlite3DbStrNDup(parser->db, (char *)span->zStart, (int)(span->zEnd - span->zStart)); diff --git a/test/sql-tap/check.test.lua b/test/sql-tap/check.test.lua index c24ae2ff1..e78f06b3b 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(51) --!./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,93 +212,10 @@ test:do_execsql_test( ); ]], { -- - + 1, "SQL error: TYPEOF function is forbidden for usage 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( - "check-2.10", - [[ - CREATE TABLE t2b( - x INTEGER CHECK( typeof(coalesce(x,0))=='integer' ) CONSTRAINT one, - PRIMARY KEY (x) - ); - ]], { - -- - 1,"near \",\": syntax error" - -- - }) - -test:do_catchsql_test( - "check-2.11", - [[ - CREATE TABLE t2c( - x INTEGER CONSTRAINT one CHECK( typeof(coalesce(x,0))=='integer' ) - CONSTRAINT two, - PRIMARY KEY (x) - ); - ]], { - -- - 1,"near \",\": syntax error" - -- - }) - test:do_execsql_test( "check-2.cleanup", [[ -- 2.19.2