Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions enginetest/queries/alter_table_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)"},
},
},
},
Expand Down Expand Up @@ -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"}},
},
},
},
Expand Down
54 changes: 46 additions & 8 deletions enginetest/queries/check_scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
},
},
},
Expand All @@ -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}},
},
},
Expand All @@ -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`)"}},
},
},
},
Expand Down Expand Up @@ -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)"},
},
},
{
Expand Down Expand Up @@ -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{
Expand All @@ -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"}},
},
},
},
Expand Down
16 changes: 8 additions & 8 deletions enginetest/queries/information_schema_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
},
{
Expand All @@ -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`)"},
},
},
{
Expand Down
6 changes: 4 additions & 2 deletions sql/plan/alter_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -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
}
Expand Down
Loading