Skip to content

Commit 7ae445a

Browse files
author
Jiayu Liu
committed
adding from str
1 parent bea9cea commit 7ae445a

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

datafusion/src/physical_plan/windows.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,16 @@ pub enum WindowFunction {
3838
impl FromStr for WindowFunction {
3939
type Err = DataFusionError;
4040
fn from_str(name: &str) -> Result<WindowFunction> {
41-
Ok(match name {
42-
"min" => WindowFunction::AggregateFunction(AggregateFunction::Min),
43-
"max" => WindowFunction::AggregateFunction(AggregateFunction::Max),
44-
"count" => WindowFunction::AggregateFunction(AggregateFunction::Count),
45-
"avg" => WindowFunction::AggregateFunction(AggregateFunction::Avg),
46-
"sum" => WindowFunction::AggregateFunction(AggregateFunction::Sum),
47-
_ => {
48-
return Err(DataFusionError::Plan(format!(
49-
"There is no built-in function named {}",
50-
name
51-
)))
52-
}
53-
})
41+
if let Ok(aggregate) = AggregateFunction::from_str(name) {
42+
Ok(WindowFunction::AggregateFunction(aggregate))
43+
} else if let Ok(build_in_function) = BuiltInWindowFunction::from_str(name) {
44+
Ok(WindowFunction::BuiltInWindowFunction(build_in_function))
45+
} else {
46+
Err(DataFusionError::Plan(format!(
47+
"There is no built-in function named {}",
48+
name
49+
)))
50+
}
5451
}
5552
}
5653

@@ -80,6 +77,27 @@ pub enum BuiltInWindowFunction {
8077
LastValue,
8178
}
8279

80+
impl FromStr for BuiltInWindowFunction {
81+
type Err = DataFusionError;
82+
fn from_str(name: &str) -> Result<BuiltInWindowFunction> {
83+
Ok(match name {
84+
"row_number" => BuiltInWindowFunction::RowNumber,
85+
"rank" => BuiltInWindowFunction::Rank,
86+
"dense_rank" => BuiltInWindowFunction::DenseRank,
87+
"first_value" => BuiltInWindowFunction::FirstValue,
88+
"last_value" => BuiltInWindowFunction::LastValue,
89+
"lag" => BuiltInWindowFunction::Lag,
90+
"lead" => BuiltInWindowFunction::Lead,
91+
_ => {
92+
return Err(DataFusionError::Plan(format!(
93+
"There is no built-in function named {}",
94+
name
95+
)))
96+
}
97+
})
98+
}
99+
}
100+
83101
/// Returns the datatype of the scalar function
84102
pub fn return_type(fun: &WindowFunction, arg_types: &[DataType]) -> Result<DataType> {
85103
// Note that this function *must* return the same type that the respective physical expression returns
@@ -90,15 +108,15 @@ pub fn return_type(fun: &WindowFunction, arg_types: &[DataType]) -> Result<DataT
90108

91109
match fun {
92110
WindowFunction::AggregateFunction(fun) => aggregates::return_type(fun, arg_types),
93-
WindowFunction::BuiltInWindowFunction(fun) => Ok(match fun {
111+
WindowFunction::BuiltInWindowFunction(fun) => match fun {
94112
BuiltInWindowFunction::RowNumber
95113
| BuiltInWindowFunction::Rank
96-
| BuiltInWindowFunction::DenseRank => DataType::UInt64,
114+
| BuiltInWindowFunction::DenseRank => Ok(DataType::UInt64),
97115
BuiltInWindowFunction::Lag
98116
| BuiltInWindowFunction::Lead
99117
| BuiltInWindowFunction::FirstValue
100-
| BuiltInWindowFunction::LastValue => arg_types[0].clone(),
101-
}),
118+
| BuiltInWindowFunction::LastValue => Ok(arg_types[0].clone()),
119+
},
102120
}
103121
}
104122

0 commit comments

Comments
 (0)