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_shuffle #4220

Merged
merged 2 commits into from
Sep 6, 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
30 changes: 30 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,36 @@ define_sql_function! {
fn array_upper<Arr: ArrayOrNullableArray + SingleValue>(array: Arr, dimension: Integer) -> Nullable<Integer>;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Randomly shuffles the first dimension of the array.
///
/// # Example
///
// This function requires postgres >= 16.0
// which we cannot expect to be widly used at the
// point of writing this comment, so we skip running this test
/// ```rust,no_run
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::array_shuffle;
/// # use diesel::sql_types::{Array, Integer};
/// # let connection = &mut establish_connection();
/// let shuffled = diesel::select(array_shuffle::<Array<Integer>, _>(vec![1, 2, 3, 4, 5]))
/// .get_result::<Vec<i32>>(connection)?;
/// assert_eq!(5, shuffled.len());
/// assert_eq!(shuffled.iter().sum::<i32>(), 15);
/// # Ok(())
/// # }
/// ```
fn array_shuffle<Arr: ArrayOrNullableArray + SingleValue>(array: Arr) -> Arr;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Converts any SQL value to json
Expand Down
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 @@ -462,6 +462,11 @@ pub type array_positions<A, E> =
#[cfg(feature = "postgres_backend")]
pub type array_ndims<A> = super::functions::array_ndims<SqlTypeOf<A>, A>;

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

/// Return type of [`to_json(element)`](super::functions::to_json())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
Expand Down
1 change: 1 addition & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ fn postgres_functions() -> _ {
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),
array_shuffle(pg_extras::array),
to_json(pg_extras::id),
)
}
Expand Down
Loading