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

Code before:
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 t
// 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))
	for _, byItem := range lt.ByItems {
		col, ok := byItem.Expr.(*expression.Column)
		if !ok {
			// ORDER BY must be simple column references for partial order optimization
			return nil
		}
		sortItems = append(sortItems, &property.SortItem{
			Col:  col,
			Desc: byItem.Desc,
		})
	}

	// Create PhysicalProperty with PartialOrderInfo
	// Use CopMultiReadTaskType for IndexLookUp
	partialOrderProp := &property.PhysicalProperty{
		TaskTp: property.CopMultiReadTaskType,
		// TODO: change it to limit + offset + N
		ExpectedCnt: math.MaxFloat64,
		PartialOrderInfo: &property.PartialOrderInfo{
			SortItems: sortItems,
		},

Review comment (suggestion):
The comment has an incomplete sentence ending with 't'. It should likely read 'property that' or 'property to'.
```suggestion
// getPhysTopNWithPartialOrderProperty generates PhysicalTopN plans with partial order property
```

Code after:
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))
	for _, byItem := range lt.ByItems {
		col, ok := byItem.Expr.(*expression.Column)
		if !ok {
			// ORDER BY must be simple column references for partial order optimization
			return nil
		}
		sortItems = append(sortItems, &property.SortItem{
			Col:  col,
			Desc: byItem.Desc,
		})
	}

	// Create PhysicalProperty with PartialOrderInfo
	// Use CopMultiReadTaskType for IndexLookUp
	partialOrderProp := &property.PhysicalProperty{
		TaskTp: property.CopMultiReadTaskType,
		// TODO: change it to limit + offset + N
		ExpectedCnt: math.MaxFloat64,
		PartialOrderInfo: &property.PartialOrderInfo{
			SortItems: sortItems,
		},