From 91f3407a5d5737c4f0c08c4d988aaf7b3cd7ebdc Mon Sep 17 00:00:00 2001
From: jcoleman <jtc331@gmail.com>
Date: Sat, 24 Sep 2022 09:58:45 -0400
Subject: [PATCH v3 1/2] Add tests before change

---
 src/test/regress/expected/select_parallel.out | 164 ++++++++++++++++++
 src/test/regress/sql/select_parallel.sql      |  45 +++++
 2 files changed, 209 insertions(+)

diff --git a/src/test/regress/expected/select_parallel.out b/src/test/regress/expected/select_parallel.out
index 91f74fe47a..ada786d434 100644
--- a/src/test/regress/expected/select_parallel.out
+++ b/src/test/regress/expected/select_parallel.out
@@ -1040,6 +1040,170 @@ explain (costs off)
                            Filter: (stringu1 ~~ '%AAAA'::text)
 (11 rows)
 
+-- There are some special cases with LATERAL...
+savepoint settings;
+set parallel_tuple_cost=0;
+explain (costs off) select *, (select t.unique1 from tenk1 t2) from tenk1 t;
+                               QUERY PLAN                               
+------------------------------------------------------------------------
+ Gather
+   Workers Planned: 4
+   ->  Parallel Seq Scan on tenk1 t
+   SubPlan 1
+     ->  Gather
+           Workers Planned: 4
+           ->  Parallel Index Only Scan using tenk1_hundred on tenk1 t2
+(7 rows)
+
+explain (costs off) select *, (select t.unique1 from tenk1 t2 limit 1) from tenk1 t;
+             QUERY PLAN             
+------------------------------------
+ Gather
+   Workers Planned: 4
+   ->  Parallel Seq Scan on tenk1 t
+   SubPlan 1
+     ->  Limit
+           ->  Seq Scan on tenk1 t2
+(6 rows)
+
+explain (costs off) select *,
+  (select t2.two from tenk1 t2 where t.unique1 = t2.unique1 limit 1)
+  from tenk1 t;
+                        QUERY PLAN                        
+----------------------------------------------------------
+ Gather
+   Workers Planned: 4
+   ->  Parallel Seq Scan on tenk1 t
+   SubPlan 1
+     ->  Limit
+           ->  Index Scan using tenk1_unique1 on tenk1 t2
+                 Index Cond: (unique1 = t.unique1)
+(7 rows)
+
+explain (costs off) select *,
+  (select t2.two from tenk1 t2 where t.unique1 = t2.unique1)
+  from tenk1 t;
+                     QUERY PLAN                     
+----------------------------------------------------
+ Gather
+   Workers Planned: 4
+   ->  Parallel Seq Scan on tenk1 t
+   SubPlan 1
+     ->  Index Scan using tenk1_unique1 on tenk1 t2
+           Index Cond: (unique1 = t.unique1)
+(6 rows)
+
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (select t.unique1 from tenk1 offset 0) l on true;
+                             QUERY PLAN                              
+---------------------------------------------------------------------
+ Nested Loop
+   ->  Gather
+         Workers Planned: 4
+         ->  Parallel Index Only Scan using tenk1_unique1 on tenk1 t
+   ->  Gather
+         Workers Planned: 4
+         ->  Parallel Index Only Scan using tenk1_hundred on tenk1
+(7 rows)
+
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (select t.unique1 from tenk1 limit 1) l on true;
+                             QUERY PLAN                              
+---------------------------------------------------------------------
+ Nested Loop
+   ->  Gather
+         Workers Planned: 4
+         ->  Parallel Index Only Scan using tenk1_unique1 on tenk1 t
+   ->  Limit
+         ->  Seq Scan on tenk1
+(6 rows)
+
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (
+  select *
+  from tenk1
+  where t.unique1 = tenk1.unique1
+  limit 1
+) l on true;
+                             QUERY PLAN                              
+---------------------------------------------------------------------
+ Nested Loop
+   ->  Gather
+         Workers Planned: 4
+         ->  Parallel Index Only Scan using tenk1_unique1 on tenk1 t
+   ->  Limit
+         ->  Index Only Scan using tenk1_unique1 on tenk1
+               Index Cond: (unique1 = t.unique1)
+(7 rows)
+
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (
+  select *
+  from tenk1
+  where t.unique1 = tenk1.unique1
+) l on true;
+                               QUERY PLAN                                
+-------------------------------------------------------------------------
+ Gather
+   Workers Planned: 4
+   ->  Parallel Hash Join
+         Hash Cond: (t.unique1 = tenk1.unique1)
+         ->  Parallel Index Only Scan using tenk1_unique1 on tenk1 t
+         ->  Parallel Hash
+               ->  Parallel Index Only Scan using tenk1_unique1 on tenk1
+(7 rows)
+
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (
+  select *
+  from tenk1
+  limit 1
+) l on true;
+                             QUERY PLAN                              
+---------------------------------------------------------------------
+ Nested Loop
+   ->  Limit
+         ->  Seq Scan on tenk1
+   ->  Gather
+         Workers Planned: 4
+         ->  Parallel Index Only Scan using tenk1_unique1 on tenk1 t
+(6 rows)
+
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (
+  select *
+  from tenk1
+) l on true;
+                               QUERY PLAN                                
+-------------------------------------------------------------------------
+ Nested Loop
+   ->  Gather
+         Workers Planned: 4
+         ->  Parallel Index Only Scan using tenk1_unique1 on tenk1 t
+   ->  Materialize
+         ->  Gather
+               Workers Planned: 4
+               ->  Parallel Index Only Scan using tenk1_hundred on tenk1
+(8 rows)
+
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (
+  select t.unique1
+  from tenk1
+) l on true;
+                               QUERY PLAN                                
+-------------------------------------------------------------------------
+ Nested Loop
+   ->  Gather
+         Workers Planned: 4
+         ->  Parallel Index Only Scan using tenk1_unique1 on tenk1 t
+   ->  Materialize
+         ->  Gather
+               Workers Planned: 4
+               ->  Parallel Index Only Scan using tenk1_hundred on tenk1
+(8 rows)
+
+rollback to savepoint settings;
 -- to increase the parallel query test coverage
 SAVEPOINT settings;
 SET LOCAL force_parallel_mode = 1;
