apache/shardingsphere · error · SQLParsingException

42000

42000

Error message

You have an error in your SQL syntax: %s

What it means

Thrown by SQLParserExecutor.parse when twoPhaseParse completes but the resulting ParseASTNode's root is an ANTLR ErrorNode. This covers the case where even the LL phase with the strict listener attached did not cancel, yet the tree is structurally invalid (error recovery produced an ErrorNode root). The raw SQL string is embedded in the message; SQLState 42000.

Source

Thrown at parser/sql/engine/core/src/main/java/org/apache/shardingsphere/sql/parser/engine/core/database/parser/SQLParserExecutor.java:51

/**
 * SQL parser executor.
 */
@RequiredArgsConstructor
public final class SQLParserExecutor {
    
    private final DatabaseType databaseType;
    
    /**
     * Parse SQL.
     *
     * @param sql SQL to be parsed
     * @return parse AST node
     * @throws SQLParsingException SQL parsing exception
     */
    public ParseASTNode parse(final String sql) {
        ParseASTNode result = twoPhaseParse(sql);
        if (result.getRootNode() instanceof ErrorNode) {
            throw new SQLParsingException(sql);
        }
        return result;
    }
    
    private ParseASTNode twoPhaseParse(final String sql) {
        DialectSQLParserFacade sqlParserFacade = DatabaseTypedSPILoader.getService(DialectSQLParserFacade.class, databaseType);
        SQLParser sqlParser = SQLParserFactory.newInstance(sql, sqlParserFacade.getLexerClass(), sqlParserFacade.getParserClass());
        try {
            ((Parser) sqlParser).getInterpreter().setPredictionMode(PredictionMode.SLL);
            return (ParseASTNode) sqlParser.parse();
        } catch (final ParseCancellationException ex) {
            ((Parser) sqlParser).reset();
            ((Parser) sqlParser).getInterpreter().setPredictionMode(PredictionMode.LL);
            ((Parser) sqlParser).removeErrorListeners();
            ((Parser) sqlParser).addErrorListener(SQLParserErrorListener.getInstance());
            try {
                return (ParseASTNode) sqlParser.parse();
            } catch (final ParseCancellationException exception) {

View on GitHub (pinned to e952770a21)

Solutions

  1. Log the full SQL string from the exception and inspect it for truncation or unsubstituted placeholders
  2. Validate SQL length/integrity at the source (no cut-off, correct encoding)
  3. Reproduce with the exact string in a unit test against the dialect parser to confirm the grammar gap
  4. Upgrade ShardingSphere if the dialect grammar fix exists upstream

Example fix

// before
String sql = readChunk(inputStream); // may cut mid-statement -> ErrorNode root

// after
String sql = readFully(inputStream); // read complete statement before parsing
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the SQL string is complete before parsing (e.g. statement ends cleanly)
if (sql == null || sql.length() < 6 || sql.indexOf('\0') >= 0) throw new IllegalArgumentException("Corrupt/truncated SQL");

Try / catch

try { executor.parse(sql); } catch (SQLParsingException e) { logSqlForDiagnosis(sql); return fallbackReject(sql); }

Prevention

When it happens

Trigger: Dialect SQL that both SLL and LL parsing survive syntactically at the token level but that attaches an ErrorNode at the root: incomplete statements ending mid-clause, mismatched constructs ANTLR recovers from, or grammar rules that embed error nodes for partial matches.

Common situations: Truncated SQL from network reads or log replay; statements cut at NUL bytes or newlines; partially-templated SQL where a placeholder was never substituted; grammar edge cases in less-used dialects.

Related errors


AI-assisted analysis of apache/shardingsphere@e952770a21 (2026-08-14). Data as JSON: /api/errors/67371bd1548cfc60. Report an issue: GitHub.