Skip to main content

datafusion_python/expr/
describe_table.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};
19use std::sync::Arc;
20
21use arrow::datatypes::Schema;
22use arrow::pyarrow::PyArrowType;
23use datafusion::logical_expr::DescribeTable;
24use pyo3::IntoPyObjectExt;
25use pyo3::prelude::*;
26
27use super::logical_node::LogicalNode;
28use crate::common::df_schema::PyDFSchema;
29use crate::sql::logical::PyLogicalPlan;
30
31#[pyclass(
32    from_py_object,
33    frozen,
34    name = "DescribeTable",
35    module = "datafusion.expr",
36    subclass
37)]
38#[derive(Clone)]
39pub struct PyDescribeTable {
40    describe: DescribeTable,
41}
42
43impl Display for PyDescribeTable {
44    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
45        write!(f, "DescribeTable")
46    }
47}
48
49#[pymethods]
50impl PyDescribeTable {
51    #[new]
52    fn new(schema: PyArrowType<Schema>, output_schema: PyDFSchema) -> Self {
53        Self {
54            describe: DescribeTable {
55                schema: Arc::new(schema.0),
56                output_schema: Arc::new(output_schema.into()),
57            },
58        }
59    }
60
61    pub fn schema(&self) -> PyArrowType<Schema> {
62        (*self.describe.schema).clone().into()
63    }
64
65    pub fn output_schema(&self) -> PyDFSchema {
66        (*self.describe.output_schema).clone().into()
67    }
68
69    fn __repr__(&self) -> PyResult<String> {
70        Ok(format!("DescribeTable({self})"))
71    }
72
73    fn __name__(&self) -> PyResult<String> {
74        Ok("DescribeTable".to_string())
75    }
76}
77
78impl From<PyDescribeTable> for DescribeTable {
79    fn from(describe: PyDescribeTable) -> Self {
80        describe.describe
81    }
82}
83
84impl From<DescribeTable> for PyDescribeTable {
85    fn from(describe: DescribeTable) -> PyDescribeTable {
86        PyDescribeTable { describe }
87    }
88}
89
90impl LogicalNode for PyDescribeTable {
91    fn inputs(&self) -> Vec<PyLogicalPlan> {
92        vec![]
93    }
94
95    fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
96        self.clone().into_bound_py_any(py)
97    }
98}