diff --git a/enginetest/queries/alter_table_queries.go b/enginetest/queries/alter_table_queries.go index a6103a8aff..fc2b1c62d8 100644 --- a/enginetest/queries/alter_table_queries.go +++ b/enginetest/queries/alter_table_queries.go @@ -135,7 +135,7 @@ var AlterTableScripts = []ScriptTest{ { Query: "SELECT * FROM information_schema.CHECK_CONSTRAINTS", Expected: []sql.Row{ - {"def", "mydb", "v1gt0", "(v1 > 0)"}, + {"def", "mydb", "v1gt0", "(`v1` > 0)"}, }, }, }, @@ -1864,7 +1864,7 @@ var RenameColumnScripts = []ScriptTest{ Query: `SELECT TC.CONSTRAINT_NAME, CC.CHECK_CLAUSE, TC.ENFORCED FROM information_schema.TABLE_CONSTRAINTS TC, information_schema.CHECK_CONSTRAINTS CC WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 'mytable' AND TC.TABLE_SCHEMA = CC.CONSTRAINT_SCHEMA AND TC.CONSTRAINT_NAME = CC.CONSTRAINT_NAME AND TC.CONSTRAINT_TYPE = 'CHECK';`, - Expected: []sql.Row{{"test_check", "(i2 < 12345)", "YES"}}, + Expected: []sql.Row{{"test_check", "(`i2` < 12345)", "YES"}}, }, }, }, diff --git a/enginetest/queries/check_scripts.go b/enginetest/queries/check_scripts.go index 2c8ac64e11..32637b3b7a 100644 --- a/enginetest/queries/check_scripts.go +++ b/enginetest/queries/check_scripts.go @@ -29,7 +29,11 @@ var CreateCheckConstraintsScripts = []ScriptTest{ Query: `SELECT TC.CONSTRAINT_NAME, CC.CHECK_CLAUSE, TC.ENFORCED FROM information_schema.TABLE_CONSTRAINTS TC, information_schema.CHECK_CONSTRAINTS CC WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 'checks' AND TC.TABLE_SCHEMA = CC.CONSTRAINT_SCHEMA AND TC.CONSTRAINT_NAME = CC.CONSTRAINT_NAME AND TC.CONSTRAINT_TYPE = 'CHECK';`, - Expected: []sql.Row{{"chk1", "(B > 0)", "YES"}, {"chk2", "(b > 0)", "NO"}, {"chk3", "(B > 1)", "YES"}, {"chk4", "(upper(C) = c)", "YES"}}, + Expected: []sql.Row{ + {"chk1", "(`B` > 0)", "YES"}, + {"chk2", "(`b` > 0)", "NO"}, + {"chk3", "(`B` > 1)", "YES"}, + {"chk4", "(upper(`C`) = `c`)", "YES"}}, }, }, }, @@ -40,9 +44,7 @@ WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 'checks' AND TC.TABLE_SCHEMA = CC.C }, Assertions: []ScriptTestAssertion{ { - Query: `SELECT LENGTH(TC.CONSTRAINT_NAME) > 0 -FROM information_schema.TABLE_CONSTRAINTS TC, information_schema.CHECK_CONSTRAINTS CC -WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 'checks' AND TC.TABLE_SCHEMA = CC.CONSTRAINT_SCHEMA AND TC.CONSTRAINT_NAME = CC.CONSTRAINT_NAME AND TC.CONSTRAINT_TYPE = 'CHECK' AND CC.CHECK_CLAUSE = '(b > 100)';`, + Query: "SELECT LENGTH(TC.CONSTRAINT_NAME) > 0 FROM information_schema.TABLE_CONSTRAINTS TC, information_schema.CHECK_CONSTRAINTS CC WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 'checks' AND TC.TABLE_SCHEMA = CC.CONSTRAINT_SCHEMA AND TC.CONSTRAINT_NAME = CC.CONSTRAINT_NAME AND TC.CONSTRAINT_TYPE = 'CHECK' AND CC.CHECK_CLAUSE = '(`b` > 100)';", Expected: []sql.Row{{true}}, }, }, @@ -66,7 +68,13 @@ CREATE TABLE T2 Query: `SELECT CC.CHECK_CLAUSE FROM information_schema.TABLE_CONSTRAINTS TC, information_schema.CHECK_CONSTRAINTS CC WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 't2' AND TC.TABLE_SCHEMA = CC.CONSTRAINT_SCHEMA AND TC.CONSTRAINT_NAME = CC.CONSTRAINT_NAME AND TC.CONSTRAINT_TYPE = 'CHECK';`, - Expected: []sql.Row{{"(c1 = c2)"}, {"(c1 > 10)"}, {"(c2 > 0)"}, {"(c3 < 100)"}, {"(c1 = 0)"}, {"(C1 > C3)"}}, + Expected: []sql.Row{ + {"(`c1` = `c2`)"}, + {"(`c1` > 10)"}, + {"(`c2` > 0)"}, + {"(`c3` < 100)"}, + {"(`c1` = 0)"}, + {"(`C1` > `C3`)"}}, }, }, }, @@ -256,8 +264,8 @@ CREATE TABLE t4 { Query: "SELECT * from information_schema.check_constraints where constraint_name IN ('mycheck', 'hcheck') ORDER BY constraint_name", Expected: []sql.Row{ - {"def", "mydb", "hcheck", "(height < 10)"}, - {"def", "mydb", "mycheck", "(test_score >= 50)"}, + {"def", "mydb", "hcheck", "(`height` < 10)"}, + {"def", "mydb", "mycheck", "(`test_score` >= 50)"}, }, }, { @@ -318,6 +326,36 @@ CREATE TABLE t4 }, }, }, + { + Name: "check constraints using keywords", + SetUpScript: []string{ + "create table t (`order` int primary key, constraint chk check (`order` > 0));", + }, + Assertions: []ScriptTestAssertion{ + { + Query: "insert into t values (0);", + ExpectedErr: sql.ErrCheckConstraintViolated, + }, + { + Query: "insert into t values (100);", + Expected: []sql.Row{ + {types.NewOkResult(1)}, + }, + }, + { + Query: "select * from t;", + Expected: []sql.Row{ + {100}, + }, + }, + { + Query: "show create table t;", + Expected: []sql.Row{ + {"t", "CREATE TABLE `t` (\n `order` int NOT NULL,\n PRIMARY KEY (`order`),\n CONSTRAINT `chk` CHECK ((`order` > 0))\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}, + }, + }, + }, + }, } var DropCheckConstraintsScripts = []ScriptTest{ @@ -336,7 +374,7 @@ var DropCheckConstraintsScripts = []ScriptTest{ Query: `SELECT TC.CONSTRAINT_NAME, CC.CHECK_CLAUSE, TC.ENFORCED FROM information_schema.TABLE_CONSTRAINTS TC, information_schema.CHECK_CONSTRAINTS CC WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 't1' AND TC.TABLE_SCHEMA = CC.CONSTRAINT_SCHEMA AND TC.CONSTRAINT_NAME = CC.CONSTRAINT_NAME AND TC.CONSTRAINT_TYPE = 'CHECK';`, - Expected: []sql.Row{{"chk3", "(c > 0)", "YES"}}, + Expected: []sql.Row{{"chk3", "(`c` > 0)", "YES"}}, }, }, }, diff --git a/enginetest/queries/information_schema_queries.go b/enginetest/queries/information_schema_queries.go index 2c25849fce..4b8c584793 100644 --- a/enginetest/queries/information_schema_queries.go +++ b/enginetest/queries/information_schema_queries.go @@ -1543,10 +1543,10 @@ FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='mydb' AND TABLE_NAME='all_ty FROM information_schema.TABLE_CONSTRAINTS TC, information_schema.CHECK_CONSTRAINTS CC WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 'checks' AND TC.TABLE_SCHEMA = CC.CONSTRAINT_SCHEMA AND TC.CONSTRAINT_NAME = CC.CONSTRAINT_NAME AND TC.CONSTRAINT_TYPE = 'CHECK';`, Expected: []sql.Row{ - {"chk1", "(B > 0)", "YES"}, - {"chk2", "(b > 0)", "NO"}, - {"chk3", "(B > 1)", "YES"}, - {"chk4", "(upper(C) = c)", "YES"}, + {"chk1", "(`B` > 0)", "YES"}, + {"chk2", "(`b` > 0)", "NO"}, + {"chk3", "(`B` > 1)", "YES"}, + {"chk4", "(upper(`C`) = `c`)", "YES"}, }, }, { @@ -1562,10 +1562,10 @@ WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 'checks' AND TC.TABLE_SCHEMA = CC.C { Query: `select * from information_schema.check_constraints where constraint_schema = 'mydb';`, Expected: []sql.Row{ - {"def", "mydb", "chk1", "(B > 0)"}, - {"def", "mydb", "chk2", "(b > 0)"}, - {"def", "mydb", "chk3", "(B > 1)"}, - {"def", "mydb", "chk4", "(upper(C) = c)"}, + {"def", "mydb", "chk1", "(`B` > 0)"}, + {"def", "mydb", "chk2", "(`b` > 0)"}, + {"def", "mydb", "chk3", "(`B` > 1)"}, + {"def", "mydb", "chk4", "(upper(`C`) = `c`)"}, }, }, { diff --git a/sql/plan/alter_check.go b/sql/plan/alter_check.go index ed7ce5f406..d32b3cf0c1 100644 --- a/sql/plan/alter_check.go +++ b/sql/plan/alter_check.go @@ -157,7 +157,9 @@ func NewCheckDefinition(ctx *sql.Context, check *sql.CheckConstraint) (*sql.Chec unqualifiedCols, _, err := transform.Expr(check.Expr, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) { gf, ok := e.(*expression.GetField) if ok { - return expression.NewGetField(gf.Index(), gf.Type(), gf.Name(), gf.IsNullable()), transform.NewTree, nil + newGf := expression.NewGetField(gf.Index(), gf.Type(), gf.Name(), gf.IsNullable()) + newGf = newGf.WithQuotedNames(sql.GlobalSchemaFormatter, true) + return newGf, transform.NewTree, nil } return e, transform.SameTree, nil }) @@ -167,7 +169,7 @@ func NewCheckDefinition(ctx *sql.Context, check *sql.CheckConstraint) (*sql.Chec return &sql.CheckDefinition{ Name: check.Name, - CheckExpression: fmt.Sprintf("%s", unqualifiedCols), + CheckExpression: unqualifiedCols.String(), Enforced: check.Enforced, }, nil }