From f9a691f83878ca648716103cd52403fdb1668c36 Mon Sep 17 00:00:00 2001 From: Ali Tariq Date: Fri, 6 Sep 2024 16:02:15 +0500 Subject: [PATCH 1/2] implement array_sample --- diesel/src/pg/expression/functions.rs | 40 +++++++++++++++++++++++- diesel/src/pg/expression/helper_types.rs | 5 +++ diesel_derives/tests/auto_type.rs | 1 + 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 9e349534b5f4..e541b45b03f5 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,44 @@ 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}; + /// # 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()); + /// # 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), ) } From a3422f69508e1621ee1f8c00e2eeaa11c6b51b72 Mon Sep 17 00:00:00 2001 From: "ali.tariq" Date: Tue, 10 Sep 2024 11:00:54 +0500 Subject: [PATCH 2/2] added doc test for null input --- diesel/src/pg/expression/functions.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index e541b45b03f5..8f0aa7de6d1e 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -1474,7 +1474,7 @@ define_sql_function! { /// # /// # fn run_test() -> QueryResult<()> { /// # use diesel::dsl::array_sample; - /// # use diesel::sql_types::{Array, Integer}; + /// # use diesel::sql_types::{Array, Integer, Nullable}; /// # let connection = &mut establish_connection(); /// /// let vec = vec![1,2,3,4,5]; @@ -1487,10 +1487,14 @@ define_sql_function! { /// 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; + fn array_sample(array: Arr, n: Integer) -> Arr; } #[cfg(feature = "postgres_backend")]