From 8c14c0ebb20e79925fdd8b6bbd4fcce91ba92dcf Mon Sep 17 00:00:00 2001
From: Dmitrii Dolgov <9erthalion6@gmail.com>
Date: Tue, 20 May 2025 16:12:05 +0200
Subject: [PATCH v4 3/3] Extend ARRAY squashing tests

Testing coverage for ARRAY expressions is not enough. Add more test
cases, similar to already existing ones.
---
 .../pg_stat_statements/expected/squashing.out | 178 ++++++++++++++++++
 contrib/pg_stat_statements/sql/squashing.sql  |  60 ++++++
 2 files changed, 238 insertions(+)

diff --git a/contrib/pg_stat_statements/expected/squashing.out b/contrib/pg_stat_statements/expected/squashing.out
index d92cfbd35fb..d628a451a1e 100644
--- a/contrib/pg_stat_statements/expected/squashing.out
+++ b/contrib/pg_stat_statements/expected/squashing.out
@@ -429,3 +429,181 @@ SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
  SELECT pg_stat_statements_reset() IS NOT NULL AS t |     1
 (2 rows)
 
+-- Nested arrays are squashed only at constants level
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t 
+---
+ t
+(1 row)
+
+SELECT ARRAY[
+    ARRAY[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
+    ARRAY[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
+    ARRAY[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
+    ARRAY[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+    ];
+                                             array                                             
+-----------------------------------------------------------------------------------------------
+ {{1,2,3,4,5,6,7,8,9,10},{1,2,3,4,5,6,7,8,9,10},{1,2,3,4,5,6,7,8,9,10},{1,2,3,4,5,6,7,8,9,10}}
+(1 row)
+
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+                       query                        | calls 
+----------------------------------------------------+-------
+ SELECT ARRAY[                                     +|     1
+     ARRAY[$1 /*, ... */],                         +| 
+     ARRAY[$2 /*, ... */],                         +| 
+     ARRAY[$3 /*, ... */],                         +| 
+     ARRAY[$4 /*, ... */]                          +| 
+     ]                                              | 
+ SELECT pg_stat_statements_reset() IS NOT NULL AS t |     1
+(2 rows)
+
+-- Relabel type
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t 
+---
+ t
+(1 row)
+
+SELECT ARRAY[1::oid, 2::oid, 3::oid, 4::oid, 5::oid, 6::oid, 7::oid, 8::oid, 9::oid];
+        array        
+---------------------
+ {1,2,3,4,5,6,7,8,9}
+(1 row)
+
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+                       query                        | calls 
+----------------------------------------------------+-------
+ SELECT ARRAY[$1 /*, ... */]                        |     1
+ SELECT pg_stat_statements_reset() IS NOT NULL AS t |     1
+(2 rows)
+
+-- Some casting expression are simplified to Const
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t 
+---
+ t
+(1 row)
+
+SELECT ARRAY[
+    ('"1"')::jsonb, ('"2"')::jsonb, ('"3"')::jsonb, ('"4"')::jsonb,
+	( '"5"')::jsonb, ( '"6"')::jsonb, ( '"7"')::jsonb, ( '"8"')::jsonb,
+	( '"9"')::jsonb, ( '"10"')::jsonb
+];
+                                       array                                        
+------------------------------------------------------------------------------------
+ {"\"1\"","\"2\"","\"3\"","\"4\"","\"5\"","\"6\"","\"7\"","\"8\"","\"9\"","\"10\""}
+(1 row)
+
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+                       query                        | calls 
+----------------------------------------------------+-------
+ SELECT ARRAY[$1 /*, ... */]                        |     1
+ SELECT pg_stat_statements_reset() IS NOT NULL AS t |     1
+(2 rows)
+
+-- CoerceViaIO
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t 
+---
+ t
+(1 row)
+
+SELECT ARRAY[
+	1::int4::casttesttype, 2::int4::casttesttype, 3::int4::casttesttype,
+	4::int4::casttesttype, 5::int4::casttesttype, 6::int4::casttesttype,
+	7::int4::casttesttype, 8::int4::casttesttype, 9::int4::casttesttype,
+	10::int4::casttesttype, 11::int4::casttesttype
+];
+           array           
+---------------------------
+ {1,2,3,4,5,6,7,8,9,10,11}
+(1 row)
+
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+                       query                        | calls 
+----------------------------------------------------+-------
+ SELECT ARRAY[$1 /*, ... */]                        |     1
+ SELECT pg_stat_statements_reset() IS NOT NULL AS t |     1
+(2 rows)
+
+-- CoerceViaIO, SubLink instead of a Const is not squashed
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t 
+---
+ t
+(1 row)
+
+SELECT ARRAY[
+	(SELECT '"1"')::jsonb, (SELECT '"2"')::jsonb, (SELECT '"3"')::jsonb,
+	(SELECT '"4"')::jsonb, (SELECT '"5"')::jsonb, (SELECT '"6"')::jsonb,
+	(SELECT '"7"')::jsonb, (SELECT '"8"')::jsonb, (SELECT '"9"')::jsonb,
+	(SELECT '"10"')::jsonb
+];
+                                       array                                        
+------------------------------------------------------------------------------------
+ {"\"1\"","\"2\"","\"3\"","\"4\"","\"5\"","\"6\"","\"7\"","\"8\"","\"9\"","\"10\""}
+(1 row)
+
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+                                query                                | calls 
+---------------------------------------------------------------------+-------
+ SELECT ARRAY[                                                      +|     1
+         (SELECT $1)::jsonb, (SELECT $2)::jsonb, (SELECT $3)::jsonb,+| 
+         (SELECT $4)::jsonb, (SELECT $5)::jsonb, (SELECT $6)::jsonb,+| 
+         (SELECT $7)::jsonb, (SELECT $8)::jsonb, (SELECT $9)::jsonb,+| 
+         (SELECT $10)::jsonb                                        +| 
+ ]                                                                   | 
+ SELECT pg_stat_statements_reset() IS NOT NULL AS t                  |     1
+(2 rows)
+
+-- Bigint, long tokens with parenthesis
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t 
+---
+ t
+(1 row)
+
+SELECT ARRAY[
+	abs(100), abs(200), abs(300), abs(400), abs(500), abs(600), abs(700),
+	abs(800), abs(900), abs(1000), ((abs(1100)))
+];
+                      array                      
+-------------------------------------------------
+ {100,200,300,400,500,600,700,800,900,1000,1100}
+(1 row)
+
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+                                 query                                  | calls 
+------------------------------------------------------------------------+-------
+ SELECT ARRAY[                                                         +|     1
+         abs($1), abs($2), abs($3), abs($4), abs($5), abs($6), abs($7),+| 
+         abs($8), abs($9), abs($10), ((abs($11)))                      +| 
+ ]                                                                      | 
+ SELECT pg_stat_statements_reset() IS NOT NULL AS t                     |     1
+(2 rows)
+
+-- Bigint, long tokens with parenthesis
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+ t 
+---
+ t
+(1 row)
+
+SELECT ARRAY[
+	1::bigint, 2::bigint, 3::bigint, 4::bigint, 5::bigint, 6::bigint,
+	7::bigint, 8::bigint, 9::bigint, 10::bigint, 11::bigint
+];
+           array           
+---------------------------
+ {1,2,3,4,5,6,7,8,9,10,11}
+(1 row)
+
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+                       query                        | calls 
+----------------------------------------------------+-------
+ SELECT ARRAY[$1 /*, ... */]                        |     1
+ SELECT pg_stat_statements_reset() IS NOT NULL AS t |     1
+(2 rows)
+
diff --git a/contrib/pg_stat_statements/sql/squashing.sql b/contrib/pg_stat_statements/sql/squashing.sql
index 03efd4b40c8..5ac624ae1f7 100644
--- a/contrib/pg_stat_statements/sql/squashing.sql
+++ b/contrib/pg_stat_statements/sql/squashing.sql
@@ -167,3 +167,63 @@ FROM cte;
 SELECT pg_stat_statements_reset() IS NOT NULL AS t;
 SELECT ARRAY[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
 SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+
+-- Nested arrays are squashed only at constants level
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+SELECT ARRAY[
+    ARRAY[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
+    ARRAY[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
+    ARRAY[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
+    ARRAY[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+    ];
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+
+-- Relabel type
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+SELECT ARRAY[1::oid, 2::oid, 3::oid, 4::oid, 5::oid, 6::oid, 7::oid, 8::oid, 9::oid];
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+
+-- Some casting expression are simplified to Const
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+SELECT ARRAY[
+    ('"1"')::jsonb, ('"2"')::jsonb, ('"3"')::jsonb, ('"4"')::jsonb,
+	( '"5"')::jsonb, ( '"6"')::jsonb, ( '"7"')::jsonb, ( '"8"')::jsonb,
+	( '"9"')::jsonb, ( '"10"')::jsonb
+];
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+
+-- CoerceViaIO
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+SELECT ARRAY[
+	1::int4::casttesttype, 2::int4::casttesttype, 3::int4::casttesttype,
+	4::int4::casttesttype, 5::int4::casttesttype, 6::int4::casttesttype,
+	7::int4::casttesttype, 8::int4::casttesttype, 9::int4::casttesttype,
+	10::int4::casttesttype, 11::int4::casttesttype
+];
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+
+-- CoerceViaIO, SubLink instead of a Const is not squashed
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+SELECT ARRAY[
+	(SELECT '"1"')::jsonb, (SELECT '"2"')::jsonb, (SELECT '"3"')::jsonb,
+	(SELECT '"4"')::jsonb, (SELECT '"5"')::jsonb, (SELECT '"6"')::jsonb,
+	(SELECT '"7"')::jsonb, (SELECT '"8"')::jsonb, (SELECT '"9"')::jsonb,
+	(SELECT '"10"')::jsonb
+];
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+
+-- Bigint, long tokens with parenthesis
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+SELECT ARRAY[
+	abs(100), abs(200), abs(300), abs(400), abs(500), abs(600), abs(700),
+	abs(800), abs(900), abs(1000), ((abs(1100)))
+];
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
+
+-- Bigint, long tokens with parenthesis
+SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+SELECT ARRAY[
+	1::bigint, 2::bigint, 3::bigint, 4::bigint, 5::bigint, 6::bigint,
+	7::bigint, 8::bigint, 9::bigint, 10::bigint, 11::bigint
+];
+SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
-- 
2.39.5 (Apple Git-154)

