Skip to main content

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