From 5ec10492cda8930c014f8db315650cffa7ea57e1 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Mon, 21 Nov 2022 08:08:44 +0900
Subject: [PATCH v5 2/2] Simplify back-parsing of SQL-callable timestamp
 functions

Per suggestion from Ted Yu.
---
 src/backend/utils/adt/ruleutils.c | 79 ++++++++++++-------------------
 1 file changed, 31 insertions(+), 48 deletions(-)

diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index c00dc5e32e..ab926bedd4 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -9997,6 +9997,33 @@ get_windowfunc_expr(WindowFunc *wfunc, deparse_context *context)
 	}
 }
 
+/*
+ * get_func_sql_syntax_time
+ *
+ * Parse back argument of SQL-syntax function call related to a time or a
+ * timestamp.  These require a specific handling with their typmod is given
+ * by the function caller through their SQL keyword.
+ */
+static void
+get_func_sql_syntax_time(List *args, deparse_context *context)
+{
+	StringInfo	buf = context->buf;
+	Const      *cons;
+
+	if (list_length(args) != 1)
+		return;
+
+	cons = (Const *) linitial(args);
+	Assert(IsA(cons, Const));
+
+	if (!cons->constisnull)
+	{
+		appendStringInfoString(buf, "(");
+		get_rule_expr((Node *) cons, context, false);
+		appendStringInfoString(buf, ")");
+	}
+}
+
 /*
  * get_func_sql_syntax		- Parse back a SQL-syntax function call
  *
@@ -10252,63 +10279,19 @@ get_func_sql_syntax(FuncExpr *expr, deparse_context *context)
 			return true;
 		case F_CURRENT_TIME:
 			appendStringInfoString(buf, "CURRENT_TIME");
-			if (list_length(expr->args) == 1)
-			{
-				Const      *cons = (Const *) linitial(expr->args);
-
-				Assert(IsA(cons, Const));
-				if (!cons->constisnull)
-				{
-					appendStringInfoString(buf, "(");
-					get_rule_expr((Node *) cons, context, false);
-					appendStringInfoString(buf, ")");
-				}
-			}
+			get_func_sql_syntax_time(expr->args, context);
 			return true;
 		case F_CURRENT_TIMESTAMP:
 			appendStringInfoString(buf, "CURRENT_TIMESTAMP");
-			if (list_length(expr->args) == 1)
-			{
-				Const      *cons = (Const *) linitial(expr->args);
-
-				Assert(IsA(cons, Const));
-				if (!cons->constisnull)
-				{
-					appendStringInfoString(buf, "(");
-					get_rule_expr((Node *) cons, context, false);
-					appendStringInfoString(buf, ")");
-				}
-			}
+			get_func_sql_syntax_time(expr->args, context);
 			return true;
 		case F_LOCALTIME:
 			appendStringInfoString(buf, "LOCALTIME");
-			if (list_length(expr->args) == 1)
-			{
-				Const      *cons = (Const *) linitial(expr->args);
-
-				Assert(IsA(cons, Const));
-				if (!cons->constisnull)
-				{
-					appendStringInfoString(buf, "(");
-					get_rule_expr((Node *) cons, context, false);
-					appendStringInfoString(buf, ")");
-				}
-			}
+			get_func_sql_syntax_time(expr->args, context);
 			return true;
 		case F_LOCALTIMESTAMP:
 			appendStringInfoString(buf, "LOCALTIMESTAMP");
-			if (list_length(expr->args) == 1)
-			{
-				Const      *cons = (Const *) linitial(expr->args);
-
-				Assert(IsA(cons, Const));
-				if (!cons->constisnull)
-				{
-					appendStringInfoString(buf, "(");
-					get_rule_expr((Node *) cons, context, false);
-					appendStringInfoString(buf, ")");
-				}
-			}
+			get_func_sql_syntax_time(expr->args, context);
 			return true;
 
 		case F_XMLEXISTS:
-- 
2.38.1

