Skip to content

Commit

Permalink
Merge pull request kayak#549 from magiskboy/subquery-in-function
Browse files Browse the repository at this point in the history
Surround subquery in the args aggregate function
  • Loading branch information
x8lucas8x authored Mar 24, 2021
2 parents 228f1f4 + 7fb5127 commit 7d0bc58
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pypika/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,12 @@ def get_function_sql(self, **kwargs: Any) -> str:

return "{name}({args}{special})".format(
name=self.name,
args=",".join(self.get_arg_sql(arg, **kwargs) for arg in self.args),
args=",".join(
p.get_sql(with_alias=False, subquery=True, **kwargs)
if hasattr(p, "get_sql")
else self.get_arg_sql(p, **kwargs)
for p in self.args
),
special=(" " + special_params_sql) if special_params_sql else "",
)

Expand Down
5 changes: 5 additions & 0 deletions pypika/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ def test__approx_percentile(self):
str(q),
)

def test__subquery_in_params_functions(self):
subquery = Query.from_('table').select('id')
func = fn.Function('func', 'id', subquery)
self.assertEqual("func('id',(SELECT id FROM table))", func.get_sql())


class ConditionTests(unittest.TestCase):
def test__case__raw(self):
Expand Down

0 comments on commit 7d0bc58

Please sign in to comment.