Skip to content

Commit

Permalink
implement array_shuffle
Browse files Browse the repository at this point in the history
  • Loading branch information
gushul committed Aug 31, 2024
1 parent 08911c8 commit 5b53bb1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
27 changes: 27 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1423,3 +1423,30 @@ 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
///
/// ```rust
/// # 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!(shuffled != vec![1, 2, 3, 4, 5]); // It's very unlikely to get the same order
/// # Ok(())
/// # }
/// ```
fn array_shuffle<Arr: ArrayOrNullableArray + SingleValue>(array: Arr) -> Arr;
}
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 @@ -461,3 +461,8 @@ pub type array_positions<A, E> =
#[allow(non_camel_case_types)]
#[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>;
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),
)
}

Expand Down

0 comments on commit 5b53bb1

Please sign in to comment.