Skip to content

Commit

Permalink
[sql] Don't ignore HAVING clause (geldata#8255)
Browse files Browse the repository at this point in the history
  • Loading branch information
msullivan authored Jan 27, 2025
1 parent 00fe1c1 commit 1ae465c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion edb/pgsql/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ class SelectStmt(Query):
# GROUP BY clauses
group_clause: typing.Optional[typing.List[Base]] = None
# HAVING expression
having: typing.Optional[BaseExpr] = None
having_clause: typing.Optional[BaseExpr] = None
# WINDOW window_name AS(...),
window_clause: typing.Optional[typing.List[Base]] = None
# List of ImplicitRow's in a VALUES query
Expand Down
4 changes: 2 additions & 2 deletions edb/pgsql/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,13 @@ def _select() -> None:
self.visit_list(node.group_clause)
self.indentation -= 2

if node.having:
if node.having_clause:
self.indentation += 1
self.new_lines = 1
self.write('HAVING')
self.new_lines = 1
self.indentation += 1
self.visit(node.having)
self.visit(node.having_clause)
self.indentation -= 2

if node.sort_clause:
Expand Down
2 changes: 1 addition & 1 deletion edb/pgsql/compiler/astutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ def select_is_simple(stmt: pgast.SelectStmt) -> bool:
not stmt.distinct_clause
and not stmt.where_clause
and not stmt.group_clause
and not stmt.having
and not stmt.having_clause
and not stmt.window_clause
and not stmt.values
and not stmt.sort_clause
Expand Down
2 changes: 1 addition & 1 deletion edb/pgsql/parser/ast_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def _build_select_stmt(n: Node, c: Context) -> pgast.SelectStmt:
or [],
where_clause=_maybe(n, c, "whereClause", _build_base_expr),
group_clause=_maybe_list(n, c, "groupClause", _build_base),
having=_maybe(n, c, "having", _build_base_expr),
having_clause=_maybe(n, c, "havingClause", _build_base_expr),
window_clause=_maybe_list(n, c, "windowClause", _build_base),
values=_maybe_list(n, c, "valuesLists", _build_base_expr),
sort_clause=_maybe_list(n, c, "sortClause", _build_sort_by),
Expand Down
4 changes: 4 additions & 0 deletions edb/pgsql/resolver/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ def resolve_SelectStmt(

group_clause = dispatch.resolve_opt_list(stmt.group_clause, ctx=subctx)

# HAVING
having = dispatch.resolve_opt(stmt.having_clause, ctx=ctx)

# SELECT projection
table = context.Table()
target_list: List[pgast.ResTarget] = []
Expand Down Expand Up @@ -174,6 +177,7 @@ def resolve_SelectStmt(
from_clause=from_clause,
target_list=target_list,
group_clause=group_clause,
having_clause=having,
where_clause=where,
sort_clause=sort_clause,
limit_offset=limit_offset,
Expand Down
11 changes: 11 additions & 0 deletions tests/test_sql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,17 @@ async def test_sql_query_subquery_splat_01(self):
[[206]],
)

async def test_sql_query_having_01(self):
res = await self.squery_values(
'''
select 1 having false
'''
)
self.assertEqual(
res,
[],
)

async def test_sql_query_unsupported_01(self):
# test error messages of unsupported queries

Expand Down

0 comments on commit 1ae465c

Please sign in to comment.