Repository: pingcap/tidb
File: pkg/planner/core/stats.go (Go)

Code before:

	if ds.SCtx().GetSessionVars().StmtCtx.EnableOptimizerDebugTrace {
		debugTraceAccessPaths(ds.SCtx(), ds.PossibleAccessPaths)
	}
	indexForce := false
	ds.AccessPathMinSelectivity, indexForce = getGeneralAttributesFromPaths(ds.PossibleAccessPaths, float64(ds.TblColHists.RealtimeCount))
	if indexForce {
		ds.SCtx().GetSessionVars().StmtCtx.SetIndexForce()
	}

	return ds.StatsInfo(), true, nil
}

func fillIndexPath(ds *logicalop.DataSource, path *util.AccessPath, conds []expression.Expression, possiblePK *model.IndexInfo) error {
	if ds.SCtx().GetSessionVars().StmtCtx.EnableOptimizerDebugTrace {
		debugtrace.EnterContextCommon(ds.SCtx())
		defer debugtrace.LeaveContextCommon(ds.SCtx())
	}
	ticiType := distsql.NotTiCIIndex
	if path.Index.IsTiCIIndex() {
		if path.Index.HybridInfo != nil && path.Index.HybridInfo.Sharding != nil {
			ticiType = distsql.TiCIShardExtraShardingKey
			path.Ranges = ranger.FullRange()
		} else if ds.TableInfo.IsCommonHandle {
			ticiType = distsql.TiCIShardCommonHandle
			path.Ranges = ranger.FullRange()
		} else {
			ticiType = distsql.TiCIShardIntHandle
			// Int Handle's range is a special one.
			path.Ranges = ranger.FullIntRange(mysql.HasUnsignedFlag(ds.TableInfo.GetPkColInfo().GetFlag()))
		}
		path.IdxCols, path.IdxColLens = expression.TiCIIndexInfo2ShardCols(ds.Columns, ds.Schema().Columns, path.Index, possiblePK)
	} else {
		path.IdxCols, path.IdxColLens = expression.IndexInfo2PrefixCols(ds.Columns, ds.Schema().Columns, path.Index)
		path.Ranges = ranger.FullRange()
	}
	path.FullIdxCols, path.FullIdxColLens = expression.IndexInfo2Cols(ds.Columns, ds.Schema().Columns, path.Index)
	path.CountAfterAccess = float64(ds.StatisticTable.RealtimeCount)
	path.MinCountAfterAccess = 0
	path.MaxCountAfterAccess = 0
	if !path.Index.Unique && !path.Index.Primary && len(path.Index.Columns) == len(path.IdxCols) && ticiType == distsql.NotTiCIIndex {
		handleCol := ds.GetPKIsHandleCol()
		if handleCol != nil && !mysql.HasUnsignedFlag(handleCol.RetType.GetFlag()) {
			alreadyHandle := false
			for _, col := range path.IdxCols {
				if col.ID == model.ExtraHandleID || col.EqualColumn(handleCol) {
					alreadyHandle = true
				}
			}
			// Don't add one column twice to the index. May cause unexpected errors.
			if !alreadyHandle {

Review comment (suggestion):
According to `deriveCommonHandleTablePathStats()`, it looks like we should use `ranger.FullNotNullRange()` here.

Code after:

	if ds.SCtx().GetSessionVars().StmtCtx.EnableOptimizerDebugTrace {
		debugTraceAccessPaths(ds.SCtx(), ds.PossibleAccessPaths)
	}
	indexForce := false
	ds.AccessPathMinSelectivity, indexForce = getGeneralAttributesFromPaths(ds.PossibleAccessPaths, float64(ds.TblColHists.RealtimeCount))
	if indexForce {
		ds.SCtx().GetSessionVars().StmtCtx.SetIndexForce()
	}

	return ds.StatsInfo(), true, nil
}

func fillIndexPath(ds *logicalop.DataSource, path *util.AccessPath, conds []expression.Expression, possiblePK *model.IndexInfo) error {
	if ds.SCtx().GetSessionVars().StmtCtx.EnableOptimizerDebugTrace {
		debugtrace.EnterContextCommon(ds.SCtx())
		defer debugtrace.LeaveContextCommon(ds.SCtx())
	}
	ticiType := distsql.NotTiCIIndex
	if path.Index.IsTiCIIndex() {
		if path.Index.HybridInfo != nil && path.Index.HybridInfo.Sharding != nil {
			ticiType = distsql.TiCIShardExtraShardingKey
			path.Ranges = ranger.FullRange()
		} else if ds.TableInfo.IsCommonHandle {
			ticiType = distsql.TiCIShardCommonHandle
			path.Ranges = ranger.FullNotNullRange()
		} else {
			ticiType = distsql.TiCIShardIntHandle
			// Int Handle's range is a special one.
			unsignedFlag := false
			// We will not get the column for the _tidb_rowid case.
			if intHandle := ds.TableInfo.GetPkColInfo(); intHandle != nil {
				unsignedFlag = mysql.HasUnsignedFlag(intHandle.GetFlag())
			}
			path.Ranges = ranger.FullIntRange(unsignedFlag)
		}
		path.IdxCols, path.IdxColLens = expression.TiCIIndexInfo2ShardCols(ds.Columns, ds.Schema().Columns, path.Index, possiblePK)
	} else {
		path.IdxCols, path.IdxColLens = expression.IndexInfo2PrefixCols(ds.Columns, ds.Schema().Columns, path.Index)
		path.Ranges = ranger.FullRange()
	}
	path.FullIdxCols, path.FullIdxColLens = expression.IndexInfo2Cols(ds.Columns, ds.Schema().Columns, path.Index)
	path.CountAfterAccess = float64(ds.StatisticTable.RealtimeCount)
	path.MinCountAfterAccess = 0
	path.MaxCountAfterAccess = 0
	if !path.Index.Unique && !path.Index.Primary && len(path.Index.Columns) == len(path.IdxCols) && ticiType == distsql.NotTiCIIndex {
		handleCol := ds.GetPKIsHandleCol()
		if handleCol != nil && !mysql.HasUnsignedFlag(handleCol.RetType.GetFlag()) {
			alreadyHandle := false
			for _, col := range path.IdxCols {
				if col.ID == model.ExtraHandleID || col.EqualColumn(handleCol) {