use crate::expression::{Expression, ValidGrouping}; use crate::pg::Pg; use crate::query_builder::{AsQuery, AstPass, FromClause, QueryFragment, QueryId, SelectStatement}; use crate::query_source::QuerySource; use crate::result::QueryResult; use crate::sql_types::{Double, SmallInt}; use crate::{JoinTo, SelectableExpression, Table}; use std::marker::PhantomData; #[doc(hidden)] pub trait TablesampleMethod: Clone { fn method_name_sql() -> &'static str; } #[derive(Clone, Copy, Debug)] /// Used to specify the `BERNOULLI` sampling method. pub struct BernoulliMethod; impl TablesampleMethod for BernoulliMethod { fn method_name_sql() -> &'static str { "BERNOULLI" } } #[derive(Clone, Copy, Debug)] /// Used to specify the `SYSTEM` sampling method. pub struct SystemMethod; impl TablesampleMethod for SystemMethod { fn method_name_sql() -> &'static str { "SYSTEM" } } /// Represents a query with a `TABLESAMPLE` clause. #[derive(Debug, Clone, Copy)] pub struct Tablesample where TSM: TablesampleMethod, { source: S, method: PhantomData, portion: i16, seed: Option, } impl Tablesample where TSM: TablesampleMethod, { pub(crate) fn new(source: S, portion: i16) -> Tablesample { Tablesample { source, method: PhantomData, portion, seed: None, } } /// This method allows you to specify the random number generator seed to use in the sampling /// method. This allows you to obtain repeatable results. pub fn with_seed(self, seed: f64) -> Tablesample { Tablesample { source: self.source, method: self.method, portion: self.portion, seed: Some(seed), } } } impl QueryId for Tablesample where S: QueryId, TSM: TablesampleMethod, { type QueryId = (); const HAS_STATIC_QUERY_ID: bool = false; } impl QuerySource for Tablesample where S: Table + Clone, TSM: TablesampleMethod, ::DefaultSelection: ValidGrouping<()> + SelectableExpression>, { type FromClause = Self; type DefaultSelection = ::DefaultSelection; fn from_clause(&self) -> Self::FromClause { self.clone() } fn default_selection(&self) -> Self::DefaultSelection { self.source.default_selection() } } impl QueryFragment for Tablesample where S: QueryFragment, TSM: TablesampleMethod, { fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> { self.source.walk_ast(out.reborrow())?; out.push_sql(" TABLESAMPLE "); out.push_sql(TSM::method_name_sql()); out.push_sql("("); out.push_bind_param::(&self.portion)?; out.push_sql(")"); if let Some(f) = &self.seed { out.push_sql(" REPEATABLE("); out.push_bind_param::(f)?; out.push_sql(")"); } Ok(()) } } impl AsQuery for Tablesample where S: Table + Clone, TSM: TablesampleMethod, ::DefaultSelection: ValidGrouping<()> + SelectableExpression>, { type SqlType = <::DefaultSelection as Expression>::SqlType; type Query = SelectStatement>; fn as_query(self) -> Self::Query { SelectStatement::simple(self) } } impl JoinTo for Tablesample where S: JoinTo, T: Table, S: Table, TSM: TablesampleMethod, { type FromClause = >::FromClause; type OnClause = >::OnClause; fn join_target(rhs: T) -> (Self::FromClause, Self::OnClause) { >::join_target(rhs) } } impl Table for Tablesample where S: Table + Clone + AsQuery, TSM: TablesampleMethod, ::PrimaryKey: SelectableExpression>, ::AllColumns: SelectableExpression>, ::DefaultSelection: ValidGrouping<()> + SelectableExpression>, { type PrimaryKey = ::PrimaryKey; type AllColumns = ::AllColumns; fn primary_key(&self) -> Self::PrimaryKey { self.source.primary_key() } fn all_columns() -> Self::AllColumns { S::all_columns() } } #[cfg(test)] mod test { use super::*; use crate::backend::Backend; use crate::query_builder::QueryBuilder; use diesel::dsl::*; use diesel::*; macro_rules! assert_sql { ($query:expr, $sql:expr) => { let mut query_builder = ::QueryBuilder::default(); $query.to_sql(&mut query_builder, &Pg).unwrap(); let sql = query_builder.finish(); assert_eq!(sql, $sql); }; } table! { users { id -> Integer, name -> VarChar, } } #[diesel_test_helper::test] fn test_generated_tablesample_sql() { assert_sql!( users::table.tablesample_bernoulli(10), "\"users\" TABLESAMPLE BERNOULLI($1)" ); assert_sql!( users::table.tablesample_system(10), "\"users\" TABLESAMPLE SYSTEM($1)" ); assert_sql!( users::table.tablesample_system(10).with_seed(42.0), "\"users\" TABLESAMPLE SYSTEM($1) REPEATABLE($2)" ); } }