datafusion_python/expr/
union.rs1use 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 fn input(&self) -> PyResult<Vec<PyLogicalPlan>> {
60 Ok(Self::inputs(self))
61 }
62
63 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}