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