Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: fix error when select view_name.col_name from view_name #14314

Merged
merged 6 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 6 additions & 5 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2783,33 +2783,34 @@ func (b *PlanBuilder) BuildDataSourceFromView(ctx context.Context, dbName model.
func (b *PlanBuilder) buildProjUponView(ctx context.Context, dbName model.CIStr, tableInfo *model.TableInfo, selectLogicalPlan Plan) (LogicalPlan, error) {
columnInfo := tableInfo.Cols()
cols := selectLogicalPlan.Schema().Clone().Columns
names := selectLogicalPlan.OutputNames().Shallow()
outputNamesOfUnderlyingSelect := selectLogicalPlan.OutputNames().Shallow()
// In the old version of VIEW implementation, tableInfo.View.Cols is used to
// store the origin columns' names of the underlying SelectStmt used when
// creating the view.
if tableInfo.View.Cols != nil {
cols = cols[:0]
names = names[:0]
outputNamesOfUnderlyingSelect = outputNamesOfUnderlyingSelect[:0]
for _, info := range columnInfo {
idx := expression.FindFieldNameIdxByColName(selectLogicalPlan.OutputNames(), info.Name.L)
if idx == -1 {
return nil, ErrViewInvalid.GenWithStackByArgs(dbName.O, tableInfo.Name.O)
}
cols = append(cols, selectLogicalPlan.Schema().Columns[idx])
names = append(names, selectLogicalPlan.OutputNames()[idx])
outputNamesOfUnderlyingSelect = append(outputNamesOfUnderlyingSelect, selectLogicalPlan.OutputNames()[idx])
}
}

projSchema := expression.NewSchema(make([]*expression.Column, 0, len(tableInfo.Columns))...)
projExprs := make([]expression.Expression, 0, len(tableInfo.Columns))
projNames := make(types.NameSlice, 0, len(tableInfo.Columns))
for i, name := range names {
for i, name := range outputNamesOfUnderlyingSelect {
origColName := name.ColName
if tableInfo.View.Cols != nil {
origColName = tableInfo.View.Cols[i]
}
projNames = append(projNames, &types.FieldName{
TblName: name.TblName,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment here for explanation?

winoros marked this conversation as resolved.
Show resolved Hide resolved
// TblName is the of view instead of the name of the underlying table.
TblName: tableInfo.Name,
OrigTblName: name.OrigTblName,
ColName: columnInfo[i].Name,
OrigColName: origColName,
Expand Down
4 changes: 4 additions & 0 deletions planner/core/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,10 @@ func (s *testPlanSuite) TestSelectView(c *C) {
sql: "select * from v",
best: "DataScan(t)->Projection",
},
{
sql: "select v.b, v.c, v.d from v",
best: "DataScan(t)->Projection",
},
}
ctx := context.TODO()
for i, tt := range tests {
Expand Down