Skip to content

Commit

Permalink
Fix: deparse DEFAULT (xxx AT TIME ZONE yyy)
Browse files Browse the repository at this point in the history
This commit has been submitted upstream at
pganalyze/libpg_query#269.

Here I commit it in order to incorporate the fix into
downstream dependents.
  • Loading branch information
tylergannon committed Dec 16, 2024
1 parent 38c866d commit c1c65ad
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions parser/postgres_deparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -2703,6 +2703,30 @@ static void deparseFuncCall(StringInfo str, FuncCall *func_call)
*/
Expr* e;
bool isLocal = list_length(func_call->args) == 1;
/*
* time values expressed `AT TIME ZONE` inside a DEFAULT value expression must be surrounded by parentheses
* in order for the deparsed DEFAULT to be valid syntax.
* The following is a simple lookback into the deparsed SQL, to see if is expression immediately follows
* a DEFAULT declaration.
*/
bool isDefaultValue = false;

{
int end = str->len;
// Step back over trailing spaces
while (end > 0 && isspace((unsigned char) str->data[end - 1]))
end--;

// Now check if, just before these trailing spaces, we have "DEFAULT"
if (end >= 7 && strncmp(str->data + (end - 7), "DEFAULT", 7) == 0)
{
isDefaultValue = true;
}
}

if (isDefaultValue) {
appendStringInfoChar(str, '(');
}

if (isLocal)
e = linitial(func_call->args);
Expand All @@ -2725,6 +2749,9 @@ static void deparseFuncCall(StringInfo str, FuncCall *func_call)
appendStringInfoString(str, " AT TIME ZONE ");
deparseExpr(str, linitial(func_call->args));
}
if (isDefaultValue) {
appendStringInfoChar(str, ')');
}
return;
} else if (func_call->funcformat == COERCE_SQL_SYNTAX &&
list_length(func_call->funcname) == 2 &&
Expand Down

0 comments on commit c1c65ad

Please sign in to comment.