Skip to content

Commit a26194f

Browse files
committed
Fix broken ruleutils support for function TRANSFORM clauses.
I chanced to notice that this dumped core due to a faulty Assert. To add insult to injury, the output has been misformatted since v11. Obviously we need some regression testing here. Discussion: https://postgr.es/m/d1cc628c-3953-4209-957b-29427acc38c8@www.fastmail.com
1 parent 652f781 commit a26194f

File tree

8 files changed

+66
-13
lines changed

8 files changed

+66
-13
lines changed

contrib/bool_plperl/expected/bool_plperl.out

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ SELECT perl2undef() IS NULL AS p;
5252
--- test transforming to perl
5353
CREATE FUNCTION bool2perl(bool, bool, bool) RETURNS void
5454
LANGUAGE plperl
55-
TRANSFORM FOR TYPE bool
55+
TRANSFORM FOR TYPE bool, for type boolean -- duplicate to test ruleutils
5656
AS $$
5757
my ($x, $y, $z) = @_;
5858

@@ -68,6 +68,21 @@ SELECT bool2perl (true, false, NULL);
6868

6969
(1 row)
7070

71+
--- test ruleutils
72+
\sf bool2perl
73+
CREATE OR REPLACE FUNCTION public.bool2perl(boolean, boolean, boolean)
74+
RETURNS void
75+
TRANSFORM FOR TYPE boolean, FOR TYPE boolean
76+
LANGUAGE plperl
77+
AS $function$
78+
my ($x, $y, $z) = @_;
79+
80+
die("NULL mistransformed") if (defined($z));
81+
die("TRUE mistransformed to UNDEF") if (!defined($x));
82+
die("FALSE mistransformed to UNDEF") if (!defined($y));
83+
die("TRUE mistransformed") if (!$x);
84+
die("FALSE mistransformed") if ($y);
85+
$function$
7186
--- test selecting bool through SPI
7287
CREATE FUNCTION spi_test() RETURNS void
7388
LANGUAGE plperl

contrib/bool_plperl/expected/bool_plperlu.out

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ SELECT perl2undef() IS NULL AS p;
5252
--- test transforming to perl
5353
CREATE FUNCTION bool2perl(bool, bool, bool) RETURNS void
5454
LANGUAGE plperlu
55-
TRANSFORM FOR TYPE bool
55+
TRANSFORM FOR TYPE bool, for type boolean -- duplicate to test ruleutils
5656
AS $$
5757
my ($x, $y, $z) = @_;
5858

@@ -68,6 +68,21 @@ SELECT bool2perl (true, false, NULL);
6868

6969
(1 row)
7070

71+
--- test ruleutils
72+
\sf bool2perl
73+
CREATE OR REPLACE FUNCTION public.bool2perl(boolean, boolean, boolean)
74+
RETURNS void
75+
TRANSFORM FOR TYPE boolean, FOR TYPE boolean
76+
LANGUAGE plperlu
77+
AS $function$
78+
my ($x, $y, $z) = @_;
79+
80+
die("NULL mistransformed") if (defined($z));
81+
die("TRUE mistransformed to UNDEF") if (!defined($x));
82+
die("FALSE mistransformed to UNDEF") if (!defined($y));
83+
die("TRUE mistransformed") if (!$x);
84+
die("FALSE mistransformed") if ($y);
85+
$function$
7186
--- test selecting bool through SPI
7287
CREATE FUNCTION spi_test() RETURNS void
7388
LANGUAGE plperlu

contrib/bool_plperl/sql/bool_plperl.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ SELECT perl2undef() IS NULL AS p;
3333

3434
CREATE FUNCTION bool2perl(bool, bool, bool) RETURNS void
3535
LANGUAGE plperl
36-
TRANSFORM FOR TYPE bool
36+
TRANSFORM FOR TYPE bool, for type boolean -- duplicate to test ruleutils
3737
AS $$
3838
my ($x, $y, $z) = @_;
3939

@@ -46,6 +46,10 @@ $$;
4646

4747
SELECT bool2perl (true, false, NULL);
4848

49+
--- test ruleutils
50+
51+
\sf bool2perl
52+
4953
--- test selecting bool through SPI
5054

5155
CREATE FUNCTION spi_test() RETURNS void

contrib/bool_plperl/sql/bool_plperlu.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ SELECT perl2undef() IS NULL AS p;
3333

3434
CREATE FUNCTION bool2perl(bool, bool, bool) RETURNS void
3535
LANGUAGE plperlu
36-
TRANSFORM FOR TYPE bool
36+
TRANSFORM FOR TYPE bool, for type boolean -- duplicate to test ruleutils
3737
AS $$
3838
my ($x, $y, $z) = @_;
3939

