Repository: kayak/pypika (python)
Issue: Some Vertica 'UNION ALL' queries need brackets
Currently PyPika creates union queries as `query1 UNION ALL query2`.

This works fine in most cases, e.g. 

```
SELECT id FROM myTable
UNION ALL
SELECT id FROM myTable
```

At least for Vertica though, there are some queries where this throws an error, e.g.:

```
SELECT id FROM myTable ORDER BY id ASC
UNION ALL
SELECT id FROM myTable ORDER BY id ASC
```

The fix is as simple as adding brackets:

```
(SELECT id FROM myTable ORDER BY id ASC)
UNION ALL
(SELECT id FROM myTable ORDER BY id ASC)
```

Pull request description: Brackets for Union Queries
Fixes #32 Added brackets around all queries in a union since some dataabase vendors require them for certain queries.

Patch:
diff --git a/pypika/queries.py b/pypika/queries.py
index f070a6a..91faa46 100644
--- a/pypika/queries.py
+++ b/pypika/queries.py
@@ -529,7 +529,7 @@ class QueryBuilder(Selectable, Term):
             return self._queryalias_sql(querystring)
 
         if with_unions:
-            querystring += self._union_sql(querystring)
+            querystring = self._union_sql(querystring)
 
         return querystring
 
@@ -614,8 +614,10 @@ class QueryBuilder(Selectable, Term):
         )
 
     def _union_sql(self, querystring):
-        unionstring = ''
         if self._unions:
+            # Some queries require brackets for unions so easier to just always use them
+            querystring = "({})".format(querystring)
+
             for (union_type, other) in self._unions:
                 if len(self._selects) != len(other._selects):
                     raise UnionException("Queries must have an equal number of select statements in a union."
@@ -623,11 +625,10 @@ class QueryBuilder(Selectable, Term):
                                          "\n\nUnion Query:\n{query2}".format(query1=querystring,
                                                                              query2=other.get_sql()))
 
-                unionstring += ' UNION{type} {query}'.format(
-                    type=union_type.value,
-                    query=other.get_sql()
-                )
-        return unionstring
+                querystring += ' UNION{type} ({query})' \
+                    .format(type=union_type.value, query=other.get_sql())
+
+        return querystring
 
     def _offset_sql(self):
         return " OFFSET {offset}".format(offset=self._offset)


Test patch:
diff --git a/pypika/tests/test_joins.py b/pypika/tests/test_joins.py
index 7d45293..ad423f9 100644
--- a/pypika/tests/test_joins.py
+++ b/pypika/tests/test_joins.py
@@ -260,15 +260,15 @@ class UnionTests(unittest.TestCase):
         query1 = Query.from_(self.table1).select(self.table1.foo)
         query2 = Query.from_(self.table2).select(self.table2.bar)
 
-        self.assertEqual('SELECT "foo" FROM "abc" UNION SELECT "bar" FROM "efg"', str(query1 + query2))
-        self.assertEqual('SELECT "foo" FROM "abc" UNION SELECT "bar" FROM "efg"', str(query1.union(query2)))
+        self.assertEqual('(SELECT "foo" FROM "abc") UNION (SELECT "bar" FROM "efg")', str(query1 + query2))
+        self.assertEqual('(SELECT "foo" FROM "abc") UNION (SELECT "bar" FROM "efg")', str(query1.union(query2)))
 
     def test_union_all(self):
         query1 = Query.from_(self.table1).select(self.table1.foo)
         query2 = Query.from_(self.table2).select(self.table2.bar)
 
-        self.assertEqual('SELECT "foo" FROM "abc" UNION ALL SELECT "bar" FROM "efg"', str(query1 * query2))
-        self.assertEqual('SELECT "foo" FROM "abc" UNION ALL SELECT "bar" FROM "efg"', str(query1.union_all(query2)))
+        self.assertEqual('(SELECT "foo" FROM "abc") UNION ALL (SELECT "bar" FROM "efg")', str(query1 * query2))
+        self.assertEqual('(SELECT "foo" FROM "abc") UNION ALL (SELECT "bar" FROM "efg")', str(query1.union_all(query2)))
 
     def test_require_equal_number_of_fields(self):
         query1 = Query.from_(self.table1).select(self.table1.foo)