From 3168dae1a1cbc4d3aa0212662abc3a71637a9d06 Mon Sep 17 00:00:00 2001 From: Lukas Fittl Date: Mon, 23 Oct 2023 10:11:57 -0700 Subject: [PATCH] Deparser: Add parens around TypeCast in IndexElem We were incorrectly omitting these parentheses, as well as missing a space outputting "CREATE INDEX ON t (col::typecastops_class)" instead of "CREATE INDEX ON t ((col::typecast) ops_class)". Fixes #213 --- src/postgres_deparse.c | 15 ++++++++------- test/deparse_tests.c | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/postgres_deparse.c b/src/postgres_deparse.c index 9677b11f..0ea0ab41 100644 --- a/src/postgres_deparse.c +++ b/src/postgres_deparse.c @@ -1826,14 +1826,15 @@ static void deparseIndexElem(StringInfo str, IndexElem* index_elem) { switch (nodeTag(index_elem->expr)) { - case T_FuncCall: - case T_SQLValueFunction: - case T_TypeCast: - case T_CoalesceExpr: - case T_MinMaxExpr: - case T_XmlExpr: - case T_XmlSerialize: + // Simple function calls can be written without wrapping parens + case T_FuncCall: // func_application + case T_SQLValueFunction: // func_expr_common_subexpr + case T_CoalesceExpr: // func_expr_common_subexpr + case T_MinMaxExpr: // func_expr_common_subexpr + case T_XmlExpr: // func_expr_common_subexpr + case T_XmlSerialize: // func_expr_common_subexpr deparseFuncExprWindowless(str, index_elem->expr); + appendStringInfoString(str, " "); break; default: appendStringInfoChar(str, '('); diff --git a/test/deparse_tests.c b/test/deparse_tests.c index 8ac26bf3..32aa783b 100644 --- a/test/deparse_tests.c +++ b/test/deparse_tests.c @@ -400,6 +400,7 @@ const char* tests[] = { "SELECT 1 FROM tbl LIMIT COALESCE($1, $2)", "SELECT (false AND true) IS FALSE", "SELECT a = (true IS FALSE)", + "CREATE INDEX myindex ON public.mytable USING btree (col1, (col2::varchar) varchar_pattern_ops)" }; size_t testsLength = __LINE__ - 4;