Skip to content

Commit e1fd61c

Browse files
committed
Ensure casting to typmod -1 generates a RelabelType.
Fix the code changed by commit 5c056b0 so that we always generate RelabelType, not something else, for a cast to unspecified typmod. Otherwise planner optimizations might not happen. It appears we missed this point because the previous experiments were done on type numeric: the parser undesirably generates a call on the numeric() length-coercion function, but then numeric_support() optimizes that down to a RelabelType, so that everything seems fine. It misbehaves for types that have a non-optimized length coercion function, such as bpchar. Per report from John Naylor. Back-patch to all supported branches, as the previous patch eventually was. Unfortunately, that no longer includes 9.6 ... we really shouldn't put this type of change into a nearly-EOL branch. Discussion: https://postgr.es/m/CAFBsxsEfbFHEkouc+FSj+3K1sHipLPbEC67L0SAe-9-da8QtYg@mail.gmail.com
1 parent f04aa65 commit e1fd61c

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

src/backend/parser/parse_coerce.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,15 @@ coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod,
760760
if (hideInputCoercion)
761761
hide_coercion_node(node);
762762

763-
pathtype = find_typmod_coercion_function(targetTypeId, &funcId);
763+
/*
764+
* A negative typmod means that no actual coercion is needed, but we still
765+
* want a RelabelType to ensure that the expression exposes the intended
766+
* typmod.
767+
*/
768+
if (targetTypMod < 0)
769+
pathtype = COERCION_PATH_NONE;
770+
else
771+
pathtype = find_typmod_coercion_function(targetTypeId, &funcId);
764772

765773
if (pathtype != COERCION_PATH_NONE)
766774
{

src/test/regress/expected/expressions.out

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,40 @@ explain (verbose, costs off) select * from numeric_view;
111111
Output: numeric_tbl.f1, (numeric_tbl.f1)::numeric(16,4), (numeric_tbl.f1)::numeric, numeric_tbl.f2, (numeric_tbl.f2)::numeric(16,4), numeric_tbl.f2
112112
(2 rows)
113113

114+
-- bpchar, lacking planner support for its length coercion function,
115+
-- could behave differently
116+
create table bpchar_tbl (f1 character(16) unique, f2 bpchar);
117+
create view bpchar_view as
118+
select
119+
f1, f1::character(14) as f114, f1::bpchar as f1n,
120+
f2, f2::character(14) as f214, f2::bpchar as f2n
121+
from bpchar_tbl;
122+
\d+ bpchar_view
123+
View "public.bpchar_view"
124+
Column | Type | Collation | Nullable | Default | Storage | Description
125+
--------+---------------+-----------+----------+---------+----------+-------------
126+
f1 | character(16) | | | | extended |
127+
f114 | character(14) | | | | extended |
128+
f1n | bpchar | | | | extended |
129+
f2 | bpchar | | | | extended |
130+
f214 | character(14) | | | | extended |
131+
f2n | bpchar | | | | extended |
132+
View definition:
133+
SELECT bpchar_tbl.f1,
134+
bpchar_tbl.f1::character(14) AS f114,
135+
bpchar_tbl.f1::bpchar AS f1n,
136+
bpchar_tbl.f2,
137+
bpchar_tbl.f2::character(14) AS f214,
138+
bpchar_tbl.f2 AS f2n
139+
FROM bpchar_tbl;
140+
141+
explain (verbose, costs off) select * from bpchar_view
142+
where f1::bpchar = 'foo';
143+
QUERY PLAN
144+
------------------------------------------------------------------------------------------------------------------------------------------------
145+
Index Scan using bpchar_tbl_f1_key on public.bpchar_tbl
146+
Output: bpchar_tbl.f1, (bpchar_tbl.f1)::character(14), (bpchar_tbl.f1)::bpchar, bpchar_tbl.f2, (bpchar_tbl.f2)::character(14), bpchar_tbl.f2
147+
Index Cond: ((bpchar_tbl.f1)::bpchar = 'foo'::bpchar)
148+
(3 rows)
149+
114150
rollback;

src/test/regress/sql/expressions.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,20 @@ create view numeric_view as
5353

5454
explain (verbose, costs off) select * from numeric_view;
5555

56+
-- bpchar, lacking planner support for its length coercion function,
57+
-- could behave differently
58+
59+
create table bpchar_tbl (f1 character(16) unique, f2 bpchar);
60+
61+
create view bpchar_view as
62+
select
63+
f1, f1::character(14) as f114, f1::bpchar as f1n,
64+
f2, f2::character(14) as f214, f2::bpchar as f2n
65+
from bpchar_tbl;
66+
67+
\d+ bpchar_view
68+
69+
explain (verbose, costs off) select * from bpchar_view
70+
where f1::bpchar = 'foo';
71+
5672
rollback;

0 commit comments

Comments
 (0)