Skip to content

Commit

Permalink
Fix rendering error of column's DEFAULT with an expression
Browse files Browse the repository at this point in the history
This fixes the case raised in pganalyze/libpg_query#188.
  • Loading branch information
lelit committed Apr 26, 2023
1 parent 4a2a9b3 commit e783ed7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pglast/printers/ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# :Created: gio 09 nov 2017 10:50:30 CET
# :Author: Lele Gaifax <[email protected]>
# :License: GNU General Public License version 3 or later
# :Copyright: © 2017, 2018, 2019, 2020, 2021, 2022 Lele Gaifax
# :Copyright: © 2017, 2018, 2019, 2020, 2021, 2022, 2023 Lele Gaifax
#

import re
Expand Down Expand Up @@ -1172,7 +1172,11 @@ def CONSTR_CHECK(self, node, output):
def CONSTR_DEFAULT(self, node, output):
output.swrite('DEFAULT ')
assert not (node.raw_expr is not None and node.cooked_expr is not None)
output.print_node(node.cooked_expr if node.raw_expr is None else node.raw_expr)
expr = node.cooked_expr if node.raw_expr is None else node.raw_expr
# Handle the ``DEFAULT (1 IN (1, 2))`` case as seen here:
# https://github.com/postgres/postgres/blob/REL_14_STABLE/src/test/regress/input/constraints.source#L41
with output.expression(isinstance(expr, ast.A_Expr)):
output.print_node(expr)

def CONSTR_EXCLUSION(self, node, output):
output.swrite('EXCLUDE USING ')
Expand Down
2 changes: 2 additions & 0 deletions tests/test_printers_roundtrip/ddl/create_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,5 @@ create tablespace foo owner current_user location '/bar'
create tablespace foo owner me location '/bar'

create tablespace foo location '/bar' with (seq_page_cost=1)

CREATE TABLE error_tbl (b1 bool DEFAULT (1 IN (1, 2)))

0 comments on commit e783ed7

Please sign in to comment.