Closed pandaxtc closed 2 months ago
Are you trying to recreate that specific example?
I'm seeing that
from pypika.dialects import MSSQLQuery
query = MSSQLQuery.from_("table").select("id").union(MSSQLQuery.from_("another_table").select("id"))
query.get_sql() # or str(query)
does indeed use the parens
(SELECT "id" FROM "table") UNION (SELECT "id" FROM "another_table")
The example you provided uses MSSQLQuery
instead of MySQLQuery
. Here's an example:
from pypika.dialects import MySQLQuery
query = MySQLQuery.from_("table").select("id").union(MSSQLQuery.from_("another_table").select("id"))
query.get_sql() # or str(query)
results in
SELECT `id` FROM `table` UNION SELECT `id` FROM `another_table`
I misread that then.
Here in the code, the wrap_set_operation_queries
is set to False.
https://github.com/kayak/pypika/blob/8841520e906970d76c5ed81c7dd5d154f0d5259d/pypika/dialects.py#L90
Would you want to make a PR to change?
In the meantime, you might be able to set the wrap_set_operation_queries
attribute of your query to True. But I'm not sure about that temporary solution
The temporary solution works, thanks. I opened a PR: https://github.com/kayak/pypika/pull/782
The MySQLQuery dialect doesn't wrap set operations in parens, but UNION queries that have LIMITs and ORDERs in the subqueries fail without parens. The documentation states:
For example, this fails:
but this does not: