Repository: pingcap/tidb
File: pkg/planner/core/operator/physicalop/physical_topn.go (Go)

Code before:
// 5. TopN -> Selection -> Projection -> DataSource
//
// Note: This function only checks if the query pattern is supported.
// The actual check of whether PartialOrderInfo can pass through Projection
// is done in LogicalProjection.TryToGetChildProp during physical optimization.
func canUsePartialOrder4TopN(lt *logicalop.LogicalTopN) bool {
	if !lt.SCtx().GetSessionVars().OptPartialOrderedIndexForTopN {
		return false
	}
	// Must have ORDER BY columns
	if len(lt.ByItems) == 0 {
		return false
	}

	return checkPartialOrderPattern(lt.SCtx(), lt.Children()[0])
}

// checkPartialOrderPattern recursively checks if the plan tree matches
// a supported pattern for partial order optimization.
// Supported intermediate operators: Selection, Projection
// Terminal operator: DataSource
func checkPartialOrderPattern(ctx base.PlanContext, plan base.LogicalPlan) bool {
	switch p := plan.(type) {
	case *logicalop.DataSource:
		// Reached DataSource - check for dirty content
		if utilfuncp.TableHasDirtyContent(ctx, p.TableInfo) {
			return false
		}
		return true

	case *logicalop.LogicalSelection:
		// Selection can pass through, check its child
		if len(p.Children()) != 1 {
			return false
		}
		return checkPartialOrderPattern(ctx, p.Children()[0])

	case *logicalop.LogicalProjection:
		// Projection can pass through (actual column check is done in TryToGetChildProp)
		if len(p.Children()) != 1 {
			return false
		}
		return checkPartialOrderPattern(ctx, p.Children()[0])

	default:
		// Other operators are not supported
		return false
	}
}

// getPhysTopNWithPartialOrderProperty generates PhysicalTopN plans with partial order property t

Review comment (suggestion):
I think you don't need this. Dirty writes will generate the Logical/Physical UnionScan currently.
So we will simply break here because there's a LogicalUnionScan

Code after:
// 5. TopN -> Selection -> Projection -> DataSource
//
// Note: This function only checks if the query pattern is supported.
// The actual check of whether PartialOrderInfo can pass through Projection
// is done in LogicalProjection.TryToGetChildProp during physical optimization.
func canUsePartialOrder4TopN(lt *logicalop.LogicalTopN) bool {
	if !lt.SCtx().GetSessionVars().OptPartialOrderedIndexForTopN {
		return false
	}
	// Must have ORDER BY columns
	if len(lt.ByItems) == 0 {
		return false
	}

	return checkPartialOrderPattern(lt.SCtx(), lt.Children()[0])
}

// checkPartialOrderPattern recursively checks if the plan tree matches
// a supported pattern for partial order optimization.
// Supported intermediate operators: Selection, Projection
// Terminal operator: DataSource
func checkPartialOrderPattern(ctx base.PlanContext, plan base.LogicalPlan) bool {
	switch p := plan.(type) {
	case *logicalop.DataSource:
		return true

	case *logicalop.LogicalSelection:
		// Selection can pass through, check its child
		if len(p.Children()) != 1 {
			return false
		}
		return checkPartialOrderPattern(ctx, p.Children()[0])

	case *logicalop.LogicalProjection:
		// Projection can pass through (actual column check is done in TryToGetChildProp)
		if len(p.Children()) != 1 {
			return false
		}
		return checkPartialOrderPattern(ctx, p.Children()[0])

	default:
		// Other operators are not supported
		return false
	}
}

// getPhysTopNWithPartialOrderProperty generates PhysicalTopN plans with partial order property
// that use partial order optimization with prefix index.
func getPhysTopNWithPartialOrderProperty(lt *logicalop.LogicalTopN, prop *property.PhysicalProperty) []base.PhysicalPlan {
	// Convert logical TopN ByItems to SortItems for PartialOrderInfo
	sortItems := make([]*property.SortItem, 0, len(lt.ByItems))