Repository: kayak/pypika (Python)

Issue:
Postgres: error when RETURNING inside WITH subquery
Hi. I'm trying to execute the following code

```python
from pypika import Table, AliasedQuery, PostgreSQLQuery as Query

customers = Table('customers')
other_table = Table('other')

sub_query = Query.into(customers).columns('a', 'b').insert(1, 2).returning('*')
aliased = AliasedQuery('an_alias')

query = Query.with_(sub_query, 'an_alias').from_(aliased).select(aliased.star)
query = query.left_join(other_table).on(aliased.a == other_table.id)

print(query)
```

and getting an error
```
Traceback (most recent call last):
  File "/home/alex/venv/lib/python3.8/site-packages/pypika/queries.py", line 1080, in __str__
    return self.get_sql(dialect=self.dialect)
  File "/home/alex/venv/lib/python3.8/site-packages/pypika/dialects.py", line 527, in get_sql
    querystring = super(PostgreQueryBuilder, self).get_sql(
  File "/home/alex/venv/lib/python3.8/site-packages/pypika/queries.py", line 1189, in get_sql
    querystring = self._with_sql(**kwargs)
  File "/home/alex/venv/lib/python3.8/site-packages/pypika/queries.py", line 1241, in _with_sql
    return "WITH " + ",".join(
  File "/home/alex/venv/lib/python3.8/site-packages/pypika/queries.py", line 1244, in <genexpr>
    + clause.get_sql(subquery=False, with_alias=False, **kwargs)
  File "/home/alex/venv/lib/python3.8/site-packages/pypika/queries.py", line 79, in get_sql
    return self.query.get_sql(**kwargs)
  File "/home/alex/venv/lib/python3.8/site-packages/pypika/dialects.py", line 538, in get_sql
    querystring += self._returning_sql(with_namespace=with_namespace, **kwargs)
TypeError: _returning_sql() got multiple values for keyword argument 'with_namespace'
```


Discussion:

Patch:
diff --git a/pypika/dialects.py b/pypika/dialects.py
index df3fb3f9..9d265529 100644
--- a/pypika/dialects.py
+++ b/pypika/dialects.py
@@ -527,15 +527,13 @@ def get_sql(self, with_alias: bool = False, subquery: bool = False, **kwargs: An
         querystring = super(PostgreQueryBuilder, self).get_sql(
               with_alias, subquery, **kwargs
         )
-        with_namespace = False
-        if self._update_table and self.from_:
-            with_namespace = True
 
         querystring += self._on_conflict_sql(**kwargs)
         querystring += self._on_conflict_action_sql(**kwargs)
 
         if self._returns:
-            querystring += self._returning_sql(with_namespace=with_namespace, **kwargs)
+            kwargs['with_namespace'] = self._update_table and self.from_
+            querystring += self._returning_sql(**kwargs)
         return querystring
 
 


Test patch:
diff --git a/pypika/tests/test_selects.py b/pypika/tests/test_selects.py
index 1f07317a..d123d6e8 100644
--- a/pypika/tests/test_selects.py
+++ b/pypika/tests/test_selects.py
@@ -1187,6 +1187,18 @@ def test_join_with_with(self):
               str(test_query),
         )
 
+    def test_select_from_with_returning(self):
+        sub_query = PostgreSQLQuery.into(self.table_abc).insert(1).returning('*')
+        test_query = (
+            Query.with_(sub_query, "an_alias")
+                .from_(AliasedQuery("an_alias"))
+                .select("*")
+        )
+        self.assertEqual(
+            'WITH an_alias AS (INSERT INTO "abc" VALUES (1) RETURNING *) SELECT * FROM an_alias',
+            str(test_query)
+        )
+
 
 class QuoteTests(unittest.TestCase):
     def test_extraneous_quotes(self):