pgcentralfoundation / pgrx

Build Postgres Extensions with Rust!
Other
3.45k stars 230 forks source link

Returning `Vec<Vec<f32>>` produces unexpected results #1762

Open jawj opened 2 weeks ago

jawj commented 2 weeks ago

I have a function that returns a nested Vec<Vec<f32>>:

#[pg_extern]
fn test_nested() -> Vec<Vec<f32>> {
    vec![vec![1.1, 1.2, 1.3], vec![2.1, 2.2, 2.3]]
}

I expected the same results as this:

# select ARRAY[ARRAY[1.1, 1.2, 1.3], ARRAY[2.1, 2.2, 2.3]];
             array             
-------------------------------
 {{1.1,1.2,1.3},{2.1,2.2,2.3}}
(1 row)

# select array_dims(ARRAY[ARRAY[1.1, 1.2, 1.3], ARRAY[2.1, 2.2, 2.3]]);
 array_dims 
------------
 [1:2][1:3]
(1 row)

But instead I get this:

# select test_nested();
            test_nested            
-----------------------------------
 {"{1.1,1.2,1.3}","{2.1,2.2,2.3}"}
(1 row)

# select array_dims(test_nested());
 array_dims 
------------
 [1:2]
(1 row)

Is this a bug, or have I misunderstood something?

Is there some other way I can return a nested real[][] array from a Rust function?

workingjubilee commented 2 weeks ago

No, we do not currently have support for multidimensional arrays, and when we do, they will not be nested Vecs.

workingjubilee commented 2 weeks ago

This is why they should not be nested Vecs:

# SELECT array_dims('{{1,2,3,4},{5,6},{7,8,9}}'::int[][]);
ERROR:  malformed array literal: "{{1,2,3,4},{5,6},{7,8,9}}"
LINE 1: SELECT array_dims('{{1,2,3,4},{5,6},{7,8,9}}'::int[][]);
                          ^
DETAIL:  Multidimensional arrays must have sub-arrays with matching dimensions.

Vec<Vec<T>> does not enforce this requirement. However, it is very simple to define a type that does (and indeed the real reason multidimensional array support has been blocked, right now, is "I'm too busy implementing fixes for everything else").

jawj commented 2 weeks ago

OK: thanks for the explanation, and for all your work on this project.

workingjubilee commented 2 weeks ago

This bug report could be considered a duplicate of https://github.com/pgcentralfoundation/pgrx/issues/968 but I am currently considering retooling the RetAbi implementations so that this just does not work.