vitessio/vitess · error

requested limit is out of range: %v

Error message

requested limit is out of range: %v

What it means

MemorySort.fetchCount validates the LIMIT/OFFSET value used by in-memory sorting; if the value is not a valid non-negative integer (Atoi fails or is negative), this error is returned. It is the MemorySort counterpart of the Limit primitive's identical check.

Source

Thrown at go/vt/vtgate/engine/memory_sort.go:140

}

func (ms *MemorySort) fetchCount(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (int, error) {
	if ms.UpperLimit == nil {
		return math.MaxInt, nil
	}
	env := evalengine.NewExpressionEnv(ctx, bindVars, vcursor)
	resolved, err := env.Evaluate(ms.UpperLimit)
	if err != nil {
		return 0, err
	}
	value := resolved.Value(vcursor.ConnCollation())
	if !value.IsIntegral() {
		return 0, sqltypes.ErrIncompatibleTypeCast
	}

	count, err := strconv.Atoi(value.RawStr())
	if err != nil || count < 0 {
		return 0, fmt.Errorf("requested limit is out of range: %v", value.RawStr())
	}
	return count, nil
}

func (ms *MemorySort) description() PrimitiveDescription {
	orderByIndexes := GenericJoin(ms.OrderBy, orderByParamsToString)
	other := map[string]any{"OrderBy": orderByIndexes}
	if ms.TruncateColumnCount > 0 {
		other["ResultColumns"] = ms.TruncateColumnCount
	}
	return PrimitiveDescription{
		OperatorType: "Sort",
		Variant:      "Memory",
		Other:        other,
	}
}

func orderByParamsToString(i any) string {

View on GitHub (pinned to 01a25a7d17)

Solutions

  1. Ensure LIMIT/OFFSET evaluate to non-negative integral values.
  2. Fix page-number arithmetic so offset = (page-1)*size never goes negative.
  3. Coerce or reject fractional/oversized values in application code before executing.

Example fix

// before
offset := (page - 1) * size // page=0 -> -10
// after
if page < 1 { page = 1 }
offset := (page - 1) * size
Defensive patterns

Strategy: validation

Validate before calling

if !validLimitOffset(limit) || !validLimitOffset(offset) {
    return errors.New("LIMIT/OFFSET must be non-negative integers")
}
func validLimitOffset(v int) bool { return v >= 0 }

Type guard

func isNonNegativeInt(v any) bool {
    n, ok := v.(int)
    return ok && n >= 0
}

Prevention

When it happens

Trigger: TryExecute/TryStreamExecute of an ORDER BY query whose LIMIT or OFFSET expression evaluates to a non-integral, non-numeric, negative, or overflow-sized value.

Common situations: Negative OFFSET computed from page arithmetic; LIMIT supplied as a float (e.g. 10.5); bind parameter serialized with quotes/spaces; huge values overflowing int.

Related errors


AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01). Data as JSON: /api/errors/f6c0488367616c6e. Report an issue: GitHub.