v47-0002-refactor-gram.y.nocfbot

application/octet-stream

Filename: v47-0002-refactor-gram.y.nocfbot
Type: application/octet-stream
Part: 1
Message: Re: Row pattern recognition
From 2d6c8a8801c45cf2e26a72f3a59cdc0a18ce4f8f Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Fri, 12 Jun 2026 17:52:47 +0800
Subject: [PATCH v47 2/6] refactor gram.y

---
 src/backend/parser/gram.y | 87 ++++++++++++++++++++-------------------
 1 file changed, 45 insertions(+), 42 deletions(-)

diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 4ae951aaeb..ace2e7fd59 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -17734,9 +17734,11 @@ row_pattern_primary:
 		;
 
 row_pattern_quantifier_opt:
-			/* EMPTY - no quantifier means exactly once; @$ is unused since
-			 * min=max=1 never produces an error */
 				{
+					/*
+					 * EMPTY - no quantifier means exactly once; @$ is unused
+					 * since min=max=1 never produces an error
+					 */
 					$$ = (Node *) makeRPRQuantifier(1, 1, false, @$);
 				}
 			| '*'
@@ -17749,15 +17751,34 @@ row_pattern_quantifier_opt:
 				}
 			| Op
 				{
-					/* Handle single Op: ? or reluctant quantifiers *?, +?, ?? */
-					if (strcmp($1, "?") == 0)
+					if (strcmp($1, "?") == 0 || strcmp($1, "?|") == 0)
+					{
 						$$ = (Node *) makeRPRQuantifier(0, 1, false, @1);
-					else if (strcmp($1, "*?") == 0)
+
+						if (strcmp($1, "?|") == 0)
+							((RPRPatternNode *) $$)->trailing_alt = true;
+					}
+					else if (strcmp($1, "*?") == 0 || strcmp($1, "*?|") == 0)
+					{
 						$$ = (Node *) makeRPRQuantifier(0, PG_INT32_MAX, true, @1);
-					else if (strcmp($1, "+?") == 0)
+
+						if (strcmp($1, "*?|") == 0)
+							((RPRPatternNode *) $$)->trailing_alt = true;
+					}
+					else if (strcmp($1, "+?") == 0 || strcmp($1, "+?|") == 0)
+					{
 						$$ = (Node *) makeRPRQuantifier(1, PG_INT32_MAX, true, @1);
-					else if (strcmp($1, "??") == 0)
+
+						if (strcmp($1, "+?|") == 0)
+							((RPRPatternNode *) $$)->trailing_alt = true;
+					}
+					else if (strcmp($1, "??") == 0 || strcmp($1, "??|") == 0)
+					{
 						$$ = (Node *) makeRPRQuantifier(0, 1, true, @1);
+
+						if (strcmp($1, "??|") == 0)
+							((RPRPatternNode *) $$)->trailing_alt = true;
+					}
 					else if (strcmp($1, "*|") == 0)
 					{
 						$$ = (Node *) makeRPRQuantifier(0, PG_INT32_MAX, false, @1);
@@ -17766,26 +17787,7 @@ row_pattern_quantifier_opt:
 					else if (strcmp($1, "+|") == 0)
 					{
 						$$ = (Node *) makeRPRQuantifier(1, PG_INT32_MAX, false, @1);
-						((RPRPatternNode *) $$)->trailing_alt = true;
-					}
-					else if (strcmp($1, "?|") == 0)
-					{
-						$$ = (Node *) makeRPRQuantifier(0, 1, false, @1);
-						((RPRPatternNode *) $$)->trailing_alt = true;
-					}
-					else if (strcmp($1, "*?|") == 0)
-					{
-						$$ = (Node *) makeRPRQuantifier(0, PG_INT32_MAX, true, @1);
-						((RPRPatternNode *) $$)->trailing_alt = true;
-					}
-					else if (strcmp($1, "+?|") == 0)
-					{
-						$$ = (Node *) makeRPRQuantifier(1, PG_INT32_MAX, true, @1);
-						((RPRPatternNode *) $$)->trailing_alt = true;
-					}
-					else if (strcmp($1, "??|") == 0)
-					{
-						$$ = (Node *) makeRPRQuantifier(0, 1, true, @1);
+
 						((RPRPatternNode *) $$)->trailing_alt = true;
 					}
 					else
@@ -17798,13 +17800,13 @@ row_pattern_quantifier_opt:
 			/* RELUCTANT quantifiers (when lexer separates tokens) */
 			| '*' Op
 				{
-					if (strcmp($2, "?") == 0)
-						$$ = (Node *) makeRPRQuantifier(0, PG_INT32_MAX, true, @1);
-					else if (strcmp($2, "?|") == 0)
+					if (strcmp($2, "?") == 0 || strcmp($2, "?|") == 0)
 					{
+						$$ = (Node *) makeRPRQuantifier(0, PG_INT32_MAX, true, @1);
+
 						/* "A* ?|B" = reluctant "A*?" plus alternation */
-						$$ = (Node *) makeRPRQuantifier(0, PG_INT32_MAX, true, @1);
-						((RPRPatternNode *) $$)->trailing_alt = true;
+						if (strcmp($2, "?|") == 0)
+							((RPRPatternNode *) $$)->trailing_alt = true;
 					}
 					else
 						ereport(ERROR,
@@ -17815,13 +17817,13 @@ row_pattern_quantifier_opt:
 				}
 			| '+' Op
 				{
-					if (strcmp($2, "?") == 0)
-						$$ = (Node *) makeRPRQuantifier(1, PG_INT32_MAX, true, @1);
-					else if (strcmp($2, "?|") == 0)
+					if (strcmp($2, "?") == 0 || strcmp($2, "?|") == 0)
 					{
+						$$ = (Node *) makeRPRQuantifier(1, PG_INT32_MAX, true, @1);
+
 						/* "A+ ?|B" = reluctant "A+?" plus alternation */
-						$$ = (Node *) makeRPRQuantifier(1, PG_INT32_MAX, true, @1);
-						((RPRPatternNode *) $$)->trailing_alt = true;
+						if (strcmp($2, "?|") == 0)
+							((RPRPatternNode *) $$)->trailing_alt = true;
 					}
 					else
 						ereport(ERROR,
@@ -17838,13 +17840,13 @@ row_pattern_quantifier_opt:
 								errmsg("invalid quantifier combination: \"%s%s\"", $1, $2),
 								errhint("Did you mean \"??\" for reluctant quantifier?"),
 								parser_errposition(@1));
-					if (strcmp($2, "?") == 0)
-						$$ = (Node *) makeRPRQuantifier(0, 1, true, @1);
-					else if (strcmp($2, "?|") == 0)
+					if (strcmp($2, "?") == 0 || strcmp($2, "?|") == 0)
 					{
+						$$ = (Node *) makeRPRQuantifier(0, 1, true, @1);
+
 						/* "A? ?|B" = reluctant "A??" plus alternation */
-						$$ = (Node *) makeRPRQuantifier(0, 1, true, @1);
-						((RPRPatternNode *) $$)->trailing_alt = true;
+						if (strcmp($2, "?|") == 0)
+							((RPRPatternNode *) $$)->trailing_alt = true;
 					}
 					else
 						ereport(ERROR,
@@ -21403,6 +21405,7 @@ makeRPRQuantifier(int32 min, int32 max, bool reluctant, int location)
 	n->min = min;
 	n->max = max;
 	n->reluctant = reluctant;
+	n->trailing_alt = false;
 	n->location = location;
 	return n;
 }
-- 
2.34.1