Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement array_ndims function #4178

Merged
merged 2 commits into from
Aug 30, 2024

Conversation

danila-b
Copy link
Contributor

@danila-b danila-b commented Aug 17, 2024

Adding support for array_ndims array postgresql function.
Connected issue: #4153

Copy link
Contributor Author

@danila-b danila-b left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR is still WIP but I would appreciate any pointers if I am doing something completely wrong from the beginning,

/// Return type of [`array_ndims(array)`](super::functions::array_ndims())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_ndims<A> = super::functions::array_ndims<SqlTypeOf<A>, A>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this even required for functions that do not have a generic return type and always return Integer?
I tried to remove it but without this generic arguments in auto_types explodes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's required for the generic arguments. define_sql_function! generates a function signature as follows:

fn array_ndims<A, Arr>(arr: Arr) -> Integer
where Arr: AsExpression<A>, A: ArrayOrNullableArray + SingleValue>;

The AsExpression part allows us to automatically convert rust values (i.e. vec![1,2,3]) to sql side binds and at the same time it allows us to accept sql side expressions with the same type at the same location (i.e table::array_column). On the other hand the #[auto_type] macro expects exactly as many generic arguments as there are function parameters. The type def in then used to map that.

/// # use diesel::sql_types::{Nullable, Array, Integer};
/// # let connection = &mut establish_connection();
///
/// let dims = diesel::select(array_ndims::<Array<_>, _>(vec![1, 2]))
Copy link
Contributor Author

@danila-b danila-b Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why, but it fails to infer the types for Array here.
Same thing since to work for array_append just fine

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works on array_append because array append has a second argument which is defined

Suggested change
/// let dims = diesel::select(array_ndims::<Array<_>, _>(vec![1, 2]))
/// let dims = diesel::select(array_ndims::<Array<Integer>, _>(vec![1, 2]))

@weiznich weiznich requested a review from a team August 18, 2024 14:47
Copy link
Contributor

@guissalustiano guissalustiano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for open the PR!

The code looks good! I add a comment about the inference error.
Could you a test with a null input to guarantee that it does not break the type signature?

/// # use diesel::sql_types::{Nullable, Array, Integer};
/// # let connection = &mut establish_connection();
///
/// let dims = diesel::select(array_ndims::<Array<_>, _>(vec![1, 2]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works on array_append because array append has a second argument which is defined

Suggested change
/// let dims = diesel::select(array_ndims::<Array<_>, _>(vec![1, 2]))
/// let dims = diesel::select(array_ndims::<Array<Integer>, _>(vec![1, 2]))

@weiznich weiznich force-pushed the feat/add/array-ndims-function branch from e601cb5 to 18aec7f Compare August 30, 2024 09:03
@weiznich weiznich enabled auto-merge August 30, 2024 09:04
@weiznich weiznich added this pull request to the merge queue Aug 30, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to a conflict with the base branch Aug 30, 2024
@weiznich weiznich force-pushed the feat/add/array-ndims-function branch from 18aec7f to 614df9b Compare August 30, 2024 10:43
@weiznich weiznich enabled auto-merge August 30, 2024 10:44
@weiznich weiznich added this pull request to the merge queue Aug 30, 2024
Merged via the queue into diesel-rs:master with commit 4be7713 Aug 30, 2024
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants