Skip to content

Add support for multi-dimensional Postgres Arrays #4958

Description

@weiznich

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions