Replies: 1 comment 2 replies
-
|
Thanks for writing this proposal. The main problem with implementing support for multidimensional arrays for diesel is not writing the actual Postgresql doesn't seem to store information about array dimensions in any way. For example postgresql is perfectly fine with the following set of queries: CREATE TEMPORARY TABLE FOO (
test_array text[],
test_multi_array text[][],
);
insert into foo values ('{"test", "test2"}', '{{"test"}, {"test2"}}');
insert into foo values ('{{"test"}, {"test2"}}', '{"test", "test2"}');
select * from foo;That will result in an output like If you check the table definition via So overall postgres doesn't differentiate between single and multidimensional arrays in any way. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Backend
PostgreSQL
What do you want to add to diesel?
Hi, is there a plan to add support for multi-dimensional arrays in diesel? I took a bit of a deep dive into diesels array implementation today as well as the Postgres byte encoding for array types and I thought I might give it a go, starting with 2D-arrays first.
Since Postgres defines MAXDIM=6 for arrays, I guess it would technically be possible to add support for all possible dimensions, but I feel like adding 2D support first would be a simpler approach.
I might totally have a blind spot here and underestimate how hard this is or miss some side effects this would cause. But after my research today I'll probably give it a go just out of curiosity anyways. If you are interested in the results, let me know.
Implementation notes
Implement
FromSql<Array<ST>, Pg> for Vec<Vec<T>>for arrays with ndim=2 and the equivalentToSqltrait.In case of a 2D array, the array header would include two additional
i32fields before the actual array payload, containingnum_elementsandlower_boundfor the 2nd dimension. With that information one can loop over the array values accordingly and parse them into a 2D vector of size ndim1 x ndim2.I haven't looked at the ToSql implementation as closely yet, but I assume taking a slice of a slice one has to construct the correct array header and then flatten the values.
Additional details
I'd keep the current assumption of
lower_bound=1for all dimensions.Checklist
Beta Was this translation helpful? Give feedback.
All reactions