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

Code before:
// In fact, there is a Case3 that can also be supported, but it will not be explained in detail here.
// Please refer to the design document for details.
func matchPartialOrderProperty(path *util.AccessPath, partialOrderInfo *property.PartialOrderInfo) *PartialOrderMatchResult {
	if partialOrderInfo == nil || path.Index == nil || len(path.IdxCols) == 0 {
		return nil
	}

	sortItems := partialOrderInfo.SortItems
	if len(sortItems) == 0 {
		return nil
	}

	allSameOrder, _ := partialOrderInfo.AllSameOrder()
	if !allSameOrder {
		return nil
	}

	// Case 1: Prefix index INDEX idx(col(N)) matches ORDER BY col
	// Check if index columns can match ORDER BY columns (allowing prefix index)
	// Constraint 1: The number of index columns must be <= the number of ORDER BY columns
	if len(path.IdxCols) > len(sortItems) {
		return nil
	}
	// Constraint 2: The last column of the index must be a prefix column
	if path.IdxColLens[len(path.IdxCols)-1] == types.UnspecifiedLength {
		// The Last column is not a prefix column, skip this index
		return nil
	}
	// Extract ORDER BY columns
	orderByCols := make([]*expression.Column, 0, len(sortItems))
	for _, item := range sortItems {
		orderByCols = append(orderByCols, item.Col)
	}

	var prefixColumnID int64
	var prefixLen int
	for i := range len(path.IdxCols) {
		// check if the same column
		if !orderByCols[i].EqualColumn(path.IdxCols[i]) {
			return nil
		}

		// meet prefix index column, match termination
		if path.IdxColLens[i] != types.UnspecifiedLength {
			// If we meet a prefix column but it's not the last index column, it's not supported.
			// e.g. prefix(a), b cannot provide partial order for ORDER BY a, b.
			if i != len(path.IdxCols)-1 {
				return nil
			}
			// Encountered a prefix index column.
			// This prefix index column can provide partial order, but subsequent columns cannot match.

Review comment (suggestion):
Inconsistent capitalization in comment: 'The Last column' should be 'The last column'.
```suggestion
		// The last column is not a prefix column, skip this index
```

Code after:
// In fact, there is a Case3 that can also be supported, but it will not be explained in detail here.
// Please refer to the design document for details.
func matchPartialOrderProperty(path *util.AccessPath, partialOrderInfo *property.PartialOrderInfo) *PartialOrderMatchResult {
	if partialOrderInfo == nil || path.Index == nil || len(path.IdxCols) == 0 {
		return nil
	}

	sortItems := partialOrderInfo.SortItems
	if len(sortItems) == 0 {
		return nil
	}

	allSameOrder, _ := partialOrderInfo.AllSameOrder()
	if !allSameOrder {
		return nil
	}

	// Case 1: Prefix index INDEX idx(col(N)) matches ORDER BY col
	// Check if index columns can match ORDER BY columns (allowing prefix index)
	// Constraint 1: The number of index columns must be <= the number of ORDER BY columns
	if len(path.IdxCols) > len(sortItems) {
		return nil
	}
	// Constraint 2: The last column of the index must be a prefix column
	if path.IdxColLens[len(path.IdxCols)-1] == types.UnspecifiedLength {
		// The last column is not a prefix column, skip this index
		return nil
	}
	// Extract ORDER BY columns
	orderByCols := make([]*expression.Column, 0, len(sortItems))
	for _, item := range sortItems {
		orderByCols = append(orderByCols, item.Col)
	}

	var prefixColumnID int64
	var prefixLen int
	for i := range len(path.IdxCols) {
		// check if the same column
		if !orderByCols[i].EqualColumn(path.IdxCols[i]) {
			return nil
		}

		// meet prefix index column, match termination
		if path.IdxColLens[i] != types.UnspecifiedLength {
			// If we meet a prefix column but it's not the last index column, it's not supported.
			// e.g. prefix(a), b cannot provide partial order for ORDER BY a, b.
			if i != len(path.IdxCols)-1 {
				return nil
			}
			// Encountered a prefix index column.
			// This prefix index column can provide partial order, but subsequent columns cannot match.