0

I have a project that requires using Calcite's SQL Parser to parse large amounts of DDL statements which are in heavy MySQL dialect.

I have isolated an issue which can be illustrated with this specific example:

create table `t1` (x int(11))

The astute reader may recognize int(11) is MySQL dialect. I need to be able to parse this. Removing the (11) in this example is not an acceptable fix.

Here's a Java JUnit test that reproduces the problem:

public class SqlParserMysqlTest {

    private static final Config CONFIG = SqlParser.Config.DEFAULT
        .withLex(Lex.MYSQL).withConformance(SqlConformanceEnum.MYSQL_5)
        .withParserFactory(SqlDdlParserImpl.FACTORY);

    @Test
    public void testMysqlCreate() throws SqlParseException {
        SqlParser sqlParser = SqlParser.create(
                new SourceStringReader("create table `t1` (x int(11))"), CONFIG);
        SqlNode sqlNode = sqlParser.parseQuery();

        Assert.assertEquals(SqlKind.CREATE_TABLE, sqlNode.getKind());

        SqlCreateTable create = (SqlCreateTable) sqlNode;
        Assert.assertEquals("T1", create.name.getSimple());
        final SqlColumnDeclaration sqlColumnDeclaration = (SqlColumnDeclaration) create.columnList
                .get(0);
        Assert.assertEquals("X", sqlColumnDeclaration.name.getSimple());
        Assert.assertEquals("INTEGER",
                sqlColumnDeclaration.dataType.getTypeName().getSimple());
    }

}

POM dependencies:

    <dependency>
        <groupId>org.apache.calcite</groupId>
        <artifactId>calcite-core</artifactId>
        <version>1.26.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.calcite</groupId>
        <artifactId>calcite-server</artifactId>
        <version>1.26.0</version>
    </dependency>

The issue is that the parser barfs on the parenthesis in int(11):

org.apache.calcite.sql.parser.SqlParseException: Encountered "(" at line 1, column 25.
Was expecting one of:
    "ARRAY" ...
    "AS" ...
    "DEFAULT" ...
    "GENERATED" ...
    "MULTISET" ...
    "NOT" ...
    "NULL" ...
    ")" ...
    "," ...
    
    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)

I know that other people are successfully using Calcite to parse MySQL syntax, so what am I doing wrong?

Alex R
  • 11,364
  • 15
  • 100
  • 180

1 Answers1

0

From the documentation on table elements, it looks like the corrected syntax could be create table `t1` (x int 11) (losing the internal parentheses) but I have no way to test it.

Joseph E
  • 33
  • 7