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