Skip to content
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

Second+ statement will not be parsed (as DefaultContext) if first statement is not correct #28

Open
surister opened this issue Jun 3, 2024 · 2 comments
Labels
bug Something isn't working grammar For antlr4 related grammar issues

Comments

@surister
Copy link
Collaborator

surister commented Jun 3, 2024

When parsing a query, if it has more than one statement, the statement of index 1+ has to start with a correct keyword to be correctly identified as an statement and therefore parsed.

For these examples the ExceptionError listener is turned off.

Ex:

sqlparse("""
    ELECT 1; -- wrong
    Begin 2; -- correct
    ELECT 2; -- wrong
""")

>>> [Statement<ELECT 1>, Statement<Begin>]
sqlparse("""
    ELECT 1; -- wrong
    Begin 2; -- correct
    SELECT 2; -- correct
""")
>>> [Statement<ELECT 1>, Statement<Begin>, Statement<SELECT 2>] 
 sqlparse("""
    ELECT 1; -- wrong
    egin 2; -- wrong
    SELECT 2; -- correct
""")

>>> [Statement<ELECT 1>]

Note in the last example how it needs a correct first keyword to keep parsing statements, even if the third 'statement' is correct, this only seems to apply from the first statement onward.

Definition of statements: https://github.com/crate/crate/blob/302c91871ca5c6b34dac07c01ef03f6f2e0017ee/libs/sql-parser/src/main/antlr/io/crate/sql/parser/antlr/SqlBaseParser.g4#L26

@surister surister added bug Something isn't working grammar For antlr4 related grammar issues labels Jun 3, 2024
@surister surister changed the title Second statement will not be parsed if first keyword is not correct Second+ statement will not be parsed if first keyword is not correct Jun 3, 2024
@surister surister changed the title Second+ statement will not be parsed if first keyword is not correct Second+ statement will not be parsed (as DefaultContext) if first keyword is not correct Jun 7, 2024
@surister
Copy link
Collaborator Author

surister commented Jun 7, 2024

statements = list(filter(lambda children: isinstance(children, SqlBaseParser.StatementContext), tree.children))

This only seems to happen when second+ statement is wrong.

For:

 -- This is a comment
SELECT A, B, C, D FROM tbl1;
-- This is a 2nd comment
SELECT 1; -- This is a 3rd comment
SELECT D, A FROM tbl1 WHERE;

tree.children is [[244], ;, [246], ;, [246], ;, <EOF>] where they are DefaultContext

For:

 -- This is a comment
SELECT A, B, C, D FROM tbl1;
-- This is a 2nd comment
SELEC 1; -- This is a 3rd comment
SELECT D, A FROM tbl1 WHERE;

tree.children is [[244], ;, SELEC, 1, ;, SELECT, D, ,, A, FROM, tbl1, WHERE, ;] where only the first one is DefaultContext and the rest are ErrorNodeImpl

@surister

This comment has been minimized.

@surister surister changed the title Second+ statement will not be parsed (as DefaultContext) if first keyword is not correct Second+ statement will not be parsed (as DefaultContext) if first statement is not correct Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working grammar For antlr4 related grammar issues
Projects
None yet
Development

No branches or pull requests

1 participant