/Users/andrewlamb/Software/datafusion/datafusion/expr/src/window_function.rs
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | use datafusion_common::ScalarValue; |
19 | | |
20 | | use crate::{expr::WindowFunction, BuiltInWindowFunction, Expr, Literal}; |
21 | | |
22 | | /// Create an expression to represent the `rank` window function |
23 | | pub fn rank() -> Expr { |
24 | | Expr::WindowFunction(WindowFunction::new(BuiltInWindowFunction::Rank, vec![])) |
25 | | } |
26 | | |
27 | | /// Create an expression to represent the `dense_rank` window function |
28 | | pub fn dense_rank() -> Expr { |
29 | | Expr::WindowFunction(WindowFunction::new( |
30 | | BuiltInWindowFunction::DenseRank, |
31 | | vec![], |
32 | | )) |
33 | | } |
34 | | |
35 | | /// Create an expression to represent the `percent_rank` window function |
36 | | pub fn percent_rank() -> Expr { |
37 | | Expr::WindowFunction(WindowFunction::new( |
38 | | BuiltInWindowFunction::PercentRank, |
39 | | vec![], |
40 | | )) |
41 | | } |
42 | | |
43 | | /// Create an expression to represent the `cume_dist` window function |
44 | | pub fn cume_dist() -> Expr { |
45 | | Expr::WindowFunction(WindowFunction::new(BuiltInWindowFunction::CumeDist, vec![])) |
46 | | } |
47 | | |
48 | | /// Create an expression to represent the `ntile` window function |
49 | | pub fn ntile(arg: Expr) -> Expr { |
50 | | Expr::WindowFunction(WindowFunction::new(BuiltInWindowFunction::Ntile, vec![arg])) |
51 | | } |
52 | | |
53 | | /// Create an expression to represent the `lag` window function |
54 | | pub fn lag( |
55 | | arg: Expr, |
56 | | shift_offset: Option<i64>, |
57 | | default_value: Option<ScalarValue>, |
58 | | ) -> Expr { |
59 | | let shift_offset_lit = shift_offset |
60 | 0 | .map(|v| v.lit()) |
61 | | .unwrap_or(ScalarValue::Null.lit()); |
62 | | let default_lit = default_value.unwrap_or(ScalarValue::Null).lit(); |
63 | | Expr::WindowFunction(WindowFunction::new( |
64 | | BuiltInWindowFunction::Lag, |
65 | | vec![arg, shift_offset_lit, default_lit], |
66 | | )) |
67 | | } |
68 | | |
69 | | /// Create an expression to represent the `lead` window function |
70 | | pub fn lead( |
71 | | arg: Expr, |
72 | | shift_offset: Option<i64>, |
73 | | default_value: Option<ScalarValue>, |
74 | | ) -> Expr { |
75 | | let shift_offset_lit = shift_offset |
76 | 0 | .map(|v| v.lit()) |
77 | | .unwrap_or(ScalarValue::Null.lit()); |
78 | | let default_lit = default_value.unwrap_or(ScalarValue::Null).lit(); |
79 | | Expr::WindowFunction(WindowFunction::new( |
80 | | BuiltInWindowFunction::Lead, |
81 | | vec![arg, shift_offset_lit, default_lit], |
82 | | )) |
83 | | } |
84 | | |
85 | | /// Create an expression to represent the `nth_value` window function |
86 | | pub fn nth_value(arg: Expr, n: i64) -> Expr { |
87 | | Expr::WindowFunction(WindowFunction::new( |
88 | | BuiltInWindowFunction::NthValue, |
89 | | vec![arg, n.lit()], |
90 | | )) |
91 | | } |