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

Code before:

	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.
			prefixColumnID = path.IdxCols[i].UniqueID
			prefixLen = path.IdxColLens[i]
			return &PartialOrderMatchResult{
				Matched:     true,
				PrefixColID: prefixColumnID,
				PrefixLen:   prefixLen,
			}
		}
	}

	return nil

Review comment (suggestion):
Using `range len(path.IdxCols)` is valid but less idiomatic. Consider using `for i := 0; i < len(path.IdxCols); i++` or `for i := range path.IdxCols` for better readability.
```suggestion
	for i := range path.IdxCols {
```

Code after:

	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.
			prefixColumnID = path.IdxCols[i].UniqueID
			prefixLen = path.IdxColLens[i]
			return &PartialOrderMatchResult{
				Matched:     true,
				PrefixColID: prefixColumnID,
				PrefixLen:   prefixLen,
			}
		}
	}

	return nil