From b524da3edb5979656a0635bba646da18c57d1ada Mon Sep 17 00:00:00 2001
From: chaotian <chaotian@vmware.com>
Date: Thu, 30 Nov 2023 10:15:48 +0800
Subject: [PATCH v1] fix dump view fails with group by clause

---
 src/backend/utils/adt/ruleutils.c | 33 ++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index ed7f40f053..faae083e77 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -6317,6 +6317,37 @@ get_rule_sortgroupclause(Index ref, List *tlist, bool force_colno,
 	}
 	else if (expr && IsA(expr, Const))
 		get_const_expr((Const *) expr, context, 1);
+	else if (expr && IsA(expr, OpExpr))
+	{
+		OpExpr		*opexpr = (OpExpr *)expr;
+		List		*args = opexpr->args;
+
+		/*
+		 * Print negative operator plus const as -'nnn'::typename
+		 * to ensure that the output will re-parse as a constant,
+		 * not as a constant plus operator.
+		 */
+		if (list_length(args) == 1)
+		{
+			char		*opname;
+			Node		*arg = (Node *) linitial(args);
+			Oid			opno = opexpr->opno;
+
+			opname = generate_operator_name(opno,
+											InvalidOid,
+											exprType(arg));
+
+			if (strlen(opname) == 1 && *opname == '-' && IsA(arg, Const))
+			{
+				appendStringInfo(buf, "%s ", opname);
+				get_const_expr((Const *) arg, context, 1);
+			}
+			else
+				get_rule_expr(expr, context, true);
+		}
+		else
+			get_rule_expr(expr, context, true);
+	}
 	else if (!expr || IsA(expr, Var))
 		get_rule_expr(expr, context, true);
 	else
@@ -10683,7 +10714,7 @@ get_const_expr(Const *constval, deparse_context *context, int showtype)
 			 * literals; but that doesn't work for INT_MIN, and it doesn't
 			 * seem that much prettier anyway.
 			 */
-			if (extval[0] != '-')
+			if (extval[0] != '-' && showtype <= 0)
 				appendStringInfoString(buf, extval);
 			else
 			{
-- 
2.39.2 (Apple Git-143)

