Apache Calcite SqlParser is failing with certain Postgres Keywords

1k Views Asked by At

I am using Calcite's SqlParser, but I am running into a few issues with Postgres queries, namely that PRIMARY, TIME, and ZONE are not being properly parsed. The following code is what I am working with:

import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.sql.ddl.SqlCreateTable;

SqlParser.Config sqlParserConfig = SqlParser
    .configBuilder()
    .setParserFactory(SqlDdlParserImpl.FACTORY)
    .build();
String sql = "CREATE TABLE \"elements_elementcomponent\" (\"id\" bigserial NOT NULL, \"created_at\" timestamp with time zone NOT NULL, \"updated_at\" timestamp with time zone NOT NULL, \"version\" double precision NOT NULL, \"git_sha\" varchar(60) NOT NULL, \"element_id\" bigint NULL, \"element_item_id\" bigint NULL, \"element_basket_id\" bigint NULL);";
SqlParser parser = SqlParser.create(sql, sqlParserConfig);
SqlCreateTable stmt = (SqlCreateTable) parser.parseQuery();

The following error, with the above keywords, occurs:

org.apache.calcite.sql.parser.SqlParseException: Encountered "PRIMARY" at line 1, column 69.
Was expecting one of:
    "AS" ...
    "DEFAULT" ...
    "GENERATED" ...
    ")" ...
    "," ...
    
    at org.apache.calcite.sql.parser.ddl.SqlDdlParserImpl.convertException(SqlDdlParserImpl.java:394)
    at org.apache.calcite.sql.parser.ddl.SqlDdlParserImpl.normalizeException(SqlDdlParserImpl.java:157)
    at org.apache.calcite.sql.parser.SqlParser.handleException(SqlParser.java:140)
    at org.apache.calcite.sql.parser.SqlParser.parseQuery(SqlParser.java:155)
    at .(#110:1)

I'm not sure why this would happen, because those keywords appear supported in Apache Calcite - SQL language - Keywords.

0

There are 0 best solutions below