Skip to content

FROM not rejected in column position #166

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

Closed
aidanhs opened this issue May 11, 2020 · 2 comments
Closed

FROM not rejected in column position #166

aidanhs opened this issue May 11, 2020 · 2 comments

Comments

@aidanhs
Copy link

aidanhs commented May 11, 2020

Parsing SELECT FROM abc succeeds with:

AST: [                                                                         
    Query(                                                                     
        Query {                                                                
            ctes: [],                                                          
            body: Select(                                                      
                Select {                                                       
                    distinct: false,                                           
                    projection: [                                              
                        ExprWithAlias {                                        
                            expr: Identifier(                                  
                                "FROM",                                        
                            ),                                                 
                            alias: "issues",                                   
                        },                                                     
                    ],                                                         
                    from: [],                                                  
                    selection: None,                                           
                    group_by: [],                                              
                    having: None,                                              
                },                                                             
            ),                                                                 
            order_by: [],                                                                                                                                     
            limit: None,                                                       
            offset: None,                                                      
            fetch: None,                                                       
        },                                                                     
    ),                                                                         
]

It seems suprising that parsing accepted FROM as a column identifier! The SQL engines I've quickly tried out (sqlite locally, a handful of online validators) all seem to indicate that it's invalid - maybe it should be rejected?

@aidanhs aidanhs changed the title FROM keyword not rejected in column position FROM not rejected in column position May 11, 2020
@nickolay
Copy link
Contributor

I agree that accepting SELECT FROM abc is surprising and I agree that this is probably invalid in many (if not all) dialects, so I would consider a fix if someone were to propose one.

But it's not obvious to me how this should be changed or why.


Some context:

There have been precedents for adding validations to reject such obviously incorrect code, e.g. in https://github.com/andygrove/sqlparser-rs/pull/118, but also for disabling such validations because some dialect was found to accept what we thought was universally rejected (https://github.com/andygrove/sqlparser-rs/issues/154).

In general, sqlparser does not promise to be a validating parser, that definitely rejects "invalid" SQL, because there's no commonly accepted definition of such a thing. I did some research on "reserved keywords" in various dialects before I implemented the current behavior:

  • the SQL specification, for example, reserves more keywords than any other real-world engine (e.g. position or period)
  • what it means for a keyword to be "reserved" also depends on the dialect. In Postgres, for example, a keyword can be fully reserved, allowed only as a function or type name, allowed only as a column or table name, or both. And even reserved keywords can appear after AS, e.g. SELECT 1 AS FROM.

@aidanhs
Copy link
Author

aidanhs commented Jun 2, 2020

Your reasoning is pretty convincing - thanks for laying it out so clearly!

@aidanhs aidanhs closed this as completed Jun 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants