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, executor: fix index merge partial table scan schema #23936

Merged
merged 19 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,10 @@ func (e *UnionExec) Next(ctx context.Context, req *chunk.Chunk) error {
return errors.Trace(result.err)
}

if result.chk.NumCols() != req.NumCols() {
return errors.Errorf("Internal error: UnionExec chunk column count mismatch, req: %d, result: %d",
req.NumCols(), result.chk.NumCols())
}
req.SwapColumns(result.chk)
result.src <- result.chk
return nil
Expand Down
17 changes: 6 additions & 11 deletions executor/index_merge_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,11 @@ func (e *IndexMergeReaderExecutor) startPartialIndexWorker(ctx context.Context,
return nil
}

func (e *IndexMergeReaderExecutor) buildPartialTableReader(ctx context.Context, workID int) Executor {
tableReaderExec := &TableReaderExecutor{
baseExecutor: newBaseExecutor(e.ctx, e.schema, e.getPartitalPlanID(workID)),
func (e *IndexMergeReaderExecutor) startPartialTableWorker(ctx context.Context, exitCh <-chan struct{}, fetchCh chan<- *lookupTableTask, workID int,
partialWorkerWg *sync.WaitGroup) error {
ts := e.partialPlans[workID][0].(*plannercore.PhysicalTableScan)
partialTableReader := &TableReaderExecutor{
baseExecutor: newBaseExecutor(e.ctx, ts.Schema(), e.getPartitalPlanID(workID)),
table: e.table,
dagPB: e.dagPBs[workID],
startTS: e.startTS,
Expand All @@ -263,26 +265,19 @@ func (e *IndexMergeReaderExecutor) buildPartialTableReader(ctx context.Context,
plans: e.partialPlans[workID],
ranges: e.ranges[workID],
}
return tableReaderExec
}

func (e *IndexMergeReaderExecutor) startPartialTableWorker(ctx context.Context, exitCh <-chan struct{}, fetchCh chan<- *lookupTableTask, workID int,
partialWorkerWg *sync.WaitGroup) error {
partialTableReader := e.buildPartialTableReader(ctx, workID)
err := partialTableReader.Open(ctx)
if err != nil {
logutil.Logger(ctx).Error("open Select result failed:", zap.Error(err))
return err
}
tableInfo := e.partialPlans[workID][0].(*plannercore.PhysicalTableScan).Table
worker := &partialTableWorker{
stats: e.stats,
sc: e.ctx,
batchSize: e.maxChunkSize,
maxBatchSize: e.ctx.GetSessionVars().IndexLookupSize,
maxChunkSize: e.maxChunkSize,
tableReader: partialTableReader,
tableInfo: tableInfo,
tableInfo: ts.Table,
}

if worker.batchSize > worker.maxBatchSize {
Expand Down
89 changes: 45 additions & 44 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,10 +826,8 @@ func (ds *DataSource) convertToIndexMergeScan(prop *property.PhysicalProperty, c
for _, partPath := range path.PartialIndexPaths {
var scan PhysicalPlan
var partialCost float64
var needExtraProj bool
if partPath.IsTablePath() {
scan, partialCost, needExtraProj = ds.convertToPartialTableScan(prop, partPath)
cop.needExtraProj = cop.needExtraProj || needExtraProj
scan, partialCost = ds.convertToPartialTableScan(prop, partPath)
} else {
scan, partialCost = ds.convertToPartialIndexScan(prop, partPath)
}
Expand All @@ -840,18 +838,14 @@ func (ds *DataSource) convertToIndexMergeScan(prop *property.PhysicalProperty, c
if prop.ExpectedCnt < ds.stats.RowCount {
totalRowCount *= prop.ExpectedCnt / ds.stats.RowCount
}
ts, partialCost, needExtraProj, err := ds.buildIndexMergeTableScan(prop, path.TableFilters, totalRowCount)
ts, partialCost, err := ds.buildIndexMergeTableScan(prop, path.TableFilters, totalRowCount)
if err != nil {
return nil, err
}
cop.needExtraProj = cop.needExtraProj || needExtraProj
totalCost += partialCost
cop.tablePlan = ts
cop.idxMergePartPlans = scans
cop.cst = totalCost
if cop.needExtraProj {
cop.originSchema = ds.schema
}
task = cop.convertToRootTask(ds.ctx)
return task, nil
}
Expand Down Expand Up @@ -888,20 +882,11 @@ func (ds *DataSource) convertToPartialIndexScan(prop *property.PhysicalProperty,
}

func (ds *DataSource) convertToPartialTableScan(prop *property.PhysicalProperty, path *util.AccessPath) (
tablePlan PhysicalPlan, partialCost float64, needExtraProj bool) {
tablePlan PhysicalPlan, partialCost float64) {
ts, partialCost, rowCount := ds.getOriginalPhysicalTableScan(prop, path, false)
if ds.tableInfo.IsCommonHandle {
commonHandle := ds.handleCols.(*CommonHandleCols)
for _, col := range commonHandle.columns {
if ts.schema.ColumnIndex(col) == -1 {
ts.Schema().Append(col)
ts.Columns = append(ts.Columns, col.ToInfo())
needExtraProj = true
}
}
}
rowSize := ds.TblColHists.GetAvgRowSize(ds.ctx, ds.TblCols, false, false)
sessVars := ds.ctx.GetSessionVars()
overwritePartialTableScanSchema(ds, ts)
tangenta marked this conversation as resolved.
Show resolved Hide resolved
if len(ts.filterCondition) > 0 {
selectivity, _, err := ds.tableStats.HistColl.Selectivity(ds.ctx, ts.filterCondition, nil)
if err != nil {
Expand All @@ -912,16 +897,50 @@ func (ds *DataSource) convertToPartialTableScan(prop *property.PhysicalProperty,
tablePlan.SetChildren(ts)
partialCost += rowCount * sessVars.CopCPUFactor
partialCost += selectivity * rowCount * rowSize * sessVars.NetworkFactor
return
return tablePlan, partialCost
}
partialCost += rowCount * rowSize * sessVars.NetworkFactor
tablePlan = ts
return tablePlan, partialCost
}

// overwritePartialTableScanSchema change the schema of partial table scan to handle columns.
func overwritePartialTableScanSchema(ds *DataSource, ts *PhysicalTableScan) {
handleCols := ds.handleCols
if handleCols == nil {
handleCols = NewIntHandleCols(ds.newExtraHandleSchemaCol())
}
hdColNum := handleCols.NumCols()
exprCols := make([]*expression.Column, 0, hdColNum)
infoCols := make([]*model.ColumnInfo, 0, hdColNum)
for i := 0; i < hdColNum; i++ {
col := handleCols.GetCol(i)
exprCols = append(exprCols, col)
infoCols = append(infoCols, col.ToInfo())
}
ts.schema = expression.NewSchema(exprCols...)
ts.Columns = infoCols
}

// setIndexMergeTableScanHandleCols set the handle columns of the table scan.
func setIndexMergeTableScanHandleCols(ds *DataSource, ts *PhysicalTableScan) (err error) {
handleCols := ds.handleCols
if handleCols == nil {
handleCols = NewIntHandleCols(ds.newExtraHandleSchemaCol())
}
hdColNum := handleCols.NumCols()
exprCols := make([]*expression.Column, 0, hdColNum)
for i := 0; i < hdColNum; i++ {
col := handleCols.GetCol(i)
exprCols = append(exprCols, col)
}
ts.HandleCols, err = handleCols.ResolveIndices(expression.NewSchema(exprCols...))
return
}

func (ds *DataSource) buildIndexMergeTableScan(prop *property.PhysicalProperty, tableFilters []expression.Expression, totalRowCount float64) (PhysicalPlan, float64, bool, error) {
func (ds *DataSource) buildIndexMergeTableScan(prop *property.PhysicalProperty, tableFilters []expression.Expression,
totalRowCount float64) (PhysicalPlan, float64, error) {
var partialCost float64
var needExtraProj bool
sessVars := ds.ctx.GetSessionVars()
ts := PhysicalTableScan{
Table: ds.tableInfo,
Expand All @@ -933,27 +952,9 @@ func (ds *DataSource) buildIndexMergeTableScan(prop *property.PhysicalProperty,
HandleCols: ds.handleCols,
}.Init(ds.ctx, ds.blockOffset)
ts.SetSchema(ds.schema.Clone())
if ts.HandleCols == nil {
handleCol := ds.getPKIsHandleCol()
if handleCol == nil {
handleCol, _ = ts.appendExtraHandleCol(ds)
}
ts.HandleCols = NewIntHandleCols(handleCol)
}
if ds.tableInfo.IsCommonHandle {
commonHandle := ds.handleCols.(*CommonHandleCols)
for _, col := range commonHandle.columns {
if ts.schema.ColumnIndex(col) == -1 {
ts.Schema().Append(col)
ts.Columns = append(ts.Columns, col.ToInfo())
needExtraProj = true
}
}
}
var err error
ts.HandleCols, err = ts.HandleCols.ResolveIndices(ts.schema)
err := setIndexMergeTableScanHandleCols(ds, ts)
if err != nil {
return nil, 0, false, err
return nil, 0, err
}
if ts.Table.PKIsHandle {
if pkColInfo := ts.Table.GetPkColInfo(); pkColInfo != nil {
Expand All @@ -977,9 +978,9 @@ func (ds *DataSource) buildIndexMergeTableScan(prop *property.PhysicalProperty,
}
sel := PhysicalSelection{Conditions: tableFilters}.Init(ts.ctx, ts.stats.ScaleByExpectCnt(selectivity*totalRowCount), ts.blockOffset)
sel.SetChildren(ts)
return sel, partialCost, needExtraProj, nil
return sel, partialCost, nil
}
return ts, partialCost, needExtraProj, nil
return ts, partialCost, nil
}

func indexCoveringCol(col *expression.Column, indexCols []*expression.Column, idxColLens []int) bool {
Expand Down
3 changes: 2 additions & 1 deletion planner/core/handle_cols.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type HandleCols interface {
// BuildHandleByDatums builds a Handle from a datum slice.
BuildHandleByDatums(row []types.Datum) (kv.Handle, error)
// BuildHandleFromIndexRow builds a Handle from index row data.
// The last column(s) of `row` must be the handle column(s).
BuildHandleFromIndexRow(row chunk.Row) (kv.Handle, error)
// ResolveIndices resolves handle column indices.
ResolveIndices(schema *expression.Schema) (HandleCols, error)
Expand All @@ -47,7 +48,7 @@ type HandleCols interface {
NumCols() int
// Compare compares two datum rows by handle order.
Compare(a, b []types.Datum) (int, error)
// GetFieldTypes return field types of columns
// GetFieldTypes return field types of columns.
GetFieldsTypes() []*types.FieldType
}

Expand Down
Loading