Skip to content

Commit

Permalink
fix: do not abuse new table ID for add partitioning event
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Dec 18, 2023
1 parent 629a1b4 commit a678c51
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions pkg/ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3100,6 +3100,7 @@ func newStatsDDLEventForJob(
)
case model.ActionAlterTablePartitioning:
event = statsutil.NewAddPartitioningEvent(
oldTblID,
tblInfo,
addedPartInfo,
)
Expand Down
7 changes: 2 additions & 5 deletions pkg/statistics/handle/ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (h *ddlHandlerImpl) HandleDDLEvent(t *util.DDLEvent) error {
// Do not update global stats, since the data have not changed!
}
case model.ActionAlterTablePartitioning:
globalTableInfo, addedPartInfo := t.GetAddPartitioningInfo()
oldSingleTableID, globalTableInfo, addedPartInfo := t.GetAddPartitioningInfo()
// Add new partition stats.
// For new partitions, it's crucial to correctly insert the count and modify count correctly.
// However, this is challenging due to the need to know the count of the new partitions.
Expand All @@ -147,10 +147,7 @@ func (h *ddlHandlerImpl) HandleDDLEvent(t *util.DDLEvent) error {
}
}
// Change id for global stats, since the data has not changed!
// Note that globalTableInfo is the new table info
// and addedPartInfo.NewTableID is actually the old table ID!
// (see onReorganizePartition)
return h.statsWriter.ChangeGlobalStatsID(addedPartInfo.NewTableID, globalTableInfo.ID)
return h.statsWriter.ChangeGlobalStatsID(oldSingleTableID, globalTableInfo.ID)
case model.ActionRemovePartitioning:
// Update id for global stats due to new table creation.
// It's important to insert and modify count accurately
Expand Down
16 changes: 11 additions & 5 deletions pkg/statistics/handle/util/ddl_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,25 @@ func (e *DDLEvent) GetTruncatePartitionInfo() (
// NewAddPartitioningEvent creates a new ddl event that converts a single table to a partitioned table.
// For example, `alter table t partition by range (c1) (partition p1 values less than (10))`.
func NewAddPartitioningEvent(
oldSingleTableID int64,
newGlobalTableInfo *model.TableInfo,
addedPartInfo *model.PartitionInfo,
) *DDLEvent {
return &DDLEvent{
tp: model.ActionAlterTablePartitioning,
tableInfo: newGlobalTableInfo,
partInfo: addedPartInfo,
tp: model.ActionAlterTablePartitioning,
oldTableID: oldSingleTableID,
tableInfo: newGlobalTableInfo,
partInfo: addedPartInfo,
}
}

// GetAddPartitioningInfo gets the table info of the table that is converted to a partitioned table.
func (e *DDLEvent) GetAddPartitioningInfo() (newGlobalTableInfo *model.TableInfo, addedPartInfo *model.PartitionInfo) {
return e.tableInfo, e.partInfo
func (e *DDLEvent) GetAddPartitioningInfo() (
oldSingleTableID int64,
newGlobalTableInfo *model.TableInfo,
addedPartInfo *model.PartitionInfo,
) {
return e.oldTableID, e.tableInfo, e.partInfo
}

// NewRemovePartitioningEvent creates a new ddl event that converts a partitioned table to a single table.
Expand Down

0 comments on commit a678c51

Please sign in to comment.