Skip to content

Commit d97de81

Browse files
authored
resolve column defaults for views (#2699)
1 parent 9ee86c8 commit d97de81

File tree

6 files changed

+85
-7
lines changed

6 files changed

+85
-7
lines changed

enginetest/queries/information_schema_queries.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,18 +1158,27 @@ FROM INFORMATION_SCHEMA.TRIGGERS WHERE trigger_schema = 'mydb'`,
11581158
SetUpScript: []string{
11591159
"USE foo",
11601160
"drop table othertable",
1161-
"CREATE TABLE t (i int primary key, j int)",
1162-
"CREATE VIEW v as select i + 1, j * 2, mod(i, j) from t",
1161+
"CREATE TABLE t (i int primary key, j int default (uuid_to_bin(uuid())));",
1162+
"CREATE VIEW v as select i + 1, j, j * 2, mod(i, j) from t;",
1163+
"create table tt (ii int primary key, jj int default (pow(1, 2)));",
1164+
"create view vv as select * from t join tt where i = ii;",
11631165
},
11641166
Assertions: []ScriptTestAssertion{
11651167
{
11661168
Query: "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'foo'",
11671169
Expected: []sql.Row{
11681170
{"def", "foo", "t", "i", uint32(1), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "PRI", "", "insert,references,select,update", "", "", nil},
1169-
{"def", "foo", "t", "j", uint32(2), nil, "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
1171+
{"def", "foo", "t", "j", uint32(2), "UUID_TO_BIN(uuid())", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
1172+
{"def", "foo", "tt", "ii", uint32(1), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "PRI", "", "insert,references,select,update", "", "", nil},
1173+
{"def", "foo", "tt", "jj", uint32(2), "power(1, 2)", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
11701174
{"def", "foo", "v", "i + 1", uint32(1), nil, "NO", "bigint", nil, nil, 19, 0, nil, nil, nil, "bigint", "", "", "insert,references,select,update", "", "", nil},
1171-
{"def", "foo", "v", "j * 2", uint32(2), nil, "YES", "bigint", nil, nil, 19, 0, nil, nil, nil, "bigint", "", "", "insert,references,select,update", "", "", nil},
1172-
{"def", "foo", "v", "mod(i, j)", uint32(3), nil, "YES", "decimal", nil, nil, 10, 0, nil, nil, nil, "decimal(10,0)", "", "", "insert,references,select,update", "", "", nil},
1175+
{"def", "foo", "v", "j", uint32(2), "UUID_TO_BIN(uuid())", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
1176+
{"def", "foo", "v", "j * 2", uint32(3), nil, "YES", "bigint", nil, nil, 19, 0, nil, nil, nil, "bigint", "", "", "insert,references,select,update", "", "", nil},
1177+
{"def", "foo", "v", "mod(i, j)", uint32(4), nil, "YES", "decimal", nil, nil, 10, 0, nil, nil, nil, "decimal(10,0)", "", "", "insert,references,select,update", "", "", nil},
1178+
{"def", "foo", "vv", "i", uint32(1), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
1179+
{"def", "foo", "vv", "j", uint32(2), "UUID_TO_BIN(uuid())", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
1180+
{"def", "foo", "vv", "ii", uint32(3), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
1181+
{"def", "foo", "vv", "jj", uint32(4), "power(1, 2)", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
11731182
},
11741183
},
11751184
},

enginetest/queries/view_queries.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,11 @@ CREATE TABLE tab1 (
203203
SetUpScript: []string{
204204
"create table t (i int primary key, j int default 100);",
205205
"insert into t(i) values (1);",
206+
"create table tt (ii int primary key, jj int default (pow(11, 2)));",
207+
"insert into tt values (1, default), (3, 4);",
206208
"create view v as select * from t;",
207209
"create view v1 as select i, j + 10 as jj from t;",
210+
"create view vv as select i, ii, j, jj, i + ii + j + jj from t join tt where i = ii;",
208211
},
209212
Assertions: []ScriptTestAssertion{
210213
{
@@ -228,6 +231,12 @@ CREATE TABLE tab1 (
228231
{"j", "int", "YES", "", "100", ""},
229232
},
230233
},
234+
{
235+
Query: "select * from v",
236+
Expected: []sql.Row{
237+
{1, 100},
238+
},
239+
},
231240
{
232241
Query: "show full columns from v1;",
233242
Expected: []sql.Row{
@@ -249,6 +258,48 @@ CREATE TABLE tab1 (
249258
{"jj", "bigint", "YES", "", nil, ""},
250259
},
251260
},
261+
{
262+
Query: "select * from v1",
263+
Expected: []sql.Row{
264+
{1, 110},
265+
},
266+
},
267+
{
268+
Query: "show full columns from vv;",
269+
Expected: []sql.Row{
270+
{"i", "int", nil, "NO", "", nil, "", "", ""},
271+
{"ii", "int", nil, "NO", "", nil, "", "", ""},
272+
{"j", "int", nil, "YES", "", "100", "", "", ""},
273+
{"jj", "int", nil, "YES", "", "(power(11, 2))", "DEFAULT_GENERATED", "", ""},
274+
{"i + ii + j + jj", "bigint", nil, "YES", "", nil, "", "", ""},
275+
},
276+
},
277+
{
278+
Query: "show columns from vv;",
279+
Expected: []sql.Row{
280+
{"i", "int", "NO", "", nil, ""},
281+
{"ii", "int", "NO", "", nil, ""},
282+
{"j", "int", "YES", "", "100", ""},
283+
{"jj", "int", "YES", "", "(power(11, 2))", "DEFAULT_GENERATED"},
284+
{"i + ii + j + jj", "bigint", "YES", "", nil, ""},
285+
},
286+
},
287+
{
288+
Query: "describe vv;",
289+
Expected: []sql.Row{
290+
{"i", "int", "NO", "", nil, ""},
291+
{"ii", "int", "NO", "", nil, ""},
292+
{"j", "int", "YES", "", "100", ""},
293+
{"jj", "int", "YES", "", "(power(11, 2))", "DEFAULT_GENERATED"},
294+
{"i + ii + j + jj", "bigint", "YES", "", nil, ""},
295+
},
296+
},
297+
{
298+
Query: "select * from vv",
299+
Expected: []sql.Row{
300+
{1, 1, 100, 121, 223},
301+
},
302+
},
252303
},
253304
},
254305
}

sql/information_schema/columns_table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func getRowsFromViews(ctx *sql.Context, catalog sql.Catalog, db DbWithNames, pri
384384
}
385385
privSetTbl := privSetDb.Table(view.Name)
386386
curPrivSetMap := getCurrentPrivSetMapForColumn(privSetDb.ToSlice(), privSetMap)
387-
for i, col := range createViewNode.Definition.Schema() {
387+
for i, col := range createViewNode.TargetSchema() {
388388
r := getRowFromColumn(ctx, i, col, db.CatalogName, db.SchemaName, view.Name, "", privSetTbl, curPrivSetMap)
389389
if r != nil {
390390
rows = append(rows, r)

sql/plan/create_view.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
type CreateView struct {
3030
UnaryNode
3131
database sql.Database
32+
targetSchema sql.Schema
3233
Name string
3334
IsReplace bool
3435
Definition *SubqueryAlias
@@ -41,6 +42,7 @@ type CreateView struct {
4142

4243
var _ sql.Node = (*CreateView)(nil)
4344
var _ sql.CollationCoercible = (*CreateView)(nil)
45+
var _ sql.SchemaTarget = (*CreateView)(nil)
4446

4547
// NewCreateView creates a CreateView node with the specified parameters,
4648
// setting its catalog to nil.
@@ -135,6 +137,18 @@ func (cv *CreateView) WithDatabase(database sql.Database) (sql.Node, error) {
135137
return &newCreate, nil
136138
}
137139

140+
// WithTargetSchema implements the SchemaTarget interface.
141+
func (cv *CreateView) WithTargetSchema(sch sql.Schema) (sql.Node, error) {
142+
ncv := *cv
143+
ncv.targetSchema = sch
144+
return &ncv, nil
145+
}
146+
147+
// TargetSchema implements the SchemaTarget interface.
148+
func (cv *CreateView) TargetSchema() sql.Schema {
149+
return cv.targetSchema
150+
}
151+
138152
// GetIsUpdatableFromCreateView returns whether the view is updatable or not.
139153
// https://dev.mysql.com/doc/refman/8.0/en/view-updatability.html
140154
func GetIsUpdatableFromCreateView(cv *CreateView) bool {

sql/plan/project.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func findDefault(node sql.Node, gf *expression.GetField) *sql.ColumnDefaultValue
5353
return findDefault(n.Child, gf)
5454
case *HashLookup:
5555
return findDefault(n.Child, gf)
56+
case *Filter:
57+
return findDefault(n.Child, gf)
5658
case *JoinNode:
5759
if defVal := findDefault(n.Left(), gf); defVal != nil {
5860
return defVal

sql/planbuilder/create_ddl.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@ func (b *Builder) buildCreateView(inScope *scope, subQuery string, fullQuery str
472472
dbName = b.ctx.GetCurrentDatabase()
473473
}
474474
db := b.resolveDb(dbName)
475-
outScope.node = plan.NewCreateView(db, c.ViewSpec.ViewName.Name.String(), queryAlias, c.OrReplace, subQuery, c.ViewSpec.Algorithm, definer, c.ViewSpec.Security)
475+
createView := plan.NewCreateView(db, c.ViewSpec.ViewName.Name.String(), queryAlias, c.OrReplace, subQuery, c.ViewSpec.Algorithm, definer, c.ViewSpec.Security)
476+
outScope.node = b.modifySchemaTarget(queryScope, createView, createView.Definition.Schema())
477+
476478
return outScope
477479
}

0 commit comments

Comments
 (0)