vitessio/vitess · error

range %d should be >= %d

Error message

range %d should be >= %d

What it means

While parsing a JSON path range clause, the second offset (the range end) must be strictly greater than the first offset (offset0). The parser in go/mysql/json/json_path.go rejects clauses like '$[1 to 1]' or '$[3 to 2]' where the range end is not greater than the range start. This enforces MySQL's semantics that a range must cover at least one element.

Source

Thrown at go/mysql/json/json_path.go:597

		return in[2:], nil
	}
	return nil, errInvalid
}

func stepArrayLocationTo(p *PathParser, in []byte) ([]byte, error) {
	var skip int
	in, skip = trim(in)
	if in == nil || skip == 0 {
		return nil, errInvalid
	}
	if in[0] >= '0' && in[0] <= '9' {
		p.step = stepArrayLocationClose
		offset, in2, err := p.lexNumeric(in)
		if err != nil {
			return nil, err
		}
		if offset <= p.path.offset0 {
			return nil, fmt.Errorf("range %d should be >= %d", offset, p.path.offset0)
		}
		p.path.offset1 = offset
		return in2, nil
	}
	if bytes.HasPrefix(in, []byte{'l', 'a', 's', 't'}) {
		p.step = stepArrayLocationLast1
		p.path.offset1 = -1
		return in[4:], nil
	}
	return nil, errInvalid
}

func stepArrayLocationClose(p *PathParser, in []byte) ([]byte, error) {
	if in, _ = trim(in); in == nil {
		return nil, errInvalid
	}
	if in[0] == ']' {
		p.step = stepPathLeg

View on GitHub (pinned to 01a25a7d17)

Solutions

  1. Fix the JSON path so the range end is strictly greater than the start, e.g. '$[1 to 3]' instead of '$[1 to 1]'
  2. If you need a single element, use a plain index like '$[1]' instead of an empty range
  3. Validate/clamp range bounds in application code before passing the path string to the parser
  4. Swap reversed bounds (use min as start, max as end) before parsing

Example fix

// before
path := "$.items[2 to 2]"
// after
path := "$.items[2]" // or "$.items[2 to 3]"
Defensive patterns

Strategy: validation

Validate before calling

func validRange(p string) error {
    // e.g. "$.a[1 to 3]": ensure end > start
    if start, end, ok := parseRangeBounds(p); ok && end <= start {
        return fmt.Errorf("range end %d must be > start %d", end, start)
    }
    return nil
}

Try / catch

if _, err := json.NormalizePath(userPath); err != nil {
    if strings.Contains(err.Error(), "should be >=") {
        return fmt.Errorf("invalid JSON path range in %q: %w", userPath, err)
    }
    return err
}

Prevention

When it happens

Trigger: Evaluating or parsing a JSON path with a 'to' range where the second numeric location is less than or equal to the first, e.g. stepArrayLocationTo reading '$.a[2 to 2]' or '$.a[5 to 1]', including cases where both values are negative or the second offset was already consumed as offset0.

Common situations: User-supplied JSON path strings from application code or SQL queries with typos in range bounds; programmatically generated ranges where start and end are equal (empty range) or reversed; off-by-one in code computing range endpoints.

Related errors


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