diesel/expression/functions/aggregate_ordering.rs
1use self::private::SqlOrdAggregate;
2#[cfg(doc)]
3use super::aggregate_expressions::{AggregateExpressionMethods, WindowExpressionMethods};
4use crate::expression::functions::declare_sql_function;
5
6#[declare_sql_function]
7extern "SQL" {
8 /// Represents a SQL `MAX` function. This function can only take types which are
9 /// ordered.
10 ///
11 /// ## Window Function Usage
12 ///
13 /// This function can be used as window function. See [`WindowExpressionMethods`] for details
14 ///
15 /// ## Aggregate Function Expression
16 ///
17 /// This function can be used as aggregate expression. See [`AggregateExpressionMethods`] for details.
18 ///
19 /// # Examples
20 ///
21 /// ## Normal function usage
22 ///
23 /// ```rust
24 /// # include!("../../doctest_setup.rs");
25 /// # use diesel::dsl::*;
26 /// #
27 /// # fn main() {
28 /// # use schema::animals::dsl::*;
29 /// # let connection = &mut establish_connection();
30 /// assert_eq!(Ok(Some(8)), animals.select(max(legs)).first(connection));
31 /// # }
32 /// ```
33 ///
34 /// ## Window function
35 ///
36 /// ```rust
37 /// # include!("../../doctest_setup.rs");
38 /// # use diesel::dsl::*;
39 /// #
40 /// # // Reading the query result requires reading memory allocated by the
41 /// # // native sqlite library, which miri cannot track provenance for.
42 /// # #[cfg(miri)]
43 /// # fn main() {}
44 /// # #[cfg(not(miri))]
45 /// # fn main() {
46 /// # use schema::animals::dsl::*;
47 /// # let connection = &mut establish_connection();
48 /// let res = animals
49 /// .select((name, max(legs).partition_by(id)))
50 /// .load::<(Option<String>, Option<i32>)>(connection);
51 ///
52 /// assert_eq!(
53 /// Ok(vec![(Some("Jack".into()), Some(4)), (None, Some(8))]),
54 /// res
55 /// );
56 /// # }
57 /// ```
58 ///
59 ///
60 /// ## Aggregate function expression
61 ///
62 /// ```rust
63 /// # include!("../../doctest_setup.rs");
64 /// # use diesel::dsl::*;
65 /// #
66 /// # fn main() {
67 /// # use schema::animals::dsl::*;
68 /// # let connection = &mut establish_connection();
69 /// # #[cfg(not(any(feature = "mysql", feature = "mariadb")))]
70 /// assert_eq!(
71 /// Ok(Some(4)),
72 /// animals
73 /// .select(max(legs).aggregate_filter(legs.lt(8)))
74 /// .first(connection)
75 /// );
76 /// # }
77 /// ```
78 #[aggregate]
79 #[window]
80 fn max<ST: SqlOrdAggregate>(expr: ST) -> ST::Ret;
81
82 /// Represents a SQL `MIN` function. This function can only take types which are
83 /// ordered.
84 ///
85 /// ## Window Function Usage
86 ///
87 /// This function can be used as window function. See [`WindowExpressionMethods`] for details
88 ///
89 /// ## Aggregate Function Expression
90 ///
91 /// This function can be used as aggregate expression. See [`AggregateExpressionMethods`] for details.
92 ///
93 /// # Examples
94 ///
95 /// ## Normal function usage
96 ///
97 /// ```rust
98 /// # include!("../../doctest_setup.rs");
99 /// # use diesel::dsl::*;
100 /// #
101 /// # fn main() {
102 /// # use schema::animals::dsl::*;
103 /// # let connection = &mut establish_connection();
104 /// assert_eq!(Ok(Some(4)), animals.select(min(legs)).first(connection));
105 /// # }
106 /// ```
107 ///
108 /// ## Window function
109 ///
110 /// ```rust
111 /// # include!("../../doctest_setup.rs");
112 /// # use diesel::dsl::*;
113 /// #
114 /// # // Reading the query result requires reading memory allocated by the
115 /// # // native sqlite library, which miri cannot track provenance for.
116 /// # #[cfg(miri)]
117 /// # fn main() {}
118 /// # #[cfg(not(miri))]
119 /// # fn main() {
120 /// # use schema::animals::dsl::*;
121 /// # let connection = &mut establish_connection();
122 /// let res = animals
123 /// .select((name, min(legs).partition_by(id)))
124 /// .load::<(Option<String>, Option<i32>)>(connection);
125 ///
126 /// assert_eq!(
127 /// Ok(vec![(Some("Jack".into()), Some(4)), (None, Some(8))]),
128 /// res
129 /// );
130 /// # }
131 /// ```
132 ///
133 /// ## Aggregate function expression
134 ///
135 /// ```rust
136 /// # include!("../../doctest_setup.rs");
137 /// # use diesel::dsl::*;
138 /// #
139 /// # fn main() {
140 /// # use schema::animals::dsl::*;
141 /// # let connection = &mut establish_connection();
142 /// # #[cfg(not(any(feature = "mysql", feature = "mariadb")))]
143 /// assert_eq!(
144 /// Ok(Some(8)),
145 /// animals
146 /// .select(min(legs).aggregate_filter(legs.gt(4)))
147 /// .first(connection)
148 /// );
149 /// # }
150 /// ```
151 #[aggregate]
152 #[window]
153 fn min<ST: SqlOrdAggregate>(expr: ST) -> ST::Ret;
154}
155
156mod private {
157 use crate::sql_types::{IntoNullable, SingleValue, SqlOrd, SqlType};
158 pub trait SqlOrdAggregate: SingleValue {
159 type Ret: SqlType + SingleValue;
160 }
161
162 impl<T> SqlOrdAggregate for T
163 where
164 T: SqlOrd + IntoNullable + SingleValue,
165 T::Nullable: SqlType + SingleValue,
166 {
167 type Ret = T::Nullable;
168 }
169}