diff --git a/pkg/executor/builder.go b/pkg/executor/builder.go index 2f31c44f17b64..32dfcc49da78b 100644 --- a/pkg/executor/builder.go +++ b/pkg/executor/builder.go @@ -3453,7 +3453,7 @@ func (b *executorBuilder) buildTableReader(v *plannercore.PhysicalTableReader) e tmp, _ := b.is.TableByID(ts.Table.ID) tbl := tmp.(table.PartitionedTable) - partitions, err := partitionPruning(b.ctx, tbl, v.PartitionInfo.PruningConds, v.PartitionInfo.PartitionNames, v.PartitionInfo.Columns, v.PartitionInfo.ColumnNames) + partitions, err := partitionPruning(b.ctx, tbl, &v.PlanPartInfo) if err != nil { b.err = err return nil @@ -3539,14 +3539,14 @@ func getPartitionKeyColOffsets(keyColIDs []int64, pt table.PartitionedTable) []i return keyColOffsets } -func (builder *dataReaderBuilder) prunePartitionForInnerExecutor(tbl table.Table, partitionInfo *plannercore.PartitionInfo, +func (builder *dataReaderBuilder) prunePartitionForInnerExecutor(tbl table.Table, physPlanPartInfo *plannercore.PhysPlanPartInfo, lookUpContent []*indexJoinLookUpContent) (usedPartition []table.PhysicalTable, canPrune bool, contentPos []int64, err error) { partitionTbl := tbl.(table.PartitionedTable) // In index join, this is called by multiple goroutines simultaneously, but partitionPruning is not thread-safe. // Use once.Do to avoid DATA RACE here. // TODO: condition based pruning can be do in advance. - condPruneResult, err := builder.partitionPruning(partitionTbl, partitionInfo.PruningConds, partitionInfo.PartitionNames, partitionInfo.Columns, partitionInfo.ColumnNames) + condPruneResult, err := builder.partitionPruning(partitionTbl, physPlanPartInfo) if err != nil { return nil, false, nil, err } @@ -3692,7 +3692,7 @@ func (b *executorBuilder) buildIndexReader(v *plannercore.PhysicalIndexReader) e b.err = exeerrors.ErrBuildExecutor return nil } - ret.partitionIDMap, err = getPartitionIdsAfterPruning(b.ctx, tbl, &v.PartitionInfo) + ret.partitionIDMap, err = getPartitionIdsAfterPruning(b.ctx, tbl, &v.PlanPartInfo) if err != nil { b.err = err return nil @@ -3702,7 +3702,7 @@ func (b *executorBuilder) buildIndexReader(v *plannercore.PhysicalIndexReader) e tmp, _ := b.is.TableByID(is.Table.ID) tbl := tmp.(table.PartitionedTable) - partitions, err := partitionPruning(b.ctx, tbl, v.PartitionInfo.PruningConds, v.PartitionInfo.PartitionNames, v.PartitionInfo.Columns, v.PartitionInfo.ColumnNames) + partitions, err := partitionPruning(b.ctx, tbl, &v.PlanPartInfo) if err != nil { b.err = err return nil @@ -3889,7 +3889,7 @@ func (b *executorBuilder) buildIndexLookUpReader(v *plannercore.PhysicalIndexLoo b.err = exeerrors.ErrBuildExecutor return nil } - ret.partitionIDMap, err = getPartitionIdsAfterPruning(b.ctx, tbl, &v.PartitionInfo) + ret.partitionIDMap, err = getPartitionIdsAfterPruning(b.ctx, tbl, &v.PlanPartInfo) if err != nil { b.err = err return nil @@ -3906,7 +3906,7 @@ func (b *executorBuilder) buildIndexLookUpReader(v *plannercore.PhysicalIndexLoo tmp, _ := b.is.TableByID(is.Table.ID) tbl := tmp.(table.PartitionedTable) - partitions, err := partitionPruning(b.ctx, tbl, v.PartitionInfo.PruningConds, v.PartitionInfo.PartitionNames, v.PartitionInfo.Columns, v.PartitionInfo.ColumnNames) + partitions, err := partitionPruning(b.ctx, tbl, &v.PlanPartInfo) if err != nil { b.err = err return nil @@ -4036,7 +4036,7 @@ func (b *executorBuilder) buildIndexMergeReader(v *plannercore.PhysicalIndexMerg } tmp, _ := b.is.TableByID(ts.Table.ID) - partitions, err := partitionPruning(b.ctx, tmp.(table.PartitionedTable), v.PartitionInfo.PruningConds, v.PartitionInfo.PartitionNames, v.PartitionInfo.Columns, v.PartitionInfo.ColumnNames) + partitions, err := partitionPruning(b.ctx, tmp.(table.PartitionedTable), &v.PlanPartInfo) if err != nil { b.err = err return nil @@ -4163,8 +4163,7 @@ func (builder *dataReaderBuilder) buildTableReaderForIndexJoin(ctx context.Conte } tbl, _ := builder.is.TableByID(tbInfo.ID) pt := tbl.(table.PartitionedTable) - partitionInfo := &v.PartitionInfo - usedPartitionList, err := builder.partitionPruning(pt, partitionInfo.PruningConds, partitionInfo.PartitionNames, partitionInfo.Columns, partitionInfo.ColumnNames) + usedPartitionList, err := builder.partitionPruning(pt, &v.PlanPartInfo) if err != nil { return nil, err } @@ -4428,7 +4427,7 @@ func (builder *dataReaderBuilder) buildIndexReaderForIndexJoin(ctx context.Conte if !ok { return nil, exeerrors.ErrBuildExecutor } - e.partitionIDMap, err = getPartitionIdsAfterPruning(builder.ctx, tbl, &v.PartitionInfo) + e.partitionIDMap, err = getPartitionIdsAfterPruning(builder.ctx, tbl, &v.PlanPartInfo) if err != nil { return nil, err } @@ -4443,7 +4442,7 @@ func (builder *dataReaderBuilder) buildIndexReaderForIndexJoin(ctx context.Conte } tbl, _ := builder.executorBuilder.is.TableByID(tbInfo.ID) - usedPartition, canPrune, contentPos, err := builder.prunePartitionForInnerExecutor(tbl, &v.PartitionInfo, lookUpContents) + usedPartition, canPrune, contentPos, err := builder.prunePartitionForInnerExecutor(tbl, &v.PlanPartInfo, lookUpContents) if err != nil { return nil, err } @@ -4503,7 +4502,7 @@ func (builder *dataReaderBuilder) buildIndexLookUpReaderForIndexJoin(ctx context if !ok { return nil, exeerrors.ErrBuildExecutor } - e.partitionIDMap, err = getPartitionIdsAfterPruning(builder.ctx, tbl, &v.PartitionInfo) + e.partitionIDMap, err = getPartitionIdsAfterPruning(builder.ctx, tbl, &v.PlanPartInfo) if err != nil { return nil, err } @@ -4518,7 +4517,7 @@ func (builder *dataReaderBuilder) buildIndexLookUpReaderForIndexJoin(ctx context } tbl, _ := builder.executorBuilder.is.TableByID(tbInfo.ID) - usedPartition, canPrune, contentPos, err := builder.prunePartitionForInnerExecutor(tbl, &v.PartitionInfo, lookUpContents) + usedPartition, canPrune, contentPos, err := builder.prunePartitionForInnerExecutor(tbl, &v.PlanPartInfo, lookUpContents) if err != nil { return nil, err } @@ -5153,19 +5152,17 @@ func (b *executorBuilder) buildAdminResetTelemetryID(v *plannercore.AdminResetTe return &AdminResetTelemetryIDExec{BaseExecutor: exec.NewBaseExecutor(b.ctx, v.Schema(), v.ID())} } -func (builder *dataReaderBuilder) partitionPruning(tbl table.PartitionedTable, conds []expression.Expression, partitionNames []model.CIStr, - columns []*expression.Column, columnNames types.NameSlice) ([]table.PhysicalTable, error) { +func (builder *dataReaderBuilder) partitionPruning(tbl table.PartitionedTable, planPartInfo *plannercore.PhysPlanPartInfo) ([]table.PhysicalTable, error) { builder.once.Do(func() { - condPruneResult, err := partitionPruning(builder.executorBuilder.ctx, tbl, conds, partitionNames, columns, columnNames) + condPruneResult, err := partitionPruning(builder.executorBuilder.ctx, tbl, planPartInfo) builder.once.condPruneResult = condPruneResult builder.once.err = err }) return builder.once.condPruneResult, builder.once.err } -func partitionPruning(ctx sessionctx.Context, tbl table.PartitionedTable, conds []expression.Expression, partitionNames []model.CIStr, - columns []*expression.Column, columnNames types.NameSlice) ([]table.PhysicalTable, error) { - idxArr, err := plannercore.PartitionPruning(ctx, tbl, conds, partitionNames, columns, columnNames) +func partitionPruning(ctx sessionctx.Context, tbl table.PartitionedTable, planPartInfo *plannercore.PhysPlanPartInfo) ([]table.PhysicalTable, error) { + idxArr, err := plannercore.PartitionPruning(ctx, tbl, planPartInfo.PruningConds, planPartInfo.PartitionNames, planPartInfo.Columns, planPartInfo.ColumnNames) if err != nil { return nil, err } @@ -5189,11 +5186,11 @@ func partitionPruning(ctx sessionctx.Context, tbl table.PartitionedTable, conds return ret, nil } -func getPartitionIdsAfterPruning(ctx sessionctx.Context, tbl table.PartitionedTable, partInfo *plannercore.PartitionInfo) (map[int64]struct{}, error) { - if partInfo == nil { - return nil, errors.New("partInfo in getPartitionIdsAfterPruning must not be nil") +func getPartitionIdsAfterPruning(ctx sessionctx.Context, tbl table.PartitionedTable, physPlanPartInfo *plannercore.PhysPlanPartInfo) (map[int64]struct{}, error) { + if physPlanPartInfo == nil { + return nil, errors.New("physPlanPartInfo in getPartitionIdsAfterPruning must not be nil") } - idxArr, err := plannercore.PartitionPruning(ctx, tbl, partInfo.PruningConds, partInfo.PartitionNames, partInfo.Columns, partInfo.ColumnNames) + idxArr, err := plannercore.PartitionPruning(ctx, tbl, physPlanPartInfo.PruningConds, physPlanPartInfo.PartitionNames, physPlanPartInfo.Columns, physPlanPartInfo.ColumnNames) if err != nil { return nil, err } diff --git a/pkg/executor/point_get.go b/pkg/executor/point_get.go index 7bae5d006e5e0..4250f15a8c47a 100644 --- a/pkg/executor/point_get.go +++ b/pkg/executor/point_get.go @@ -120,7 +120,7 @@ type PointGetExecutor struct { tblInfo *model.TableInfo handle kv.Handle idxInfo *model.IndexInfo - partInfo *model.PartitionDefinition + partitionDef *model.PartitionDefinition idxKey kv.Key handleVal []byte idxVals []types.Datum @@ -162,7 +162,7 @@ func (e *PointGetExecutor) Init(p *plannercore.PointGetPlan) { e.lockWaitTime = 0 } e.rowDecoder = decoder - e.partInfo = p.PartitionInfo + e.partitionDef = p.PartitionDef e.columns = p.Columns e.buildVirtualColumnInfo() } @@ -221,8 +221,8 @@ func (e *PointGetExecutor) Next(ctx context.Context, req *chunk.Chunk) error { var tblID int64 var err error - if e.partInfo != nil { - tblID = e.partInfo.ID + if e.partitionDef != nil { + tblID = e.partitionDef.ID } else { tblID = e.tblInfo.ID } @@ -510,8 +510,8 @@ func (e *PointGetExecutor) verifyTxnScope() error { var tblName string var partName string is := e.Ctx().GetInfoSchema().(infoschema.InfoSchema) - if e.partInfo != nil { - tblID = e.partInfo.ID + if e.partitionDef != nil { + tblID = e.partitionDef.ID tblInfo, _, partInfo := is.FindTableByPartitionID(tblID) tblName = tblInfo.Meta().Name.String() partName = partInfo.Name.String() diff --git a/pkg/planner/core/access_object.go b/pkg/planner/core/access_object.go index ea59145cbc956..162e321643771 100644 --- a/pkg/planner/core/access_object.go +++ b/pkg/planner/core/access_object.go @@ -307,8 +307,8 @@ func (p *PointGetPlan) AccessObject() AccessObject { Database: p.dbName, Table: p.TblInfo.Name.O, } - if p.PartitionInfo != nil { - res.Partitions = []string{p.PartitionInfo.Name.O} + if p.PartitionDef != nil { + res.Partitions = []string{p.PartitionDef.Name.O} } if p.IndexInfo != nil { index := IndexAccess{ @@ -333,8 +333,8 @@ func (p *BatchPointGetPlan) AccessObject() AccessObject { Database: p.dbName, Table: p.TblInfo.Name.O, } - for _, partitionInfo := range p.PartitionInfos { - res.Partitions = append(res.Partitions, partitionInfo.Name.O) + for _, partitionDef := range p.PartitionDefs { + res.Partitions = append(res.Partitions, partitionDef.Name.O) } if p.IndexInfo != nil { index := IndexAccess{ @@ -353,7 +353,7 @@ func (p *BatchPointGetPlan) AccessObject() AccessObject { return res } -func getDynamicAccessPartition(sctx sessionctx.Context, tblInfo *model.TableInfo, partitionInfo *PartitionInfo, asName string) (res *DynamicPartitionAccessObject) { +func getDynamicAccessPartition(sctx sessionctx.Context, tblInfo *model.TableInfo, physPlanPartInfo *PhysPlanPartInfo, asName string) (res *DynamicPartitionAccessObject) { pi := tblInfo.GetPartitionInfo() if pi == nil || !sctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() { return nil @@ -377,7 +377,7 @@ func getDynamicAccessPartition(sctx sessionctx.Context, tblInfo *model.TableInfo } tbl := tmp.(table.PartitionedTable) - idxArr, err := PartitionPruning(sctx, tbl, partitionInfo.PruningConds, partitionInfo.PartitionNames, partitionInfo.Columns, partitionInfo.ColumnNames) + idxArr, err := PartitionPruning(sctx, tbl, physPlanPartInfo.PruningConds, physPlanPartInfo.PartitionNames, physPlanPartInfo.Columns, physPlanPartInfo.ColumnNames) if err != nil { res.err = "partition pruning error:" + err.Error() return res @@ -398,7 +398,7 @@ func (p *PhysicalTableReader) accessObject(sctx sessionctx.Context) AccessObject if !sctx.GetSessionVars().StmtCtx.UseDynamicPartitionPrune() { return DynamicPartitionAccessObjects(nil) } - if len(p.PartitionInfos) == 0 { + if len(p.TableScanAndPartitionInfos) == 0 { ts, ok := p.TablePlans[0].(*PhysicalTableScan) if !ok { return OtherAccessObject("") @@ -407,20 +407,20 @@ func (p *PhysicalTableReader) accessObject(sctx sessionctx.Context) AccessObject if ts.TableAsName != nil && len(ts.TableAsName.O) > 0 { asName = ts.TableAsName.O } - res := getDynamicAccessPartition(sctx, ts.Table, &p.PartitionInfo, asName) + res := getDynamicAccessPartition(sctx, ts.Table, &p.PlanPartInfo, asName) if res == nil { return DynamicPartitionAccessObjects(nil) } return DynamicPartitionAccessObjects{res} } - if len(p.PartitionInfos) == 1 { - ts := p.PartitionInfos[0].tableScan - partInfo := p.PartitionInfos[0].partitionInfo + if len(p.TableScanAndPartitionInfos) == 1 { + tp := p.TableScanAndPartitionInfos[0] + ts := tp.tableScan asName := "" if ts.TableAsName != nil && len(ts.TableAsName.O) > 0 { asName = ts.TableAsName.O } - res := getDynamicAccessPartition(sctx, ts.Table, &partInfo, asName) + res := getDynamicAccessPartition(sctx, ts.Table, &tp.physPlanPartInfo, asName) if res == nil { return DynamicPartitionAccessObjects(nil) } @@ -428,17 +428,16 @@ func (p *PhysicalTableReader) accessObject(sctx sessionctx.Context) AccessObject } res := make(DynamicPartitionAccessObjects, 0) - for _, info := range p.PartitionInfos { + for _, info := range p.TableScanAndPartitionInfos { if info.tableScan.Table.GetPartitionInfo() == nil { continue } ts := info.tableScan - partInfo := info.partitionInfo asName := "" if ts.TableAsName != nil && len(ts.TableAsName.O) > 0 { asName = ts.TableAsName.O } - accessObj := getDynamicAccessPartition(sctx, ts.Table, &partInfo, asName) + accessObj := getDynamicAccessPartition(sctx, ts.Table, &info.physPlanPartInfo, asName) if accessObj != nil { res = append(res, accessObj) } @@ -458,7 +457,7 @@ func (p *PhysicalIndexReader) accessObject(sctx sessionctx.Context) AccessObject if is.TableAsName != nil && len(is.TableAsName.O) > 0 { asName = is.TableAsName.O } - res := getDynamicAccessPartition(sctx, is.Table, &p.PartitionInfo, asName) + res := getDynamicAccessPartition(sctx, is.Table, &p.PlanPartInfo, asName) if res == nil { return DynamicPartitionAccessObjects(nil) } @@ -474,7 +473,7 @@ func (p *PhysicalIndexLookUpReader) accessObject(sctx sessionctx.Context) Access if ts.TableAsName != nil && len(ts.TableAsName.O) > 0 { asName = ts.TableAsName.O } - res := getDynamicAccessPartition(sctx, ts.Table, &p.PartitionInfo, asName) + res := getDynamicAccessPartition(sctx, ts.Table, &p.PlanPartInfo, asName) if res == nil { return DynamicPartitionAccessObjects(nil) } @@ -490,7 +489,7 @@ func (p *PhysicalIndexMergeReader) accessObject(sctx sessionctx.Context) AccessO if ts.TableAsName != nil && len(ts.TableAsName.O) > 0 { asName = ts.TableAsName.O } - res := getDynamicAccessPartition(sctx, ts.Table, &p.PartitionInfo, asName) + res := getDynamicAccessPartition(sctx, ts.Table, &p.PlanPartInfo, asName) if res == nil { return DynamicPartitionAccessObjects(nil) } diff --git a/pkg/planner/core/exhaust_physical_plans.go b/pkg/planner/core/exhaust_physical_plans.go index 82bc8e20d4e1c..785498491f663 100644 --- a/pkg/planner/core/exhaust_physical_plans.go +++ b/pkg/planner/core/exhaust_physical_plans.go @@ -1108,13 +1108,13 @@ func (p *LogicalJoin) constructInnerTableScanTask( tblColHists: ds.TblColHists, keepOrder: ts.KeepOrder, } - copTask.partitionInfo = PartitionInfo{ + copTask.physPlanPartInfo = PhysPlanPartInfo{ PruningConds: ds.allConds, PartitionNames: ds.partitionNames, Columns: ds.TblCols, ColumnNames: ds.names, } - ts.PartitionInfo = copTask.partitionInfo + ts.PlanPartInfo = copTask.physPlanPartInfo selStats := ts.StatsInfo().Scale(selectivity) ts.addPushedDownSelection(copTask, selStats) t := copTask.convertToRootTask(ds.SCtx()) @@ -1272,7 +1272,7 @@ func (p *LogicalJoin) constructInnerIndexScanTask( tblCols: ds.TblCols, keepOrder: is.KeepOrder, } - cop.partitionInfo = PartitionInfo{ + cop.physPlanPartInfo = PhysPlanPartInfo{ PruningConds: ds.allConds, PartitionNames: ds.partitionNames, Columns: ds.TblCols, diff --git a/pkg/planner/core/find_best_task.go b/pkg/planner/core/find_best_task.go index 6a8fdc6c981c2..55247b0979f49 100644 --- a/pkg/planner/core/find_best_task.go +++ b/pkg/planner/core/find_best_task.go @@ -1346,7 +1346,7 @@ func (ds *DataSource) convertToIndexMergeScan(prop *property.PhysicalProperty, c indexPlanFinished: false, tblColHists: ds.TblColHists, } - cop.partitionInfo = PartitionInfo{ + cop.physPlanPartInfo = PhysPlanPartInfo{ PruningConds: pushDownNot(ds.SCtx(), ds.allConds), PartitionNames: ds.partitionNames, Columns: ds.TblCols, @@ -1757,7 +1757,7 @@ func (ds *DataSource) convertToIndexScan(prop *property.PhysicalProperty, tblCols: ds.TblCols, expectCnt: uint64(prop.ExpectedCnt), } - cop.partitionInfo = PartitionInfo{ + cop.physPlanPartInfo = PhysPlanPartInfo{ PruningConds: pushDownNot(ds.SCtx(), ds.allConds), PartitionNames: ds.partitionNames, Columns: ds.TblCols, @@ -2179,7 +2179,7 @@ func (ds *DataSource) convertToTableScan(prop *property.PhysicalProperty, candid partTp: property.AnyType, tblColHists: ds.TblColHists, } - ts.PartitionInfo = PartitionInfo{ + ts.PlanPartInfo = PhysPlanPartInfo{ PruningConds: pushDownNot(ds.SCtx(), ds.allConds), PartitionNames: ds.partitionNames, Columns: ds.TblCols, @@ -2213,13 +2213,13 @@ func (ds *DataSource) convertToTableScan(prop *property.PhysicalProperty, candid indexPlanFinished: true, tblColHists: ds.TblColHists, } - copTask.partitionInfo = PartitionInfo{ + copTask.physPlanPartInfo = PhysPlanPartInfo{ PruningConds: pushDownNot(ds.SCtx(), ds.allConds), PartitionNames: ds.partitionNames, Columns: ds.TblCols, ColumnNames: ds.names, } - ts.PartitionInfo = copTask.partitionInfo + ts.PlanPartInfo = copTask.physPlanPartInfo task = copTask if candidate.isMatchProp { copTask.keepOrder = true @@ -2298,7 +2298,7 @@ func (ds *DataSource) convertToPointGet(prop *property.PhysicalProperty, candida LockWaitTime: ds.SCtx().GetSessionVars().LockWaitTimeout, Columns: ds.Columns, }.Init(ds.SCtx(), ds.tableStats.ScaleByExpectCnt(accessCnt), ds.QueryBlockOffset()) - var partitionInfo *model.PartitionDefinition + var partitionDef *model.PartitionDefinition pi := ds.tableInfo.GetPartitionInfo() if ds.isPartition { // static prune @@ -2306,12 +2306,12 @@ func (ds *DataSource) convertToPointGet(prop *property.PhysicalProperty, candida for i := range pi.Definitions { def := pi.Definitions[i] if def.ID == ds.physicalTableID { - partitionInfo = &def + partitionDef = &def break } } } - if partitionInfo == nil { + if partitionDef == nil { return invalidTask } } else if pi != nil { @@ -2324,9 +2324,9 @@ func (ds *DataSource) convertToPointGet(prop *property.PhysicalProperty, candida if len(pi.Definitions) != 1 { return invalidTask } - partitionInfo = &pi.Definitions[0] + partitionDef = &pi.Definitions[0] } else if len(idxs) == 1 { - partitionInfo = &pi.Definitions[idxs[0]] + partitionDef = &pi.Definitions[idxs[0]] } else { return invalidTask } @@ -2335,7 +2335,7 @@ func (ds *DataSource) convertToPointGet(prop *property.PhysicalProperty, candida if candidate.path.IsIntHandlePath { pointGetPlan.Handle = kv.IntHandle(candidate.path.Ranges[0].LowVal[0].GetInt64()) pointGetPlan.UnsignedHandle = mysql.HasUnsignedFlag(ds.handleCols.GetCol(0).RetType.GetFlag()) - pointGetPlan.PartitionInfo = partitionInfo + pointGetPlan.PartitionDef = partitionDef pointGetPlan.accessCols = ds.TblCols // Add filter condition to table plan now. if len(candidate.path.TableFilters) > 0 { @@ -2350,7 +2350,7 @@ func (ds *DataSource) convertToPointGet(prop *property.PhysicalProperty, candida pointGetPlan.IdxCols = candidate.path.IdxCols pointGetPlan.IdxColLens = candidate.path.IdxColLens pointGetPlan.IndexValues = candidate.path.Ranges[0].LowVal - pointGetPlan.PartitionInfo = partitionInfo + pointGetPlan.PartitionDef = partitionDef if candidate.path.IsSingleScan { pointGetPlan.accessCols = candidate.path.IdxCols } else { diff --git a/pkg/planner/core/fragment.go b/pkg/planner/core/fragment.go index dca5f03198a12..95eea5bd349ad 100644 --- a/pkg/planner/core/fragment.go +++ b/pkg/planner/core/fragment.go @@ -554,7 +554,7 @@ func (e *mppTaskGenerator) constructMPPTasksImpl(ctx context.Context, ts *Physic tbl := tmp.(table.PartitionedTable) if !tiFlashStaticPrune { var partitions []table.PhysicalTable - partitions, err = partitionPruning(e.ctx, tbl, ts.PartitionInfo.PruningConds, ts.PartitionInfo.PartitionNames, ts.PartitionInfo.Columns, ts.PartitionInfo.ColumnNames) + partitions, err = partitionPruning(e.ctx, tbl, ts.PlanPartInfo.PruningConds, ts.PlanPartInfo.PartitionNames, ts.PlanPartInfo.Columns, ts.PlanPartInfo.ColumnNames) if err != nil { return nil, errors.Trace(err) } diff --git a/pkg/planner/core/physical_plans.go b/pkg/planner/core/physical_plans.go index 4c250cc91cf35..6bf372df63dbe 100644 --- a/pkg/planner/core/physical_plans.go +++ b/pkg/planner/core/physical_plans.go @@ -76,8 +76,8 @@ var ( ) type tableScanAndPartitionInfo struct { - tableScan *PhysicalTableScan - partitionInfo PartitionInfo + tableScan *PhysicalTableScan + physPlanPartInfo PhysPlanPartInfo } // MemoryUsage return the memory usage of tableScanAndPartitionInfo @@ -86,7 +86,7 @@ func (t *tableScanAndPartitionInfo) MemoryUsage() (sum int64) { return } - sum += t.partitionInfo.MemoryUsage() + sum += t.physPlanPartInfo.MemoryUsage() if t.tableScan != nil { sum += t.tableScan.MemoryUsage() } @@ -136,23 +136,23 @@ type PhysicalTableReader struct { IsCommonHandle bool // Used by partition table. - PartitionInfo PartitionInfo + PlanPartInfo PhysPlanPartInfo // Used by MPP, because MPP plan may contain join/union/union all, it is possible that a physical table reader contains more than 1 table scan - PartitionInfos []tableScanAndPartitionInfo + TableScanAndPartitionInfos []tableScanAndPartitionInfo } -// PartitionInfo indicates partition helper info in physical plan. -type PartitionInfo struct { +// PhysPlanPartInfo indicates partition helper info in physical plan. +type PhysPlanPartInfo struct { PruningConds []expression.Expression PartitionNames []model.CIStr Columns []*expression.Column ColumnNames types.NameSlice } -const emptyPartitionInfoSize = int64(unsafe.Sizeof(PartitionInfo{})) +const emptyPartitionInfoSize = int64(unsafe.Sizeof(PhysPlanPartInfo{})) -// MemoryUsage return the memory usage of PartitionInfo -func (pi *PartitionInfo) MemoryUsage() (sum int64) { +// MemoryUsage return the memory usage of PhysPlanPartInfo +func (pi *PhysPlanPartInfo) MemoryUsage() (sum int64) { if pi == nil { return } @@ -210,12 +210,12 @@ func (p *PhysicalTableReader) MemoryUsage() (sum int64) { return } - sum = p.physicalSchemaProducer.MemoryUsage() + size.SizeOfUint8*2 + size.SizeOfBool + p.PartitionInfo.MemoryUsage() + sum = p.physicalSchemaProducer.MemoryUsage() + size.SizeOfUint8*2 + size.SizeOfBool + p.PlanPartInfo.MemoryUsage() if p.tablePlan != nil { sum += p.tablePlan.MemoryUsage() } // since TablePlans is the flats of tablePlan, so we don't count it - for _, pInfo := range p.PartitionInfos { + for _, pInfo := range p.TableScanAndPartitionInfos { sum += pInfo.MemoryUsage() } return @@ -235,7 +235,7 @@ func setMppOrBatchCopForTableScan(curPlan PhysicalPlan) { // GetPhysicalTableReader returns PhysicalTableReader for logical TiKVSingleGather. func (sg *TiKVSingleGather) GetPhysicalTableReader(schema *expression.Schema, stats *property.StatsInfo, props ...*property.PhysicalProperty) *PhysicalTableReader { reader := PhysicalTableReader{}.Init(sg.SCtx(), sg.QueryBlockOffset()) - reader.PartitionInfo = PartitionInfo{ + reader.PlanPartInfo = PhysPlanPartInfo{ PruningConds: sg.Source.allConds, PartitionNames: sg.Source.partitionNames, Columns: sg.Source.TblCols, @@ -315,7 +315,7 @@ type PhysicalIndexReader struct { OutputColumns []*expression.Column // Used by partition table. - PartitionInfo PartitionInfo + PlanPartInfo PhysPlanPartInfo } // Clone implements PhysicalPlan interface. @@ -387,7 +387,7 @@ func (p *PhysicalIndexReader) MemoryUsage() (sum int64) { return } - sum = p.physicalSchemaProducer.MemoryUsage() + p.PartitionInfo.MemoryUsage() + sum = p.physicalSchemaProducer.MemoryUsage() + p.PlanPartInfo.MemoryUsage() if p.indexPlan != nil { p.indexPlan.MemoryUsage() } @@ -444,7 +444,7 @@ type PhysicalIndexLookUpReader struct { CommonHandleCols []*expression.Column // Used by partition table. - PartitionInfo PartitionInfo + PlanPartInfo PhysPlanPartInfo // required by cost calculation expectedCnt uint64 @@ -529,7 +529,7 @@ func (p *PhysicalIndexLookUpReader) MemoryUsage() (sum int64) { return } - sum = p.physicalSchemaProducer.MemoryUsage() + size.SizeOfBool*2 + p.PartitionInfo.MemoryUsage() + size.SizeOfUint64 + sum = p.physicalSchemaProducer.MemoryUsage() + size.SizeOfBool*2 + p.PlanPartInfo.MemoryUsage() + size.SizeOfUint64 if p.indexPlan != nil { sum += p.indexPlan.MemoryUsage() @@ -576,7 +576,7 @@ type PhysicalIndexMergeReader struct { tablePlan PhysicalPlan // Used by partition table. - PartitionInfo PartitionInfo + PlanPartInfo PhysPlanPartInfo KeepOrder bool @@ -632,7 +632,7 @@ func (p *PhysicalIndexMergeReader) MemoryUsage() (sum int64) { return } - sum = p.physicalSchemaProducer.MemoryUsage() + p.PartitionInfo.MemoryUsage() + sum = p.physicalSchemaProducer.MemoryUsage() + p.PlanPartInfo.MemoryUsage() if p.tablePlan != nil { sum += p.tablePlan.MemoryUsage() } @@ -865,7 +865,7 @@ type PhysicalTableScan struct { isChildOfIndexLookUp bool - PartitionInfo PartitionInfo + PlanPartInfo PhysPlanPartInfo SampleInfo *TableSampleInfo @@ -1014,7 +1014,7 @@ func (ts *PhysicalTableScan) MemoryUsage() (sum int64) { } sum = emptyPhysicalTableScanSize + ts.physicalSchemaProducer.MemoryUsage() + ts.DBName.MemoryUsage() + - int64(cap(ts.HandleIdx))*size.SizeOfInt + ts.PartitionInfo.MemoryUsage() + int64(len(ts.rangeInfo)) + int64(cap(ts.HandleIdx))*size.SizeOfInt + ts.PlanPartInfo.MemoryUsage() + int64(len(ts.rangeInfo)) if ts.TableAsName != nil { sum += ts.TableAsName.MemoryUsage() } diff --git a/pkg/planner/core/plan_cache.go b/pkg/planner/core/plan_cache.go index af94bc7513ca8..4ff089c029340 100644 --- a/pkg/planner/core/plan_cache.go +++ b/pkg/planner/core/plan_cache.go @@ -488,7 +488,7 @@ func rebuildRange(p Plan) error { } // The code should never run here as long as we're not using point get for partition table. // And if we change the logic one day, here work as defensive programming to cache the error. - if x.PartitionInfo != nil { + if x.PartitionDef != nil { // TODO: relocate the partition after rebuilding range to make PlanCache support PointGet return errors.New("point get for partition table can not use plan cache") } diff --git a/pkg/planner/core/point_get_plan.go b/pkg/planner/core/point_get_plan.go index 6aacdd27f8be8..658a9aa8b2e2a 100644 --- a/pkg/planner/core/point_get_plan.go +++ b/pkg/planner/core/point_get_plan.go @@ -71,7 +71,7 @@ type PointGetPlan struct { schema *expression.Schema TblInfo *model.TableInfo IndexInfo *model.IndexInfo - PartitionInfo *model.PartitionDefinition + PartitionDef *model.PartitionDefinition Handle kv.Handle HandleConstant *expression.Constant handleFieldType *types.FieldType @@ -272,8 +272,8 @@ func (p *PointGetPlan) MemoryUsage() (sum int64) { if p.schema != nil { sum += p.schema.MemoryUsage() } - if p.PartitionInfo != nil { - sum += p.PartitionInfo.MemoryUsage() + if p.PartitionDef != nil { + sum += p.PartitionDef.MemoryUsage() } if p.HandleConstant != nil { sum += p.HandleConstant.MemoryUsage() @@ -315,7 +315,7 @@ type BatchPointGetPlan struct { dbName string TblInfo *model.TableInfo IndexInfo *model.IndexInfo - PartitionInfos []*model.PartitionDefinition + PartitionDefs []*model.PartitionDefinition Handles []kv.Handle HandleType *types.FieldType HandleParams []*expression.Constant // record all Parameters for Plan-Cache @@ -493,7 +493,7 @@ func (p *BatchPointGetPlan) MemoryUsage() (sum int64) { sum = emptyBatchPointGetPlanSize + p.baseSchemaProducer.MemoryUsage() + int64(len(p.dbName)) + int64(cap(p.IdxColLens))*size.SizeOfInt + int64(cap(p.Handles))*size.SizeOfInterface + - int64(cap(p.PartitionInfos)+cap(p.HandleParams)+cap(p.IndexColTypes)+cap(p.IdxCols)+cap(p.Columns)+cap(p.accessCols))*size.SizeOfPointer + int64(cap(p.PartitionDefs)+cap(p.HandleParams)+cap(p.IndexColTypes)+cap(p.IdxCols)+cap(p.Columns)+cap(p.accessCols))*size.SizeOfPointer if p.HandleType != nil { sum += p.HandleType.MemoryUsage() } @@ -658,7 +658,7 @@ func newBatchPointGetPlan( var handles = make([]kv.Handle, len(patternInExpr.List)) var handleParams = make([]*expression.Constant, len(patternInExpr.List)) var pos2PartitionDefinition = make(map[int]*model.PartitionDefinition) - partitionInfos := make([]*model.PartitionDefinition, 0, len(patternInExpr.List)) + partitionDefs := make([]*model.PartitionDefinition, 0, len(patternInExpr.List)) for i, item := range patternInExpr.List { // SELECT * FROM t WHERE (key) in ((1), (2)) if p, ok := item.(*ast.ParenthesesExpr); ok { @@ -693,7 +693,7 @@ func newBatchPointGetPlan( handleParams[i] = con pairs := []nameValuePair{{colName: handleCol.Name.L, colFieldType: item.GetType(), value: *intDatum, con: con}} if tbl.GetPartitionInfo() != nil { - tmpPartitionDefinition, _, pos, isTableDual := getPartitionInfo(ctx, tbl, pairs) + tmpPartitionDefinition, _, pos, isTableDual := getPartitionDef(ctx, tbl, pairs) if isTableDual { return nil } @@ -711,18 +711,18 @@ func newBatchPointGetPlan( } sort.Ints(posArr) for _, pos := range posArr { - partitionInfos = append(partitionInfos, pos2PartitionDefinition[pos]) + partitionDefs = append(partitionDefs, pos2PartitionDefinition[pos]) } - if len(partitionInfos) == 0 { - partitionInfos = nil + if len(partitionDefs) == 0 { + partitionDefs = nil } p := &BatchPointGetPlan{ - TblInfo: tbl, - Handles: handles, - HandleParams: handleParams, - HandleType: &handleCol.FieldType, - PartitionExpr: partitionExpr, - PartitionInfos: partitionInfos, + TblInfo: tbl, + Handles: handles, + HandleParams: handleParams, + HandleType: &handleCol.FieldType, + PartitionExpr: partitionExpr, + PartitionDefs: partitionDefs, } return p.Init(ctx, statsInfo, schema, names, 0) @@ -779,7 +779,7 @@ func newBatchPointGetPlan( indexValues := make([][]types.Datum, len(patternInExpr.List)) indexValueParams := make([][]*expression.Constant, len(patternInExpr.List)) - partitionInfos := make([]*model.PartitionDefinition, 0, len(patternInExpr.List)) + partitionDefs := make([]*model.PartitionDefinition, 0, len(patternInExpr.List)) var pos2PartitionDefinition = make(map[int]*model.PartitionDefinition) var indexTypes []*types.FieldType @@ -881,7 +881,7 @@ func newBatchPointGetPlan( indexValues[i] = values indexValueParams[i] = valuesParams if tbl.GetPartitionInfo() != nil { - tmpPartitionDefinition, _, pos, isTableDual := getPartitionInfo(ctx, tbl, pairs) + tmpPartitionDefinition, _, pos, isTableDual := getPartitionDef(ctx, tbl, pairs) if isTableDual { return nil } @@ -899,10 +899,10 @@ func newBatchPointGetPlan( } sort.Ints(posArr) for _, pos := range posArr { - partitionInfos = append(partitionInfos, pos2PartitionDefinition[pos]) + partitionDefs = append(partitionDefs, pos2PartitionDefinition[pos]) } - if len(partitionInfos) == 0 { - partitionInfos = nil + if len(partitionDefs) == 0 { + partitionDefs = nil } p := &BatchPointGetPlan{ TblInfo: tbl, @@ -912,7 +912,7 @@ func newBatchPointGetPlan( IndexColTypes: indexTypes, PartitionColPos: pos, PartitionExpr: partitionExpr, - PartitionInfos: partitionInfos, + PartitionDefs: partitionDefs, } return p.Init(ctx, statsInfo, schema, names, 0) @@ -1062,21 +1062,21 @@ func tryPointGetPlan(ctx sessionctx.Context, selStmt *ast.SelectStmt, check bool return nil } - var partitionInfo *model.PartitionDefinition + var partitionDef *model.PartitionDefinition var pos int if pi != nil { - partitionInfo, pos, _, isTableDual = getPartitionInfo(ctx, tbl, pairs) + partitionDef, pos, _, isTableDual = getPartitionDef(ctx, tbl, pairs) if isTableDual { p := newPointGetPlan(ctx, tblName.Schema.O, schema, tbl, names) p.IsTableDual = true return p } - if partitionInfo == nil { + if partitionDef == nil { return checkTblIndexForPointPlan(ctx, tblName, schema, names, pairs, nil, pos, true, isTableDual, check) } // Take partition selection into consideration. if len(tblName.PartitionNames) > 0 { - if !partitionNameInSet(partitionInfo.Name, tblName.PartitionNames) { + if !partitionNameInSet(partitionDef.Name, tblName.PartitionNames) { p := newPointGetPlan(ctx, tblName.Schema.O, schema, tbl, names) p.IsTableDual = true return p @@ -1097,17 +1097,17 @@ func tryPointGetPlan(ctx sessionctx.Context, selStmt *ast.SelectStmt, check bool p.UnsignedHandle = mysql.HasUnsignedFlag(fieldType.GetFlag()) p.handleFieldType = fieldType p.HandleConstant = handlePair.con - p.PartitionInfo = partitionInfo + p.PartitionDef = partitionDef return p } else if handlePair.value.Kind() != types.KindNull { return nil } - return checkTblIndexForPointPlan(ctx, tblName, schema, names, pairs, partitionInfo, pos, false, isTableDual, check) + return checkTblIndexForPointPlan(ctx, tblName, schema, names, pairs, partitionDef, pos, false, isTableDual, check) } func checkTblIndexForPointPlan(ctx sessionctx.Context, tblName *ast.TableName, schema *expression.Schema, - names []*types.FieldName, pairs []nameValuePair, partitionInfo *model.PartitionDefinition, + names []*types.FieldName, pairs []nameValuePair, partitionDef *model.PartitionDefinition, pos int, globalIndexCheck, isTableDual, check bool) *PointGetPlan { if globalIndexCheck { // when partitions are specified or some partition is in ddl, not use point get plan for global index. @@ -1180,8 +1180,8 @@ func checkTblIndexForPointPlan(ctx sessionctx.Context, tblName *ast.TableName, s p.IndexValues = idxValues p.IndexConstants = idxConstant p.ColsFieldType = colsFieldType - p.PartitionInfo = partitionInfo - if p.PartitionInfo != nil { + p.PartitionDef = partitionDef + if p.PartitionDef != nil { p.partitionColumnPos = findPartitionIdx(idxInfo, pos, pairs) } return p @@ -1875,7 +1875,7 @@ func buildHandleCols(ctx sessionctx.Context, tbl *model.TableInfo, schema *expre return &IntHandleCols{col: handleCol} } -func getPartitionInfo(ctx sessionctx.Context, tbl *model.TableInfo, pairs []nameValuePair) (*model.PartitionDefinition, int, int, bool) { +func getPartitionDef(ctx sessionctx.Context, tbl *model.TableInfo, pairs []nameValuePair) (*model.PartitionDefinition, int, int, bool) { partitionExpr := getPartitionExpr(ctx, tbl) if partitionExpr == nil { return nil, 0, 0, false diff --git a/pkg/planner/core/task.go b/pkg/planner/core/task.go index ce81dc1e91180..8e0cea5b88232 100644 --- a/pkg/planner/core/task.go +++ b/pkg/planner/core/task.go @@ -92,7 +92,7 @@ type copTask struct { rootTaskConds []expression.Expression // For table partition. - partitionInfo PartitionInfo + physPlanPartInfo PhysPlanPartInfo // expectCnt is the expected row count of upper task, 0 for unlimited. // It's used for deciding whether using paging distsql. @@ -191,7 +191,7 @@ func (t *copTask) MemoryUsage() (sum int64) { } sum = size.SizeOfInterface*(2+int64(cap(t.idxMergePartPlans)+cap(t.rootTaskConds))) + size.SizeOfBool*3 + size.SizeOfUint64 + - size.SizeOfPointer*(3+int64(cap(t.commonHandleCols)+cap(t.tblCols))) + size.SizeOfSlice*4 + t.partitionInfo.MemoryUsage() + size.SizeOfPointer*(3+int64(cap(t.commonHandleCols)+cap(t.tblCols))) + size.SizeOfSlice*4 + t.physPlanPartInfo.MemoryUsage() if t.indexPlan != nil { sum += t.indexPlan.MemoryUsage() } @@ -591,7 +591,7 @@ func buildIndexLookUpTask(ctx sessionctx.Context, t *copTask) *rootTask { expectedCnt: t.expectCnt, keepOrder: t.keepOrder, }.Init(ctx, t.tablePlan.QueryBlockOffset()) - p.PartitionInfo = t.partitionInfo + p.PlanPartInfo = t.physPlanPartInfo setTableScanToTableRowIDScan(p.tablePlan) p.SetStats(t.tablePlan.StatsInfo()) // Do not inject the extra Projection even if t.needExtraProj is set, or the schema between the phase-1 agg and @@ -695,7 +695,7 @@ func (t *copTask) convertToRootTaskImpl(ctx sessionctx.Context) *rootTask { AccessMVIndex: t.idxMergeAccessMVIndex, KeepOrder: t.keepOrder, }.Init(ctx, t.idxMergePartPlans[0].QueryBlockOffset()) - p.PartitionInfo = t.partitionInfo + p.PlanPartInfo = t.physPlanPartInfo setTableScanToTableRowIDScan(p.tablePlan) newTask.p = p t.handleRootTaskConds(ctx, newTask) @@ -712,7 +712,7 @@ func (t *copTask) convertToRootTaskImpl(ctx sessionctx.Context) *rootTask { newTask = buildIndexLookUpTask(ctx, t) } else if t.indexPlan != nil { p := PhysicalIndexReader{indexPlan: t.indexPlan}.Init(ctx, t.indexPlan.QueryBlockOffset()) - p.PartitionInfo = t.partitionInfo + p.PlanPartInfo = t.physPlanPartInfo p.SetStats(t.indexPlan.StatsInfo()) newTask.p = p } else { @@ -731,7 +731,7 @@ func (t *copTask) convertToRootTaskImpl(ctx sessionctx.Context) *rootTask { StoreType: ts.StoreType, IsCommonHandle: ts.Table.IsCommonHandle, }.Init(ctx, t.tablePlan.QueryBlockOffset()) - p.PartitionInfo = t.partitionInfo + p.PlanPartInfo = t.physPlanPartInfo p.SetStats(t.tablePlan.StatsInfo()) // If agg was pushed down in attach2Task(), the partial agg was placed on the top of tablePlan, the final agg was @@ -2602,7 +2602,7 @@ func (t *mppTask) MemoryUsage() (sum int64) { func collectPartitionInfosFromMPPPlan(p *PhysicalTableReader, mppPlan PhysicalPlan) { switch x := mppPlan.(type) { case *PhysicalTableScan: - p.PartitionInfos = append(p.PartitionInfos, tableScanAndPartitionInfo{x, x.PartitionInfo}) + p.TableScanAndPartitionInfos = append(p.TableScanAndPartitionInfos, tableScanAndPartitionInfo{x, x.PlanPartInfo}) default: for _, ch := range mppPlan.Children() { collectPartitionInfosFromMPPPlan(p, ch) diff --git a/pkg/server/conn.go b/pkg/server/conn.go index 05a3b13180162..177e4c203ac26 100644 --- a/pkg/server/conn.go +++ b/pkg/server/conn.go @@ -1848,8 +1848,8 @@ func (cc *clientConn) prefetchPointPlanKeys(ctx context.Context, stmts []ast.Stm var tableID int64 switch v := p.(type) { case *plannercore.PointGetPlan: - if v.PartitionInfo != nil { - tableID = v.PartitionInfo.ID + if v.PartitionDef != nil { + tableID = v.PartitionDef.ID } else { tableID = v.TblInfo.ID } @@ -1865,12 +1865,12 @@ func (cc *clientConn) prefetchPointPlanKeys(ctx context.Context, stmts []ast.Stm rowKeys = append(rowKeys, tablecodec.EncodeRowKeyWithHandle(tableID, v.Handle)) } case *plannercore.BatchPointGetPlan: - if v.PartitionInfos != nil && len(v.PartitionIDs) == 0 { + if v.PartitionDefs != nil && len(v.PartitionIDs) == 0 { // skip when PartitionIDs is not initialized. return nil } getPhysID := func(i int) int64 { - if v.PartitionInfos == nil { + if v.PartitionDefs == nil { return v.TblInfo.ID } return v.PartitionIDs[i] diff --git a/pkg/statistics/handle/ddl/exchange_partition.go b/pkg/statistics/handle/ddl/exchange_partition.go index 7edb932ab49c7..af93bc905e2fe 100644 --- a/pkg/statistics/handle/ddl/exchange_partition.go +++ b/pkg/statistics/handle/ddl/exchange_partition.go @@ -123,7 +123,7 @@ func getCountsAndModifyCounts( func exchangePartitionLogFields( globalTableSchemaName string, globalTableInfo *model.TableInfo, - originalPartInfo model.PartitionDefinition, + originalPartDef model.PartitionDefinition, originalTableInfo *model.TableInfo, countDelta, modifyCountDelta, partCount, partModifyCount, @@ -135,8 +135,8 @@ func exchangePartitionLogFields( zap.String("globalTableName", globalTableInfo.Name.O), zap.Int64("countDelta", countDelta), zap.Int64("modifyCountDelta", modifyCountDelta), - zap.Int64("partitionID", originalPartInfo.ID), - zap.String("partitionName", originalPartInfo.Name.O), + zap.Int64("partitionID", originalPartDef.ID), + zap.String("partitionName", originalPartDef.Name.O), zap.Int64("partitionCount", partCount), zap.Int64("partitionModifyCount", partModifyCount), zap.Int64("tableID", originalTableInfo.ID),