@@ -72,33 +72,51 @@ impl fmt::Display for WindowFunction {
7272/// An aggregate function that is part of a built-in window function
7373#[ derive( Debug , Clone , PartialEq , Eq ) ]
7474pub enum BuiltInWindowFunction {
75- /// row number
75+ /// number of the current row within its partition, counting from 1
7676 RowNumber ,
77- /// rank
77+ /// rank of the current row with gaps; same as row_number of its first peer
7878 Rank ,
79- /// dense rank
79+ /// ank of the current row without gaps; this function counts peer groups
8080 DenseRank ,
81- /// lag
81+ /// relative rank of the current row: (rank - 1) / (total rows - 1)
82+ PercentRank ,
83+ /// relative rank of the current row: (number of rows preceding or peer with current row) / (total rows)
84+ CumeDist ,
85+ /// integer ranging from 1 to the argument value, dividing the partition as equally as possible
86+ Ntile ,
87+ /// returns value evaluated at the row that is offset rows before the current row within the partition;
88+ /// if there is no such row, instead return default (which must be of the same type as value).
89+ /// Both offset and default are evaluated with respect to the current row.
90+ /// If omitted, offset defaults to 1 and default to null
8291 Lag ,
83- /// lead
92+ /// returns value evaluated at the row that is offset rows after the current row within the partition;
93+ /// if there is no such row, instead return default (which must be of the same type as value).
94+ /// Both offset and default are evaluated with respect to the current row.
95+ /// If omitted, offset defaults to 1 and default to null
8496 Lead ,
85- /// first value
97+ /// returns value evaluated at the row that is the first row of the window frame
8698 FirstValue ,
87- /// last value
99+ /// returns value evaluated at the row that is the last row of the window frame
88100 LastValue ,
101+ /// returns value evaluated at the row that is the nth row of the window frame (counting from 1); null if no such row
102+ NthValue ,
89103}
90104
91105impl FromStr for BuiltInWindowFunction {
92106 type Err = DataFusionError ;
93107 fn from_str ( name : & str ) -> Result < BuiltInWindowFunction > {
94- Ok ( match name {
108+ Ok ( match name. to_lowercase ( ) . as_str ( ) {
95109 "row_number" => BuiltInWindowFunction :: RowNumber ,
96110 "rank" => BuiltInWindowFunction :: Rank ,
97111 "dense_rank" => BuiltInWindowFunction :: DenseRank ,
98- "first_value" => BuiltInWindowFunction :: FirstValue ,
99- "last_value" => BuiltInWindowFunction :: LastValue ,
112+ "percent_rank" => BuiltInWindowFunction :: PercentRank ,
113+ "cume_dist" => BuiltInWindowFunction :: CumeDist ,
114+ "ntile" => BuiltInWindowFunction :: Ntile ,
100115 "lag" => BuiltInWindowFunction :: Lag ,
101116 "lead" => BuiltInWindowFunction :: Lead ,
117+ "first_value" => BuiltInWindowFunction :: FirstValue ,
118+ "last_value" => BuiltInWindowFunction :: LastValue ,
119+ "nth_value" => BuiltInWindowFunction :: NthValue ,
102120 _ => {
103121 return Err ( DataFusionError :: Plan ( format ! (
104122 "There is no built-in window function named {}" ,
@@ -123,10 +141,15 @@ pub fn return_type(fun: &WindowFunction, arg_types: &[DataType]) -> Result<DataT
123141 BuiltInWindowFunction :: RowNumber
124142 | BuiltInWindowFunction :: Rank
125143 | BuiltInWindowFunction :: DenseRank => Ok ( DataType :: UInt64 ) ,
144+ BuiltInWindowFunction :: PercentRank | BuiltInWindowFunction :: CumeDist => {
145+ Ok ( DataType :: Float64 )
146+ }
147+ BuiltInWindowFunction :: Ntile => Ok ( DataType :: UInt32 ) ,
126148 BuiltInWindowFunction :: Lag
127149 | BuiltInWindowFunction :: Lead
128150 | BuiltInWindowFunction :: FirstValue
129- | BuiltInWindowFunction :: LastValue => Ok ( arg_types[ 0 ] . clone ( ) ) ,
151+ | BuiltInWindowFunction :: LastValue
152+ | BuiltInWindowFunction :: NthValue => Ok ( arg_types[ 0 ] . clone ( ) ) ,
130153 } ,
131154 }
132155}
@@ -139,11 +162,42 @@ fn signature(fun: &WindowFunction) -> Signature {
139162 WindowFunction :: BuiltInWindowFunction ( fun) => match fun {
140163 BuiltInWindowFunction :: RowNumber
141164 | BuiltInWindowFunction :: Rank
142- | BuiltInWindowFunction :: DenseRank => Signature :: Any ( 0 ) ,
165+ | BuiltInWindowFunction :: DenseRank
166+ | BuiltInWindowFunction :: PercentRank
167+ | BuiltInWindowFunction :: CumeDist => Signature :: Any ( 0 ) ,
143168 BuiltInWindowFunction :: Lag
144169 | BuiltInWindowFunction :: Lead
145170 | BuiltInWindowFunction :: FirstValue
146171 | BuiltInWindowFunction :: LastValue => Signature :: Any ( 1 ) ,
172+ BuiltInWindowFunction :: Ntile => Signature :: Exact ( vec ! [ DataType :: UInt64 ] ) ,
173+ BuiltInWindowFunction :: NthValue => Signature :: Any ( 2 ) ,
147174 } ,
148175 }
149176}
177+
178+ #[ cfg( test) ]
179+ mod tests {
180+ use super :: * ;
181+ use arrow:: datatypes:: { DataType , Field } ;
182+
183+ #[ test]
184+ fn test_window_function_from_str ( ) -> Result < ( ) > {
185+ assert_eq ! (
186+ WindowFunction :: from_str( "max" ) ?,
187+ WindowFunction :: AggregateFunction ( AggregateFunction :: Max )
188+ ) ;
189+ assert_eq ! (
190+ WindowFunction :: from_str( "min" ) ?,
191+ WindowFunction :: AggregateFunction ( AggregateFunction :: Min )
192+ ) ;
193+ assert_eq ! (
194+ WindowFunction :: from_str( "avg" ) ?,
195+ WindowFunction :: AggregateFunction ( AggregateFunction :: Avg )
196+ ) ;
197+ assert_eq ! (
198+ WindowFunction :: from_str( "cum_dist" ) ?,
199+ WindowFunction :: BuiltInWindowFunction ( BuiltInWindowFunction :: CumeDist )
200+ ) ;
201+ Ok ( ( ) )
202+ }
203+ }
0 commit comments