dgraph-io/dgraph · error

Out of range for peek

Error message

Out of range for peek

What it means

ItemIterator.Peek returns the next num items without consuming them, but it rejects the request when the peek window extends past the end of the buffered lexed items. The parser calls Peek while deciding how to parse expressions (math functions, variable names, arguments, geo arguments, function calls, cascade directives), and this error signals the token stream simply ended.

Source

Thrown at lex/lexer.go:116

	}
	return false
}

// Restore restores the iterator to position specified.
func (p *ItemIterator) Restore(pos int) {
	x.AssertTrue(pos <= len(p.l.items) && pos >= -1)
	p.idx = pos
}

// Save returns the current position of the iterator which we can use for restoring later.
func (p *ItemIterator) Save() int {
	return p.idx
}

// Peek returns the next n items without consuming them.
func (p *ItemIterator) Peek(num int) ([]Item, error) {
	if (p.idx + num + 1) > len(p.l.items) {
		return nil, errors.Errorf("Out of range for peek")
	}
	return p.l.items[p.idx+1 : p.idx+num+1], nil
}

// PeekOne returns the next 1 item without consuming it.
func (p *ItemIterator) PeekOne() (Item, bool) {
	if p.idx+1 >= len(p.l.items) {
		return Item{
			line:   -1,
			column: -1, // use negative number to indicate out of range
		}, false
	}
	return p.l.items[p.idx+1], true
}

// A RuneWidth represents a consecutive string of runes with the same width
// and the number of runes is stored in count.
// The reason we maintain this information is to properly backup when multiple look-aheads happen.

View on GitHub (pinned to 759e242be6)

Solutions

  1. Inspect the query around the reported position and complete the truncated expression (add the missing arguments/closing parenthesis)
  2. Ensure the HTTP body/gRPC payload is not being truncated by proxies or string-size limits
  3. Wrap parser calls and surface a clearer 'unexpected end of query' message to users
  4. If writing Go code against the lexer directly, call PeekOne (returns bool) or check iterator bounds before Peek

Example fix

// before
items, err := it.Peek(3) // err: Out of range for peek on 'func: eq(name'
// after
items, err := it.Peek(3)
if err != nil {
    return fmt.Errorf("query ended unexpectedly while parsing %q: %w", partial, err)
}
Defensive patterns

Strategy: validation

Validate before calling

// Guard: does the query text end mid-expression?
if strings.HasSuffix(strings.TrimSpace(q), "(") || strings.HasSuffix(strings.TrimSpace(q), ",") {
    return fmt.Errorf("query appears truncated: %q", q)
}

Try / catch

items, err := it.Peek(n)
if err != nil {
    // token stream exhausted: treat as unexpected-end-of-query
    return fmt.Errorf("unexpected end of query while parsing: %w", err)
}

Prevention

When it happens

Trigger: Calling ItemIterator.Peek(num) when p.idx + num + 1 >= len(items); practically, this happens when the parser tries to look ahead past the end of the query, e.g. a query that stops mid-expression like 'math(' or an incomplete function argument list.

Common situations: Truncated queries sent to Dgraph: network tooling cut the request, a template rendered only part of the query, or a user submitted an unfinished query ending right after a function name or opening parenthesis.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/f1c291a1529c9c6b. Report an issue: GitHub.