Skip to content

Commit cf1535b

Browse files
authored
backtick column names in check constraints (#3040)
1 parent 220353c commit cf1535b

File tree

4 files changed

+60
-20
lines changed

4 files changed

+60
-20
lines changed

enginetest/queries/alter_table_queries.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ var AlterTableScripts = []ScriptTest{
135135
{
136136
Query: "SELECT * FROM information_schema.CHECK_CONSTRAINTS",
137137
Expected: []sql.Row{
138-
{"def", "mydb", "v1gt0", "(v1 > 0)"},
138+
{"def", "mydb", "v1gt0", "(`v1` > 0)"},
139139
},
140140
},
141141
},
@@ -1864,7 +1864,7 @@ var RenameColumnScripts = []ScriptTest{
18641864
Query: `SELECT TC.CONSTRAINT_NAME, CC.CHECK_CLAUSE, TC.ENFORCED
18651865
FROM information_schema.TABLE_CONSTRAINTS TC, information_schema.CHECK_CONSTRAINTS CC
18661866
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';`,
1867-
Expected: []sql.Row{{"test_check", "(i2 < 12345)", "YES"}},
1867+
Expected: []sql.Row{{"test_check", "(`i2` < 12345)", "YES"}},
18681868
},
18691869
},
18701870
},

enginetest/queries/check_scripts.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ var CreateCheckConstraintsScripts = []ScriptTest{
2929
Query: `SELECT TC.CONSTRAINT_NAME, CC.CHECK_CLAUSE, TC.ENFORCED
3030
FROM information_schema.TABLE_CONSTRAINTS TC, information_schema.CHECK_CONSTRAINTS CC
3131
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';`,
32-
Expected: []sql.Row{{"chk1", "(B > 0)", "YES"}, {"chk2", "(b > 0)", "NO"}, {"chk3", "(B > 1)", "YES"}, {"chk4", "(upper(C) = c)", "YES"}},
32+
Expected: []sql.Row{
33+
{"chk1", "(`B` > 0)", "YES"},
34+
{"chk2", "(`b` > 0)", "NO"},
35+
{"chk3", "(`B` > 1)", "YES"},
36+
{"chk4", "(upper(`C`) = `c`)", "YES"}},
3337
},
3438
},
3539
},
@@ -40,9 +44,7 @@ WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 'checks' AND TC.TABLE_SCHEMA = CC.C
4044
},
4145
Assertions: []ScriptTestAssertion{
4246
{
43-
Query: `SELECT LENGTH(TC.CONSTRAINT_NAME) > 0
44-
FROM information_schema.TABLE_CONSTRAINTS TC, information_schema.CHECK_CONSTRAINTS CC
45-
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)';`,
47+
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)';",
4648
Expected: []sql.Row{{true}},
4749
},
4850
},
@@ -66,7 +68,13 @@ CREATE TABLE T2
6668
Query: `SELECT CC.CHECK_CLAUSE
6769
FROM information_schema.TABLE_CONSTRAINTS TC, information_schema.CHECK_CONSTRAINTS CC
6870
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';`,
69-
Expected: []sql.Row{{"(c1 = c2)"}, {"(c1 > 10)"}, {"(c2 > 0)"}, {"(c3 < 100)"}, {"(c1 = 0)"}, {"(C1 > C3)"}},
71+
Expected: []sql.Row{
72+
{"(`c1` = `c2`)"},
73+
{"(`c1` > 10)"},
74+
{"(`c2` > 0)"},
75+
{"(`c3` < 100)"},
76+
{"(`c1` = 0)"},
77+
{"(`C1` > `C3`)"}},
7078
},
7179
},
7280
},
@@ -256,8 +264,8 @@ CREATE TABLE t4
256264
{
257265
Query: "SELECT * from information_schema.check_constraints where constraint_name IN ('mycheck', 'hcheck') ORDER BY constraint_name",
258266
Expected: []sql.Row{
259-
{"def", "mydb", "hcheck", "(height < 10)"},
260-
{"def", "mydb", "mycheck", "(test_score >= 50)"},
267+
{"def", "mydb", "hcheck", "(`height` < 10)"},
268+
{"def", "mydb", "mycheck", "(`test_score` >= 50)"},
261269
},
262270
},
263271
{
@@ -318,6 +326,36 @@ CREATE TABLE t4
318326
},
319327
},
320328
},
329+
{
330+
Name: "check constraints using keywords",
331+
SetUpScript: []string{
332+
"create table t (`order` int primary key, constraint chk check (`order` > 0));",
333+
},
334+
Assertions: []ScriptTestAssertion{
335+
{
336+
Query: "insert into t values (0);",
337+
ExpectedErr: sql.ErrCheckConstraintViolated,
338+
},
339+
{
340+
Query: "insert into t values (100);",
341+
Expected: []sql.Row{
342+
{types.NewOkResult(1)},
343+
},
344+
},
345+
{
346+
Query: "select * from t;",
347+
Expected: []sql.Row{
348+
{100},
349+
},
350+
},
351+
{
352+
Query: "show create table t;",
353+
Expected: []sql.Row{
354+
{"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"},
355+
},
356+
},
357+
},
358+
},
321359
}
322360

