Skip to main content

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