kayak / pypika

PyPika is a python SQL query builder that exposes the full richness of the SQL language using a syntax that reflects the resulting query. PyPika excels at all sorts of SQL queries but is especially useful for data analysis.
http://pypika.readthedocs.io/en/latest/
Apache License 2.0
2.43k stars 293 forks source link

Double quotes inconsistency in WITH clause #710

Open gandroz opened 1 year ago

gandroz commented 1 year ago

Working with Oracle databases can be tricky sometime, especially with double quotes and aliases. In the current state, there is a bug around the WITH clause that does not encapsulate the alias inside double quotes which is inconsistent with other clauses. It can lead to invalid identifier on Oracale as demonstrated with the query

sub_query = Query().from_(my_table).where(my_table.ID == "12345").select(my_table.ID)

QN = AliasedQuery("sub_query_with")

query = (
    Query()
    .with_(sub_query, "sub_query_with")
    .from_(QN)
    .select(
        QN.star
    )
)

df_temp = claim_conn.read_dataframe(query.get_sql())

A workaround has been proposed here working with UPPERCASE aliases, but with a proper management of double quotes inside WITH clause it would be better

mcsj120 commented 1 year ago

I raised a PR here that may help resolve this PR #691

If you test this and it works for your use case (I use MySQL for this library, not Oracle), you can likely deploy the current repo with these files modified.