|  | 
|  | 1 | +// Licensed to the Apache Software Foundation (ASF) under one | 
|  | 2 | +// or more contributor license agreements.  See the NOTICE file | 
|  | 3 | +// distributed with this work for additional information | 
|  | 4 | +// regarding copyright ownership.  The ASF licenses this file | 
|  | 5 | +// to you under the Apache License, Version 2.0 (the | 
|  | 6 | +// "License"); you may not use this file except in compliance | 
|  | 7 | +// with the License.  You may obtain a copy of the License at | 
|  | 8 | +// | 
|  | 9 | +//   http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | +// | 
|  | 11 | +// Unless required by applicable law or agreed to in writing, | 
|  | 12 | +// software distributed under the License is distributed on an | 
|  | 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
|  | 14 | +// KIND, either express or implied.  See the License for the | 
|  | 15 | +// specific language governing permissions and limitations | 
|  | 16 | +// under the License. | 
|  | 17 | + | 
|  | 18 | +use crate::aggregate_function::AggregateFunction; | 
|  | 19 | +use datafusion_common::{DataFusionError, Result}; | 
|  | 20 | +use std::{fmt, str::FromStr}; | 
|  | 21 | + | 
|  | 22 | +/// WindowFunction | 
|  | 23 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] | 
|  | 24 | +pub enum WindowFunction { | 
|  | 25 | +    /// window function that leverages an aggregate function | 
|  | 26 | +    AggregateFunction(AggregateFunction), | 
|  | 27 | +    /// window function that leverages a built-in window function | 
|  | 28 | +    BuiltInWindowFunction(BuiltInWindowFunction), | 
|  | 29 | +} | 
|  | 30 | + | 
|  | 31 | +impl FromStr for WindowFunction { | 
|  | 32 | +    type Err = DataFusionError; | 
|  | 33 | +    fn from_str(name: &str) -> Result<WindowFunction> { | 
|  | 34 | +        let name = name.to_lowercase(); | 
|  | 35 | +        if let Ok(aggregate) = AggregateFunction::from_str(name.as_str()) { | 
|  | 36 | +            Ok(WindowFunction::AggregateFunction(aggregate)) | 
|  | 37 | +        } else if let Ok(built_in_function) = | 
|  | 38 | +            BuiltInWindowFunction::from_str(name.as_str()) | 
|  | 39 | +        { | 
|  | 40 | +            Ok(WindowFunction::BuiltInWindowFunction(built_in_function)) | 
|  | 41 | +        } else { | 
|  | 42 | +            Err(DataFusionError::Plan(format!( | 
|  | 43 | +                "There is no window function named {}", | 
|  | 44 | +                name | 
|  | 45 | +            ))) | 
|  | 46 | +        } | 
|  | 47 | +    } | 
|  | 48 | +} | 
|  | 49 | + | 
|  | 50 | +impl fmt::Display for BuiltInWindowFunction { | 
|  | 51 | +    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 
|  | 52 | +        match self { | 
|  | 53 | +            BuiltInWindowFunction::RowNumber => write!(f, "ROW_NUMBER"), | 
|  | 54 | +            BuiltInWindowFunction::Rank => write!(f, "RANK"), | 
|  | 55 | +            BuiltInWindowFunction::DenseRank => write!(f, "DENSE_RANK"), | 
|  | 56 | +            BuiltInWindowFunction::PercentRank => write!(f, "PERCENT_RANK"), | 
|  | 57 | +            BuiltInWindowFunction::CumeDist => write!(f, "CUME_DIST"), | 
|  | 58 | +            BuiltInWindowFunction::Ntile => write!(f, "NTILE"), | 
|  | 59 | +            BuiltInWindowFunction::Lag => write!(f, "LAG"), | 
|  | 60 | +            BuiltInWindowFunction::Lead => write!(f, "LEAD"), | 
|  | 61 | +            BuiltInWindowFunction::FirstValue => write!(f, "FIRST_VALUE"), | 
|  | 62 | +            BuiltInWindowFunction::LastValue => write!(f, "LAST_VALUE"), | 
|  | 63 | +            BuiltInWindowFunction::NthValue => write!(f, "NTH_VALUE"), | 
|  | 64 | +        } | 
|  | 65 | +    } | 
|  | 66 | +} | 
|  | 67 | + | 
|  | 68 | +impl fmt::Display for WindowFunction { | 
|  | 69 | +    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 
|  | 70 | +        match self { | 
|  | 71 | +            WindowFunction::AggregateFunction(fun) => fun.fmt(f), | 
|  | 72 | +            WindowFunction::BuiltInWindowFunction(fun) => fun.fmt(f), | 
|  | 73 | +        } | 
|  | 74 | +    } | 
|  | 75 | +} | 
|  | 76 | + | 
|  | 77 | +/// An aggregate function that is part of a built-in window function | 
|  | 78 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] | 
|  | 79 | +pub enum BuiltInWindowFunction { | 
|  | 80 | +    /// number of the current row within its partition, counting from 1 | 
|  | 81 | +    RowNumber, | 
|  | 82 | +    /// rank of the current row with gaps; same as row_number of its first peer | 
|  | 83 | +    Rank, | 
|  | 84 | +    /// ank of the current row without gaps; this function counts peer groups | 
|  | 85 | +    DenseRank, | 
|  | 86 | +    /// relative rank of the current row: (rank - 1) / (total rows - 1) | 
|  | 87 | +    PercentRank, | 
|  | 88 | +    /// relative rank of the current row: (number of rows preceding or peer with current row) / (total rows) | 
|  | 89 | +    CumeDist, | 
|  | 90 | +    /// integer ranging from 1 to the argument value, dividing the partition as equally as possible | 
|  | 91 | +    Ntile, | 
|  | 92 | +    /// returns value evaluated at the row that is offset rows before 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 | 
|  | 96 | +    Lag, | 
|  | 97 | +    /// returns value evaluated at the row that is offset rows after the current row within the partition; | 
|  | 98 | +    /// if there is no such row, instead return default (which must be of the same type as value). | 
|  | 99 | +    /// Both offset and default are evaluated with respect to the current row. | 
|  | 100 | +    /// If omitted, offset defaults to 1 and default to null | 
|  | 101 | +    Lead, | 
|  | 102 | +    /// returns value evaluated at the row that is the first row of the window frame | 
|  | 103 | +    FirstValue, | 
|  | 104 | +    /// returns value evaluated at the row that is the last row of the window frame | 
|  | 105 | +    LastValue, | 
|  | 106 | +    /// returns value evaluated at the row that is the nth row of the window frame (counting from 1); null if no such row | 
|  | 107 | +    NthValue, | 
|  | 108 | +} | 
|  | 109 | + | 
|  | 110 | +impl FromStr for BuiltInWindowFunction { | 
|  | 111 | +    type Err = DataFusionError; | 
|  | 112 | +    fn from_str(name: &str) -> Result<BuiltInWindowFunction> { | 
|  | 113 | +        Ok(match name.to_uppercase().as_str() { | 
|  | 114 | +            "ROW_NUMBER" => BuiltInWindowFunction::RowNumber, | 
|  | 115 | +            "RANK" => BuiltInWindowFunction::Rank, | 
|  | 116 | +            "DENSE_RANK" => BuiltInWindowFunction::DenseRank, | 
|  | 117 | +            "PERCENT_RANK" => BuiltInWindowFunction::PercentRank, | 
|  | 118 | +            "CUME_DIST" => BuiltInWindowFunction::CumeDist, | 
|  | 119 | +            "NTILE" => BuiltInWindowFunction::Ntile, | 
|  | 120 | +            "LAG" => BuiltInWindowFunction::Lag, | 
|  | 121 | +            "LEAD" => BuiltInWindowFunction::Lead, | 
|  | 122 | +            "FIRST_VALUE" => BuiltInWindowFunction::FirstValue, | 
|  | 123 | +            "LAST_VALUE" => BuiltInWindowFunction::LastValue, | 
|  | 124 | +            "NTH_VALUE" => BuiltInWindowFunction::NthValue, | 
|  | 125 | +            _ => { | 
|  | 126 | +                return Err(DataFusionError::Plan(format!( | 
|  | 127 | +                    "There is no built-in window function named {}", | 
|  | 128 | +                    name | 
|  | 129 | +                ))) | 
|  | 130 | +            } | 
|  | 131 | +        }) | 
|  | 132 | +    } | 
|  | 133 | +} | 
|  | 134 | + | 
|  | 135 | +#[cfg(test)] | 
|  | 136 | +mod tests { | 
|  | 137 | +    use super::*; | 
|  | 138 | + | 
|  | 139 | +    #[test] | 
|  | 140 | +    fn test_window_function_case_insensitive() -> Result<()> { | 
|  | 141 | +        let names = vec![ | 
|  | 142 | +            "row_number", | 
|  | 143 | +            "rank", | 
|  | 144 | +            "dense_rank", | 
|  | 145 | +            "percent_rank", | 
|  | 146 | +            "cume_dist", | 
|  | 147 | +            "ntile", | 
|  | 148 | +            "lag", | 
|  | 149 | +            "lead", | 
|  | 150 | +            "first_value", | 
|  | 151 | +            "last_value", | 
|  | 152 | +            "nth_value", | 
|  | 153 | +            "min", | 
|  | 154 | +            "max", | 
|  | 155 | +            "count", | 
|  | 156 | +            "avg", | 
|  | 157 | +            "sum", | 
|  | 158 | +        ]; | 
|  | 159 | +        for name in names { | 
|  | 160 | +            let fun = WindowFunction::from_str(name)?; | 
|  | 161 | +            let fun2 = WindowFunction::from_str(name.to_uppercase().as_str())?; | 
|  | 162 | +            assert_eq!(fun, fun2); | 
|  | 163 | +            assert_eq!(fun.to_string(), name.to_uppercase()); | 
|  | 164 | +        } | 
|  | 165 | +        Ok(()) | 
|  | 166 | +    } | 
|  | 167 | + | 
|  | 168 | +    #[test] | 
|  | 169 | +    fn test_window_function_from_str() -> Result<()> { | 
|  | 170 | +        assert_eq!( | 
|  | 171 | +            WindowFunction::from_str("max")?, | 
|  | 172 | +            WindowFunction::AggregateFunction(AggregateFunction::Max) | 
|  | 173 | +        ); | 
|  | 174 | +        assert_eq!( | 
|  | 175 | +            WindowFunction::from_str("min")?, | 
|  | 176 | +            WindowFunction::AggregateFunction(AggregateFunction::Min) | 
|  | 177 | +        ); | 
|  | 178 | +        assert_eq!( | 
|  | 179 | +            WindowFunction::from_str("avg")?, | 
|  | 180 | +            WindowFunction::AggregateFunction(AggregateFunction::Avg) | 
|  | 181 | +        ); | 
|  | 182 | +        assert_eq!( | 
|  | 183 | +            WindowFunction::from_str("cume_dist")?, | 
|  | 184 | +            WindowFunction::BuiltInWindowFunction(BuiltInWindowFunction::CumeDist) | 
|  | 185 | +        ); | 
|  | 186 | +        assert_eq!( | 
|  | 187 | +            WindowFunction::from_str("first_value")?, | 
|  | 188 | +            WindowFunction::BuiltInWindowFunction(BuiltInWindowFunction::FirstValue) | 
|  | 189 | +        ); | 
|  | 190 | +        assert_eq!( | 
|  | 191 | +            WindowFunction::from_str("LAST_value")?, | 
|  | 192 | +            WindowFunction::BuiltInWindowFunction(BuiltInWindowFunction::LastValue) | 
|  | 193 | +        ); | 
|  | 194 | +        assert_eq!( | 
|  | 195 | +            WindowFunction::from_str("LAG")?, | 
|  | 196 | +            WindowFunction::BuiltInWindowFunction(BuiltInWindowFunction::Lag) | 
|  | 197 | +        ); | 
|  | 198 | +        assert_eq!( | 
|  | 199 | +            WindowFunction::from_str("LEAD")?, | 
|  | 200 | +            WindowFunction::BuiltInWindowFunction(BuiltInWindowFunction::Lead) | 
|  | 201 | +        ); | 
|  | 202 | +        Ok(()) | 
|  | 203 | +    } | 
|  | 204 | +} | 
0 commit comments