Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e0f5048

Browse files
committed
Fix handling of "undef" in contrib/jsonb_plperl.
Perl has multiple internal representations of "undef", and just testing for SvTYPE(x) == SVt_NULL doesn't recognize all of them, leading to "cannot transform this Perl type to jsonb" errors. Use the approved test SvOK() instead. Report and patch by Ivan Panchenko. Back-patch to v11 where this module was added. Discussion: https://postgr.es/m/1564783533.324795401@f193.i.mail.ru
1 parent 803466b commit e0f5048

File tree

5 files changed

+73
-7
lines changed

5 files changed

+73
-7
lines changed

contrib/jsonb_plperl/expected/jsonb_plperl.out

+21-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ SELECT testRegexpResultToJsonb();
6666
0
6767
(1 row)
6868

69+
-- this revealed a different bug
70+
CREATE FUNCTION testTextToJsonbObject(text) RETURNS jsonb
71+
LANGUAGE plperl
72+
TRANSFORM FOR TYPE jsonb
73+
AS $$
74+
my $x = shift;
75+
return {a => $x};
76+
$$;
77+
SELECT testTextToJsonbObject('abc');
78+
testtexttojsonbobject
79+
-----------------------
80+
{"a": "abc"}
81+
(1 row)
82+
83+
SELECT testTextToJsonbObject(NULL);
84+
testtexttojsonbobject
85+
-----------------------
86+
{"a": null}
87+
(1 row)
88+
6989
CREATE FUNCTION roundtrip(val jsonb, ref text = '') RETURNS jsonb
7090
LANGUAGE plperl
7191
TRANSFORM FOR TYPE jsonb
@@ -230,4 +250,4 @@ SELECT roundtrip('{"1": {"2": [3, 4, 5]}, "2": 3}', 'HASH');
230250

231251
\set VERBOSITY terse \\ -- suppress cascade details
232252
DROP EXTENSION plperl CASCADE;
233-
NOTICE: drop cascades to 7 other objects
253+
NOTICE: drop cascades to 8 other objects

contrib/jsonb_plperl/expected/jsonb_plperlu.out

+21-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ SELECT testRegexpResultToJsonb();
6666
0
6767
(1 row)
6868

69+
-- this revealed a different bug
70+
CREATE FUNCTION testTextToJsonbObject(text) RETURNS jsonb
71+
LANGUAGE plperlu
72+
TRANSFORM FOR TYPE jsonb
73+
AS $$
74+
my $x = shift;
75+
return {a => $x};
76+
$$;
77+
SELECT testTextToJsonbObject('abc');
78+
testtexttojsonbobject
79+
-----------------------
80+
{"a": "abc"}
81+
(1 row)
82+
83+
SELECT testTextToJsonbObject(NULL);
84+
testtexttojsonbobject
85+
-----------------------
86+
{"a": null}
87+
(1 row)
88+
6989
CREATE FUNCTION roundtrip(val jsonb, ref text = '') RETURNS jsonb
7090
LANGUAGE plperlu
7191
TRANSFORM FOR TYPE jsonb
@@ -257,4 +277,4 @@ INFO: $VAR1 = {'1' => {'2' => ['3','4','5']},'2' => '3'};
257277

258278
\set VERBOSITY terse \\ -- suppress cascade details
259279
DROP EXTENSION plperlu CASCADE;
260-
NOTICE: drop cascades to 7 other objects
280+
NOTICE: drop cascades to 8 other objects

contrib/jsonb_plperl/jsonb_plperl.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,12 @@ SV_to_JsonbValue(SV *in, JsonbParseState **jsonb_state, bool is_elem)
189189
case SVt_PVHV:
190190
return HV_to_JsonbValue((HV *) in, jsonb_state);
191191

192-
case SVt_NULL:
193-
out.type = jbvNull;
194-
break;
195-
196192
default:
197-
if (SvUOK(in))
193+
if (!SvOK(in))
194+
{
195+
out.type = jbvNull;
196+
}
197+
else if (SvUOK(in))
198198
{
199199
/*
200200
* If UV is >=64 bits, we have no better way to make this

contrib/jsonb_plperl/sql/jsonb_plperl.sql

+13
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ $$;
5757
SELECT testRegexpResultToJsonb();
5858

5959

60+
-- this revealed a different bug
61+
CREATE FUNCTION testTextToJsonbObject(text) RETURNS jsonb
62+
LANGUAGE plperl
63+
TRANSFORM FOR TYPE jsonb
64+
AS $$
65+
my $x = shift;
66+
return {a => $x};
67+
$$;
68+
69+
SELECT testTextToJsonbObject('abc');
70+
SELECT testTextToJsonbObject(NULL);
71+
72+
6073
CREATE FUNCTION roundtrip(val jsonb, ref text = '') RETURNS jsonb
6174
LANGUAGE plperl
6275
TRANSFORM FOR TYPE jsonb

contrib/jsonb_plperl/sql/jsonb_plperlu.sql

+13
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ $$;
5757
SELECT testRegexpResultToJsonb();
5858

5959

60+
-- this revealed a different bug
61+
CREATE FUNCTION testTextToJsonbObject(text) RETURNS jsonb
62+
LANGUAGE plperlu
63+
TRANSFORM FOR TYPE jsonb
64+
AS $$
65+
my $x = shift;
66+
return {a => $x};
67+
$$;
68+
69+
SELECT testTextToJsonbObject('abc');
70+
SELECT testTextToJsonbObject(NULL);
71+
72+
6073
CREATE FUNCTION roundtrip(val jsonb, ref text = '') RETURNS jsonb
6174
LANGUAGE plperlu
6275
TRANSFORM FOR TYPE jsonb

0 commit comments

Comments
 (0)