diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 9e349534b5f4..8f0aa7de6d1e 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -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"); @@ -1455,6 +1455,48 @@ define_sql_function! { fn array_shuffle(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::, _, _>(vec.clone(),3)) + /// .get_result::>(connection)?; + /// assert_eq!(3, sampled.len()); + /// assert!(sampled.iter().all(|x| vec.contains(x))); + /// + /// let vec: Vec = Vec::new(); + /// let sampled = diesel::select(array_sample::, _, _>(vec,0)) + /// .get_result::>(connection)?; + /// assert_eq!(0, sampled.len()); + /// + /// let sampled = diesel::select(array_sample::>, _, _>(None::>,1)) + /// .get_result::>>(connection)?; + /// assert!(sampled.is_none()); + /// # Ok(()) + /// # } + /// ``` + fn array_sample(array: Arr, n: Integer) -> Arr; +} + #[cfg(feature = "postgres_backend")] define_sql_function! { /// Converts any SQL value to json diff --git a/diesel/src/pg/expression/helper_types.rs b/diesel/src/pg/expression/helper_types.rs index 9e26a0f9329e..7261315d4a78 100644 --- a/diesel/src/pg/expression/helper_types.rs +++ b/diesel/src/pg/expression/helper_types.rs @@ -467,6 +467,11 @@ pub type array_ndims = super::functions::array_ndims, A>; #[cfg(feature = "postgres_backend")] pub type array_shuffle = super::functions::array_shuffle, 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 = super::functions::array_sample, A, N>; + /// Return type of [`to_json(element)`](super::functions::to_json()) #[allow(non_camel_case_types)] #[cfg(feature = "postgres_backend")] diff --git a/diesel_derives/tests/auto_type.rs b/diesel_derives/tests/auto_type.rs index c7d0e2a96761..c8ac04fdd512 100644 --- a/diesel_derives/tests/auto_type.rs +++ b/diesel_derives/tests/auto_type.rs @@ -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), ) }