apache / datafusion-sqlparser-rs

Extensible SQL Lexer and Parser for Rust
Apache License 2.0
2.8k stars 543 forks source link

Expression prefix parsed instead of identifier #1496

Open yoavcloud opened 3 weeks ago

yoavcloud commented 3 weeks ago

When a statement uses identifiers that match certain expression prefix keywords, the parser will try to parse the expression over parsing the token as an identifier. The following statements work on Snowflake but the parser errors on the SELECT. If a different identifier name is selected, for example, interval1 then the parser works as expected.

create or replace temporary table test(interval int);
insert into test (interval) values (1);
insert into test (interval) values (2);
select max(interval) from test;
yoavcloud commented 2 weeks ago

The solution outline I'm thinking about is to try to parse the expression but if that results in an error, try to parse an identifier, if the keyword is not reserved by the dialect.

For the query above, Snowflake doesn't reserve the INTERVAL keyword so it can be used as an identifier without quoting, but BigQuery does.

yoavcloud commented 1 week ago

PR to fix this: https://github.com/apache/datafusion-sqlparser-rs/pull/1513