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

release-1.0: cherry-pick #5447 #5585

Merged
merged 1 commit into from
Jan 9, 2018
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
13 changes: 13 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ func (c *ColumnInfo) IsGenerated() bool {
// for use of execution phase.
const ExtraHandleID = -1

// ExtraHandleName is the name of ExtraHandle Column.
var ExtraHandleName = NewCIStr("_rowid")

// TableInfo provides meta data describing a DB table.
type TableInfo struct {
ID int64 `json:"id"`
Expand Down Expand Up @@ -169,6 +172,16 @@ func (t *TableInfo) GetPkColInfo() *ColumnInfo {
return nil
}

// NewExtraHandleColInfo mocks a column info for extra handle column.
func NewExtraHandleColInfo() *ColumnInfo {
colInfo := &ColumnInfo{
ID: ExtraHandleID,
Name: ExtraHandleName,
}
colInfo.Flag = mysql.PriKeyFlag
return colInfo
}

// ColumnIsInIndex checks whether c is included in any indices of t.
func (t *TableInfo) ColumnIsInIndex(c *ColumnInfo) bool {
for _, index := range t.Indices {
Expand Down
6 changes: 3 additions & 3 deletions plan/column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/util/types"
)

Expand Down Expand Up @@ -167,7 +168,6 @@ func (p *DataSource) PruneColumns(parentUsedCols []*expression.Column) {
if p.unionScanSchema != nil {
used[handleIdx] = true
}
firstCol, firstColInfo := p.schema.Columns[0], p.Columns[0]
for i := len(used) - 1; i >= 0; i-- {
if !used[i] {
p.schema.Columns = append(p.schema.Columns[:i], p.schema.Columns[i+1:]...)
Expand All @@ -181,8 +181,8 @@ func (p *DataSource) PruneColumns(parentUsedCols []*expression.Column) {
// For SQL like `select 1 from t`, tikv's response will be empty if no column is in schema.
// So we'll force to push one if schema doesn't have any column.
if p.schema.Len() == 0 {
p.schema.Append(firstCol)
p.Columns = append(p.Columns, firstColInfo)
p.Columns = append(p.Columns, model.NewExtraHandleColInfo())
p.schema.Append(p.newExtraHandleSchemaCol())
}
}

Expand Down
10 changes: 5 additions & 5 deletions plan/explain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ func (s *testExplainSuite) TestExplain(c *C) {
{
"select sum(6 in (select c2 from t2)) from t1",
[]string{
"TableScan_22 cop table:t1, range:(-inf,+inf), keep order:true 8000",
"TableReader_23 HashSemiJoin_20 root data:TableScan_22 8000",
"TableScan_11 cop table:t1, range:(-inf,+inf), keep order:false 8000",
"TableReader_12 HashSemiJoin_10 root data:TableScan_11 8000",
"TableScan_13 Selection_14 cop table:t2, range:(-inf,+inf), keep order:false 10",
"Selection_14 TableScan_13 cop eq(6, test.t2.c2) 10",
"TableReader_15 HashSemiJoin_20 root data:Selection_14 10",
"HashSemiJoin_20 StreamAgg_9 TableReader_23,TableReader_15 root right:TableReader_15, aux 8000",
"StreamAgg_9 HashSemiJoin_20 root type:stream, funcs:sum(5_aux_0) 1",
"TableReader_15 HashSemiJoin_10 root data:Selection_14 10",
"HashSemiJoin_10 HashAgg_8 TableReader_12,TableReader_15 root right:TableReader_15, aux 8000",
"HashAgg_8 HashSemiJoin_10 root type:complete, funcs:sum(5_aux_0) 1",
},
},
}
Expand Down
12 changes: 12 additions & 0 deletions plan/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,18 @@ func (b *planBuilder) buildTableDual() LogicalPlan {
return dual
}

func (ds *DataSource) newExtraHandleSchemaCol() *expression.Column {
return &expression.Column{
FromID: ds.id,
DBName: ds.DBName,
TblName: ds.tableInfo.Name,
ColName: model.ExtraHandleName,
RetType: types.NewFieldType(mysql.TypeLonglong),
Position: len(ds.tableInfo.Columns), // set a unique position
ID: model.ExtraHandleID,
}
}

func (b *planBuilder) buildDataSource(tn *ast.TableName) LogicalPlan {
handle := sessionctx.GetDomain(b.ctx).StatsHandle()
var statisticTable *statistics.Table
Expand Down