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 equivalent ToSql trait.
In case of a 2D array, the array header would include two additional i32 fields before the actual array payload, containing num_elements and lower_bound for 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=1 for all dimensions.
Checklist
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