Skip to content

Commit 9d51733

Browse files
committed
Tighten array dimensionality checks in Python -> SQL array conversion.
Like plperl before f47004a, plpython wasn't being sufficiently careful about checking that list-of-list structures represent rectangular arrays, so that it would accept some cases in which different parts of the "array" are nested to different depths. This was exacerbated by Python's weak distinction between sequences and lists, so that in some cases strings could get treated as though they are lists (and burst into individual characters) even though a different ordering of the upper-level list would give a different result. Some of this behavior was unreachable (without risking a crash) before 81eaaf6. It seems like a good idea to clean it all up in the same releases, rather than shipping a non-crashing but nonetheless visibly buggy behavior in the name of minimal change. Hence, back-patch. Per bug #17912 and further testing by Alexander Lakhin. Discussion: https://postgr.es/m/17912-82ceed78731d9cdc@postgresql.org
1 parent 66ab266 commit 9d51733

File tree

4 files changed

+270
-128
lines changed

4 files changed

+270
-128
lines changed

src/pl/plpython/expected/plpython_types.out

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -687,23 +687,74 @@ SELECT * FROM test_type_conversion_array_mixed2();
687687
ERROR: invalid input syntax for type integer: "abc"
688688
CONTEXT: while creating return value
689689
PL/Python function "test_type_conversion_array_mixed2"
690-
CREATE FUNCTION test_type_conversion_array_mixed3() RETURNS text[] AS $$
691-
return [[], 'a']
690+
-- check output of multi-dimensional arrays
691+
CREATE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
692+
return [['a'], ['b'], ['c']]
692693
$$ LANGUAGE plpythonu;
693-
SELECT * FROM test_type_conversion_array_mixed3();
694-
test_type_conversion_array_mixed3
694+
select test_type_conversion_md_array_out();
695+
test_type_conversion_md_array_out
695696
-----------------------------------
696-
{[],a}
697+
{{a},{b},{c}}
697698
(1 row)
698699

700+
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
701+
return [[], []]
702+
$$ LANGUAGE plpythonu;
703+
select test_type_conversion_md_array_out();
704+
test_type_conversion_md_array_out
705+
-----------------------------------
706+
{}
707+
(1 row)
708+
709+
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
710+
return [[], [1]]
711+
$$ LANGUAGE plpythonu;
712+
select test_type_conversion_md_array_out(); -- fail
713+
ERROR: multidimensional arrays must have array expressions with matching dimensions
714+
CONTEXT: while creating return value
715+
PL/Python function "test_type_conversion_md_array_out"
716+
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
717+
return [[], 1]
718+
$$ LANGUAGE plpythonu;
719+
select test_type_conversion_md_array_out(); -- fail
720+
ERROR: multidimensional arrays must have array expressions with matching dimensions
721+
CONTEXT: while creating return value
722+
PL/Python function "test_type_conversion_md_array_out"
723+
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
724+
return [1, []]
725+
$$ LANGUAGE plpythonu;
726+
select test_type_conversion_md_array_out(); -- fail
727+
ERROR: multidimensional arrays must have array expressions with matching dimensions
728+
CONTEXT: while creating return value
729+
PL/Python function "test_type_conversion_md_array_out"
730+
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
731+
return [[1], [[]]]
732+
$$ LANGUAGE plpythonu;
733+
select test_type_conversion_md_array_out(); -- fail
734+
ERROR: multidimensional arrays must have array expressions with matching dimensions
735+
CONTEXT: while creating return value
736+
PL/Python function "test_type_conversion_md_array_out"
699737
CREATE FUNCTION test_type_conversion_mdarray_malformed() RETURNS int[] AS $$
700738
return [[1,2,3],[4,5]]
701739
$$ LANGUAGE plpythonu;
702740
SELECT * FROM test_type_conversion_mdarray_malformed();
703-
ERROR: wrong length of inner sequence: has length 2, but 3 was expected
704-
DETAIL: To construct a multidimensional array, the inner sequences must all have the same length.
741+
ERROR: multidimensional arrays must have array expressions with matching dimensions
705742
CONTEXT: while creating return value
706743
PL/Python function "test_type_conversion_mdarray_malformed"
744+
CREATE FUNCTION test_type_conversion_mdarray_malformed2() RETURNS text[] AS $$
745+
return [[1,2,3], "abc"]
746+
$$ LANGUAGE plpythonu;
747+
SELECT * FROM test_type_conversion_mdarray_malformed2();
748+
ERROR: multidimensional arrays must have array expressions with matching dimensions
749+
CONTEXT: while creating return value
750+
PL/Python function "test_type_conversion_mdarray_malformed2"
751+
CREATE FUNCTION test_type_conversion_mdarray_malformed3() RETURNS text[] AS $$
752+
return ["abc", [1,2,3]]
753+
$$ LANGUAGE plpythonu;
754+
SELECT * FROM test_type_conversion_mdarray_malformed3();
755+
ERROR: multidimensional arrays must have array expressions with matching dimensions
756+
CONTEXT: while creating return value
757+
PL/Python function "test_type_conversion_mdarray_malformed3"
707758
CREATE FUNCTION test_type_conversion_mdarray_toodeep() RETURNS int[] AS $$
708759
return [[[[[[[1]]]]]]]
709760
$$ LANGUAGE plpythonu;

