3

Following is the configuration that I'm using to parse MySQL statements. I am able to parse and process DML statements fine but I can't seem to parse any DDL statements (CREATE TABLE, ALTER TABLE, DROP TABLE, etc.).

public static SqlNode parse(String query) {
    SqlParser.Config sqlParserConfig = SqlParser.configBuilder()
        .setConformance(SqlConformanceEnum.MYSQL_5)
        .setLex(Lex.MYSQL)
        .build();

    SqlParser parser = SqlParser.create(query, sqlParserConfig);

    try {
        return parser.parseStmt();
    } catch (SqlParseException e) {
        lastErrorMessage = e.getMessage();
        return null;
    }
}

When I try to parse a CREATE TABLE statement, it gives me the following error message.

Encountered "ALTER TABLE" at line 1, column 1.
Was expecting one of:
    "SET" ...
    "RESET" ...
    "ALTER" "SYSTEM" ...
    "ALTER" "SESSION" ...
    "WITH" ...
    "+" ...
    "-" ...
    "NOT" ...
    "EXISTS" ...
    <UNSIGNED_INTEGER_LITERAL> ...
    <DECIMAL_NUMERIC_LITERAL> ...
    <APPROX_NUMERIC_LITERAL> ...
    <BINARY_STRING_LITERAL> ...
    <PREFIXED_STRING_LITERAL> ...
    <QUOTED_STRING> ...
    <UNICODE_STRING_LITERAL> ...
... 

I can see classes like SqlAlter, SqlCreate,SqlDrop in the library. Is there a different way to parse DDLs?

I'm using 1.17.0 version.

Dilip Raj Baral
  • 3,060
  • 6
  • 34
  • 62
  • Seems like `parseStmt` calls into `parseQuery`, which is documented as "Parses a SELECT statement and reuses parser.". So it seems like both of these methods can only parse a select-statements (https://github.com/apache/calcite/blob/03c88b69c27ce7df22eee889503c70e6fe6bb016/core/src/main/java/org/apache/calcite/sql/parser/SqlParser.java#L160) – Caramiriel Dec 16 '18 at 09:56
  • @Caramiriel No. The documentation says so but they can parse INSERT, UPDATE and DELETE statements as well. – Dilip Raj Baral Dec 16 '18 at 10:17

1 Answers1

3

I think you are just missing the appropriate parser factory. Try the following:

SqlParser.Config sqlParserConfig = SqlParser.configBuilder()
    .setParserFactory(SqlDdlParserImpl.FACTORY)
    .setConformance(SqlConformanceEnum.MYSQL_5)
    .setLex(Lex.MYSQL)
    .build();

SqlParser parser = SqlParser.create(query, sqlParserConfig);
zabetak
  • 412
  • 4
  • 13