v7-0002-Extend-Const-cast-suppression-to-RelabelType-node.patch

text/plain

Filename: v7-0002-Extend-Const-cast-suppression-to-RelabelType-node.patch
Type: text/plain
Part: 1
Message: Re: [PATCH] postgres_fdw: suppress explicit casts in text:text comparisons (was: column option to override foreign types)

Patch

Format: format-patch
Series: patch v7-0002
Subject: Extend Const cast suppression to RelabelType nodes implicitly casting Vars
File+
contrib/postgres_fdw/deparse.c 10 2
contrib/postgres_fdw/expected/postgres_fdw.out 9 9
From 9aaa6938abde6075a449f23a986ec880c5397d73 Mon Sep 17 00:00:00 2001
From: Dian M Fay <dian.m.fay@gmail.com>
Date: Wed, 10 Nov 2021 23:01:30 -0500
Subject: [PATCH v7 2/2] Extend Const cast suppression to RelabelType nodes
 implicitly casting Vars

---
 contrib/postgres_fdw/deparse.c                 | 12 ++++++++++--
 contrib/postgres_fdw/expected/postgres_fdw.out | 18 +++++++++---------
 2 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/contrib/postgres_fdw/deparse.c b/contrib/postgres_fdw/deparse.c
index 83c1749882..285da3c09b 100644
--- a/contrib/postgres_fdw/deparse.c
+++ b/contrib/postgres_fdw/deparse.c
@@ -3015,11 +3015,19 @@ deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
 		{
 			if (IsA(left, Const))
 			{
-				canSuppressLeftConstCast = IsA(right, Var);
+				/*
+				 * Allow suppressing explicit casts of RelabelType nodes too as
+				 * long as it's _implicitly_ casting a Var.
+				 */
+				canSuppressLeftConstCast = IsA(right, Var) || (IsA(right, RelabelType) &&
+						((RelabelType *) right)->relabelformat == COERCE_IMPLICIT_CAST &&
+						IsA(((RelabelType *) right)->arg, Var));
 			}
 			else if (IsA(right, Const))
 			{
-				canSuppressRightConstCast = IsA(left, Var);
+				canSuppressRightConstCast = IsA(left, Var) || (IsA(left, RelabelType) &&
+						((RelabelType *) left)->relabelformat == COERCE_IMPLICIT_CAST &&
+						IsA(((RelabelType *) left)->arg, Var));
 			}
 		}
 
diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out
index a07035c51e..3cee0a8c12 100644
--- a/contrib/postgres_fdw/expected/postgres_fdw.out
+++ b/contrib/postgres_fdw/expected/postgres_fdw.out
@@ -341,11 +341,11 @@ SELECT * FROM ft1 WHERE false;
 
 -- with WHERE clause
 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE t1.c1 = 101 AND t1.c6 = '1' AND t1.c7 >= '1';
-                                                               QUERY PLAN                                                               
-----------------------------------------------------------------------------------------------------------------------------------------
+                                                            QUERY PLAN                                                            
+----------------------------------------------------------------------------------------------------------------------------------
  Foreign Scan on public.ft1 t1
    Output: c1, c2, c3, c4, c5, c6, c7, c8
-   Remote SQL: SELECT "C 1", c2, c3, c4, c5, c6, c7, c8 FROM "S 1"."T 1" WHERE ((c7 >= '1')) AND (("C 1" = 101)) AND ((c6 = '1'::text))
+   Remote SQL: SELECT "C 1", c2, c3, c4, c5, c6, c7, c8 FROM "S 1"."T 1" WHERE ((c7 >= '1')) AND (("C 1" = 101)) AND ((c6 = '1'))
 (3 rows)
 
 SELECT * FROM ft1 t1 WHERE t1.c1 = 101 AND t1.c6 = '1' AND t1.c7 >= '1';
@@ -707,11 +707,11 @@ EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE c1 = (ARRAY[c1,c2,3])[1]
 (3 rows)
 
 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE c6 = E'foo''s\\bar';  -- check special chars
-                                                 QUERY PLAN                                                  
--------------------------------------------------------------------------------------------------------------
+                                              QUERY PLAN                                               
+-------------------------------------------------------------------------------------------------------
  Foreign Scan on public.ft1 t1
    Output: c1, c2, c3, c4, c5, c6, c7, c8
-   Remote SQL: SELECT "C 1", c2, c3, c4, c5, c6, c7, c8 FROM "S 1"."T 1" WHERE ((c6 = E'foo''s\\bar'::text))
+   Remote SQL: SELECT "C 1", c2, c3, c4, c5, c6, c7, c8 FROM "S 1"."T 1" WHERE ((c6 = E'foo''s\\bar'))
 (3 rows)
 
 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE c8 = 'foo';  -- can't be sent to remote
@@ -4325,11 +4325,11 @@ explain (verbose, costs off) select * from ft3 where f2 = 'foo';
 (3 rows)
 
 explain (verbose, costs off) select * from ft3 where f3 = 'foo';
-                                  QUERY PLAN                                  
-------------------------------------------------------------------------------
+                               QUERY PLAN                               
+------------------------------------------------------------------------
  Foreign Scan on public.ft3
    Output: f1, f2, f3
-   Remote SQL: SELECT f1, f2, f3 FROM public.loct3 WHERE ((f3 = 'foo'::text))
+   Remote SQL: SELECT f1, f2, f3 FROM public.loct3 WHERE ((f3 = 'foo'))
 (3 rows)
 
 explain (verbose, costs off) select * from ft3 f, loct3 l
-- 
2.33.1