|
| 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 datafusion_common::{DataFusionError, ScalarValue}; |
| 19 | +use datafusion_expr::window_frame::{WindowFrame, WindowFrameBound, WindowFrameUnits}; |
| 20 | +use pyo3::prelude::*; |
| 21 | +use std::fmt::{Display, Formatter}; |
| 22 | + |
| 23 | +use crate::errors::py_datafusion_err; |
| 24 | + |
| 25 | +#[pyclass(name = "WindowFrame", module = "datafusion", subclass)] |
| 26 | +#[derive(Clone)] |
| 27 | +pub struct PyWindowFrame { |
| 28 | + frame: WindowFrame, |
| 29 | +} |
| 30 | + |
| 31 | +impl From<PyWindowFrame> for WindowFrame { |
| 32 | + fn from(frame: PyWindowFrame) -> Self { |
| 33 | + frame.frame |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl From<WindowFrame> for PyWindowFrame { |
| 38 | + fn from(frame: WindowFrame) -> PyWindowFrame { |
| 39 | + PyWindowFrame { frame } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl Display for PyWindowFrame { |
| 44 | + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { |
| 45 | + write!( |
| 46 | + f, |
| 47 | + "OVER ({} BETWEEN {} AND {})", |
| 48 | + self.frame.units, self.frame.start_bound, self.frame.end_bound |
| 49 | + ) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[pymethods] |
| 54 | +impl PyWindowFrame { |
| 55 | + #[new(unit, start_bound, end_bound)] |
| 56 | + pub fn new(units: &str, start_bound: Option<u64>, end_bound: Option<u64>) -> PyResult<Self> { |
| 57 | + let units = units.to_ascii_lowercase(); |
| 58 | + let units = match units.as_str() { |
| 59 | + "rows" => WindowFrameUnits::Rows, |
| 60 | + "range" => WindowFrameUnits::Range, |
| 61 | + "groups" => WindowFrameUnits::Groups, |
| 62 | + _ => { |
| 63 | + return Err(py_datafusion_err(DataFusionError::NotImplemented(format!( |
| 64 | + "{:?}", |
| 65 | + units, |
| 66 | + )))); |
| 67 | + } |
| 68 | + }; |
| 69 | + let start_bound = match start_bound { |
| 70 | + Some(start_bound) => { |
| 71 | + WindowFrameBound::Preceding(ScalarValue::UInt64(Some(start_bound))) |
| 72 | + } |
| 73 | + None => match units { |
| 74 | + WindowFrameUnits::Range => WindowFrameBound::Preceding(ScalarValue::UInt64(None)), |
| 75 | + WindowFrameUnits::Rows => WindowFrameBound::Preceding(ScalarValue::UInt64(None)), |
| 76 | + WindowFrameUnits::Groups => { |
| 77 | + return Err(py_datafusion_err(DataFusionError::NotImplemented(format!( |
| 78 | + "{:?}", |
| 79 | + units, |
| 80 | + )))); |
| 81 | + } |
| 82 | + }, |
| 83 | + }; |
| 84 | + let end_bound = match end_bound { |
| 85 | + Some(end_bound) => WindowFrameBound::Following(ScalarValue::UInt64(Some(end_bound))), |
| 86 | + None => match units { |
| 87 | + WindowFrameUnits::Rows => WindowFrameBound::Following(ScalarValue::UInt64(None)), |
| 88 | + WindowFrameUnits::Range => WindowFrameBound::Following(ScalarValue::UInt64(None)), |
| 89 | + WindowFrameUnits::Groups => { |
| 90 | + return Err(py_datafusion_err(DataFusionError::NotImplemented(format!( |
| 91 | + "{:?}", |
| 92 | + units, |
| 93 | + )))); |
| 94 | + } |
| 95 | + }, |
| 96 | + }; |
| 97 | + Ok(PyWindowFrame { |
| 98 | + frame: WindowFrame { |
| 99 | + units, |
| 100 | + start_bound, |
| 101 | + end_bound, |
| 102 | + }, |
| 103 | + }) |
| 104 | + } |
| 105 | + |
| 106 | + /// Get a String representation of this window frame |
| 107 | + fn __repr__(&self) -> String { |
| 108 | + format!("{}", self) |
| 109 | + } |
| 110 | +} |
0 commit comments