diesel/expression/functions/window_functions.rs
1#[cfg(doc)]
2use super::aggregate_expressions::WindowExpressionMethods;
3use crate::sql_types::helper::CombinedNullableValue;
4use crate::sql_types::{Integer, IntoNotNullable, IntoNullable, SingleValue, SqlType};
5use diesel_derives::declare_sql_function;
6
7#[declare_sql_function]
8extern "SQL" {
9
10 /// Number of th current row within its partition
11 ///
12 /// Returns the number of the current row within its partition, counting from 1.
13 ///
14 /// This function must be used as window function. You need to call at least one
15 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
16 /// clause. It cannot be used outside of `SELECT` clauses.
17 ///
18 /// ```
19 /// # include!("../../doctest_setup.rs");
20 /// # use diesel::dsl::*;
21 /// #
22 /// # // Reading the query result requires reading memory allocated by the
23 /// # // native sqlite library, which miri cannot track provenance for.
24 /// # #[cfg(miri)]
25 /// # fn main() {}
26 /// # #[cfg(not(miri))]
27 /// # fn main() -> QueryResult<()> {
28 /// # use schema::posts::dsl::*;
29 /// # let connection = &mut establish_connection();
30 /// let res = posts
31 /// .select((title, user_id, row_number().partition_by(user_id)))
32 /// .load::<(String, i32, i64)>(connection)?;
33 /// let expected = vec![
34 /// ("My first post".to_owned(), 1, 1),
35 /// ("About Rust".into(), 1, 2),
36 /// ("My first post too".into(), 2, 1),
37 /// ];
38 /// assert_eq!(expected, res);
39 /// # Ok(())
40 /// # }
41 /// ```
42 #[window]
43 fn row_number() -> BigInt;
44
45 /// Rank of current row within its partition, with gaps
46 ///
47 /// Returns the rank of the current row, with gaps;
48 /// that is, the row_number of the first row in its peer group.
49 ///
50 /// This function must be used as window function. You need to call at least one
51 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
52 /// clause. It cannot be used outside of `SELECT` clauses.
53 ///
54 /// For MySQL this function requires you to call [`.window_order()`](WindowExpressionMethods::window_order())
55 ///
56 /// ```
57 /// # include!("../../doctest_setup.rs");
58 /// # use diesel::dsl::*;
59 /// #
60 /// # // Reading the query result requires reading memory allocated by the
61 /// # // native sqlite library, which miri cannot track provenance for.
62 /// # #[cfg(miri)]
63 /// # fn main() {}
64 /// # #[cfg(not(miri))]
65 /// # fn main() -> QueryResult<()> {
66 /// # use schema::posts::dsl::*;
67 /// # let connection = &mut establish_connection();
68 /// let res = posts
69 /// .select((
70 /// title,
71 /// user_id,
72 /// rank().partition_by(user_id).window_order(user_id),
73 /// ))
74 /// .load::<(String, i32, i64)>(connection)?;
75 /// let expected = vec![
76 /// ("My first post".to_owned(), 1, 1),
77 /// ("About Rust".into(), 1, 1),
78 /// ("My first post too".into(), 2, 1),
79 /// ];
80 /// assert_eq!(expected, res);
81 /// # Ok(())
82 /// # }
83 /// ```
84 #[window(dialect(
85 BuiltInWindowFunctionRequireOrder,
86 crate::backend::sql_dialect::built_in_window_function_require_order::NoOrderRequired
87 ))]
88 #[cfg_attr(
89 feature = "mysql_backend",
90 window(backends(diesel::mysql::Mysql), require_order = true)
91 )]
92 #[cfg_attr(
93 feature = "mariadb_backend",
94 window(backends(diesel::mariadb::Mariadb), require_order = true)
95 )]
96 fn rank() -> BigInt;
97
98 /// Rank of current row within its partition, without gaps
99 ///
100 /// Returns the rank of the current row, without gaps;
101 /// this function effectively counts peer groups.
102 ///
103 /// This function must be used as window function. You need to call at least one
104 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
105 /// clause. It cannot be used outside of `SELECT` clauses.
106 ///
107 /// For MySQL this function requires you to call [`.window_order()`](WindowExpressionMethods::window_order())
108 ///
109 /// ```
110 /// # include!("../../doctest_setup.rs");
111 /// # use diesel::dsl::*;
112 /// #
113 /// # // Reading the query result requires reading memory allocated by the
114 /// # // native sqlite library, which miri cannot track provenance for.
115 /// # #[cfg(miri)]
116 /// # fn main() {}
117 /// # #[cfg(not(miri))]
118 /// # fn main() -> QueryResult<()> {
119 /// # use schema::posts::dsl::*;
120 /// # let connection = &mut establish_connection();
121 /// let res = posts
122 /// .select((
123 /// title,
124 /// user_id,
125 /// dense_rank().partition_by(user_id).window_order(user_id),
126 /// ))
127 /// .load::<(String, i32, i64)>(connection)?;
128 /// let expected = vec![
129 /// ("My first post".to_owned(), 1, 1),
130 /// ("About Rust".into(), 1, 1),
131 /// ("My first post too".into(), 2, 1),
132 /// ];
133 /// assert_eq!(expected, res);
134 /// # Ok(())
135 /// # }
136 /// ```
137 #[window(dialect(
138 BuiltInWindowFunctionRequireOrder,
139 crate::backend::sql_dialect::built_in_window_function_require_order::NoOrderRequired
140 ))]
141 #[cfg_attr(
142 feature = "mysql_backend",
143 window(backends(diesel::mysql::Mysql), require_order = true)
144 )]
145 #[cfg_attr(
146 feature = "mariadb_backend",
147 window(backends(diesel::mariadb::Mariadb), require_order = true)
148 )]
149 fn dense_rank() -> BigInt;
150
151 /// Percentage rank value
152 ///
153 /// Returns the relative rank of the current row,
154 /// that is (rank - 1) / (total partition rows - 1).
155 /// The value thus ranges from 0 to 1 inclusive.
156 ///
157 /// This function must be used as window function. You need to call at least one
158 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
159 /// clause. It cannot be used outside of `SELECT` clauses.
160 ///
161 /// For MySQL this function requires you to call [`.window_order()`](WindowExpressionMethods::window_order())
162 ///
163 /// ```
164 /// # include!("../../doctest_setup.rs");
165 /// # use diesel::dsl::*;
166 /// #
167 /// # // Reading the query result requires reading memory allocated by the
168 /// # // native sqlite library, which miri cannot track provenance for.
169 /// # #[cfg(miri)]
170 /// # fn main() {}
171 /// # #[cfg(not(miri))]
172 /// # fn main() -> QueryResult<()> {
173 /// # use schema::posts::dsl::*;
174 /// # let connection = &mut establish_connection();
175 /// let res = posts
176 /// .select((
177 /// title,
178 /// user_id,
179 /// percent_rank().partition_by(user_id).window_order(user_id),
180 /// ))
181 /// .load::<(String, i32, f64)>(connection)?;
182 /// let expected = vec![
183 /// ("My first post".to_owned(), 1, 0.0),
184 /// ("About Rust".into(), 1, 0.0),
185 /// ("My first post too".into(), 2, 0.0),
186 /// ];
187 /// assert_eq!(expected, res);
188 /// # Ok(())
189 /// # }
190 /// ```
191 #[window(dialect(
192 BuiltInWindowFunctionRequireOrder,
193 crate::backend::sql_dialect::built_in_window_function_require_order::NoOrderRequired
194 ))]
195 #[cfg_attr(
196 feature = "mysql_backend",
197 window(backends(diesel::mysql::Mysql), require_order = true)
198 )]
199 #[cfg_attr(
200 feature = "mariadb_backend",
201 window(backends(diesel::mariadb::Mariadb), require_order = true)
202 )]
203 fn percent_rank() -> Double;
204
205 /// Cumulative distribution value
206 ///
207 /// Returns the cumulative distribution,
208 /// that is (number of partition rows preceding or peers with current row) / (total partition rows).
209 /// The value thus ranges from 1/N to 1.
210 ///
211 /// This function must be used as window function. You need to call at least one
212 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
213 /// clause. It cannot be used outside of `SELECT` clauses.
214 ///
215 /// For MySQL this function requires you to call [`.window_order()`](WindowExpressionMethods::window_order())
216 ///
217 /// ```
218 /// # include!("../../doctest_setup.rs");
219 /// # use diesel::dsl::*;
220 /// #
221 /// # // Reading the query result requires reading memory allocated by the
222 /// # // native sqlite library, which miri cannot track provenance for.
223 /// # #[cfg(miri)]
224 /// # fn main() {}
225 /// # #[cfg(not(miri))]
226 /// # fn main() -> QueryResult<()> {
227 /// # use schema::posts::dsl::*;
228 /// # let connection = &mut establish_connection();
229 /// let res = posts
230 /// .select((
231 /// title,
232 /// user_id,
233 /// cume_dist().partition_by(user_id).window_order(user_id),
234 /// ))
235 /// .load::<(String, i32, f64)>(connection)?;
236 /// let expected = vec![
237 /// ("My first post".to_owned(), 1, 1.0),
238 /// ("About Rust".into(), 1, 1.0),
239 /// ("My first post too".into(), 2, 1.0),
240 /// ];
241 /// assert_eq!(expected, res);
242 /// # Ok(())
243 /// # }
244 /// ```
245 #[window(dialect(
246 BuiltInWindowFunctionRequireOrder,
247 crate::backend::sql_dialect::built_in_window_function_require_order::NoOrderRequired
248 ))]
249 #[cfg_attr(
250 feature = "mysql_backend",
251 window(backends(diesel::mysql::Mysql), require_order = true)
252 )]
253 #[cfg_attr(
254 feature = "mariadb_backend",
255 window(backends(diesel::mariadb::Mariadb), require_order = true)
256 )]
257 fn cume_dist() -> Double;
258
259 /// Bucket number of current row within its partition
260 ///
261 /// Returns an integer ranging from 1 to the argument value,
262 /// dividing the partition as equally as possible.
263 ///
264 ///
265 /// This function must be used as window function. You need to call at least one
266 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
267 /// clause. It cannot be used outside of `SELECT` clauses.
268 ///
269 /// ```
270 /// # include!("../../doctest_setup.rs");
271 /// # use diesel::dsl::*;
272 /// #
273 /// # // Reading the query result requires reading memory allocated by the
274 /// # // native sqlite library, which miri cannot track provenance for.
275 /// # #[cfg(miri)]
276 /// # fn main() {}
277 /// # #[cfg(not(miri))]
278 /// # fn main() -> QueryResult<()> {
279 /// # use schema::posts::dsl::*;
280 /// # let connection = &mut establish_connection();
281 /// let res = posts
282 /// .select((title, user_id, ntile(2).partition_by(user_id)))
283 /// .load::<(String, i32, i32)>(connection)?;
284 /// let expected = vec![
285 /// ("My first post".to_owned(), 1, 1),
286 /// ("About Rust".into(), 1, 2),
287 /// ("My first post too".into(), 2, 1),
288 /// ];
289 /// assert_eq!(expected, res);
290 /// # Ok(())
291 /// # }
292 /// ```
293 #[window]
294 fn ntile(num_buckets: Integer) -> Integer;
295
296 /// Value of argument from row lagging current row within partition
297 ///
298 /// Returns value evaluated at the row that is one row before the current
299 /// row within the partition. If there is no such row, NULL is returned instead.
300 ///
301 /// See [`lag_with_offset`] and [`lag_with_offset_and_default`] for variants with configurable offset
302 /// and default values.
303 ///
304 /// This function must be used as window function. You need to call at least one
305 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
306 /// clause. It cannot be used outside of `SELECT` clauses.
307 ///
308 /// For MySQL this function requires you to call [`.window_order()`](WindowExpressionMethods::window_order())
309 ///
310 /// ```
311 /// # include!("../../doctest_setup.rs");
312 /// # use diesel::dsl::*;
313 /// #
314 /// # // Reading the query result requires reading memory allocated by the
315 /// # // native sqlite library, which miri cannot track provenance for.
316 /// # #[cfg(miri)]
317 /// # fn main() {}
318 /// # #[cfg(not(miri))]
319 /// # fn main() -> QueryResult<()> {
320 /// # use schema::posts::dsl::*;
321 /// # let connection = &mut establish_connection();
322 /// let res = posts
323 /// .select((
324 /// title,
325 /// user_id,
326 /// lag(id).partition_by(user_id).window_order(user_id),
327 /// ))
328 /// .load::<(String, i32, Option<i32>)>(connection)?;
329 /// let expected = vec![
330 /// ("My first post".to_owned(), 1, None),
331 /// ("About Rust".into(), 1, Some(1)),
332 /// ("My first post too".into(), 2, None),
333 /// ];
334 /// assert_eq!(expected, res);
335 /// # Ok(())
336 /// # }
337 /// ```
338 #[window(dialect(
339 BuiltInWindowFunctionRequireOrder,
340 crate::backend::sql_dialect::built_in_window_function_require_order::NoOrderRequired
341 ))]
342 #[cfg_attr(
343 feature = "mysql_backend",
344 window(backends(diesel::mysql::Mysql), require_order = true)
345 )]
346 #[cfg_attr(
347 feature = "mariadb_backend",
348 window(backends(diesel::mariadb::Mariadb), require_order = true)
349 )]
350 fn lag<T: SqlType + SingleValue + IntoNullable<Nullable: SingleValue>>(value: T)
351 -> T::Nullable;
352
353 /// Value of argument from row lagging current row within partition
354 ///
355 /// Returns value evaluated at the row that is offset rows before the current
356 /// row within the partition; If there is no such row, NULL is returned instead.
357 ///
358 /// This function must be used as window function. You need to call at least one
359 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
360 /// clause. It cannot be used outside of `SELECT` clauses.
361 ///
362 /// For MySQL this function requires you to call [`.window_order()`](WindowExpressionMethods::window_order())
363 ///
364 /// ```
365 /// # include!("../../doctest_setup.rs");
366 /// # use diesel::dsl::*;
367 /// #
368 /// # // Reading the query result requires reading memory allocated by the
369 /// # // native sqlite library, which miri cannot track provenance for.
370 /// # #[cfg(miri)]
371 /// # fn main() {}
372 /// # #[cfg(not(miri))]
373 /// # fn main() -> QueryResult<()> {
374 /// # use schema::posts::dsl::*;
375 /// # let connection = &mut establish_connection();
376 /// let res = posts
377 /// .select((
378 /// title,
379 /// user_id,
380 /// lag_with_offset(id, 1)
381 /// .partition_by(user_id)
382 /// .window_order(user_id),
383 /// ))
384 /// .load::<(String, i32, Option<i32>)>(connection)?;
385 /// let expected = vec![
386 /// ("My first post".to_owned(), 1, None),
387 /// ("About Rust".into(), 1, Some(1)),
388 /// ("My first post too".into(), 2, None),
389 /// ];
390 /// assert_eq!(expected, res);
391 /// # Ok(())
392 /// # }
393 /// ```
394 #[doc(alias = "lag")]
395 #[sql_name = "lag"]
396 #[window(dialect(
397 BuiltInWindowFunctionRequireOrder,
398 crate::backend::sql_dialect::built_in_window_function_require_order::NoOrderRequired
399 ))]
400 #[cfg_attr(
401 feature = "mysql_backend",
402 window(backends(diesel::mysql::Mysql), require_order = true)
403 )]
404 #[cfg_attr(
405 feature = "mariadb_backend",
406 window(backends(diesel::mariadb::Mariadb), require_order = true)
407 )]
408 fn lag_with_offset<T: SqlType + SingleValue + IntoNullable<Nullable: SingleValue>>(
409 value: T,
410 offset: Integer,
411 ) -> T::Nullable;
412
413 /// Value of argument from row lagging current row within partition
414 ///
415 /// Returns value evaluated at the row that is offset rows before the current
416 /// row within the partition; if there is no such row, instead returns default
417 /// (which must be of a type compatible with value).
418 /// Both offset and default are evaluated with respect to the current row.
419 /// If omitted, offset defaults to 1 and default to NULL.
420 ///
421 /// This function returns a nullable value if either the value or the default expression are
422 /// nullable.
423 ///
424 /// This function must be used as window function. You need to call at least one
425 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
426 /// clause. It cannot be used outside of `SELECT` clauses.
427 ///
428 /// For MySQL this function requires you to call [`.window_order()`](WindowExpressionMethods::window_order())
429 ///
430 /// ```
431 /// # include!("../../doctest_setup.rs");
432 /// # use diesel::dsl::*;
433 /// #
434 /// # // Reading the query result requires reading memory allocated by the
435 /// # // native sqlite library, which miri cannot track provenance for.
436 /// # #[cfg(not(feature = "mariadb"))]
437 /// # #[cfg(miri)]
438 /// # fn main() {}
439 /// # #[cfg(all(not(miri), not(feature = "mariadb")))]
440 /// # fn main() -> QueryResult<()> {
441 /// # use schema::posts::dsl::*;
442 /// # use diesel::sql_types::{Integer, Nullable};
443 /// # let connection = &mut establish_connection();
444 /// let res = posts
445 /// .select((
446 /// title,
447 /// user_id,
448 /// lag_with_offset_and_default(id, 1, user_id)
449 /// .partition_by(user_id)
450 /// .window_order(user_id),
451 /// ))
452 /// .load::<(String, i32, i32)>(connection)?;
453 /// let expected = vec![
454 /// ("My first post".to_owned(), 1, 1),
455 /// ("About Rust".into(), 1, 1),
456 /// ("My first post too".into(), 2, 2),
457 /// ];
458 /// assert_eq!(expected, res);
459 ///
460 /// let res = posts
461 /// .select((
462 /// title,
463 /// user_id,
464 /// lag_with_offset_and_default(None::<i32>.into_sql::<Nullable<Integer>>(), 1, user_id)
465 /// .partition_by(user_id)
466 /// .window_order(user_id),
467 /// ))
468 /// .load::<(String, i32, Option<i32>)>(connection)?;
469 /// let expected = vec![
470 /// ("My first post".to_owned(), 1, Some(1)),
471 /// ("About Rust".into(), 1, None),
472 /// ("My first post too".into(), 2, Some(2)),
473 /// ];
474 /// assert_eq!(expected, res);
475 ///
476 /// let res = posts
477 /// .select((
478 /// title,
479 /// user_id,
480 /// lag_with_offset_and_default(id, 1, None::<i32>.into_sql::<Nullable<Integer>>())
481 /// .partition_by(user_id)
482 /// .window_order(user_id),
483 /// ))
484 /// .load::<(String, i32, Option<i32>)>(connection)?;
485 /// let expected = vec![
486 /// ("My first post".to_owned(), 1, None),
487 /// ("About Rust".into(), 1, Some(1)),
488 /// ("My first post too".into(), 2, None),
489 /// ];
490 /// assert_eq!(expected, res);
491 /// # Ok(())
492 /// # }
493 /// # #[cfg(feature = "mariadb")]
494 /// # fn main() {}
495 /// ```
496 #[doc(alias = "lag")]
497 #[sql_name = "lag"]
498 #[window(dialect(
499 BuiltInWindowFunctionRequireOrder,
500 crate::backend::sql_dialect::built_in_window_function_require_order::NoOrderRequired
501 ))]
502 #[cfg_attr(
503 feature = "mysql_backend",
504 window(backends(diesel::mysql::Mysql), require_order = true)
505 )]
506 #[cfg_attr(
507 feature = "mariadb_backend",
508 window(backends(diesel::mariadb::Mariadb), require_order = true)
509 )]
510 fn lag_with_offset_and_default<
511 T: SqlType
512 + SingleValue
513 + IntoNotNullable<NotNullable: self::private::SameType<T2::NotNullable>>
514 + CombinedNullableValue<T2, T::NotNullable>,
515 T2: SqlType + SingleValue + IntoNotNullable,
516 >(
517 value: T,
518 offset: Integer,
519 default: T2,
520 ) -> T::Out;
521
522 /// Value of argument from row leading current row within partition
523 ///
524 /// Returns value evaluated at the row that is offset rows after the current
525 /// row within the partition; if there is no such row,
526 /// `NULL` will be returned instead.
527 ///
528 /// See [`lead_with_offset`] and [`lead_with_offset_and_default`] for variants with configurable offset
529 /// and default values.
530 ///
531 /// This function must be used as window function. You need to call at least one
532 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
533 /// clause. It cannot be used outside of `SELECT` clauses.
534 ///
535 /// For MySQL this function requires you to call [`.window_order()`](WindowExpressionMethods::window_order())
536 ///
537 /// ```
538 /// # include!("../../doctest_setup.rs");
539 /// # use diesel::dsl::*;
540 /// #
541 /// # // Reading the query result requires reading memory allocated by the
542 /// # // native sqlite library, which miri cannot track provenance for.
543 /// # #[cfg(miri)]
544 /// # fn main() {}
545 /// # #[cfg(not(miri))]
546 /// # fn main() -> QueryResult<()> {
547 /// # use schema::posts::dsl::*;
548 /// # let connection = &mut establish_connection();
549 /// let res = posts
550 /// .select((
551 /// title,
552 /// user_id,
553 /// lead(id).partition_by(user_id).window_order(user_id),
554 /// ))
555 /// .load::<(String, i32, Option<i32>)>(connection)?;
556 /// let expected = vec![
557 /// ("My first post".to_owned(), 1, Some(2)),
558 /// ("About Rust".into(), 1, None),
559 /// ("My first post too".into(), 2, None),
560 /// ];
561 /// assert_eq!(expected, res);
562 /// # Ok(())
563 /// # }
564 /// ```
565 #[window(dialect(
566 BuiltInWindowFunctionRequireOrder,
567 crate::backend::sql_dialect::built_in_window_function_require_order::NoOrderRequired
568 ))]
569 #[cfg_attr(
570 feature = "mysql_backend",
571 window(backends(diesel::mysql::Mysql), require_order = true)
572 )]
573 #[cfg_attr(
574 feature = "mariadb_backend",
575 window(backends(diesel::mariadb::Mariadb), require_order = true)
576 )]
577 fn lead<T: SqlType + SingleValue + IntoNullable<Nullable: SingleValue>>(
578 value: T,
579 ) -> T::Nullable;
580
581 /// Value of argument from row leading current row within partition
582 ///
583 /// Returns value evaluated at the row that is offset rows after the current
584 /// row within the partition; if there is no such row,
585 /// `NULL` is returned instead
586 ///
587 ///
588 /// This function must be used as window function. You need to call at least one
589 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
590 /// clause. It cannot be used outside of `SELECT` clauses.
591 ///
592 /// For MySQL this function requires you to call [`.window_order()`](WindowExpressionMethods::window_order())
593 ///
594 /// ```
595 /// # include!("../../doctest_setup.rs");
596 /// # use diesel::dsl::*;
597 /// #
598 /// # // Reading the query result requires reading memory allocated by the
599 /// # // native sqlite library, which miri cannot track provenance for.
600 /// # #[cfg(miri)]
601 /// # fn main() {}
602 /// # #[cfg(not(miri))]
603 /// # fn main() -> QueryResult<()> {
604 /// # use schema::posts::dsl::*;
605 /// # let connection = &mut establish_connection();
606 /// let res = posts
607 /// .select((
608 /// title,
609 /// user_id,
610 /// lead_with_offset(id, 1)
611 /// .partition_by(user_id)
612 /// .window_order(user_id),
613 /// ))
614 /// .load::<(String, i32, Option<i32>)>(connection)?;
615 /// let expected = vec![
616 /// ("My first post".to_owned(), 1, Some(2)),
617 /// ("About Rust".into(), 1, None),
618 /// ("My first post too".into(), 2, None),
619 /// ];
620 /// assert_eq!(expected, res);
621 /// # Ok(())
622 /// # }
623 /// ```
624 #[doc(alias = "lead")]
625 #[sql_name = "lead"]
626 #[window(dialect(
627 BuiltInWindowFunctionRequireOrder,
628 crate::backend::sql_dialect::built_in_window_function_require_order::NoOrderRequired
629 ))]
630 #[cfg_attr(
631 feature = "mysql_backend",
632 window(backends(diesel::mysql::Mysql), require_order = true)
633 )]
634 #[cfg_attr(
635 feature = "mariadb_backend",
636 window(backends(diesel::mariadb::Mariadb), require_order = true)
637 )]
638 fn lead_with_offset<T: SqlType + SingleValue + IntoNullable<Nullable: SingleValue>>(
639 value: T,
640 offset: Integer,
641 ) -> T::Nullable;
642
643 /// Value of argument from row leading current row within partition
644 ///
645 /// Returns value evaluated at the row that is offset rows after the current
646 /// row within the partition; if there is no such row,
647 /// instead returns default (which must be of a type compatible with value).
648 /// Both offset and default are evaluated with respect to the current row.
649 /// If omitted, offset defaults to 1 and default to NULL.
650 ///
651 /// This function returns a nullable value if either the value or the default expression are
652 /// nullable.
653 ///
654 /// This function must be used as window function. You need to call at least one
655 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
656 /// clause. It cannot be used outside of `SELECT` clauses.
657 ///
658 /// For MySQL this function requires you to call [`.window_order()`](WindowExpressionMethods::window_order())
659 ///
660 /// ```
661 /// # include!("../../doctest_setup.rs");
662 /// # use diesel::dsl::*;
663 /// #
664 /// # // Reading the query result requires reading memory allocated by the
665 /// # // native sqlite library, which miri cannot track provenance for.
666 /// # #[cfg(not(feature = "mariadb"))]
667 /// # #[cfg(miri)]
668 /// # fn main() {}
669 /// # #[cfg(all(not(miri), not(feature = "mariadb")))]
670 /// # fn main() -> QueryResult<()> {
671 /// # use schema::posts::dsl::*;
672 /// # use diesel::sql_types::{Integer, Nullable};
673 /// # let connection = &mut establish_connection();
674 /// let res = posts
675 /// .select((
676 /// title,
677 /// user_id,
678 /// lead_with_offset_and_default(id, 1, user_id)
679 /// .partition_by(user_id)
680 /// .window_order(user_id),
681 /// ))
682 /// .load::<(String, i32, i32)>(connection)?;
683 /// let expected = vec![
684 /// ("My first post".to_owned(), 1, 2),
685 /// ("About Rust".into(), 1, 1),
686 /// ("My first post too".into(), 2, 2),
687 /// ];
688 /// assert_eq!(expected, res);
689 ///
690 /// let res = posts
691 /// .select((
692 /// title,
693 /// user_id,
694 /// lead_with_offset_and_default(None::<i32>.into_sql::<Nullable<Integer>>(), 1, user_id)
695 /// .partition_by(user_id)
696 /// .window_order(user_id),
697 /// ))
698 /// .load::<(String, i32, Option<i32>)>(connection)?;
699 /// let expected = vec![
700 /// ("My first post".to_owned(), 1, None),
701 /// ("About Rust".into(), 1, Some(1)),
702 /// ("My first post too".into(), 2, Some(2)),
703 /// ];
704 /// assert_eq!(expected, res);
705 ///
706 /// let res = posts
707 /// .select((
708 /// title,
709 /// user_id,
710 /// lead_with_offset_and_default(id, 1, None::<i32>.into_sql::<Nullable<Integer>>())
711 /// .partition_by(user_id)
712 /// .window_order(user_id),
713 /// ))
714 /// .load::<(String, i32, Option<i32>)>(connection)?;
715 /// let expected = vec![
716 /// ("My first post".to_owned(), 1, Some(2)),
717 /// ("About Rust".into(), 1, None),
718 /// ("My first post too".into(), 2, None),
719 /// ];
720 /// assert_eq!(expected, res);
721 /// # Ok(())
722 /// # }
723 /// # #[cfg(feature = "mariadb")]
724 /// # fn main() {}
725 /// ```
726 #[doc(alias = "lead")]
727 #[sql_name = "lead"]
728 #[window(dialect(
729 BuiltInWindowFunctionRequireOrder,
730 crate::backend::sql_dialect::built_in_window_function_require_order::NoOrderRequired
731 ))]
732 #[cfg_attr(
733 feature = "mysql_backend",
734 window(backends(diesel::mysql::Mysql), require_order = true)
735 )]
736 #[cfg_attr(
737 feature = "mariadb_backend",
738 window(backends(diesel::mariadb::Mariadb), require_order = true)
739 )]
740 fn lead_with_offset_and_default<
741 T: SqlType
742 + SingleValue
743 + IntoNotNullable<NotNullable: self::private::SameType<T2::NotNullable>>
744 + CombinedNullableValue<T2, T::NotNullable>,
745 T2: SqlType + SingleValue + IntoNotNullable,
746 >(
747 value: T,
748 offset: Integer,
749 default: T2,
750 ) -> T::Out;
751
752 /// Value of argument from first row of window frame
753 ///
754 /// Returns value evaluated at the row that is the first row of the window frame.
755 ///
756 ///
757 /// This function must be used as window function. You need to call at least one
758 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
759 /// clause. It cannot be used outside of `SELECT` clauses.
760 ///
761 /// ```
762 /// # include!("../../doctest_setup.rs");
763 /// # use diesel::dsl::*;
764 /// #
765 /// # // Reading the query result requires reading memory allocated by the
766 /// # // native sqlite library, which miri cannot track provenance for.
767 /// # #[cfg(miri)]
768 /// # fn main() {}
769 /// # #[cfg(not(miri))]
770 /// # fn main() -> QueryResult<()> {
771 /// # use schema::posts::dsl::*;
772 /// # let connection = &mut establish_connection();
773 /// let res = posts
774 /// .select((title, user_id, first_value(id).partition_by(user_id)))
775 /// .load::<(String, i32, i32)>(connection)?;
776 /// let expected = vec![
777 /// ("My first post".to_owned(), 1, 1),
778 /// ("About Rust".into(), 1, 1),
779 /// ("My first post too".into(), 2, 3),
780 /// ];
781 /// assert_eq!(expected, res);
782 /// # Ok(())
783 /// # }
784 /// ```
785 #[window]
786 fn first_value<T: SqlType + SingleValue>(value: T) -> T;
787
788 /// Value of argument from last row of window frame
789 ///
790 /// Returns value evaluated at the row that is the last row of the window frame.
791 ///
792 ///
793 /// This function must be used as window function. You need to call at least one
794 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
795 /// clause. It cannot be used outside of `SELECT` clauses.
796 ///
797 /// ```
798 /// # include!("../../doctest_setup.rs");
799 /// # use diesel::dsl::*;
800 /// #
801 /// # // Reading the query result requires reading memory allocated by the
802 /// # // native sqlite library, which miri cannot track provenance for.
803 /// # #[cfg(miri)]
804 /// # fn main() {}
805 /// # #[cfg(not(miri))]
806 /// # fn main() -> QueryResult<()> {
807 /// # use schema::posts::dsl::*;
808 /// # let connection = &mut establish_connection();
809 /// let res = posts
810 /// .select((title, user_id, last_value(id).partition_by(user_id)))
811 /// .load::<(String, i32, i32)>(connection)?;
812 /// let expected = vec![
813 /// ("My first post".to_owned(), 1, 2),
814 /// ("About Rust".into(), 1, 2),
815 /// ("My first post too".into(), 2, 3),
816 /// ];
817 /// assert_eq!(expected, res);
818 /// # Ok(())
819 /// # }
820 /// ```
821 #[window]
822 fn last_value<T: SqlType + SingleValue>(value: T) -> T;
823
824 /// Value of argument from N-th row of window frame
825 ///
826 /// Returns value evaluated at the row that is the n'th row of the window frame (counting from 1);
827 /// returns NULL if there is no such row.
828 ///
829 ///
830 /// This function must be used as window function. You need to call at least one
831 /// of the methods [`WindowExpressionMethods`] from to use this function in your `SELECT`
832 /// clause. It cannot be used outside of `SELECT` clauses.
833 ///
834 /// ```
835 /// # include!("../../doctest_setup.rs");
836 /// # use diesel::dsl::*;
837 /// #
838 /// # // Reading the query result requires reading memory allocated by the
839 /// # // native sqlite library, which miri cannot track provenance for.
840 /// # #[cfg(miri)]
841 /// # fn main() {}
842 /// # #[cfg(not(miri))]
843 /// # fn main() -> QueryResult<()> {
844 /// # use schema::posts::dsl::*;
845 /// # let connection = &mut establish_connection();
846 /// let res = posts
847 /// .select((title, user_id, nth_value(id, 2).partition_by(user_id)))
848 /// .load::<(String, i32, Option<i32>)>(connection)?;
849 /// let expected = vec![
850 /// ("My first post".to_owned(), 1, Some(2)),
851 /// ("About Rust".into(), 1, Some(2)),
852 /// ("My first post too".into(), 2, None),
853 /// ];
854 /// assert_eq!(expected, res);
855 /// # Ok(())
856 /// # }
857 /// ```
858 #[window]
859 fn nth_value<T: SqlType + SingleValue + IntoNullable<Nullable: SingleValue>>(
860 value: T,
861 n: Integer,
862 ) -> T::Nullable;
863}
864
865mod private {
866 pub trait SameType<T> {}
867
868 impl<T> SameType<T> for T {}
869}