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