From e783ed7298325b8ce85706cc080b9a2f3d89fde9 Mon Sep 17 00:00:00 2001 From: Lele Gaifax Date: Wed, 26 Apr 2023 21:29:55 +0200 Subject: [PATCH] Fix rendering error of column's DEFAULT with an expression This fixes the case raised in https://github.com/pganalyze/libpg_query/issues/188. --- pglast/printers/ddl.py | 8 ++++++-- tests/test_printers_roundtrip/ddl/create_table.sql | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pglast/printers/ddl.py b/pglast/printers/ddl.py index e82512b1..290bf5a1 100644 --- a/pglast/printers/ddl.py +++ b/pglast/printers/ddl.py @@ -3,7 +3,7 @@ # :Created: gio 09 nov 2017 10:50:30 CET # :Author: Lele Gaifax # :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 @@ -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 ') diff --git a/tests/test_printers_roundtrip/ddl/create_table.sql b/tests/test_printers_roundtrip/ddl/create_table.sql index 0b28fe67..d6ffa45b 100644 --- a/tests/test_printers_roundtrip/ddl/create_table.sql +++ b/tests/test_printers_roundtrip/ddl/create_table.sql @@ -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)))