pypika
pypika copied to clipboard
MySQL queries should wrap set operations
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:
You cannot have a query block with a trailing ORDER BY or LIMIT without wrapping it in parentheses...
For example, this fails:
SELECT 1 LIMIT 1 UNION SELECT 2 LIMIT 1;
but this does not:
(SELECT 1 LIMIT 1) UNION (SELECT 2 LIMIT 1);
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