datafusion_python/expr/
unnest.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::Unnest;
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 = "Unnest", module = "datafusion.expr", subclass)]
27#[derive(Clone)]
28pub struct PyUnnest {
29    unnest_: Unnest,
30}
31
32impl From<Unnest> for PyUnnest {
33    fn from(unnest_: Unnest) -> PyUnnest {
34        PyUnnest { unnest_ }
35    }
36}
37
38impl From<PyUnnest> for Unnest {
39    fn from(unnest_: PyUnnest) -> Self {
40        unnest_.unnest_
41    }
42}
43
44impl Display for PyUnnest {
45    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
46        write!(
47            f,
48            "Unnest
49            Inputs: {:?}
50            Schema: {:?}",
51            &self.unnest_.input, &self.unnest_.schema,
52        )
53    }
54}
55
56#[pymethods]
57impl PyUnnest {
58    /// Retrieves the input `LogicalPlan` to this `Unnest` node
59    fn input(&self) -> PyResult<Vec<PyLogicalPlan>> {
60        Ok(Self::inputs(self))
61    }
62
63    /// Resulting Schema for this `Unnest` node instance
64    fn schema(&self) -> PyResult<PyDFSchema> {
65        Ok(self.unnest_.schema.as_ref().clone().into())
66    }
67
68    fn __repr__(&self) -> PyResult<String> {
69        Ok(format!("Unnest({})", self))
70    }
71
72    fn __name__(&self) -> PyResult<String> {
73        Ok("Unnest".to_string())
74    }
75}
76
77impl LogicalNode for PyUnnest {
78    fn inputs(&self) -> Vec<PyLogicalPlan> {
79        vec![PyLogicalPlan::from((*self.unnest_.input).clone())]
80    }
81
82    fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
83        self.clone().into_bound_py_any(py)
84    }
85}