@@ -46,6 +46,10 @@ $$;
4646

4747
SELECT bool2perl (true, false, NULL);
4848

49+
--- test ruleutils
50+
51+
\sf bool2perl
52+
4953
--- test selecting bool through SPI
5054

5155
CREATE FUNCTION spi_test() RETURNS void

contrib/hstore_plpython/expected/hstore_plpython.out

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,29 @@ SELECT test1arr(array['aa=>bb, cc=>NULL'::hstore, 'dd=>ee']);
4747
(1 row)
4848

4949
-- test python -> hstore
50-
CREATE FUNCTION test2() RETURNS hstore
50+
CREATE FUNCTION test2(a int, b text) RETURNS hstore
5151
LANGUAGE plpythonu
5252
TRANSFORM FOR TYPE hstore
5353
AS $$
54-
val = {'a': 1, 'b': 'boo', 'c': None}
54+
val = {'a': a, 'b': b, 'c': None}
5555
return val
5656
$$;
57-
SELECT test2();
57+
SELECT test2(1, 'boo');
5858
test2
5959
---------------------------------
6060
"a"=>"1", "b"=>"boo", "c"=>NULL
6161
(1 row)
6262

63+
--- test ruleutils
64+
\sf test2
65+
CREATE OR REPLACE FUNCTION public.test2(a integer, b text)
66+
RETURNS hstore
67+
TRANSFORM FOR TYPE hstore
68+
LANGUAGE plpythonu
69+
AS $function$
70+
val = {'a': a, 'b': b, 'c': None}
71+
return val
72+
$function$
6373
-- test python -> hstore[]
6474
CREATE FUNCTION test2arr() RETURNS hstore[]
6575
LANGUAGE plpythonu

contrib/hstore_plpython/sql/hstore_plpython.sql

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,18 @@ SELECT test1arr(array['aa=>bb, cc=>NULL'::hstore, 'dd=>ee']);
4040

4141

4242
-- test python -> hstore
43-
CREATE FUNCTION test2() RETURNS hstore
43+
CREATE FUNCTION test2(a int, b text) RETURNS hstore
4444
LANGUAGE plpythonu
4545
TRANSFORM FOR TYPE hstore
4646
AS $$
47-
val = {'a': 1, 'b': 'boo', 'c': None}
47+
val = {'a': a, 'b': b, 'c': None}
4848
return val
4949
$$;
5050

51-
SELECT test2();
51+
SELECT test2(1, 'boo');
52+
53+
--- test ruleutils
54+
\sf test2
5255

5356

5457
-- test python -> hstore[]

src/backend/utils/adt/ruleutils.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3127,13 +3127,14 @@ print_function_trftypes(StringInfo buf, HeapTuple proctup)
31273127
{
31283128
int i;
31293129

3130-
appendStringInfoString(buf, "\n TRANSFORM ");
3130+
appendStringInfoString(buf, " TRANSFORM ");
31313131
for (i = 0; i < ntypes; i++)
31323132
{
31333133
if (i != 0)
31343134
appendStringInfoString(buf, ", ");
31353135
appendStringInfo(buf, "FOR TYPE %s", format_type_be(trftypes[i]));
31363136
}
3137+
appendStringInfoChar(buf, '\n');
31373138
}
31383139
}
31393140

src/backend/utils/fmgr/funcapi.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,9 @@ get_func_arg_info(HeapTuple procTup,
11821182
/*
11831183
* get_func_trftypes
11841184
*
1185-
* Returns the number of transformed types used by function.
1185+
* Returns the number of transformed types used by the function.
1186+
* If there are any, a palloc'd array of the type OIDs is returned
1187+
* into *p_trftypes.
11861188
*/
11871189
int
11881190
get_func_trftypes(HeapTuple procTup,
@@ -1211,7 +1213,6 @@ get_func_trftypes(HeapTuple procTup,
12111213
ARR_HASNULL(arr) ||
12121214
ARR_ELEMTYPE(arr) != OIDOID)
12131215
elog(ERROR, "protrftypes is not a 1-D Oid array");
1214-
Assert(nelems >= ((Form_pg_proc) GETSTRUCT(procTup))->pronargs);
12151216
*p_trftypes = (Oid *) palloc(nelems * sizeof(Oid));
12161217
memcpy(*p_trftypes, ARR_DATA_PTR(arr),
12171218
nelems * sizeof(Oid));

0 commit comments

Comments
 (0)