|
17 | 17 |
|
18 | 18 | //! Built-in functions module contains all the built-in functions definitions. |
19 | 19 |
|
20 | | -use std::fmt; |
21 | | -use std::str::FromStr; |
22 | | - |
23 | | -use crate::type_coercion::functions::data_types; |
24 | | -use crate::utils; |
25 | | -use crate::{Signature, Volatility}; |
26 | | -use datafusion_common::{plan_datafusion_err, plan_err, DataFusionError, Result}; |
27 | | - |
28 | | -use arrow::datatypes::DataType; |
29 | | - |
30 | 20 | use strum_macros::EnumIter; |
31 | 21 |
|
32 | | -impl fmt::Display for BuiltInWindowFunction { |
33 | | - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
34 | | - write!(f, "{}", self.name()) |
35 | | - } |
36 | | -} |
37 | | - |
38 | 22 | /// A [window function] built in to DataFusion |
39 | 23 | /// |
40 | 24 | /// [Window Function]: https://en.wikipedia.org/wiki/Window_function_(SQL) |
41 | 25 | #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, EnumIter)] |
42 | 26 | pub enum BuiltInWindowFunction { |
43 | | - /// returns value evaluated at the row that is the first row of the window frame |
44 | | - FirstValue, |
45 | | - /// Returns value evaluated at the row that is the last row of the window frame |
46 | | - LastValue, |
47 | | - /// Returns value evaluated at the row that is the nth row of the window frame (counting from 1); returns null if no such row |
48 | | - NthValue, |
49 | | -} |
50 | | - |
51 | | -impl BuiltInWindowFunction { |
52 | | - pub fn name(&self) -> &str { |
53 | | - use BuiltInWindowFunction::*; |
54 | | - match self { |
55 | | - FirstValue => "first_value", |
56 | | - LastValue => "last_value", |
57 | | - NthValue => "NTH_VALUE", |
58 | | - } |
59 | | - } |
60 | | -} |
61 | | - |
62 | | -impl FromStr for BuiltInWindowFunction { |
63 | | - type Err = DataFusionError; |
64 | | - fn from_str(name: &str) -> Result<BuiltInWindowFunction> { |
65 | | - Ok(match name.to_uppercase().as_str() { |
66 | | - "FIRST_VALUE" => BuiltInWindowFunction::FirstValue, |
67 | | - "LAST_VALUE" => BuiltInWindowFunction::LastValue, |
68 | | - "NTH_VALUE" => BuiltInWindowFunction::NthValue, |
69 | | - _ => return plan_err!("There is no built-in window function named {name}"), |
70 | | - }) |
71 | | - } |
72 | | -} |
73 | | - |
74 | | -/// Returns the datatype of the built-in window function |
75 | | -impl BuiltInWindowFunction { |
76 | | - pub fn return_type(&self, input_expr_types: &[DataType]) -> Result<DataType> { |
77 | | - // Note that this function *must* return the same type that the respective physical expression returns |
78 | | - // or the execution panics. |
79 | | - |
80 | | - // Verify that this is a valid set of data types for this function |
81 | | - data_types(input_expr_types, &self.signature()) |
82 | | - // Original errors are all related to wrong function signature |
83 | | - // Aggregate them for better error message |
84 | | - .map_err(|_| { |
85 | | - plan_datafusion_err!( |
86 | | - "{}", |
87 | | - utils::generate_signature_error_msg( |
88 | | - &format!("{self}"), |
89 | | - self.signature(), |
90 | | - input_expr_types, |
91 | | - ) |
92 | | - ) |
93 | | - })?; |
94 | | - |
95 | | - match self { |
96 | | - BuiltInWindowFunction::FirstValue |
97 | | - | BuiltInWindowFunction::LastValue |
98 | | - | BuiltInWindowFunction::NthValue => Ok(input_expr_types[0].clone()), |
99 | | - } |
100 | | - } |
101 | | - |
102 | | - /// The signatures supported by the built-in window function `fun`. |
103 | | - pub fn signature(&self) -> Signature { |
104 | | - // Note: The physical expression must accept the type returned by this function or the execution panics. |
105 | | - match self { |
106 | | - BuiltInWindowFunction::FirstValue | BuiltInWindowFunction::LastValue => { |
107 | | - Signature::any(1, Volatility::Immutable) |
108 | | - } |
109 | | - BuiltInWindowFunction::NthValue => Signature::any(2, Volatility::Immutable), |
110 | | - } |
111 | | - } |
112 | | -} |
113 | | - |
114 | | -#[cfg(test)] |
115 | | -mod tests { |
116 | | - use super::*; |
117 | | - use strum::IntoEnumIterator; |
118 | | - #[test] |
119 | | - // Test for BuiltInWindowFunction's Display and from_str() implementations. |
120 | | - // For each variant in BuiltInWindowFunction, it converts the variant to a string |
121 | | - // and then back to a variant. The test asserts that the original variant and |
122 | | - // the reconstructed variant are the same. This assertion is also necessary for |
123 | | - // function suggestion. See https://github.com/apache/datafusion/issues/8082 |
124 | | - fn test_display_and_from_str() { |
125 | | - for func_original in BuiltInWindowFunction::iter() { |
126 | | - let func_name = func_original.to_string(); |
127 | | - let func_from_str = BuiltInWindowFunction::from_str(&func_name).unwrap(); |
128 | | - assert_eq!(func_from_str, func_original); |
129 | | - } |
130 | | - } |
| 27 | + Stub, |
131 | 28 | } |
0 commit comments