-
Notifications
You must be signed in to change notification settings - Fork 578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to parse Trino statement with non-escaped limit column #3247
Comments
this is a big pain to deal with. considering most other engines limit is a reserved keyword, i'm not inclined to make this change. it breaks a lot of other things, so closing this as won't fix. |
Don't we just need to add LIMIT to the trino parser's |
from sqlglot import parse_one
from sqlglot.dialects.trino import Trino
from sqlglot.tokens import TokenType
Trino.Parser.ID_VAR_TOKENS.add(TokenType.LIMIT)
q = """
SELECT limit FROM limit ORDER BY limit LIMIT 1
"""
print(repr(parse_one(q, dialect="trino"))) Select(
expressions=[
Column(
this=Identifier(this=limit, quoted=False))],
limit=Limit(
expression=Literal(this=1, is_string=False)),
from=From(
this=Table(
this=Identifier(this=limit, quoted=False))),
order=Order(
expressions=[
Ordered(
this=Column(
this=Identifier(this=limit, quoted=False)),
nulls_first=False)])) |
It's not that simple. I think your example works by accident and may have actually uncovered an interesting bug. Basically, Even if you add Using the above logic you can easily construct an example where Trino's parser would fail: >>> from sqlglot import parse_one
>>> from sqlglot.dialects.trino import Trino
>>> from sqlglot.tokens import TokenType
>>>
>>> Trino.Parser.ID_VAR_TOKENS.add(TokenType.LIMIT)
>>>
>>> q = """
... SELECT a LIMIT 1
... """
>>>
>>> print(repr(parse_one(q, dialect="trino")))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "sqlglot/sqlglot/__init__.py", line 124, in parse_one
result = dialect.parse(sql, **opts)
File "sqlglot/sqlglot/dialects/dialect.py", line 490, in parse
return self.parser(**opts).parse(self.tokenize(sql), sql)
File "sqlglot/sqlglot/parser.py", line 1167, in parse
return self._parse(
File "sqlglot/sqlglot/parser.py", line 1236, in _parse
self.raise_error("Invalid expression / Unexpected token")
File "sqlglot/sqlglot/parser.py", line 1277, in raise_error
raise error
sqlglot.errors.ParseError: Invalid expression / Unexpected token. Line 2, Col: 16.
SELECT a LIMIT 1
~ |
don’t a ton of tests break when you add limit to id var? |
Yes, if you add it in the base parser. If you add it to a dialect that doesn't override |
i added limit to table alias tokens in base as well and all the tests broke. |
Ah I see. FWIW, Here's trino's grammar. Some tests on trino: -- fails
SELECT 1 limit
-- succeeds
SELECT 1 AS limit
-- fails
SELECT limit FROM (SELECT 1 AS limit)
-- succeeds
SELECT "limit" FROM (SELECT 1 AS limit) @john-bodley where did this come up? Does |
@barakalon this toy example is from Superset performing a Regardless, |
Based on |
oh - ha! |
According to the documentation
LIMIT
is not a reserved keyword, though it seems like SQLGlot is unable to parse statements referring to a column namedlimit
unless it is escaped.Before you file an issue
parse_one(sql, read="spark")
ast.sql(dialect="duckdb")
Fully reproducible code snippet
Please include a fully reproducible code snippet or the input sql, dialect, and expected output.
Note that SQLGlot is able to parse the following statement if the
limit
column is escaped.Official Documentation
The text was updated successfully, but these errors were encountered: