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 a bug that can't find column in indexLookupJoin with virtual generated column (#18516) #19439

Merged
merged 5 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6558,6 +6558,14 @@ func (s *testIntegrationSuite) TestIssue17727(c *C) {
tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Rows("1"))
}

func (s *testIntegrationSuite) TestIssue18515(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b json, c int AS (JSON_EXTRACT(b, '$.population')), key(c));")
tk.MustExec("select /*+ TIDB_INLJ(t2) */ t1.a, t1.c, t2.a from t t1, t t2 where t1.c=t2.c;")
}

func (s *testIntegrationSerialSuite) TestIssue17989(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
2 changes: 0 additions & 2 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,6 @@ func (ds *DataSource) buildIndexMergeTableScan(prop *property.PhysicalProperty,
physicalTableID: ds.physicalTableID,
}.Init(ds.ctx, ds.blockOffset)
ts.SetSchema(ds.schema.Clone())
ts.Columns = ExpandVirtualColumn(ts.Columns, ts.schema, ts.Table.Columns)
if ts.Table.PKIsHandle {
if pkColInfo := ts.Table.GetPkColInfo(); pkColInfo != nil {
if ds.statisticTable.Columns[pkColInfo.ID] != nil {
Expand Down Expand Up @@ -799,7 +798,6 @@ func (ds *DataSource) convertToIndexScan(prop *property.PhysicalProperty, candid
physicalTableID: ds.physicalTableID,
}.Init(ds.ctx, is.blockOffset)
ts.SetSchema(ds.schema.Clone())
ts.Columns = ExpandVirtualColumn(ts.Columns, ts.schema, ts.Table.Columns)
cop.tablePlan = ts
}
cop.cst = cost
Expand Down
8 changes: 8 additions & 0 deletions planner/core/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,14 @@ func (p BatchPointGetPlan) Init(ctx sessionctx.Context, stats *property.StatsInf
return &p
}

// Init initializes PointGetPlan.
func (p PointGetPlan) Init(ctx sessionctx.Context, stats *property.StatsInfo, offset int, props ...*property.PhysicalProperty) *PointGetPlan {
p.basePlan = newBasePlan(ctx, plancodec.TypePointGet, offset)
p.stats = stats
p.Columns = ExpandVirtualColumn(p.Columns, p.schema, p.TblInfo.Columns)
return &p
}

// flattenPushDownPlan converts a plan tree to a list, whose head is the leaf node like table scan.
func flattenPushDownPlan(p PhysicalPlan) []PhysicalPlan {
plans := make([]PhysicalPlan, 0, 5)
Expand Down
18 changes: 16 additions & 2 deletions planner/core/physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ func (ts *PhysicalTableScan) IsPartition() (bool, int64) {
// ExpandVirtualColumn expands the virtual column's dependent columns to ts's schema and column.
func ExpandVirtualColumn(columns []*model.ColumnInfo, schema *expression.Schema,
colsInfo []*model.ColumnInfo) []*model.ColumnInfo {
copyColumn := make([]*model.ColumnInfo, len(columns))
copy(copyColumn, columns)
var extraColumn *expression.Column
var extraColumnModel *model.ColumnInfo
if schema.Columns[len(schema.Columns)-1].ID == model.ExtraHandleID {
extraColumn = schema.Columns[len(schema.Columns)-1]
extraColumnModel = copyColumn[len(copyColumn)-1]
schema.Columns = schema.Columns[:len(schema.Columns)-1]
copyColumn = copyColumn[:len(copyColumn)-1]
}
schemaColumns := schema.Columns
for _, col := range schemaColumns {
if col.VirtualExpr == nil {
Expand All @@ -273,11 +283,15 @@ func ExpandVirtualColumn(columns []*model.ColumnInfo, schema *expression.Schema,
for _, baseCol := range baseCols {
if !schema.Contains(baseCol) {
schema.Columns = append(schema.Columns, baseCol)
columns = append(columns, FindColumnInfoByID(colsInfo, baseCol.ID))
copyColumn = append(copyColumn, FindColumnInfoByID(colsInfo, baseCol.ID))
}
}
}
return columns
if extraColumn != nil {
schema.Columns = append(schema.Columns, extraColumn)
copyColumn = append(copyColumn, extraColumnModel)
}
return copyColumn
}

//SetIsChildOfIndexLookUp is to set the bool if is a child of IndexLookUpReader
Expand Down
8 changes: 0 additions & 8 deletions planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@ type nameValuePair struct {
param *driver.ParamMarkerExpr
}

// Init initializes PointGetPlan.
func (p PointGetPlan) Init(ctx sessionctx.Context, stats *property.StatsInfo, offset int, props ...*property.PhysicalProperty) *PointGetPlan {
p.basePlan = newBasePlan(ctx, plancodec.TypePointGet, offset)
p.stats = stats
p.Columns = ExpandVirtualColumn(p.Columns, p.schema, p.TblInfo.Columns)
return &p
}

// Schema implements the Plan interface.
func (p *PointGetPlan) Schema() *expression.Schema {
return p.schema
Expand Down
8 changes: 7 additions & 1 deletion planner/core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,13 @@ func finishCopTask(ctx sessionctx.Context, task task) task {
// Network cost of transferring rows of table scan to TiDB.
if t.tablePlan != nil {
t.cst += t.count() * sessVars.NetworkFactor * t.tblColHists.GetAvgRowSize(ctx, t.tablePlan.Schema().Columns, false, false)

tp := t.tablePlan
for len(tp.Children()) > 0 {
tp = tp.Children()[0]
}
ts := tp.(*PhysicalTableScan)
ts.Columns = ExpandVirtualColumn(ts.Columns, ts.schema, ts.Table.Columns)
}
t.cst /= copIterWorkers
newTask := &rootTask{
Expand Down Expand Up @@ -676,7 +683,6 @@ func finishCopTask(ctx sessionctx.Context, task task) task {
StoreType: ts.StoreType,
}.Init(ctx, t.tablePlan.SelectBlockOffset())
p.stats = t.tablePlan.statsInfo()
ts.Columns = ExpandVirtualColumn(ts.Columns, ts.schema, ts.Table.Columns)
newTask.p = p
}

Expand Down