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_sample #4239

Merged
merged 2 commits into from
Sep 10, 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
44 changes: 43 additions & 1 deletion diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ define_sql_function! {
/// # Example
///
// This function requires postgres >= 16.0
// which we cannot expect to be widly used at the
// which we cannot expect to be widely used at the
// point of writing this comment, so we skip running this test
/// ```rust,no_run
/// # include!("../../doctest_setup.rs");
Expand All @@ -1455,6 +1455,48 @@ define_sql_function! {
fn array_shuffle<Arr: ArrayOrNullableArray + SingleValue>(array: Arr) -> Arr;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Returns an array of n items randomly selected from array.
/// n may not exceed the length of the array.
///
/// # Example
///
// This function requires postgres >= 16.0
// which we cannot expect to be widely 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_sample;
/// # use diesel::sql_types::{Array, Integer, Nullable};
/// # let connection = &mut establish_connection();
///
/// let vec = vec![1,2,3,4,5];
/// let sampled = diesel::select(array_sample::<Array<Integer>, _, _>(vec.clone(),3))
/// .get_result::<Vec<i32>>(connection)?;
/// assert_eq!(3, sampled.len());
/// assert!(sampled.iter().all(|x| vec.contains(x)));
///
/// let vec: Vec<i32> = Vec::new();
/// let sampled = diesel::select(array_sample::<Array<Integer>, _, _>(vec,0))
/// .get_result::<Vec<i32>>(connection)?;
/// assert_eq!(0, sampled.len());
///
/// let sampled = diesel::select(array_sample::<Nullable<Array<Integer>>, _, _>(None::<Vec<i32>>,1))
/// .get_result::<Option<Vec<i32>>>(connection)?;
/// assert!(sampled.is_none());
/// # Ok(())
/// # }
/// ```
fn array_sample<Arr: ArrayOrNullableArray + SingleValue>(array: Arr, n: Integer) -> 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 @@ -467,6 +467,11 @@ pub type array_ndims<A> = super::functions::array_ndims<SqlTypeOf<A>, A>;
#[cfg(feature = "postgres_backend")]
pub type array_shuffle<A> = super::functions::array_shuffle<SqlTypeOf<A>, A>;

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

/// 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 @@ -433,6 +433,7 @@ fn postgres_functions() -> _ {
array_positions(pg_extras::array, pg_extras::id),
array_ndims(pg_extras::array),
array_shuffle(pg_extras::array),
array_sample(pg_extras::array, pg_extras::id),
to_json(pg_extras::id),
)
}
Expand Down
Loading