Tarantool development patches archive
 help / color / mirror / Atom feed
From: Kirill Shcherbatov <kshcherbatov@tarantool.org>
To: tarantool-patches@freelists.org, korablev@tarantool.org
Cc: Kirill Shcherbatov <kshcherbatov@tarantool.org>
Subject: [tarantool-patches] [PATCH v1 2/4] sql: disallow use of TYPEOF in Check
Date: Thu, 10 Jan 2019 16:54:48 +0300	[thread overview]
Message-ID: <467d962bb970e77481307b7e73892e35c1a6601f.1547128310.git.kshcherbatov@tarantool.org> (raw)
In-Reply-To: <cover.1547128310.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 7e5bcc518..9c6b1b49a 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -756,12 +756,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 1f369fb02..1176ad500 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:[~2019-01-10 13:54 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-10 13:54 [tarantool-patches] [PATCH v1 0/4] sql: Checks on server side Kirill Shcherbatov
2019-01-10 13:54 ` [tarantool-patches] [PATCH v1 1/4] box: rename space->opts checks to checks_ast Kirill Shcherbatov
2019-01-11 14:05   ` [tarantool-patches] " Konstantin Osipov
2019-01-18 18:00   ` Konstantin Osipov
2019-01-10 13:54 ` Kirill Shcherbatov [this message]
2019-01-11 14:06   ` [tarantool-patches] Re: [PATCH v1 2/4] sql: disallow use of TYPEOF in Check Konstantin Osipov
2019-01-11 14:07   ` Konstantin Osipov
2019-01-18 18:04   ` Konstantin Osipov
2019-01-10 13:54 ` [tarantool-patches] [PATCH v1 3/4] box: exported sql_bind structure and API Kirill Shcherbatov
2019-01-10 13:54 ` [tarantool-patches] [PATCH v1 4/4] sql: make sql checks on server side Kirill Shcherbatov
2019-01-11 14:12   ` [tarantool-patches] " Konstantin Osipov
2019-01-11 14:14   ` Konstantin Osipov
2019-01-18 18:11   ` Konstantin Osipov
2019-01-21 14:47   ` n.pettik

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=467d962bb970e77481307b7e73892e35c1a6601f.1547128310.git.kshcherbatov@tarantool.org \
    --to=kshcherbatov@tarantool.org \
    --cc=korablev@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [tarantool-patches] [PATCH v1 2/4] 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