Skip to content

Commit 49c1183

Browse files
committed
implement select from view
1 parent 29c17a1 commit 49c1183

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

executor/executor_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -3416,3 +3416,34 @@ func (s *testSuite) TestSelectHashPartitionTable(c *C) {
34163416
" └─TableScan_13 10000.00 cop table:th, partition:, range:[-inf,+inf], keep order:false, stats:pseudo",
34173417
))
34183418
}
3419+
3420+
func (s *testSuite) TestSelectView(c *C) {
3421+
tk := testkit.NewTestKit(c, s.store)
3422+
tk.MustExec("use test")
3423+
tk.MustExec("create table view_t (a int,b int)")
3424+
tk.MustExec("insert into view_t values(1,2)")
3425+
tk.MustExec("create view view1 as select * from view_t")
3426+
tk.MustExec("create view view2(c,d) as select * from view_t")
3427+
tk.MustExec("create view view3(c,d) as select a,b from view_t")
3428+
tk.MustQuery("select * from view1;").Check(testkit.Rows("1 2"))
3429+
tk.MustQuery("select * from view2;").Check(testkit.Rows("1 2"))
3430+
tk.MustQuery("select * from view3;").Check(testkit.Rows("1 2"))
3431+
tk.MustExec("drop table view_t;")
3432+
tk.MustExec("create table view_t(c int,d int)")
3433+
_, err := tk.Exec("select * from view1")
3434+
c.Assert(err.Error(), Equals, "[planner:1054]Unknown column 'a' in 'field_list'")
3435+
_, err = tk.Exec("select * from view2")
3436+
c.Assert(err.Error(), Equals, "[planner:1054]Unknown column 'a' in 'field_list'")
3437+
_, err = tk.Exec("select * from view3")
3438+
c.Assert(err.Error(), Equals, "[planner:1054]Unknown column 'a' in 'field list'")
3439+
tk.MustExec("drop table view_t;")
3440+
tk.MustExec("create table view_t(a int,b int,c int)")
3441+
tk.MustExec("insert into view_t values(1,2,3)")
3442+
_, err = tk.Exec("select * from view1")
3443+
c.Assert(err.Error(), Equals, "View view1's UnderTable has changed with different column count")
3444+
_, err = tk.Exec("select * from view2")
3445+
c.Assert(err.Error(), Equals, "View view2's UnderTable has changed with different column count")
3446+
tk.MustQuery("select * from view3;").Check(testkit.Rows("1 2"))
3447+
defer tk.MustExec("drop table view_t;")
3448+
defer tk.MustExec("drop view view1,view2,view3;")
3449+
}

planner/core/logical_plan_builder.go

+32
Original file line numberDiff line numberDiff line change
@@ -1901,6 +1901,38 @@ func (b *PlanBuilder) buildDataSource(tn *ast.TableName) (LogicalPlan, error) {
19011901

19021902
tableInfo := tbl.Meta()
19031903
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, dbName.L, tableInfo.Name.L, "")
1904+
charset, collation := b.ctx.GetSessionVars().GetCharsetInfo()
1905+
if tableInfo.IsView() {
1906+
var (
1907+
selectNode ast.StmtNode
1908+
selectLogicalPlan Plan
1909+
)
1910+
selectNode, err = parser.New().ParseOneStmt(tableInfo.View.SelectStmt, charset, collation)
1911+
if err != nil {
1912+
return nil, err
1913+
}
1914+
selectLogicalPlan, err = b.Build(selectNode)
1915+
if err != nil {
1916+
return nil, err
1917+
}
1918+
var viewCols []*expression.Column
1919+
var selectSchema = selectLogicalPlan.Schema()
1920+
if len(selectSchema.Columns) != len(tableInfo.View.Cols) {
1921+
return nil, errors.New(fmt.Sprintf("View %s's UnderTable has changed with different column count", tableInfo.Name.L))
1922+
}
1923+
for i := range tableInfo.View.Cols {
1924+
if selectSchema.Columns[i].ColName != tableInfo.View.Cols[i] {
1925+
return nil, ErrUnknownColumn.GenWithStackByArgs(tableInfo.View.Cols[i].O, "field_list")
1926+
}
1927+
selectSchema.Columns[i].ColName = tableInfo.Cols()[i].Name
1928+
selectSchema.Columns[i].OrigColName = tableInfo.View.Cols[i]
1929+
viewCols = append(viewCols, selectSchema.Columns[i])
1930+
}
1931+
selectProjection := LogicalProjection{Exprs: expression.Column2Exprs(viewCols)}.Init(b.ctx)
1932+
selectProjection.SetChildren(selectLogicalPlan.(LogicalPlan))
1933+
selectProjection.SetSchema(selectLogicalPlan.Schema())
1934+
return selectProjection, nil
1935+
}
19041936

19051937
if tableInfo.GetPartitionInfo() != nil {
19061938
b.optFlag = b.optFlag | flagPartitionProcessor

0 commit comments

Comments
 (0)