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