From 34e0062b5abd1b4d13192f8c6d298952d4ce3566 Mon Sep 17 00:00:00 2001
From: ChangAo Chen <cca5507@qq.com>
Date: Fri, 24 Apr 2026 13:29:14 +0800
Subject: [PATCH v1] Make transformAExprIn() return a flattened bool expression
 directly

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

diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c
index f535f3b9351..11d8fa2d2e5 100644
--- a/src/backend/parser/parse_expr.c
+++ b/src/backend/parser/parse_expr.c
@@ -1140,7 +1140,7 @@ transformAExprNullIf(ParseState *pstate, A_Expr *a)
 static Node *
 transformAExprIn(ParseState *pstate, A_Expr *a)
 {
-	Node	   *result = NULL;
+	List	   *result_list = NIL;
 	Node	   *lexpr;
 	List	   *rexprs;
 	List	   *rvars;
@@ -1219,7 +1219,7 @@ transformAExprIn(ParseState *pstate, A_Expr *a)
 			array_type = get_array_type(scalar_type);
 		else
 			array_type = InvalidOid;
-		if (array_type != InvalidOid)
+		if (OidIsValid(array_type))
 		{
 			/*
 			 * OK: coerce all the right-hand non-Var inputs to the common type
@@ -1253,12 +1253,13 @@ transformAExprIn(ParseState *pstate, A_Expr *a)
 			newa->list_start = has_rvars ? -1 : a->rexpr_list_start;
 			newa->list_end = has_rvars ? -1 : a->rexpr_list_end;
 
-			result = (Node *) make_scalar_array_op(pstate,
-												   a->name,
-												   useOr,
-												   lexpr,
-												   (Node *) newa,
-												   a->location);
+			result_list = lappend(result_list,
+								  make_scalar_array_op(pstate,
+													   a->name,
+													   useOr,
+													   lexpr,
+													   (Node *) newa,
+													   a->location));
 
 			/* Consider only the Vars (if any) in the loop below */
 			rexprs = rvars;
@@ -1294,16 +1295,19 @@ transformAExprIn(ParseState *pstate, A_Expr *a)
 								   a->location);
 		}
 
-		cmp = coerce_to_boolean(pstate, cmp, "IN");
-		if (result == NULL)
-			result = cmp;
-		else
-			result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
-										   list_make2(result, cmp),
-										   a->location);
+		result_list = lappend(result_list,
+							  coerce_to_boolean(pstate, cmp, "IN"));
 	}
 
-	return result;
+	if (result_list == NIL)
+		return NULL;
+
+	if (list_length(result_list) == 1)
+		return (Node *) linitial(result_list);
+
+	return (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
+								 result_list,
+								 a->location);
 }
 
 static Node *
-- 
2.34.1

