datafusion_python/expr/
drop_catalog_schema.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 datafusion::common::SchemaReference;
22use datafusion::logical_expr::DropCatalogSchema;
23use datafusion::sql::TableReference;
24use pyo3::exceptions::PyValueError;
25use pyo3::prelude::*;
26use pyo3::IntoPyObjectExt;
27
28use super::logical_node::LogicalNode;
29use crate::common::df_schema::PyDFSchema;
30use crate::sql::logical::PyLogicalPlan;
31
32#[pyclass(
33    frozen,
34    name = "DropCatalogSchema",
35    module = "datafusion.expr",
36    subclass
37)]
38#[derive(Clone)]
39pub struct PyDropCatalogSchema {
40    drop: DropCatalogSchema,
41}
42
43impl From<PyDropCatalogSchema> for DropCatalogSchema {
44    fn from(drop: PyDropCatalogSchema) -> Self {
45        drop.drop
46    }
47}
48
49impl From<DropCatalogSchema> for PyDropCatalogSchema {
50    fn from(drop: DropCatalogSchema) -> PyDropCatalogSchema {
51        PyDropCatalogSchema { drop }
52    }
53}
54
55impl Display for PyDropCatalogSchema {
56    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
57        write!(f, "DropCatalogSchema")
58    }
59}
60
61fn parse_schema_reference(name: String) -> PyResult<SchemaReference> {
62    match name.into() {
63        TableReference::Bare { table } => Ok(SchemaReference::Bare { schema: table }),
64        TableReference::Partial { schema, table } => Ok(SchemaReference::Full {
65            schema: table,
66            catalog: schema,
67        }),
68        TableReference::Full {
69            catalog: _,
70            schema: _,
71            table: _,
72        } => Err(PyErr::new::<PyValueError, String>(
73            "Invalid schema specifier (has 3 parts)".to_string(),
74        )),
75    }
76}
77
78#[pymethods]
79impl PyDropCatalogSchema {
80    #[new]
81    fn new(name: String, schema: PyDFSchema, if_exists: bool, cascade: bool) -> PyResult<Self> {
82        let name = parse_schema_reference(name)?;
83        Ok(PyDropCatalogSchema {
84            drop: DropCatalogSchema {
85                name,
86                schema: Arc::new(schema.into()),
87                if_exists,
88                cascade,
89            },
90        })
91    }
92
93    fn name(&self) -> PyResult<String> {
94        Ok(self.drop.name.to_string())
95    }
96
97    fn schema(&self) -> PyDFSchema {
98        (*self.drop.schema).clone().into()
99    }
100
101    fn if_exists(&self) -> PyResult<bool> {
102        Ok(self.drop.if_exists)
103    }
104
105    fn cascade(&self) -> PyResult<bool> {
106        Ok(self.drop.cascade)
107    }
108
109    fn __repr__(&self) -> PyResult<String> {
110        Ok(format!("DropCatalogSchema({self})"))
111    }
112}
113
114impl LogicalNode for PyDropCatalogSchema {
115    fn inputs(&self) -> Vec<PyLogicalPlan> {
116        vec![]
117    }
118
119    fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
120        self.clone().into_bound_py_any(py)
121    }
122}