From 9a502185f8b49088f95656b5c826b3b0258fb9b2 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sun, 22 Sep 2019 08:38:59 -0700
Subject: [PATCH v1 09/12] Fix determination when tuple deforming can be JITed.

This broke in 675af5c01e297, and can lead to tuple deforming in inner
nodes not being performed, where previously possible.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:
---
 src/backend/executor/execExpr.c   |  2 ++
 src/test/regress/expected/jit.out | 30 +++++++++++++++---------------
 2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/src/backend/executor/execExpr.c b/src/backend/executor/execExpr.c
index 512ab4029ef..ecaa3ed98f9 100644
--- a/src/backend/executor/execExpr.c
+++ b/src/backend/executor/execExpr.c
@@ -2396,6 +2396,7 @@ ExecComputeSlotInfo(ExprState *state, ExprEvalStep *op)
 		{
 			isfixed = true;
 			tts_ops = parent->innerops;
+			desc = ExecGetResultType(is);
 		}
 		else if (is)
 		{
@@ -2415,6 +2416,7 @@ ExecComputeSlotInfo(ExprState *state, ExprEvalStep *op)
 		{
 			isfixed = true;
 			tts_ops = parent->outerops;
+			desc = ExecGetResultType(os);
 		}
 		else if (os)
 		{
diff --git a/src/test/regress/expected/jit.out b/src/test/regress/expected/jit.out
index 64690415a4b..4db4ae6d352 100644
--- a/src/test/regress/expected/jit.out
+++ b/src/test/regress/expected/jit.out
@@ -396,17 +396,17 @@ SELECT a.data || b.data FROM jittest_simple a JOIN jittest_simple b USING(id);
 COMMIT;
 -- check that inner/outer tuple deforming can be inferred for upper nodes, agg case
 EXPLAIN (VERBOSE, COSTS OFF, JIT_DETAILS) SELECT count(*), count(data), string_agg(data, ':') FROM jittest_simple;
-                                                                           QUERY PLAN                                                                           
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
+                                                                             QUERY PLAN                                                                              
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Aggregate
    Project: count(*), count(data), string_agg(data, ':'::text); JIT-Expr: evalexpr_0_0
    Phase 1 using strategy "All":
-     Transition Function: int8inc(TRANS), int8inc_any(TRANS, data), string_agg_transfn(TRANS, data, ':'::text); JIT-Expr: evalexpr_0_1, JIT-Deform-Outer: false
+     Transition Function: int8inc(TRANS), int8inc_any(TRANS, data), string_agg_transfn(TRANS, data, ':'::text); JIT-Expr: evalexpr_0_1, JIT-Deform-Outer: deform_0_2
      All Group
    ->  Seq Scan on public.jittest_simple
          Output: id, data
  JIT:
-   Functions: 2 (2 for expression evaluation)
+   Functions: 3 (2 for expression evaluation, 1 for tuple deforming)
    Options: Inlining false, Optimization false, Expressions true, Deforming true
 (10 rows)
 
@@ -424,17 +424,17 @@ BEGIN;
 SET LOCAL enable_hashagg = true;
 SET LOCAL enable_sort = false;
 EXPLAIN (VERBOSE, COSTS OFF, JIT_DETAILS) SELECT data, string_agg(id::text, ', ') FROM jittest_simple GROUP BY data;
-                                                         QUERY PLAN                                                          
------------------------------------------------------------------------------------------------------------------------------
+                                                            QUERY PLAN                                                            
+----------------------------------------------------------------------------------------------------------------------------------
  HashAggregate
-   Project: data, string_agg((id)::text, ', '::text); JIT-Expr: evalexpr_0_0, JIT-Deform-Outer: false
+   Project: data, string_agg((id)::text, ', '::text); JIT-Expr: evalexpr_0_0, JIT-Deform-Outer: deform_0_1
    Phase 0 using strategy "Hash":
-     Transition Function: string_agg_transfn(TRANS, (id)::text, ', '::text); JIT-Expr: evalexpr_0_1, JIT-Deform-Outer: false
+     Transition Function: string_agg_transfn(TRANS, (id)::text, ', '::text); JIT-Expr: evalexpr_0_2, JIT-Deform-Outer: deform_0_3
      Hash Group: jittest_simple.data; JIT-Expr: false
    ->  Seq Scan on public.jittest_simple
          Output: id, data
  JIT:
-   Functions: 2 (2 for expression evaluation)
+   Functions: 4 (2 for expression evaluation, 2 for tuple deforming)
    Options: Inlining false, Optimization false, Expressions true, Deforming true
 (10 rows)
 
@@ -452,20 +452,20 @@ BEGIN;
 SET LOCAL enable_hashagg = false;
 SET LOCAL enable_sort = true;
 EXPLAIN (VERBOSE, COSTS OFF, JIT_DETAILS) SELECT data, string_agg(id::text, ', ') FROM jittest_simple GROUP BY data;
-                                                         QUERY PLAN                                                          
------------------------------------------------------------------------------------------------------------------------------
+                                                            QUERY PLAN                                                            
+----------------------------------------------------------------------------------------------------------------------------------
  GroupAggregate
-   Project: data, string_agg((id)::text, ', '::text); JIT-Expr: evalexpr_0_2, JIT-Deform-Outer: false
+   Project: data, string_agg((id)::text, ', '::text); JIT-Expr: evalexpr_0_2, JIT-Deform-Outer: deform_0_3
    Phase 1 using strategy "Sorted Input":
-     Transition Function: string_agg_transfn(TRANS, (id)::text, ', '::text); JIT-Expr: evalexpr_0_4, JIT-Deform-Outer: false
-     Sorted Input Group: jittest_simple.data; JIT-Expr: evalexpr_0_3, JIT-Deform-Outer: false, JIT-Deform-Inner: false
+     Transition Function: string_agg_transfn(TRANS, (id)::text, ', '::text); JIT-Expr: evalexpr_0_5, JIT-Deform-Outer: deform_0_6
+     Sorted Input Group: jittest_simple.data; JIT-Expr: evalexpr_0_4, JIT-Deform-Outer: false, JIT-Deform-Inner: false
    ->  Sort
          Output: data, id
          Sort Key: jittest_simple.data
          ->  Seq Scan on public.jittest_simple
                Project: data, id; JIT-Expr: evalexpr_0_0, JIT-Deform-Scan: deform_0_1
  JIT:
-   Functions: 5 (4 for expression evaluation, 1 for tuple deforming)
+   Functions: 7 (4 for expression evaluation, 3 for tuple deforming)
    Options: Inlining false, Optimization false, Expressions true, Deforming true
 (13 rows)
 
-- 
2.23.0.162.gf1d4a28250

