Skip to content

Commit bea9cea

Browse files
author
Jiayu Liu
committed
signature and return types
1 parent bddd426 commit bea9cea

File tree

2 files changed

+22
-50
lines changed

2 files changed

+22
-50
lines changed

datafusion/src/physical_plan/aggregates.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static TIMESTAMPS: &[DataType] = &[
182182
];
183183

184184
/// the signatures supported by the function `fun`.
185-
fn signature(fun: &AggregateFunction) -> Signature {
185+
pub fn signature(fun: &AggregateFunction) -> Signature {
186186
// note: the physical expression must accept the type returned by this function or the execution panics.
187187
match fun {
188188
AggregateFunction::Count => Signature::Any(1),

datafusion/src/physical_plan/windows.rs

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
//!
2121
//! see also https://www.postgresql.org/docs/current/functions-window.html
2222
23-
use super::expressions::{avg_return_type, sum_return_type};
2423
use super::{functions::Signature, type_coercion::data_types};
2524
use crate::error::{DataFusionError, Result};
26-
use crate::physical_plan::aggregates::AggregateFunction;
27-
use arrow::datatypes::{DataType, TimeUnit};
25+
use crate::physical_plan::{aggregates, aggregates::AggregateFunction};
26+
use arrow::datatypes::DataType;
2827
use std::{fmt, str::FromStr};
2928

3029
/// WindowFunction
@@ -90,59 +89,32 @@ pub fn return_type(fun: &WindowFunction, arg_types: &[DataType]) -> Result<DataT
9089
data_types(arg_types, &signature(fun))?;
9190

9291
match fun {
93-
WindowFunction::AggregateFunction(fun) => match fun {
94-
AggregateFunction::Count => Ok(DataType::UInt64),
95-
AggregateFunction::Max | AggregateFunction::Min => Ok(arg_types[0].clone()),
96-
AggregateFunction::Sum => sum_return_type(&arg_types[0]),
97-
AggregateFunction::Avg => avg_return_type(&arg_types[0]),
98-
},
99-
WindowFunction::BuiltInWindowFunction(_) => Ok(arg_types[0].clone()),
92+
WindowFunction::AggregateFunction(fun) => aggregates::return_type(fun, arg_types),
93+
WindowFunction::BuiltInWindowFunction(fun) => Ok(match fun {
94+
BuiltInWindowFunction::RowNumber
95+
| BuiltInWindowFunction::Rank
96+
| BuiltInWindowFunction::DenseRank => DataType::UInt64,
97+
BuiltInWindowFunction::Lag
98+
| BuiltInWindowFunction::Lead
99+
| BuiltInWindowFunction::FirstValue
100+
| BuiltInWindowFunction::LastValue => arg_types[0].clone(),
101+
}),
100102
}
101103
}
102104

103-
static NUMERICS: &[DataType] = &[
104-
DataType::Int8,
105-
DataType::Int16,
106-
DataType::Int32,
107-
DataType::Int64,
108-
DataType::UInt8,
109-
DataType::UInt16,
110-
DataType::UInt32,
111-
DataType::UInt64,
112-
DataType::Float32,
113-
DataType::Float64,
114-
];
115-
116-
static STRINGS: &[DataType] = &[DataType::Utf8, DataType::LargeUtf8];
117-
118-
static TIMESTAMPS: &[DataType] = &[
119-
DataType::Timestamp(TimeUnit::Second, None),
120-
DataType::Timestamp(TimeUnit::Millisecond, None),
121-
DataType::Timestamp(TimeUnit::Microsecond, None),
122-
DataType::Timestamp(TimeUnit::Nanosecond, None),
123-
];
124-
125105
/// the signatures supported by the function `fun`.
126106
fn signature(fun: &WindowFunction) -> Signature {
127107
// note: the physical expression must accept the type returned by this function or the execution panics.
128108
match fun {
129-
WindowFunction::AggregateFunction(fun) => match fun {
130-
AggregateFunction::Count => Signature::Any(1),
131-
AggregateFunction::Min | AggregateFunction::Max => {
132-
let valid = STRINGS
133-
.iter()
134-
.chain(NUMERICS.iter())
135-
.chain(TIMESTAMPS.iter())
136-
.cloned()
137-
.collect::<Vec<_>>();
138-
Signature::Uniform(1, valid)
139-
}
140-
AggregateFunction::Avg | AggregateFunction::Sum => {
141-
Signature::Uniform(1, NUMERICS.to_vec())
142-
}
109+
WindowFunction::AggregateFunction(fun) => aggregates::signature(fun),
110+
WindowFunction::BuiltInWindowFunction(fun) => match fun {
111+
BuiltInWindowFunction::RowNumber
112+
| BuiltInWindowFunction::Rank
113+
| BuiltInWindowFunction::DenseRank => Signature::Any(0),
114+
BuiltInWindowFunction::Lag
115+
| BuiltInWindowFunction::Lead
116+
| BuiltInWindowFunction::FirstValue
117+
| BuiltInWindowFunction::LastValue => Signature::Any(1),
143118
},
144-
WindowFunction::BuiltInWindowFunction(_) => {
145-
Signature::Uniform(1, NUMERICS.to_vec())
146-
}
147119
}
148120
}

0 commit comments

Comments
 (0)