Skip to content

Commit

Permalink
Deparser: Add parens around TypeCast in IndexElem
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lfittl committed Oct 23, 2023
1 parent 009f19d commit 3168dae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/postgres_deparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, '(');
Expand Down
1 change: 1 addition & 0 deletions test/deparse_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 3168dae

Please sign in to comment.