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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1361,3 +1361,32 @@ define_sql_function! {
elem: E,
) -> Nullable<Array<Integer>>;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Returns the number of dimensions of the array
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::array_ndims;
/// # use diesel::sql_types::{Nullable, Array, Integer};
/// # let connection = &mut establish_connection();
///
/// // diesel currently only supports 1D arrays
/// let dims = diesel::select(array_ndims::<Array<Integer>, _>(vec![1, 2]))
/// .get_result::<i32>(connection)?;
/// assert_eq!(1, dims);
///
/// # Ok(())
/// # }
/// ```
fn array_ndims<Arr: ArrayOrNullableArray + SingleValue>(arr: Arr) -> Integer;
}
5 changes: 5 additions & 0 deletions diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,8 @@ pub type array_position_with_subscript<A, E, S> =
#[cfg(feature = "postgres_backend")]
pub type array_positions<A, E> =
super::functions::array_positions<SqlTypeOf<A>, SqlTypeOf<E>, A, E>;

/// 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.

1 change: 1 addition & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ fn postgres_functions() -> _ {
array_position(pg_extras::array, pg_extras::id),
array_position_with_subscript(pg_extras::array, pg_extras::id, pg_extras::id),
array_positions(pg_extras::array, pg_extras::id),
array_ndims(pg_extras::array),
)
}

Expand Down
Loading