src/pl/plpython/expected/plpython_types_3.out

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -687,23 +687,74 @@ SELECT * FROM test_type_conversion_array_mixed2();
687687
ERROR: invalid input syntax for type integer: "abc"
688688
CONTEXT: while creating return value
689689
PL/Python function "test_type_conversion_array_mixed2"
690-
CREATE FUNCTION test_type_conversion_array_mixed3() RETURNS text[] AS $$
691-
return [[], 'a']
690+
-- check output of multi-dimensional arrays
691+
CREATE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
692+
return [['a'], ['b'], ['c']]
692693
$$ LANGUAGE plpython3u;
693-
SELECT * FROM test_type_conversion_array_mixed3();
694-
test_type_conversion_array_mixed3
694+
select test_type_conversion_md_array_out();
695+
test_type_conversion_md_array_out
695696
-----------------------------------
696-
{[],a}
697+
{{a},{b},{c}}
697698
(1 row)
698699

700+
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
701+
return [[], []]
702+
$$ LANGUAGE plpython3u;
703+
select test_type_conversion_md_array_out();
704+
test_type_conversion_md_array_out
705+
-----------------------------------
706+
{}
707+
(1 row)
708+
709+
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
710+
return [[], [1]]
711+
$$ LANGUAGE plpython3u;
712+
select test_type_conversion_md_array_out(); -- fail
713+
ERROR: multidimensional arrays must have array expressions with matching dimensions
714+
CONTEXT: while creating return value
715+
PL/Python function "test_type_conversion_md_array_out"
716+
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
717+
return [[], 1]
718+
$$ LANGUAGE plpython3u;
719+
select test_type_conversion_md_array_out(); -- fail
720+
ERROR: multidimensional arrays must have array expressions with matching dimensions
721+
CONTEXT: while creating return value
722+
PL/Python function "test_type_conversion_md_array_out"
723+
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
724+
return [1, []]
725+
$$ LANGUAGE plpython3u;
726+
select test_type_conversion_md_array_out(); -- fail
727+
ERROR: multidimensional arrays must have array expressions with matching dimensions
728+
CONTEXT: while creating return value
729+
PL/Python function "test_type_conversion_md_array_out"
730+
CREATE OR REPLACE FUNCTION test_type_conversion_md_array_out() RETURNS text[] AS $$
731+
return [[1], [[]]]
732+
$$ LANGUAGE plpython3u;
733+
select test_type_conversion_md_array_out(); -- fail
734+
ERROR: multidimensional arrays must have array expressions with matching dimensions
735+
CONTEXT: while creating return value
736+
PL/Python function "test_type_conversion_md_array_out"
699737
CREATE FUNCTION test_type_conversion_mdarray_malformed() RETURNS int[] AS $$
700738
return [[1,2,3],[4,5]]
701739
$$ LANGUAGE plpython3u;
702740
SELECT * FROM test_type_conversion_mdarray_malformed();
703-
ERROR: wrong length of inner sequence: has length 2, but 3 was expected
704-
DETAIL: To construct a multidimensional array, the inner sequences must all have the same length.
741+
ERROR: multidimensional arrays must have array expressions with matching dimensions
705742
CONTEXT: while creating return value
706743
PL/Python function "test_type_conversion_mdarray_malformed"
744+
CREATE FUNCTION test_type_conversion_mdarray_malformed2() RETURNS text[] AS $$
745+
return [[1,2,3], "abc"]
746+
$$ LANGUAGE plpython3u;
747+
SELECT * FROM test_type_conversion_mdarray_malformed2();
748+
ERROR: multidimensional arrays must have array expressions with matching dimensions
749+
CONTEXT: while creating return value
750+
PL/Python function "test_type_conversion_mdarray_malformed2"
751+
CREATE FUNCTION test_type_conversion_mdarray_malformed3() RETURNS text[] AS $$
752+
return ["abc", [1,2,3]]
753+
$$ LANGUAGE plpython3u;
754+
SELECT * FROM test_type_conversion_mdarray_malformed3();
755+
ERROR: multidimensional arrays must have array expressions with matching dimensions
756+
CONTEXT: while creating return value
757+
PL/Python function "test_type_conversion_mdarray_malformed3"
707758
CREATE FUNCTION test_type_conversion_mdarray_toodeep() RETURNS int[] AS $$
708759
return [[[[[[[1]]]]]]]
709760
$$ LANGUAGE plpython3u;

0 commit comments

Comments
 (0)