-
Notifications
You must be signed in to change notification settings - Fork 603
Fail to parse - "select * from (table_name) alias" (Snowflake) #223
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
Comments
It looks like Edit: Ah, of course this is expected, as seen in #154. |
As noted in #154, This becomes hard to reason about. The SQL standard allows |
I think it's reasonable not to handle this in this library, I can think of some workarounds. This is snowflake :) and just double-checked that this syntax works. |
Its docs don't explain how it handles this.. Out of curiosity, how does it react to |
Don't sure exactly what the roles of this syntax .. |
Thanks @eyalleshem! I think forking |
In my previous comment I forgot that there's also the problem of representing this in AST. @eyalleshem, as you indicated you'd like to work on this, please start by documenting the grammar the Snowflake dialect will implement here, and how it can fit into our AST. |
I opened a PR - should be enable that,
thats enable me to not change the AST , and in case of |
Ah, so the only difference on the parser level is that of the three TableFactor variants (Table, Derived, or Nested), we currently allow aliases on the first two only, where your version parses it after Nested(Join) as well, but uses the logic you describe to find the inner TableFactor (Table or Derived) to store it in. This is a smart solution, but I don't feel we gain much by not changing the AST, and it would be simpler to change NestedJoin to a
|
Well,
I agree that if we encounter other DB that allow single table in parens- but not follow the assumptions that describe above , maybe it's should be handled differently. But if you think it's better , i don't care about changing the behaviour.. |
I realize that it is more convenient for consumers to have some semantic processing happen in the library, but simply parsing SQL is rather hard, so I'd rather not complicate matters by attempting to guess the semantics during parsing... Happy to hear what others think as well! |
@nickolay - according to your suggestion - you would enable parsing joins with aliases even on the generic dialect ? For example - do you think that this statement should be parsed in the generic dialect: I asked that because the only difference that i see between snowflake and the ANSI sql here that the question if they allow "redundant" parens around single table factor , but snowflake not allow to alias "real joins" ( like the other dialect). so i don't sure that i feel comfortable to parse that on snowflake, but throw an error in the other dialect. |
I think maybe i have other suggestion - In snowflake dialect : if the This solution will eliminate the problem of aliasing real joins (joins with multiple tables) , as currently no database support that. And redundant parens around single table will not treat anymore as joins (I think that what confused my most- because real join is between tables ). Btw , this solution will have impact only on snowflake dialect... |
That's how it could work if my yesterday's suggestion were implemented, yes. There's no requirement that the generic dialect must be able to parse that, it follows naturally from the proposed design of the AST and lack of validation in the generic dialect... Your new idea sounds promising though. If losing the information about the redundant parens is fine, I think it could be an elegant solution. |
I update the PD according to the suggestion , |
Snowflake diverges from the standard and from most of the other implementations by allowing extra parentheses not only around a join, but around lone table names (e.g. `FROM (mytable [AS alias])`) and around derived tables (e.g. `FROM ((SELECT ...) [AS alias])`) as well. Initially this was implemented in apache#154 by (ab)using `TableFactor::NestedJoin` to represent anything nested in extra set of parens. Afterwards we learned in apache#223 that in cases of such extraneous nesting Snowflake allows specifying the alias both inside and outside parens, but not both - consider: FROM (table_factor AS inner_alias) AS outer_alias We've considered implementing this by changing `TableFactor::NestedJoin` to a `TableFactor::Nested { inner: TableWithJoins, alias: Option<TableAlias> }`, but that seemed too generic, as no known dialect supports duplicate aliases, as shown above, nor naming nested joins `(foo NATURAL JOIN bar) alias`. So we decided on making a smaller change (with no modifications to the AST), that is also more appropriate to the contributors to the Snowflake dialect: 1) Revert apache#154 by rejecting `FROM (table or derived table)` in most dialects. 2) For `dialect_of!(self is SnowflakeDialect | GenericDialect)` parse and strip the extraneous parentheses, e.g. `(mytable) AS alias` -> `(mytable AS alias)` Co-authored-by: Eyal Leshem <[email protected]>
Related to #154
sqlparser now parses
select * from (table_name)
. However, it does not parseselect * from (table_name) alias
.Would it make sense to add this functionality? If so, would that mean adding an
alias
field toTableWithJoins
?The text was updated successfully, but these errors were encountered: