datafusion_python/expr/
union.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 datafusion::logical_expr::logical_plan::Union;
19use pyo3::{prelude::*, IntoPyObjectExt};
20use std::fmt::{self, Display, Formatter};
21
22use crate::common::df_schema::PyDFSchema;
23use crate::expr::logical_node::LogicalNode;
24use crate::sql::logical::PyLogicalPlan;
25
26#[pyclass(name = "Union", module = "datafusion.expr", subclass)]
27#[derive(Clone)]
28pub struct PyUnion {
29    union_: Union,
30}
31
32impl From<Union> for PyUnion {
33    fn from(union_: Union) -> PyUnion {
34        PyUnion { union_ }
35    }
36}
37
38impl From<PyUnion> for Union {
39    fn from(union_: PyUnion) -> Self {
40        union_.union_
41    }
42}
43
44impl Display for PyUnion {
45    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
46        write!(
47            f,
48            "Union
49            Inputs: {:?}
50            Schema: {:?}",
51            &self.union_.inputs, &self.union_.schema,
52        )
53    }
54}
55
56#[pymethods]
57impl PyUnion {
58    /// Retrieves the input `LogicalPlan` to this `Union` node
59    fn input(&self) -> PyResult<Vec<PyLogicalPlan>> {
60        Ok(Self::inputs(self))
61    }
62
63    /// Resulting Schema for this `Union` node instance
64    fn schema(&self) -> PyResult<PyDFSchema> {
65        Ok(self.union_.schema.as_ref().clone().into())
66    }
67
68    fn __repr__(&self) -> PyResult<String> {
69        Ok(format!("Union({})", self))
70    }
71
72    fn __name__(&self) -> PyResult<String> {
73        Ok("Union".to_string())
74    }
75}
76
77impl LogicalNode for PyUnion {
78    fn inputs(&self) -> Vec<PyLogicalPlan> {
79        self.union_
80            .inputs
81            .iter()
82            .map(|x| x.as_ref().clone().into())
83            .collect()
84    }
85
86    fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
87        self.clone().into_bound_py_any(py)
88    }
89}