Skip to content

Commit

Permalink
Merge branch 'master' into fix_bug_8700
Browse files Browse the repository at this point in the history
  • Loading branch information
ngaut authored Jan 6, 2019
2 parents 8993094 + 78a51a4 commit 7b0e53d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
17 changes: 17 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/infoschema"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/privilege"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/sessionctx/variable"
Expand Down Expand Up @@ -329,6 +330,22 @@ func (e *ShowExec) fetchShowColumns() error {
}

cols := tb.Cols()
if tb.Meta().IsView() {
// Because view's undertable's column could change or recreate, so view's column type may change overtime.
// To avoid this situation we need to generate a logical plan and extract current column types from Schema.
planBuilder := plannercore.NewPlanBuilder(e.ctx, e.is)
viewLogicalPlan, err := planBuilder.BuildDataSourceFromView(e.DBName, tb.Meta())
if err != nil {
return err
}
viewSchema := viewLogicalPlan.Schema()
for _, col := range cols {
viewColumn := viewSchema.FindColumnByName(col.Name.L)
if viewColumn != nil {
col.FieldType = *viewColumn.GetType()
}
}
}
for _, col := range cols {
if e.Column != nil && e.Column.Name.L != col.Name.L {
continue
Expand Down
3 changes: 3 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ func (s *testSuite2) TestShow2(c *C) {
tk.MustExec(`create table if not exists t (c int) comment '注释'`)
tk.MustExec("create or replace view v as select * from t")
tk.MustQuery(`show columns from t`).Check(testutil.RowsWithSep(",", "c,int(11),YES,,<nil>,"))
tk.MustQuery(`describe t`).Check(testutil.RowsWithSep(",", "c,int(11),YES,,<nil>,"))
tk.MustQuery(`show columns from v`).Check(testutil.RowsWithSep(",", "c,int(11),YES,,<nil>,"))
tk.MustQuery(`describe v`).Check(testutil.RowsWithSep(",", "c,int(11),YES,,<nil>,"))
tk.MustQuery("show collation where Charset = 'utf8' and Collation = 'utf8_bin'").Check(testutil.RowsWithSep(",", "utf8_bin,utf8,83,,Yes,1"))

tk.MustQuery("show tables").Check(testkit.Rows("t", "v"))
Expand Down
5 changes: 3 additions & 2 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1984,7 +1984,7 @@ func (b *PlanBuilder) buildDataSource(tn *ast.TableName) (LogicalPlan, error) {
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, dbName.L, tableInfo.Name.L, "", nil)

if tableInfo.IsView() {
return b.buildDataSourceFromView(dbName, tableInfo)
return b.BuildDataSourceFromView(dbName, tableInfo)
}

if tableInfo.GetPartitionInfo() != nil {
Expand Down Expand Up @@ -2086,7 +2086,8 @@ func (b *PlanBuilder) buildDataSource(tn *ast.TableName) (LogicalPlan, error) {
return result, nil
}

func (b *PlanBuilder) buildDataSourceFromView(dbName model.CIStr, tableInfo *model.TableInfo) (LogicalPlan, error) {
// BuildDataSourceFromView is used to build LogicalPlan from view
func (b *PlanBuilder) BuildDataSourceFromView(dbName model.CIStr, tableInfo *model.TableInfo) (LogicalPlan, error) {
charset, collation := b.ctx.GetSessionVars().GetCharsetInfo()
selectNode, err := parser.New().ParseOneStmt(tableInfo.View.SelectStmt, charset, collation)
if err != nil {
Expand Down

0 comments on commit 7b0e53d

Please sign in to comment.