Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 14 additions & 5 deletions enginetest/queries/information_schema_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -1158,18 +1158,27 @@ FROM INFORMATION_SCHEMA.TRIGGERS WHERE trigger_schema = 'mydb'`,
SetUpScript: []string{
"USE foo",
"drop table othertable",
"CREATE TABLE t (i int primary key, j int)",
"CREATE VIEW v as select i + 1, j * 2, mod(i, j) from t",
"CREATE TABLE t (i int primary key, j int default (uuid_to_bin(uuid())));",
"CREATE VIEW v as select i + 1, j, j * 2, mod(i, j) from t;",
"create table tt (ii int primary key, jj int default (pow(1, 2)));",
"create view vv as select * from t join tt where i = ii;",
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'foo'",
Expected: []sql.Row{
{"def", "foo", "t", "i", uint32(1), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "PRI", "", "insert,references,select,update", "", "", nil},
{"def", "foo", "t", "j", uint32(2), nil, "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
{"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},
{"def", "foo", "tt", "ii", uint32(1), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "PRI", "", "insert,references,select,update", "", "", nil},
{"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},
{"def", "foo", "v", "i + 1", uint32(1), nil, "NO", "bigint", nil, nil, 19, 0, nil, nil, nil, "bigint", "", "", "insert,references,select,update", "", "", nil},
{"def", "foo", "v", "j * 2", uint32(2), nil, "YES", "bigint", nil, nil, 19, 0, nil, nil, nil, "bigint", "", "", "insert,references,select,update", "", "", nil},
{"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},
{"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},
{"def", "foo", "v", "j * 2", uint32(3), nil, "YES", "bigint", nil, nil, 19, 0, nil, nil, nil, "bigint", "", "", "insert,references,select,update", "", "", nil},
{"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},
{"def", "foo", "vv", "i", uint32(1), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
{"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},
{"def", "foo", "vv", "ii", uint32(3), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
{"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},
},
},
},
Expand Down
33 changes: 33 additions & 0 deletions enginetest/queries/view_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,11 @@ CREATE TABLE tab1 (
SetUpScript: []string{
"create table t (i int primary key, j int default 100);",
"insert into t(i) values (1);",
"create table tt (ii int primary key, jj int default (pow(11, 2)));",
"insert into tt values (1, default), (3, 4);",
"create view v as select * from t;",
"create view v1 as select i, j + 10 as jj from t;",
"create view vv as select i, ii, j, jj, i + ii + j + jj from t join tt where i = ii;",
},
Assertions: []ScriptTestAssertion{
{
Expand Down Expand Up @@ -249,6 +252,36 @@ CREATE TABLE tab1 (
{"jj", "bigint", "YES", "", nil, ""},
},
},
{
Query: "show full columns from vv;",
Expected: []sql.Row{
{"i", "int", nil, "NO", "", nil, "", "", ""},
{"ii", "int", nil, "NO", "", nil, "", "", ""},
{"j", "int", nil, "YES", "", "100", "", "", ""},
{"jj", "int", nil, "YES", "", "(power(11, 2))", "DEFAULT_GENERATED", "", ""},
{"i + ii + j + jj", "bigint", nil, "YES", "", nil, "", "", ""},
},
},
{
Query: "show columns from vv;",
Expected: []sql.Row{
{"i", "int", "NO", "", nil, ""},
{"ii", "int", "NO", "", nil, ""},
{"j", "int", "YES", "", "100", ""},
{"jj", "int", "YES", "", "(power(11, 2))", "DEFAULT_GENERATED"},
{"i + ii + j + jj", "bigint", "YES", "", nil, ""},
},
},
{
Query: "describe vv;",
Expected: []sql.Row{
{"i", "int", "NO", "", nil, ""},
{"ii", "int", "NO", "", nil, ""},
{"j", "int", "YES", "", "100", ""},
{"jj", "int", "YES", "", "(power(11, 2))", "DEFAULT_GENERATED"},
{"i + ii + j + jj", "bigint", "YES", "", nil, ""},
},
},
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion sql/information_schema/columns_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func getRowsFromViews(ctx *sql.Context, catalog sql.Catalog, db DbWithNames, pri
}
privSetTbl := privSetDb.Table(view.Name)
curPrivSetMap := getCurrentPrivSetMapForColumn(privSetDb.ToSlice(), privSetMap)
for i, col := range createViewNode.Definition.Schema() {
for i, col := range createViewNode.TargetSchema() {
r := getRowFromColumn(ctx, i, col, db.CatalogName, db.SchemaName, view.Name, "", privSetTbl, curPrivSetMap)
if r != nil {
rows = append(rows, r)
Expand Down
14 changes: 14 additions & 0 deletions sql/plan/create_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
type CreateView struct {
UnaryNode
database sql.Database
targetSchema sql.Schema
Name string
IsReplace bool
Definition *SubqueryAlias
Expand All @@ -41,6 +42,7 @@ type CreateView struct {

var _ sql.Node = (*CreateView)(nil)
var _ sql.CollationCoercible = (*CreateView)(nil)
var _ sql.SchemaTarget = (*CreateView)(nil)

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

// WithTargetSchema implements the SchemaTarget interface.
func (cv *CreateView) WithTargetSchema(sch sql.Schema) (sql.Node, error) {
ncv := *cv
ncv.targetSchema = sch
return &ncv, nil
}

// TargetSchema implements the SchemaTarget interface.
func (cv *CreateView) TargetSchema() sql.Schema {
return cv.targetSchema
}

// GetIsUpdatableFromCreateView returns whether the view is updatable or not.
// https://dev.mysql.com/doc/refman/8.0/en/view-updatability.html
func GetIsUpdatableFromCreateView(cv *CreateView) bool {
Expand Down
2 changes: 2 additions & 0 deletions sql/plan/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func findDefault(node sql.Node, gf *expression.GetField) *sql.ColumnDefaultValue
return findDefault(n.Child, gf)
case *HashLookup:
return findDefault(n.Child, gf)
case *Filter:
return findDefault(n.Child, gf)
case *JoinNode:
if defVal := findDefault(n.Left(), gf); defVal != nil {
return defVal
Expand Down
4 changes: 3 additions & 1 deletion sql/planbuilder/create_ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ func (b *Builder) buildCreateView(inScope *scope, subQuery string, fullQuery str
dbName = b.ctx.GetCurrentDatabase()
}
db := b.resolveDb(dbName)
outScope.node = plan.NewCreateView(db, c.ViewSpec.ViewName.Name.String(), queryAlias, c.OrReplace, subQuery, c.ViewSpec.Algorithm, definer, c.ViewSpec.Security)
createView := plan.NewCreateView(db, c.ViewSpec.ViewName.Name.String(), queryAlias, c.OrReplace, subQuery, c.ViewSpec.Algorithm, definer, c.ViewSpec.Security)
outScope.node = b.modifySchemaTarget(queryScope, createView, createView.Definition.Schema())

return outScope
}
Loading