cube-js/cube · error · UserError

SQL Parsing Error: ${this.errors.map(({ msg, column, line })

Error message

SQL Parsing Error:
${this.errors.map(({ msg, column, line }) => `${line}:${column} ${msg}`).join('\n')}

What it means

Aggregated parse-error report thrown by SqlParser.throwErrorsIfAny(). After ANTLR parsing of the SQL statement, all lexer/parser syntax errors are raised together as a UserError with 'line:column msg' lines. Callers like extractWhereConditions and extractTableFrom invoke it, so any SQL passed to these helpers must be parseable by the embedded GenericSql grammar.

Source

Thrown at packages/cubejs-schema-compiler/src/parser/SqlParser.ts:170

    lexer.addErrorListener(new ExprErrorListener());

    const parser = new GenericSqlParser(
      new CommonTokenStream(lexer)
    );
    parser.buildParseTrees = true;
    parser.removeErrorListeners();
    parser.addErrorListener(new ExprErrorListener());

    return parser.statement();
  }

  public canParse() {
    return !this.errors.length;
  }

  public throwErrorsIfAny() {
    if (this.errors.length) {
      throw new UserError(
        `SQL Parsing Error:\n${this.errors.map(({ msg, column, line }) => `${line}:${column} ${msg}`).join('\n')}`
      );
    }
  }

  public isSimpleAsteriskQuery(): boolean {
    if (!this.canParse()) {
      return false;
    }

    let result = false;

    this.ast.accept(nodeVisitor({
      visitNode(ctx) {
        if (ctx instanceof QueryContext) {
          const selectItems = ctx.getTypedRuleContext(SelectFieldsContext, 0);
          if (selectItems && selectItems.getText() === '*') {
            result = true;

View on GitHub (pinned to 7d981676b3)

Solutions

  1. Read each line:column entry and fix or simplify the SQL at those positions
  2. Rewrite dialect-specific syntax to ANSI SQL (e.g. LOWER(a)=LOWER(b) instead of ILIKE, CAST(x AS t) instead of x::t)
  3. Remove trailing semicolons and extra statements; pass a single statement
  4. Test the query with isSimpleAsteriskQuery()/canParse() and fall back gracefully when it cannot parse

Example fix

// before
extractTableFrom("SELECT * FROM t WHERE a::text = 'x'")
// after
extractTableFrom("SELECT * FROM t WHERE CAST(a AS TEXT) = 'x'")
Defensive patterns

Strategy: try-catch

Validate before calling

const parser = new SqlParser(sql);
if (!parser.canParse()) {
  throw new Error('SQL not parseable by Cube generic SQL grammar: ' + sql);
}

Try / catch

try {
  const table = extractTableFrom(sql);
} catch (e) {
  if (e instanceof UserError && e.message.startsWith('SQL Parsing Error:')) {
    console.error(e.message); // line:column per error
    // fall back: skip rewrite or ask user to simplify query
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling SqlParser-based helpers (extractWhereConditions, extractTableFrom) with SQL that the ANTLR GenericSqlLexer/GenericSqlParser cannot parse — unsupported dialect constructs, missing FROM clauses, odd operators, or truncated statements.

Common situations: Cube Security Context / query rewrite features inspecting user SQL: dialect-specific functions (ILIKE, :: casts, ::type, LIMIT syntax variants), semicolon-terminated multi-statements, or vendor syntax the generic grammar rejects.

Related errors


AI-assisted analysis of cube-js/cube@7d981676b3 (2026-09-02). Data as JSON: /api/errors/4b15a0d5fa868119. Report an issue: GitHub.