From 1245c0b8253ed0a1e2aa4b800a396a840b61eab3 Mon Sep 17 00:00:00 2001
From: "Zizhuan Liu(X-MAN)" <44973863@qq.com>
Date: Sun, 31 May 2026 18:29:30 +0800
Subject: [PATCH v2] Make transformAExprIn() return a flattened bool expression
 directly

---
 src/backend/parser/parse_expr.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c
index f1003e5..cc3e23a 100644
--- a/src/backend/parser/parse_expr.c
+++ b/src/backend/parser/parse_expr.c
@@ -1148,6 +1148,7 @@ transformAExprIn(ParseState *pstate, A_Expr *a)
 	bool		useOr;
 	ListCell   *l;
 	bool		has_rvars = false;
+	bool		build_bool_expr = false;
 
 	/*
 	 * If the operator is <>, combine with AND not OR.
@@ -1295,12 +1296,29 @@ transformAExprIn(ParseState *pstate, A_Expr *a)
 		}
 
 		cmp = coerce_to_boolean(pstate, cmp, "IN");
-		if (result == NULL)
+
+		/*
+		 * Flatten boolean expressions generated in this loop by
+		 * appending to args.
+		 *
+		 * If a->rexpr has a single argument, assign and break;
+		 * otherwise, build a new boolean expression and set the
+		 * flag.
+		 */
+		if (build_bool_expr)
+			((BoolExpr *) result)->args = lappend(((BoolExpr *) result)->args, cmp);
+		else if (result == NULL && list_length(rexprs) == 1)
+		{
 			result = cmp;
+			break;
+		}
 		else
+		{
 			result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
-										   list_make2(result, cmp),
+										   result ? list_make2(result, cmp) : list_make1(cmp),
 										   a->location);
+			build_bool_expr = true;
+		}
 	}
 
 	return result;
-- 
2.43.0

