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