Tarantool development patches archive
 help / color / mirror / Atom feed
From: Kirill Shcherbatov <kshcherbatov@tarantool.org>
To: tarantool-patches@freelists.org, v.shpilevoy@tarantool.org
Cc: Kirill Shcherbatov <kshcherbatov@tarantool.org>
Subject: [tarantool-patches] [PATCH v1 2/6] sql: disallow use of TYPEOF in Check
Date: Thu, 29 Nov 2018 16:09:46 +0300	[thread overview]
Message-ID: <20eef78e5670691bd9932463f752daf11db96966.1543496928.git.kshcherbatov@tarantool.org> (raw)
In-Reply-To: <cover.1543496928.git.kshcherbatov@tarantool.org>

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(
         -- </check-1.17>
     })
 
-test:do_execsql_test(
+test:do_catchsql_test(
     "check-2.1",
     [[
         CREATE TABLE t2(
@@ -212,93 +212,10 @@ test:do_execsql_test(
         );
     ]], {
         -- <check-2.1>
-
+        1, "SQL error: TYPEOF function is forbidden for usage in Check constraint"
         -- </check-2.1>
     })
 
-test:do_execsql_test(
-    "check-2.2",
-    [[
-        INSERT INTO t2 VALUES(1, 1,2.2,'three');
-        SELECT x, y, z FROM t2;
-    ]], {
-        -- <check-2.2>
-        1, 2.2, "three"
-        -- </check-2.2>
-    })
-
---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;
-    ]], {
-        -- <check-2.3>
-        1, 2.2, "three", "", "", ""
-        -- </check-2.3>
-    })
-
-test:do_catchsql_test(
-    "check-2.4",
-    [[
-        INSERT INTO t2 VALUES(3, 1.1, NULL, NULL);
-    ]], {
-        -- <check-2.4>
-        1, "CHECK constraint failed: ONE"
-        -- </check-2.4>
-    })
-
-test:do_catchsql_test(
-    "check-2.5",
-    [[
-        INSERT INTO t2 VALUES(4, NULL, 5, NULL);
-    ]], {
-        -- <check-2.5>
-        1, "CHECK constraint failed: TWO"
-        -- </check-2.5>
-    })
-
-test:do_catchsql_test(
-    "check-2.6",
-    [[
-        INSERT INTO t2 VALUES(5, NULL, NULL, 3.14159);
-    ]], {
-        -- <check-2.6>
-        1, "CHECK constraint failed: THREE"
-        -- </check-2.6>
-    })
-
--- 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)
-        );
-    ]], {
-        -- <check-2.10>
-        1,"near \",\": syntax error"
-        -- </check-2.10>
-    })
-
-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)
-        );
-    ]], {
-        -- <check-2.10>
-        1,"near \",\": syntax error"
-        -- </check-2.10>
-    })
-
 test:do_execsql_test(
     "check-2.cleanup",
     [[
-- 
2.19.2

  parent reply	other threads:[~2018-11-29 13:10 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-29 13:09 [tarantool-patches] [PATCH v1 0/6] sql: Checks on server side Kirill Shcherbatov
2018-11-29 13:09 ` [tarantool-patches] [PATCH v1 1/6] box: rename space->opts checks to checks_ast Kirill Shcherbatov
2018-11-29 13:09 ` Kirill Shcherbatov [this message]
2018-11-29 13:09 ` [tarantool-patches] [PATCH v1 3/6] box: exported sql_bind structure and API Kirill Shcherbatov
2018-11-29 13:09 ` [tarantool-patches] [PATCH v1 4/6] sql: release OP_Idx{Insert,Replace} p2 argument Kirill Shcherbatov
2018-11-29 13:09 ` [tarantool-patches] [PATCH v1 5/6] sql: fix incomplete UPDATE on IdxInsert failured Kirill Shcherbatov
2018-11-29 13:09 ` [tarantool-patches] [PATCH v1 6/6] sql: make sql checks on server side Kirill Shcherbatov
2018-11-29 14:09   ` [tarantool-patches] " Kirill Shcherbatov
2018-11-29 18:41   ` Kirill Shcherbatov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20eef78e5670691bd9932463f752daf11db96966.1543496928.git.kshcherbatov@tarantool.org \
    --to=kshcherbatov@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [tarantool-patches] [PATCH v1 2/6] sql: disallow use of TYPEOF in Check' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox