2018-04-03 21:47:18 +08:00
|
|
|
CREATE EXTENSION jsonb_plperl CASCADE;
|
|
|
|
|
|
|
|
|
|
|
|
CREATE FUNCTION testHVToJsonb() RETURNS jsonb
|
|
|
|
LANGUAGE plperl
|
|
|
|
TRANSFORM FOR TYPE jsonb
|
|
|
|
AS $$
|
|
|
|
$val = {a => 1, b => 'boo', c => undef};
|
|
|
|
return $val;
|
|
|
|
$$;
|
|
|
|
|
|
|
|
SELECT testHVToJsonb();
|
|
|
|
|
|
|
|
|
|
|
|
CREATE FUNCTION testAVToJsonb() RETURNS jsonb
|
|
|
|
LANGUAGE plperl
|
|
|
|
TRANSFORM FOR TYPE jsonb
|
|
|
|
AS $$
|
|
|
|
$val = [{a => 1, b => 'boo', c => undef}, {d => 2}];
|
|
|
|
return $val;
|
|
|
|
$$;
|
|
|
|
|
|
|
|
SELECT testAVToJsonb();
|
|
|
|
|
|
|
|
|
|
|
|
CREATE FUNCTION testSVToJsonb() RETURNS jsonb
|
|
|
|
LANGUAGE plperl
|
|
|
|
TRANSFORM FOR TYPE jsonb
|
|
|
|
AS $$
|
|
|
|
$val = 1;
|
|
|
|
return $val;
|
|
|
|
$$;
|
|
|
|
|
|
|
|
SELECT testSVToJsonb();
|
|
|
|
|
|
|
|
|
Fix platform and Perl-version dependencies in new jsonb_plperl code.
Testing SvTYPE() directly is more fraught with problems than one might
think, because depending on context Perl might be storing a scalar value
in one of several forms, eg both numeric and string values. This resulted
in Perl-version-dependent buildfarm test failures. Instead use the SvTYPE
test only to distinguish non-scalar cases (AV, HV, NULL). Disambiguate
scalars by testing SvIOK, SvNOK, then SvPOK. This creates a preference
order for how we will resolve cases where the value is available in more
than one form, which seems fine to me.
Furthermore, because we're now dealing directly with a "double" value
in the SvNOK case, we can get rid of an inadequate and unportable
string-comparison test for infinities, and use isinf() instead.
(We do need some additional #include and "-lm" infrastructure to use
that in a contrib module, per prior experiences.)
In passing, prevent the regression test results from depending on DROP
CASCADE order; I've not seen that malfunction, but it's trouble waiting
to happen.
Discussion: https://postgr.es/m/E1f3MMJ-0006bf-B0@gemulon.postgresql.org
2018-04-04 23:28:33 +08:00
|
|
|
-- this revealed a bug in the original implementation
|
|
|
|
CREATE FUNCTION testRegexpResultToJsonb() RETURNS jsonb
|
|
|
|
LANGUAGE plperl
|
|
|
|
TRANSFORM FOR TYPE jsonb
|
|
|
|
AS $$
|
|
|
|
return ('1' =~ m(0\t2));
|
|
|
|
$$;
|
|
|
|
|
|
|
|
SELECT testRegexpResultToJsonb();
|
|
|
|
|
|
|
|
|
Fix excessive enreferencing in jsonb-to-plperl transform.
We want, say, 2 to be transformed as 2, not \\2 which is what the
original coding produced. Perl's standard seems to be to add an RV
wrapper only for hash and array SVs, so do it like that.
This was missed originally because the test cases only checked what came
out of a round trip back to SQL, and the strip-all-dereferences loop at
the top of SV_to_JsonbValue hides the extra refs from view. As a better
test, print the Perl value with Data::Dumper, like the hstore_plperlu
tests do. While we can't do that in the plperl test, only plperlu,
that should be good enough because this code is the same for both PLs.
But also add a simplistic test for extra REFs, which we can do in both.
That strip-all-dereferences behavior is now a bit dubious; it's unlike
what happens for other Perl-to-SQL conversions. However, the best
thing to do seems to be to leave it alone and make the other conversions
act similarly. That will be done separately.
Dagfinn Ilmari Mannsåker, adjusted a bit by me
Discussion: https://postgr.es/m/d8jlgbq66t9.fsf@dalvik.ping.uio.no
2018-06-19 02:31:42 +08:00
|
|
|
CREATE FUNCTION roundtrip(val jsonb, ref text = '') RETURNS jsonb
|
2018-04-03 21:47:18 +08:00
|
|
|
LANGUAGE plperl
|
|
|
|
TRANSFORM FOR TYPE jsonb
|
|
|
|
AS $$
|
Fix excessive enreferencing in jsonb-to-plperl transform.
We want, say, 2 to be transformed as 2, not \\2 which is what the
original coding produced. Perl's standard seems to be to add an RV
wrapper only for hash and array SVs, so do it like that.
This was missed originally because the test cases only checked what came
out of a round trip back to SQL, and the strip-all-dereferences loop at
the top of SV_to_JsonbValue hides the extra refs from view. As a better
test, print the Perl value with Data::Dumper, like the hstore_plperlu
tests do. While we can't do that in the plperl test, only plperlu,
that should be good enough because this code is the same for both PLs.
But also add a simplistic test for extra REFs, which we can do in both.
That strip-all-dereferences behavior is now a bit dubious; it's unlike
what happens for other Perl-to-SQL conversions. However, the best
thing to do seems to be to leave it alone and make the other conversions
act similarly. That will be done separately.
Dagfinn Ilmari Mannsåker, adjusted a bit by me
Discussion: https://postgr.es/m/d8jlgbq66t9.fsf@dalvik.ping.uio.no
2018-06-19 02:31:42 +08:00
|
|
|
# can't use Data::Dumper, but let's at least check for unexpected ref type
|
|
|
|
die 'unexpected '.(ref($_[0]) || 'not a').' reference'
|
|
|
|
if ref($_[0]) ne $_[1];
|
2018-04-03 21:47:18 +08:00
|
|
|
return $_[0];
|
|
|
|
$$;
|
|
|
|
|
|
|
|
|
Fix excessive enreferencing in jsonb-to-plperl transform.
We want, say, 2 to be transformed as 2, not \\2 which is what the
original coding produced. Perl's standard seems to be to add an RV
wrapper only for hash and array SVs, so do it like that.
This was missed originally because the test cases only checked what came
out of a round trip back to SQL, and the strip-all-dereferences loop at
the top of SV_to_JsonbValue hides the extra refs from view. As a better
test, print the Perl value with Data::Dumper, like the hstore_plperlu
tests do. While we can't do that in the plperl test, only plperlu,
that should be good enough because this code is the same for both PLs.
But also add a simplistic test for extra REFs, which we can do in both.
That strip-all-dereferences behavior is now a bit dubious; it's unlike
what happens for other Perl-to-SQL conversions. However, the best
thing to do seems to be to leave it alone and make the other conversions
act similarly. That will be done separately.
Dagfinn Ilmari Mannsåker, adjusted a bit by me
Discussion: https://postgr.es/m/d8jlgbq66t9.fsf@dalvik.ping.uio.no
2018-06-19 02:31:42 +08:00
|
|
|
SELECT roundtrip('null') is null;
|
2018-04-03 21:47:18 +08:00
|
|
|
SELECT roundtrip('1');
|
|
|
|
SELECT roundtrip('1E+131071');
|
|
|
|
SELECT roundtrip('-1');
|
|
|
|
SELECT roundtrip('1.2');
|
|
|
|
SELECT roundtrip('-1.2');
|
|
|
|
SELECT roundtrip('"string"');
|
|
|
|
SELECT roundtrip('"NaN"');
|
|
|
|
|
|
|
|
SELECT roundtrip('true');
|
|
|
|
SELECT roundtrip('false');
|
|
|
|
|
Fix excessive enreferencing in jsonb-to-plperl transform.
We want, say, 2 to be transformed as 2, not \\2 which is what the
original coding produced. Perl's standard seems to be to add an RV
wrapper only for hash and array SVs, so do it like that.
This was missed originally because the test cases only checked what came
out of a round trip back to SQL, and the strip-all-dereferences loop at
the top of SV_to_JsonbValue hides the extra refs from view. As a better
test, print the Perl value with Data::Dumper, like the hstore_plperlu
tests do. While we can't do that in the plperl test, only plperlu,
that should be good enough because this code is the same for both PLs.
But also add a simplistic test for extra REFs, which we can do in both.
That strip-all-dereferences behavior is now a bit dubious; it's unlike
what happens for other Perl-to-SQL conversions. However, the best
thing to do seems to be to leave it alone and make the other conversions
act similarly. That will be done separately.
Dagfinn Ilmari Mannsåker, adjusted a bit by me
Discussion: https://postgr.es/m/d8jlgbq66t9.fsf@dalvik.ping.uio.no
2018-06-19 02:31:42 +08:00
|
|
|
SELECT roundtrip('[]', 'ARRAY');
|
|
|
|
SELECT roundtrip('[null, null]', 'ARRAY');
|
|
|
|
SELECT roundtrip('[1, 2, 3]', 'ARRAY');
|
|
|
|
SELECT roundtrip('[-1, 2, -3]', 'ARRAY');
|
|
|
|
SELECT roundtrip('[1.2, 2.3, 3.4]', 'ARRAY');
|
|
|
|
SELECT roundtrip('[-1.2, 2.3, -3.4]', 'ARRAY');
|
|
|
|
SELECT roundtrip('["string1", "string2"]', 'ARRAY');
|
|
|
|
SELECT roundtrip('[["string1", "string2"]]', 'ARRAY');
|
|
|
|
|
|
|
|
SELECT roundtrip('{}', 'HASH');
|
|
|
|
SELECT roundtrip('{"1": null}', 'HASH');
|
|
|
|
SELECT roundtrip('{"1": 1}', 'HASH');
|
|
|
|
SELECT roundtrip('{"1": -1}', 'HASH');
|
|
|
|
SELECT roundtrip('{"1": 1.1}', 'HASH');
|
|
|
|
SELECT roundtrip('{"1": -1.1}', 'HASH');
|
|
|
|
SELECT roundtrip('{"1": "string1"}', 'HASH');
|
|
|
|
|
|
|
|
SELECT roundtrip('{"1": {"2": [3, 4, 5]}, "2": 3}', 'HASH');
|
2018-04-03 21:47:18 +08:00
|
|
|
|
|
|
|
|
Fix platform and Perl-version dependencies in new jsonb_plperl code.
Testing SvTYPE() directly is more fraught with problems than one might
think, because depending on context Perl might be storing a scalar value
in one of several forms, eg both numeric and string values. This resulted
in Perl-version-dependent buildfarm test failures. Instead use the SvTYPE
test only to distinguish non-scalar cases (AV, HV, NULL). Disambiguate
scalars by testing SvIOK, SvNOK, then SvPOK. This creates a preference
order for how we will resolve cases where the value is available in more
than one form, which seems fine to me.
Furthermore, because we're now dealing directly with a "double" value
in the SvNOK case, we can get rid of an inadequate and unportable
string-comparison test for infinities, and use isinf() instead.
(We do need some additional #include and "-lm" infrastructure to use
that in a contrib module, per prior experiences.)
In passing, prevent the regression test results from depending on DROP
CASCADE order; I've not seen that malfunction, but it's trouble waiting
to happen.
Discussion: https://postgr.es/m/E1f3MMJ-0006bf-B0@gemulon.postgresql.org
2018-04-04 23:28:33 +08:00
|
|
|
\set VERBOSITY terse \\ -- suppress cascade details
|
2018-04-03 21:47:18 +08:00
|
|
|
DROP EXTENSION plperl CASCADE;
|