datafusion_python/expr/
conditional_expr.rs

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
18use crate::{errors::PyDataFusionResult, expr::PyExpr};
19use datafusion::logical_expr::conditional_expressions::CaseBuilder;
20use pyo3::prelude::*;
21
22#[pyclass(name = "CaseBuilder", module = "datafusion.expr", subclass)]
23pub struct PyCaseBuilder {
24    pub case_builder: CaseBuilder,
25}
26
27impl From<PyCaseBuilder> for CaseBuilder {
28    fn from(case_builder: PyCaseBuilder) -> Self {
29        case_builder.case_builder
30    }
31}
32
33impl From<CaseBuilder> for PyCaseBuilder {
34    fn from(case_builder: CaseBuilder) -> PyCaseBuilder {
35        PyCaseBuilder { case_builder }
36    }
37}
38
39#[pymethods]
40impl PyCaseBuilder {
41    fn when(&mut self, when: PyExpr, then: PyExpr) -> PyCaseBuilder {
42        PyCaseBuilder {
43            case_builder: self.case_builder.when(when.expr, then.expr),
44        }
45    }
46
47    fn otherwise(&mut self, else_expr: PyExpr) -> PyDataFusionResult<PyExpr> {
48        Ok(self.case_builder.otherwise(else_expr.expr)?.clone().into())
49    }
50
51    fn end(&mut self) -> PyDataFusionResult<PyExpr> {
52        Ok(self.case_builder.end()?.clone().into())
53    }
54}