Skip to content

Commit

Permalink
Merge pull request #4220 from gushul/feat/add/array_shuffle
Browse files Browse the repository at this point in the history
implement array_shuffle
  • Loading branch information
weiznich authored Sep 6, 2024
2 parents 3d4b19d + 26cc45a commit bc0e030
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
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

0 comments on commit bc0e030

Please sign in to comment.