Skip to content

Commit

Permalink
planner: respect tidb_analyze_default_column_choice when build anal…
Browse files Browse the repository at this point in the history
…yze job

Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Jun 26, 2024
1 parent 6b2dc3f commit 64d7b81
Showing 1 changed file with 50 additions and 11 deletions.
61 changes: 50 additions & 11 deletions pkg/planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,7 @@ func (b *PlanBuilder) getMustAnalyzedColumns(tbl *ast.TableName, cols *calcOnceM
return cols.data, nil
}

// getPredicateColumns gets the columns used in predicates.
func (b *PlanBuilder) getPredicateColumns(tbl *ast.TableName, cols *calcOnceMap) (map[int64]struct{}, error) {
// Already calculated in the previous call.
if cols.calculated {
Expand Down Expand Up @@ -2017,22 +2018,40 @@ func (b *PlanBuilder) getFullAnalyzeColumnsInfo(
}

switch columnChoice {
case model.DefaultChoice, model.AllColumns:
return tbl.TableInfo.Columns, nil, nil
case model.PredicateColumns:
if mustAllColumns {
case model.DefaultChoice:
currentDefaultColumnChoice := variable.AnalyzeDefaultColumnChoice.Load()
switch currentDefaultColumnChoice {
case model.AllColumns.String():
return tbl.TableInfo.Columns, nil, nil
case model.PredicateColumns.String():
columns, err := b.getColumnsBasedOnPredicateColumns(
tbl,
predicateCols,
mustAnalyzedCols,
mustAllColumns,
)
if err != nil {
return nil, nil, err
}
return columns, nil, nil
default:
// Usually, this won't happen.
logutil.BgLogger().Warn("Unknown default column choice", zap.String("choice", currentDefaultColumnChoice))
return tbl.TableInfo.Columns, nil, nil
}
predicate, err := b.getPredicateColumns(tbl, predicateCols)
if err != nil {
return nil, nil, err
}
mustAnalyzed, err := b.getMustAnalyzedColumns(tbl, mustAnalyzedCols)
case model.AllColumns:
return tbl.TableInfo.Columns, nil, nil
case model.PredicateColumns:
columns, err := b.getColumnsBasedOnPredicateColumns(
tbl,
predicateCols,
mustAnalyzedCols,
mustAllColumns,
)
if err != nil {
return nil, nil, err
}
colSet := combineColumnSets(predicate, mustAnalyzed)
return getColumnListFromSet(tbl.TableInfo.Columns, colSet), nil, nil
return columns, nil, nil
case model.ColumnList:
colSet := getColumnSetFromSpecifiedCols(specifiedCols)
mustAnalyzed, err := b.getMustAnalyzedColumns(tbl, mustAnalyzedCols)
Expand All @@ -2058,6 +2077,26 @@ func (b *PlanBuilder) getFullAnalyzeColumnsInfo(
return nil, nil, nil
}

func (b *PlanBuilder) getColumnsBasedOnPredicateColumns(
tbl *ast.TableName,
predicateCols, mustAnalyzedCols *calcOnceMap,
rewriteAllStatsNeeded bool,
) ([]*model.ColumnInfo, error) {
if rewriteAllStatsNeeded {
return tbl.TableInfo.Columns, nil
}
predicate, err := b.getPredicateColumns(tbl, predicateCols)
if err != nil {
return nil, err
}
mustAnalyzed, err := b.getMustAnalyzedColumns(tbl, mustAnalyzedCols)
if err != nil {
return nil, err
}
colSet := combineColumnSets(predicate, mustAnalyzed)
return getColumnListFromSet(tbl.TableInfo.Columns, colSet), nil
}

// Helper function to combine two column sets.
func combineColumnSets(sets ...map[int64]struct{}) map[int64]struct{} {
result := make(map[int64]struct{})
Expand Down

0 comments on commit 64d7b81

Please sign in to comment.