Skip to content

Commit a711b36

Browse files
committed
Fix under-parenthesized display of AT TIME ZONE constructs.
In commit 40c24bf, I forgot to use get_rule_expr_paren() for the arguments of AT TIME ZONE, resulting in possibly not printing parens for expressions that need it. But get_rule_expr_paren() wouldn't have gotten it right anyway, because isSimpleNode() hadn't been taught that COERCE_SQL_SYNTAX parent nodes don't guarantee sufficient parentheses. Improve all that. Also use this methodology for F_IS_NORMALIZED, so that we don't print useless parens for that. In passing, remove a comment that was obsoleted later. Per report from Duncan Sands. Back-patch to v14 where this code came in. (Before that, we didn't try to print AT TIME ZONE that way, so there was no bug just ugliness.) Discussion: https://postgr.es/m/f41566aa-a057-6628-4b7c-b48770ecb84a@deepbluecap.com
1 parent f79cca5 commit a711b36

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8290,11 +8290,12 @@ isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
82908290
{
82918291
case T_FuncExpr:
82928292
{
8293-
/* special handling for casts */
8293+
/* special handling for casts and COERCE_SQL_SYNTAX */
82948294
CoercionForm type = ((FuncExpr *) parentNode)->funcformat;
82958295

82968296
if (type == COERCE_EXPLICIT_CAST ||
8297-
type == COERCE_IMPLICIT_CAST)
8297+
type == COERCE_IMPLICIT_CAST ||
8298+
type == COERCE_SQL_SYNTAX)
82988299
return false;
82998300
return true; /* own parentheses */
83008301
}
@@ -8342,11 +8343,12 @@ isSimpleNode(Node *node, Node *parentNode, int prettyFlags)
83428343
return false;
83438344
case T_FuncExpr:
83448345
{
8345-
/* special handling for casts */
8346+
/* special handling for casts and COERCE_SQL_SYNTAX */
83468347
CoercionForm type = ((FuncExpr *) parentNode)->funcformat;
83478348

83488349
if (type == COERCE_EXPLICIT_CAST ||
8349-
type == COERCE_IMPLICIT_CAST)
8350+
type == COERCE_IMPLICIT_CAST ||
8351+
type == COERCE_SQL_SYNTAX)
83508352
return false;
83518353
return true; /* own parentheses */
83528354
}
@@ -10117,9 +10119,11 @@ get_func_sql_syntax(FuncExpr *expr, deparse_context *context)
1011710119
case F_TIMEZONE_TEXT_TIMETZ:
1011810120
/* AT TIME ZONE ... note reversed argument order */
1011910121
appendStringInfoChar(buf, '(');
10120-
get_rule_expr((Node *) lsecond(expr->args), context, false);
10122+
get_rule_expr_paren((Node *) lsecond(expr->args), context, false,
10123+
(Node *) expr);
1012110124
appendStringInfoString(buf, " AT TIME ZONE ");
10122-
get_rule_expr((Node *) linitial(expr->args), context, false);
10125+
get_rule_expr_paren((Node *) linitial(expr->args), context, false,
10126+
(Node *) expr);
1012310127
appendStringInfoChar(buf, ')');
1012410128
return true;
1012510129

@@ -10171,9 +10175,10 @@ get_func_sql_syntax(FuncExpr *expr, deparse_context *context)
1017110175

1017210176
case F_IS_NORMALIZED:
1017310177
/* IS xxx NORMALIZED */
10174-
appendStringInfoString(buf, "((");
10175-
get_rule_expr((Node *) linitial(expr->args), context, false);
10176-
appendStringInfoString(buf, ") IS");
10178+
appendStringInfoString(buf, "(");
10179+
get_rule_expr_paren((Node *) linitial(expr->args), context, false,
10180+
(Node *) expr);
10181+
appendStringInfoString(buf, " IS");
1017710182
if (list_length(expr->args) == 2)
1017810183
{
1017910184
Const *con = (Const *) lsecond(expr->args);
@@ -10194,11 +10199,6 @@ get_func_sql_syntax(FuncExpr *expr, deparse_context *context)
1019410199
appendStringInfoChar(buf, ')');
1019510200
return true;
1019610201

10197-
/*
10198-
* XXX EXTRACT, a/k/a date_part(), is intentionally not covered
10199-
* yet. Add it after we change the return type to numeric.
10200-
*/
10201-
1020210202
case F_NORMALIZE:
1020310203
/* NORMALIZE() */
1020410204
appendStringInfoString(buf, "NORMALIZE(");

src/test/regress/expected/create_view.out

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,7 @@ select pg_get_viewdef('tt20v', true);
19221922
-- reverse-listing of various special function syntaxes required by SQL
19231923
create view tt201v as
19241924
select
1925+
('2022-12-01'::date + '1 day'::interval) at time zone 'UTC' as atz,
19251926
extract(day from now()) as extr,
19261927
(now(), '1 day'::interval) overlaps
19271928
(current_timestamp(2), '1 day'::interval) as o,
@@ -1944,10 +1945,11 @@ select
19441945
select pg_get_viewdef('tt201v', true);
19451946
pg_get_viewdef
19461947
-----------------------------------------------------------------------------------------------
1947-
SELECT EXTRACT(day FROM now()) AS extr, +
1948+
SELECT (('12-01-2022'::date + '@ 1 day'::interval) AT TIME ZONE 'UTC'::text) AS atz, +
1949+
EXTRACT(day FROM now()) AS extr, +
19481950
((now(), '@ 1 day'::interval) OVERLAPS (CURRENT_TIMESTAMP(2), '@ 1 day'::interval)) AS o,+
1949-
(('foo'::text) IS NORMALIZED) AS isn, +
1950-
(('foo'::text) IS NFKC NORMALIZED) AS isnn, +
1951+
('foo'::text IS NORMALIZED) AS isn, +
1952+
('foo'::text IS NFKC NORMALIZED) AS isnn, +
19511953
NORMALIZE('foo'::text) AS n, +
19521954
NORMALIZE('foo'::text, NFKD) AS nfkd, +
19531955
OVERLAY('foo'::text PLACING 'bar'::text FROM 2) AS ovl, +

src/test/regress/sql/create_view.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ select pg_get_viewdef('tt20v', true);
703703

704704
create view tt201v as
705705
select
706+
('2022-12-01'::date + '1 day'::interval) at time zone 'UTC' as atz,
706707
extract(day from now()) as extr,
707708
(now(), '1 day'::interval) overlaps
708709
(current_timestamp(2), '1 day'::interval) as o,

0 commit comments

Comments
 (0)