diff --git a/src/test/regress/sql/select_parallel.sql b/src/test/regress/sql/select_parallel.sql
index 62fb68c7a0..ed3691d807 100644
--- a/src/test/regress/sql/select_parallel.sql
+++ b/src/test/regress/sql/select_parallel.sql
@@ -390,6 +390,51 @@ explain (costs off, verbose)
 explain (costs off)
   select * from tenk1 a where two in
     (select two from tenk1 b where stringu1 like '%AAAA' limit 3);
+-- There are some special cases with LATERAL...
+savepoint settings;
+set parallel_tuple_cost=0;
+explain (costs off) select *, (select t.unique1 from tenk1 t2) from tenk1 t;
+explain (costs off) select *, (select t.unique1 from tenk1 t2 limit 1) from tenk1 t;
+explain (costs off) select *,
+  (select t2.two from tenk1 t2 where t.unique1 = t2.unique1 limit 1)
+  from tenk1 t;
+explain (costs off) select *,
+  (select t2.two from tenk1 t2 where t.unique1 = t2.unique1)
+  from tenk1 t;
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (select t.unique1 from tenk1 offset 0) l on true;
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (select t.unique1 from tenk1 limit 1) l on true;
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (
+  select *
+  from tenk1
+  where t.unique1 = tenk1.unique1
+  limit 1
+) l on true;
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (
+  select *
+  from tenk1
+  where t.unique1 = tenk1.unique1
+) l on true;
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (
+  select *
+  from tenk1
+  limit 1
+) l on true;
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (
+  select *
+  from tenk1
+) l on true;
+explain (costs off) select t.unique1 from tenk1 t
+join lateral (
+  select t.unique1
+  from tenk1
+) l on true;
+rollback to savepoint settings;
 
 -- to increase the parallel query test coverage
 SAVEPOINT settings;
-- 
2.32.1 (Apple Git-133)

