diff --git a/enginetest/queries/information_schema_queries.go b/enginetest/queries/information_schema_queries.go index cfb7188362..e1a751aa33 100644 --- a/enginetest/queries/information_schema_queries.go +++ b/enginetest/queries/information_schema_queries.go @@ -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}, }, }, }, diff --git a/enginetest/queries/view_queries.go b/enginetest/queries/view_queries.go index 67d94f5140..dce470f12b 100644 --- a/enginetest/queries/view_queries.go +++ b/enginetest/queries/view_queries.go @@ -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{ { @@ -228,6 +231,12 @@ CREATE TABLE tab1 ( {"j", "int", "YES", "", "100", ""}, }, }, + { + Query: "select * from v", + Expected: []sql.Row{ + {1, 100}, + }, + }, { Query: "show full columns from v1;", Expected: []sql.Row{ @@ -249,6 +258,48 @@ CREATE TABLE tab1 ( {"jj", "bigint", "YES", "", nil, ""}, }, }, + { + Query: "select * from v1", + Expected: []sql.Row{ + {1, 110}, + }, + }, + { + 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, ""}, + }, + }, + { + Query: "select * from vv", + Expected: []sql.Row{ + {1, 1, 100, 121, 223}, + }, + }, }, }, } diff --git a/sql/information_schema/columns_table.go b/sql/information_schema/columns_table.go index 4bffe88b34..01360dde1c 100644 --- a/sql/information_schema/columns_table.go +++ b/sql/information_schema/columns_table.go @@ -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) diff --git a/sql/plan/create_view.go b/sql/plan/create_view.go index 3901286adb..b42f84d13d 100644 --- a/sql/plan/create_view.go +++ b/sql/plan/create_view.go @@ -29,6 +29,7 @@ import ( type CreateView struct { UnaryNode database sql.Database + targetSchema sql.Schema Name string IsReplace bool Definition *SubqueryAlias @@ -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. @@ -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 { diff --git a/sql/plan/project.go b/sql/plan/project.go index 814f63bd13..d329571b9c 100644 --- a/sql/plan/project.go +++ b/sql/plan/project.go @@ -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 diff --git a/sql/planbuilder/create_ddl.go b/sql/planbuilder/create_ddl.go index 91114e0cd5..7c3a35bd12 100644 --- a/sql/planbuilder/create_ddl.go +++ b/sql/planbuilder/create_ddl.go @@ -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 }