Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deparser: Add parens around TypeCast in IndexElem #214

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/postgres_deparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef enum DeparseNodeContext {
DEPARSE_NODE_CONTEXT_CREATE_TYPE,
DEPARSE_NODE_CONTEXT_ALTER_TYPE,
DEPARSE_NODE_CONTEXT_SET_STATEMENT,
DEPARSE_NODE_CONTEXT_FUNC_EXPR,
// Identifier vs constant context
DEPARSE_NODE_CONTEXT_IDENTIFIER,
DEPARSE_NODE_CONTEXT_CONSTANT
Expand Down Expand Up @@ -1782,7 +1783,7 @@ static void deparseFuncExprWindowless(StringInfo str, Node* node)
deparseSQLValueFunction(str, castNode(SQLValueFunction, node));
break;
case T_TypeCast:
deparseTypeCast(str, castNode(TypeCast, node), DEPARSE_NODE_CONTEXT_NONE);
deparseTypeCast(str, castNode(TypeCast, node), DEPARSE_NODE_CONTEXT_FUNC_EXPR);
break;
case T_CoalesceExpr:
deparseCoalesceExpr(str, castNode(CoalesceExpr, node));
Expand Down Expand Up @@ -1826,14 +1827,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 Expand Up @@ -3545,7 +3547,7 @@ static void deparseTypeCast(StringInfo str, TypeCast *type_cast, DeparseNodeCont

Assert(type_cast->typeName != NULL);

if (IsA(type_cast->arg, A_Expr))
if (IsA(type_cast->arg, A_Expr) || context == DEPARSE_NODE_CONTEXT_FUNC_EXPR)
{
appendStringInfoString(str, "CAST(");
deparseExpr(str, type_cast->arg);
Expand Down
2 changes: 2 additions & 0 deletions test/deparse_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ const char* tests[] = {
"ALTER TABLE a DISABLE TRIGGER b",
"ALTER TABLE a DISABLE TRIGGER ALL",
"ALTER TABLE a DISABLE TRIGGER USER",
"CREATE INDEX myindex ON public.mytable USING btree (col1, (col2::varchar) varchar_pattern_ops)",
"SELECT * FROM CAST(1 AS text)"
};

size_t testsLength = __LINE__ - 4;
Loading