323361
var DropCheckConstraintsScripts = []ScriptTest{
@@ -336,7 +374,7 @@ var DropCheckConstraintsScripts = []ScriptTest{
336374
Query: `SELECT TC.CONSTRAINT_NAME, CC.CHECK_CLAUSE, TC.ENFORCED
337375
FROM information_schema.TABLE_CONSTRAINTS TC, information_schema.CHECK_CONSTRAINTS CC
338376
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';`,
339-
Expected: []sql.Row{{"chk3", "(c > 0)", "YES"}},
377+
Expected: []sql.Row{{"chk3", "(`c` > 0)", "YES"}},
340378
},
341379
},
342380
},

enginetest/queries/information_schema_queries.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,10 +1543,10 @@ FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='mydb' AND TABLE_NAME='all_ty
15431543
FROM information_schema.TABLE_CONSTRAINTS TC, information_schema.CHECK_CONSTRAINTS CC
15441544
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';`,
15451545
Expected: []sql.Row{
1546-
{"chk1", "(B > 0)", "YES"},
1547-
{"chk2", "(b > 0)", "NO"},
1548-
{"chk3", "(B > 1)", "YES"},
1549-
{"chk4", "(upper(C) = c)", "YES"},
1546+
{"chk1", "(`B` > 0)", "YES"},
1547+
{"chk2", "(`b` > 0)", "NO"},
1548+
{"chk3", "(`B` > 1)", "YES"},
1549+
{"chk4", "(upper(`C`) = `c`)", "YES"},
15501550
},
15511551
},
15521552
{
@@ -1562,10 +1562,10 @@ WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 'checks' AND TC.TABLE_SCHEMA = CC.C
15621562
{
15631563
Query: `select * from information_schema.check_constraints where constraint_schema = 'mydb';`,
15641564
Expected: []sql.Row{
1565-
{"def", "mydb", "chk1", "(B > 0)"},
1566-
{"def", "mydb", "chk2", "(b > 0)"},
1567-
{"def", "mydb", "chk3", "(B > 1)"},
1568-
{"def", "mydb", "chk4", "(upper(C) = c)"},
1565+
{"def", "mydb", "chk1", "(`B` > 0)"},
1566+
{"def", "mydb", "chk2", "(`b` > 0)"},
1567+
{"def", "mydb", "chk3", "(`B` > 1)"},
1568+
{"def", "mydb", "chk4", "(upper(`C`) = `c`)"},
15691569
},
15701570
},
15711571
{

sql/plan/alter_check.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ func NewCheckDefinition(ctx *sql.Context, check *sql.CheckConstraint) (*sql.Chec
157157
unqualifiedCols, _, err := transform.Expr(check.Expr, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
158158
gf, ok := e.(*expression.GetField)
159159
if ok {
160-
return expression.NewGetField(gf.Index(), gf.Type(), gf.Name(), gf.IsNullable()), transform.NewTree, nil
160+
newGf := expression.NewGetField(gf.Index(), gf.Type(), gf.Name(), gf.IsNullable())
161+
newGf = newGf.WithQuotedNames(sql.GlobalSchemaFormatter, true)
162+
return newGf, transform.NewTree, nil
161163
}
162164
return e, transform.SameTree, nil
163165
})
@@ -167,7 +169,7 @@ func NewCheckDefinition(ctx *sql.Context, check *sql.CheckConstraint) (*sql.Chec
167169

168170
return &sql.CheckDefinition{
169171
Name: check.Name,
170-
CheckExpression: fmt.Sprintf("%s", unqualifiedCols),
172+
CheckExpression: unqualifiedCols.String(),
171173
Enforced: check.Enforced,
172174
}, nil
173175
}

0 commit comments

Comments
 (0)