|
20 | 20 | //! |
21 | 21 | //! see also https://www.postgresql.org/docs/current/functions-window.html |
22 | 22 |
|
| 23 | +use crate::arrow::datatypes::Field; |
23 | 24 | use crate::error::{DataFusionError, Result}; |
24 | 25 | use crate::physical_plan::{ |
25 | 26 | aggregates, aggregates::AggregateFunction, functions::Signature, |
26 | | - type_coercion::data_types, |
| 27 | + type_coercion::data_types, PhysicalExpr, WindowAccumulator, |
27 | 28 | }; |
28 | 29 | use arrow::datatypes::DataType; |
| 30 | +use std::any::Any; |
| 31 | +use std::sync::Arc; |
29 | 32 | use std::{fmt, str::FromStr}; |
30 | 33 |
|
31 | 34 | /// WindowFunction |
@@ -143,52 +146,92 @@ impl FromStr for BuiltInWindowFunction { |
143 | 146 |
|
144 | 147 | /// Returns the datatype of the window function |
145 | 148 | pub fn return_type(fun: &WindowFunction, arg_types: &[DataType]) -> Result<DataType> { |
| 149 | + match fun { |
| 150 | + WindowFunction::AggregateFunction(fun) => aggregates::return_type(fun, arg_types), |
| 151 | + WindowFunction::BuiltInWindowFunction(fun) => { |
| 152 | + return_type_for_built_in(fun, arg_types) |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +/// Returns the datatype of the built-in window function |
| 158 | +pub(super) fn return_type_for_built_in( |
| 159 | + fun: &BuiltInWindowFunction, |
| 160 | + arg_types: &[DataType], |
| 161 | +) -> Result<DataType> { |
146 | 162 | // Note that this function *must* return the same type that the respective physical expression returns |
147 | 163 | // or the execution panics. |
148 | 164 |
|
149 | 165 | // verify that this is a valid set of data types for this function |
150 | | - data_types(arg_types, &signature(fun))?; |
| 166 | + data_types(arg_types, &signature_for_built_in(fun))?; |
151 | 167 |
|
152 | 168 | match fun { |
153 | | - WindowFunction::AggregateFunction(fun) => aggregates::return_type(fun, arg_types), |
154 | | - WindowFunction::BuiltInWindowFunction(fun) => match fun { |
155 | | - BuiltInWindowFunction::RowNumber |
156 | | - | BuiltInWindowFunction::Rank |
157 | | - | BuiltInWindowFunction::DenseRank => Ok(DataType::UInt64), |
158 | | - BuiltInWindowFunction::PercentRank | BuiltInWindowFunction::CumeDist => { |
159 | | - Ok(DataType::Float64) |
160 | | - } |
161 | | - BuiltInWindowFunction::Ntile => Ok(DataType::UInt32), |
162 | | - BuiltInWindowFunction::Lag |
163 | | - | BuiltInWindowFunction::Lead |
164 | | - | BuiltInWindowFunction::FirstValue |
165 | | - | BuiltInWindowFunction::LastValue |
166 | | - | BuiltInWindowFunction::NthValue => Ok(arg_types[0].clone()), |
167 | | - }, |
| 169 | + BuiltInWindowFunction::RowNumber |
| 170 | + | BuiltInWindowFunction::Rank |
| 171 | + | BuiltInWindowFunction::DenseRank => Ok(DataType::UInt64), |
| 172 | + BuiltInWindowFunction::PercentRank | BuiltInWindowFunction::CumeDist => { |
| 173 | + Ok(DataType::Float64) |
| 174 | + } |
| 175 | + BuiltInWindowFunction::Ntile => Ok(DataType::UInt32), |
| 176 | + BuiltInWindowFunction::Lag |
| 177 | + | BuiltInWindowFunction::Lead |
| 178 | + | BuiltInWindowFunction::FirstValue |
| 179 | + | BuiltInWindowFunction::LastValue |
| 180 | + | BuiltInWindowFunction::NthValue => Ok(arg_types[0].clone()), |
168 | 181 | } |
169 | 182 | } |
170 | 183 |
|
171 | 184 | /// the signatures supported by the function `fun`. |
172 | | -fn signature(fun: &WindowFunction) -> Signature { |
173 | | - // note: the physical expression must accept the type returned by this function or the execution panics. |
| 185 | +pub fn signature(fun: &WindowFunction) -> Signature { |
174 | 186 | match fun { |
175 | 187 | WindowFunction::AggregateFunction(fun) => aggregates::signature(fun), |
176 | | - WindowFunction::BuiltInWindowFunction(fun) => match fun { |
177 | | - BuiltInWindowFunction::RowNumber |
178 | | - | BuiltInWindowFunction::Rank |
179 | | - | BuiltInWindowFunction::DenseRank |
180 | | - | BuiltInWindowFunction::PercentRank |
181 | | - | BuiltInWindowFunction::CumeDist => Signature::Any(0), |
182 | | - BuiltInWindowFunction::Lag |
183 | | - | BuiltInWindowFunction::Lead |
184 | | - | BuiltInWindowFunction::FirstValue |
185 | | - | BuiltInWindowFunction::LastValue => Signature::Any(1), |
186 | | - BuiltInWindowFunction::Ntile => Signature::Exact(vec![DataType::UInt64]), |
187 | | - BuiltInWindowFunction::NthValue => Signature::Any(2), |
188 | | - }, |
| 188 | + WindowFunction::BuiltInWindowFunction(fun) => signature_for_built_in(fun), |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +/// the signatures supported by the built-in window function `fun`. |
| 193 | +pub(super) fn signature_for_built_in(fun: &BuiltInWindowFunction) -> Signature { |
| 194 | + // note: the physical expression must accept the type returned by this function or the execution panics. |
| 195 | + match fun { |
| 196 | + BuiltInWindowFunction::RowNumber |
| 197 | + | BuiltInWindowFunction::Rank |
| 198 | + | BuiltInWindowFunction::DenseRank |
| 199 | + | BuiltInWindowFunction::PercentRank |
| 200 | + | BuiltInWindowFunction::CumeDist => Signature::Any(0), |
| 201 | + BuiltInWindowFunction::Lag |
| 202 | + | BuiltInWindowFunction::Lead |
| 203 | + | BuiltInWindowFunction::FirstValue |
| 204 | + | BuiltInWindowFunction::LastValue => Signature::Any(1), |
| 205 | + BuiltInWindowFunction::Ntile => Signature::Exact(vec![DataType::UInt64]), |
| 206 | + BuiltInWindowFunction::NthValue => Signature::Any(2), |
189 | 207 | } |
190 | 208 | } |
191 | 209 |
|
| 210 | +/// A window expression that is a built-in window function |
| 211 | +pub trait BuiltInWindowFunctionExpr: Send + Sync + std::fmt::Debug { |
| 212 | + /// Returns the aggregate expression as [`Any`](std::any::Any) so that it can be |
| 213 | + /// downcast to a specific implementation. |
| 214 | + fn as_any(&self) -> &dyn Any; |
| 215 | + |
| 216 | + /// the field of the final result of this aggregation. |
| 217 | + fn field(&self) -> Result<Field>; |
| 218 | + |
| 219 | + /// expressions that are passed to the Accumulator. |
| 220 | + /// Single-column aggregations such as `sum` return a single value, others (e.g. `cov`) return many. |
| 221 | + fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>>; |
| 222 | + |
| 223 | + /// Human readable name such as `"MIN(c2)"` or `"RANK()"`. The default |
| 224 | + /// implementation returns placeholder text. |
| 225 | + fn name(&self) -> &str { |
| 226 | + "BuiltInWindowFunctionExpr: default name" |
| 227 | + } |
| 228 | + |
| 229 | + /// the accumulator used to accumulate values from the expressions. |
| 230 | + /// the accumulator expects the same number of arguments as `expressions` and must |
| 231 | + /// return states with the same description as `state_fields` |
| 232 | + fn create_accumulator(&self) -> Result<Box<dyn WindowAccumulator>>; |
| 233 | +} |
| 234 | + |
192 | 235 | #[cfg(test)] |
193 | 236 | mod tests { |
194 | 237 | use super::*; |
|
0 commit comments