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