-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Implement array_ndims function #4178
Conversation
There was a problem hiding this 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>; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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])) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
/// let dims = diesel::select(array_ndims::<Array<_>, _>(vec![1, 2])) | |
/// let dims = diesel::select(array_ndims::<Array<Integer>, _>(vec![1, 2])) |
There was a problem hiding this 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])) |
There was a problem hiding this comment.
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
/// let dims = diesel::select(array_ndims::<Array<_>, _>(vec![1, 2])) | |
/// let dims = diesel::select(array_ndims::<Array<Integer>, _>(vec![1, 2])) |
e601cb5
to
18aec7f
Compare
18aec7f
to
614df9b
Compare
Adding support for
array_ndims
array postgresql function.Connected issue: #4153