You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by Arthur Pan <pa...@berkeley.edu> on 2022/04/15 07:08:58 UTC

FOREIGN KEY support in SqlDdlParser

Dear dev,

I'm trying to parse a create table statement with foreign key into SqlNode
using the following code snippet:

String createTable = "CREATE TABLE emp (\n" +
        "    empno INTEGER,\n" +
        "    name VARCHAR NOT NULL,\n" +
        "    deptno INTEGER,\n" +
        "    FOREIGN KEY (deptno) REFERENCES dept(deptno)\n" +
        ")";
SqlParser.Config schemaParserConfig = SqlParser.Config.DEFAULT
        .withParserFactory(SqlDdlParserImpl.FACTORY)
        .withLex(Lex.MYSQL);
SqlParser schemaParser = SqlParser.create(createTable, schemaParserConfig);
SqlNode schemaNode = schemaParser.parseStmt();

but it seems that the parser (from calcite-server) could not recognize the
"FOREIGN" keyword and it will throw an exception. If the foreign key row is
commented out, while the trailing comma above is removed, this statement
could be parsed correctly.

If the parser supports foreign key constraints, how should I use it (or is
there anything wrong/missing in the code above)? Otherwise is there a
reason why the foreign key constraint is not supported? I would appreciate
any help on this.

Sincerely,
Arthur Pan

Re: FOREIGN KEY support in SqlDdlParser

Posted by Julian Hyde <jh...@gmail.com>.
I’m not keen on supporting foreign key syntax in the parser without also implementing foreign keys in the engine. That’s a big task.

Maybe the Babel parser could support it. 

Julian

> On Apr 16, 2022, at 05:43, Benchao Li <li...@apache.org> wrote:
> 
> Hi Arthur,
> 
> I checked out the parser in Calcite, and I think you are right,
> Calcite's server module does not support 'FOREIGN KEY' now.
> 
> Searching in Jira, there is no related issues about this feature.
> IMHO, Calcite could support it.
> 
> 
> Arthur Pan <pa...@berkeley.edu> 于2022年4月15日周五 22:52写道:
> 
>> Dear dev,
>> 
>> I'm trying to parse a create table statement with foreign key into SqlNode
>> using the following code snippet:
>> 
>> String createTable = "CREATE TABLE emp (\n" +
>>        "    empno INTEGER,\n" +
>>        "    name VARCHAR NOT NULL,\n" +
>>        "    deptno INTEGER,\n" +
>>        "    FOREIGN KEY (deptno) REFERENCES dept(deptno)\n" +
>>        ")";
>> SqlParser.Config schemaParserConfig = SqlParser.Config.DEFAULT
>>        .withParserFactory(SqlDdlParserImpl.FACTORY)
>>        .withLex(Lex.MYSQL);
>> SqlParser schemaParser = SqlParser.create(createTable, schemaParserConfig);
>> SqlNode schemaNode = schemaParser.parseStmt();
>> 
>> but it seems that the parser (from calcite-server) could not recognize the
>> "FOREIGN" keyword and it will throw an exception. If the foreign key row is
>> commented out, while the trailing comma above is removed, this statement
>> could be parsed correctly.
>> 
>> If the parser supports foreign key constraints, how should I use it (or is
>> there anything wrong/missing in the code above)? Otherwise is there a
>> reason why the foreign key constraint is not supported? I would appreciate
>> any help on this.
>> 
>> Sincerely,
>> Arthur Pan
>> 
> 
> 
> -- 
> 
> Best,
> Benchao Li

Re: FOREIGN KEY support in SqlDdlParser

Posted by Benchao Li <li...@apache.org>.
Hi Arthur,

I checked out the parser in Calcite, and I think you are right,
Calcite's server module does not support 'FOREIGN KEY' now.

Searching in Jira, there is no related issues about this feature.
IMHO, Calcite could support it.


Arthur Pan <pa...@berkeley.edu> 于2022年4月15日周五 22:52写道:

> Dear dev,
>
> I'm trying to parse a create table statement with foreign key into SqlNode
> using the following code snippet:
>
> String createTable = "CREATE TABLE emp (\n" +
>         "    empno INTEGER,\n" +
>         "    name VARCHAR NOT NULL,\n" +
>         "    deptno INTEGER,\n" +
>         "    FOREIGN KEY (deptno) REFERENCES dept(deptno)\n" +
>         ")";
> SqlParser.Config schemaParserConfig = SqlParser.Config.DEFAULT
>         .withParserFactory(SqlDdlParserImpl.FACTORY)
>         .withLex(Lex.MYSQL);
> SqlParser schemaParser = SqlParser.create(createTable, schemaParserConfig);
> SqlNode schemaNode = schemaParser.parseStmt();
>
> but it seems that the parser (from calcite-server) could not recognize the
> "FOREIGN" keyword and it will throw an exception. If the foreign key row is
> commented out, while the trailing comma above is removed, this statement
> could be parsed correctly.
>
> If the parser supports foreign key constraints, how should I use it (or is
> there anything wrong/missing in the code above)? Otherwise is there a
> reason why the foreign key constraint is not supported? I would appreciate
> any help on this.
>
> Sincerely,
> Arthur Pan
>


-- 

Best,
Benchao Li