From 181f55dfe0525b98234c02a4f76df1a8c2e3a61b Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Wed, 23 Oct 2024 15:00:07 +0900
Subject: [PATCH v12 2/2] Some edits from me..

---
 src/backend/parser/analyze.c | 33 +++++++++++++++++++--------------
 src/backend/parser/gram.y    | 19 ++++++++-----------
 2 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c
index dc71693619..7709fb5e36 100644
--- a/src/backend/parser/analyze.c
+++ b/src/backend/parser/analyze.c
@@ -243,17 +243,17 @@ parse_sub_analyze(Node *parseTree, ParseState *parentParseState,
  * 		Set query's location and length from statement and ParseState
  *
  * Some statements, like PreparableStmt, can be located within parentheses.
- * For example "(SELECT 1)" or "COPY (UPDATE ...) to x;". For those, we can't
- * use the whole string from the statement's location or the SQL string will
- * yield "SELECT 1)". The parser will set stmt_len, reflecting the size of the
- * statement within the parentheses. Thus, when stmt_len is available, we need
- * to use it for the Query's stmt_len.
+ * For example "(SELECT 1)" or "COPY (UPDATE ...) to x;".  For those, we
+ * cannot use the whole string from the statement's location or the SQL
+ * string would yield incorrectly.  The parser will set stmt_len, reflecting
+ * the size of the statement within the parentheses.  Thus, when stmt_len is
+ * available, we need to use it for the Query's stmt_len.
  *
- * For other cases, the parser can't provide the length of individual statements.
- * However, we have the statement's location plus the length (p_stmt_len) and
- * location (p_stmt_location) of the top level RawStmt, stored in pstate. Thus,
- * the statement's length is the RawStmt's length minus how much we've advanced
- * in the RawStmt's string.
+ * For other cases, the parser can't provide the length of individual
+ * statements.  However, we have the statement's location plus its length
+ * (p_stmt_len) and location (p_stmt_location) of the top level RawStmt,
+ * stored in pstate.  Thus, the statement's length is the RawStmt's length
+ * minus how much we've advanced in the RawStmt's string.
  */
 static void
 setQueryLocationAndLength(ParseState *pstate, Query *qry, Node *parseTree)
@@ -266,6 +266,7 @@ setQueryLocationAndLength(ParseState *pstate, Query *qry, Node *parseTree)
 			qry->stmt_location = ((InsertStmt *) parseTree)->stmt_location;
 			stmt_len = ((InsertStmt *) parseTree)->stmt_len;
 			break;
+
 		case T_DeleteStmt:
 			qry->stmt_location = ((DeleteStmt *) parseTree)->stmt_location;
 			stmt_len = ((DeleteStmt *) parseTree)->stmt_len;
@@ -294,16 +295,20 @@ setQueryLocationAndLength(ParseState *pstate, Query *qry, Node *parseTree)
 			qry->stmt_location = pstate->p_stmt_location;
 			break;
 	}
+
 	if (stmt_len > 0)
+	{
 		/* Statement's length is known, use it */
 		qry->stmt_len = stmt_len;
+	}
 	else
-
+	{
 		/*
-		 * Compute the statement's length from statement's location and
-		 * RawStmt's length and location
+		 * Compute the statement's length from the statement's location and
+		 * the RawStmt's length and location.
 		 */
 		qry->stmt_len = pstate->p_stmt_len - (qry->stmt_location - pstate->p_stmt_location);
+	}
 }
 
 /*
@@ -486,7 +491,7 @@ transformStmt(ParseState *pstate, Node *parseTree)
 			 */
 			result = makeNode(Query);
 			result->commandType = CMD_UTILITY;
-			result->utilityStmt = parseTree;
+			result->utilityStmt = (Node *) parseTree;
 			break;
 	}
 
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 172a26e10e..45d02e942a 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -12152,8 +12152,6 @@ InsertStmt:
 					$5->onConflictClause = $6;
 					$5->returningList = $7;
 					$5->withClause = $1;
-					if (@$ < 0)			/* see comments for YYLLOC_DEFAULT */
-						@$ = @2;
 					$5->stmt_location = @$;
 					$$ = (Node *) $5;
 				}
@@ -12308,8 +12306,6 @@ DeleteStmt: opt_with_clause DELETE_P FROM relation_expr_opt_alias
 					n->whereClause = $6;
 					n->returningList = $7;
 					n->withClause = $1;
-					if (@$ < 0)			/* see comments for YYLLOC_DEFAULT */
-						@$ = @2;
 					n->stmt_location = @$;
 					$$ = (Node *) n;
 				}
@@ -12385,8 +12381,6 @@ UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias
 					n->whereClause = $7;
 					n->returningList = $8;
 					n->withClause = $1;
-					if (@$ < 0)			/* see comments for YYLLOC_DEFAULT */
-						@$ = @2;
 					n->stmt_location = @$;
 					$$ = (Node *) n;
 				}
@@ -12465,8 +12459,6 @@ MergeStmt:
 					m->joinCondition = $8;
 					m->mergeWhenClauses = $9;
 					m->returningList = $10;
-					if (@$ < 0)			/* see comments for YYLLOC_DEFAULT */
-						@$ = @2;
 					m->stmt_location = @$;
 
 					$$ = (Node *) m;
@@ -12711,13 +12703,13 @@ select_with_parens:
 			'(' select_no_parens ')'
 				{
 					SelectStmt *n = (SelectStmt *) $2;
+
 					/*
 					 * As SelectStmt's location starts at the SELECT keyword,
 					 * we need to track the length of the SelectStmt within
 					 * parentheses to be able to extract the relevant part
-					 * of the query. Without this, the RawStmt's length will
-					 * be used and will include the closing parenthesis,
-					 * "SELECT 1)".
+					 * of the query.  Without this, the RawStmt's length would
+					 * be used and would include the closing parenthesis.
 					 */
 					n->stmt_len = @3 - @2;
 					$$ = $2;
@@ -18603,26 +18595,31 @@ updatePreparableStmtEnd(Node *n, int end_location)
 	if (IsA(n, SelectStmt))
 	{
 		SelectStmt *stmt = (SelectStmt *)n;
+
 		stmt->stmt_len = end_location - stmt->stmt_location;
 	}
 	else if (IsA(n, InsertStmt))
 	{
 		InsertStmt *stmt = (InsertStmt *)n;
+
 		stmt->stmt_len = end_location - stmt->stmt_location;
 	}
 	else if (IsA(n, UpdateStmt))
 	{
 		UpdateStmt *stmt = (UpdateStmt *)n;
+
 		stmt->stmt_len = end_location - stmt->stmt_location;
 	}
 	else if (IsA(n, DeleteStmt))
 	{
 		DeleteStmt *stmt = (DeleteStmt *)n;
+
 		stmt->stmt_len = end_location - stmt->stmt_location;
 	}
 	else if (IsA(n, MergeStmt))
 	{
 		MergeStmt *stmt = (MergeStmt *)n;
+
 		stmt->stmt_len = end_location - stmt->stmt_location;
 	}
 	else
-- 
2.45.2

