prestodb/presto · error · ParsingException

${name} is too large (stack overflow while parsing)

Error message

${name} is too large (stack overflow while parsing)

What it means

SqlParser catches StackOverflowError during ANTLR parsing and rethrows it as a ParsingException saying the input is too large. Deeply nested expressions exceed the parser's recursive-descent stack depth, so the library converts the JVM-level crash into a controlled error named after the statement kind.

Source

Thrown at presto-parser/src/main/java/com/facebook/presto/sql/parser/SqlParser.java:186

            ParserRuleContext tree;
            try {
                // first, try parsing with potentially faster SLL mode
                parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
                tree = parseFunction.apply(parser);
            }
            catch (ParseCancellationException ex) {
                // if we fail, parse with LL mode
                tokenStream.reset(); // rewind input stream
                parser.reset();

                parser.getInterpreter().setPredictionMode(PredictionMode.LL);
                tree = parseFunction.apply(parser);
            }

            return new AstBuilder(parsingOptions).visit(tree);
        }
        catch (StackOverflowError e) {
            throw new ParsingException(name + " is too large (stack overflow while parsing)");
        }
    }

    private class PostProcessor
            extends SqlBaseBaseListener
    {
        private final List<String> ruleNames;
        private final Consumer<ParsingWarning> warningConsumer;

        public PostProcessor(List<String> ruleNames, Consumer<ParsingWarning> warningConsumer)
        {
            this.ruleNames = ruleNames;
            this.warningConsumer = requireNonNull(warningConsumer, "warningConsumer is null");
        }

        @Override
        public void exitUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext context)
        {

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Reduce expression nesting: break giant OR/AND chains into IN lists or temp tables
  2. Run parsing with a larger thread stack (e.g. new Thread(group, runnable, name, 8MB))
  3. Split very large statements into smaller ones
  4. Pre-process generated SQL to flatten redundant parentheses

Example fix

// before
String sql = "SELECT * FROM t WHERE a=1 OR a=2 OR ... OR a=10000"; // stack overflow
// after
String sql = "SELECT * FROM t WHERE a IN (1,2,...,10000)";
Defensive patterns

Strategy: validation

Validate before calling

// Reject excessively deep nesting before parsing
int depth = 0, max = 0;
for (char c : sql.toCharArray()) {
    if (c == '(') max = Math.max(max, ++depth);
    else if (c == ')') depth--;
}
if (max > 300) throw new IllegalArgumentException("Query nesting too deep (" + max + ")");

Type guard

boolean nestingWithinLimit(String sql, int limit) {
    int depth = 0;
    for (char c : sql.toCharArray()) {
        if (c == '(' && ++depth > limit) return false;
        if (c == ')') depth--;
    }
    return true;
}

Try / catch

try {
    return sqlParser.createStatement(sql);
} catch (ParsingException e) {
    if (e.getMessage() != null && e.getMessage().contains("stack overflow")) {
        throw new QueryTooLargeException(sql.length(), e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Parsing SQL whose expression nesting is extremely deep — hundreds of nested parentheses, huge OR/AND chains, or deeply nested subqueries — via createStatement, createExpression, or createReturn.

Common situations: Machine-generated SQL (ORMs, query builders, OR-chain filters from large id lists), programmatic query generators without depth limits.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/f3a3af003e152b8a. Report an issue: GitHub.