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

Issues with WITH clause in SqlParser #17278

Closed
dnltsk opened this issue Feb 10, 2022 · 1 comment
Closed

Issues with WITH clause in SqlParser #17278

dnltsk opened this issue Feb 10, 2022 · 1 comment

Comments

@dnltsk
Copy link

dnltsk commented Feb 10, 2022

I have the use-case to extract all table names used by complex queries. Obviously the SqlParser, respectively the QuerySpecification has trouble when the Query stars with a WITH clause.

This is how I parse the Query

import com.facebook.presto.sql.parser.SqlParser
import com.facebook.presto.sql.tree.Query
import com.facebook.presto.sql.tree.QuerySpecification

val parser = SqlParser()
val query = parser.createStatement(sql, ParsingOptions()) as Query
val body = query.queryBody as QuerySpecification
println(body.from.get())

The following query

    WITH query AS (
        select a from db1.table1
    )
    ( select * from query )

throws the Exception

java.lang.ClassCastException: class com.facebook.presto.sql.tree.Union cannot be cast to class com.facebook.presto.sql.tree.QuerySpecification (com.facebook.presto.sql.tree.Union and com.facebook.presto.sql.tree.QuerySpecification are in unnamed module of loader 'app')

Interestingly I was able to solve this by wrapping the query with a dummy SELECT *-Statement

 SELECT * FROM (
    WITH query AS (
        select a from db1.table1
    )
    ( select * from query )
 )

By that the Statement the SqlParser/QuerySpecification can successfully everything via the created TableSubquery

TableSubquery{Query{with=With{recursive=false, queries=[WithQuery{name=query, query=Query{queryBody=QuerySpecification{select=Select{distinct=false, selectItems=[a]}, from=Optional[Table{db1.table1}], where=null, groupBy=Optional.empty, having=null, orderBy=Optional.empty, offset=null, limit=null}, orderBy=Optional.empty}, columnNames=Optional.empty}]}, queryBody=TableSubquery{Query{queryBody=QuerySpecification{select=Select{distinct=false, selectItems=[*]}, from=Optional[Table{query}], where=null, groupBy=Optional.empty, having=null, orderBy=Optional.empty, offset=null, limit=null}, orderBy=Optional.empty}}, orderBy=Optional.empty}}

The Question is if it could be possible to improve the parser to it could works without the workaround.

@rubenssoto
Copy link

There is a PR to solve this issue

#17083

You can ask for community help, I have the same problem here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants