datafusion_python/expr/
create_external_table.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::collections::HashMap;
19use std::fmt::{self, Display, Formatter};
20use std::sync::Arc;
21
22use datafusion::logical_expr::CreateExternalTable;
23use pyo3::prelude::*;
24use pyo3::IntoPyObjectExt;
25
26use super::logical_node::LogicalNode;
27use super::sort_expr::PySortExpr;
28use crate::common::df_schema::PyDFSchema;
29use crate::common::schema::PyConstraints;
30use crate::expr::PyExpr;
31use crate::sql::logical::PyLogicalPlan;
32
33#[pyclass(
34    frozen,
35    name = "CreateExternalTable",
36    module = "datafusion.expr",
37    subclass
38)]
39#[derive(Clone)]
40pub struct PyCreateExternalTable {
41    create: CreateExternalTable,
42}
43
44impl From<PyCreateExternalTable> for CreateExternalTable {
45    fn from(create: PyCreateExternalTable) -> Self {
46        create.create
47    }
48}
49
50impl From<CreateExternalTable> for PyCreateExternalTable {
51    fn from(create: CreateExternalTable) -> PyCreateExternalTable {
52        PyCreateExternalTable { create }
53    }
54}
55
56impl Display for PyCreateExternalTable {
57    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
58        write!(
59            f,
60            "CreateExternalTable: {:?}{}",
61            self.create.name, self.create.constraints
62        )
63    }
64}
65
66#[pymethods]
67impl PyCreateExternalTable {
68    #[allow(clippy::too_many_arguments)]
69    #[new]
70    #[pyo3(signature = (schema, name, location, file_type, table_partition_cols, if_not_exists, or_replace, temporary, order_exprs, unbounded, options, constraints, column_defaults, definition=None))]
71    pub fn new(
72        schema: PyDFSchema,
73        name: String,
74        location: String,
75        file_type: String,
76        table_partition_cols: Vec<String>,
77        if_not_exists: bool,
78        or_replace: bool,
79        temporary: bool,
80        order_exprs: Vec<Vec<PySortExpr>>,
81        unbounded: bool,
82        options: HashMap<String, String>,
83        constraints: PyConstraints,
84        column_defaults: HashMap<String, PyExpr>,
85        definition: Option<String>,
86    ) -> Self {
87        let create = CreateExternalTable {
88            schema: Arc::new(schema.into()),
89            name: name.into(),
90            location,
91            file_type,
92            table_partition_cols,
93            if_not_exists,
94            or_replace,
95            temporary,
96            definition,
97            order_exprs: order_exprs
98                .into_iter()
99                .map(|vec| vec.into_iter().map(|s| s.into()).collect::<Vec<_>>())
100                .collect::<Vec<_>>(),
101            unbounded,
102            options,
103            constraints: constraints.constraints,
104            column_defaults: column_defaults
105                .into_iter()
106                .map(|(k, v)| (k, v.into()))
107                .collect(),
108        };
109        PyCreateExternalTable { create }
110    }
111
112    pub fn schema(&self) -> PyDFSchema {
113        (*self.create.schema).clone().into()
114    }
115
116    pub fn name(&self) -> PyResult<String> {
117        Ok(self.create.name.to_string())
118    }
119
120    pub fn location(&self) -> String {
121        self.create.location.clone()
122    }
123
124    pub fn file_type(&self) -> String {
125        self.create.file_type.clone()
126    }
127
128    pub fn table_partition_cols(&self) -> Vec<String> {
129        self.create.table_partition_cols.clone()
130    }
131
132    pub fn if_not_exists(&self) -> bool {
133        self.create.if_not_exists
134    }
135
136    pub fn temporary(&self) -> bool {
137        self.create.temporary
138    }
139
140    pub fn definition(&self) -> Option<String> {
141        self.create.definition.clone()
142    }
143
144    pub fn order_exprs(&self) -> Vec<Vec<PySortExpr>> {
145        self.create
146            .order_exprs
147            .iter()
148            .map(|vec| vec.iter().map(|s| s.clone().into()).collect())
149            .collect()
150    }
151
152    pub fn unbounded(&self) -> bool {
153        self.create.unbounded
154    }
155
156    pub fn options(&self) -> HashMap<String, String> {
157        self.create.options.clone()
158    }
159
160    pub fn constraints(&self) -> PyConstraints {
161        PyConstraints {
162            constraints: self.create.constraints.clone(),
163        }
164    }
165
166    pub fn column_defaults(&self) -> HashMap<String, PyExpr> {
167        self.create
168            .column_defaults
169            .iter()
170            .map(|(k, v)| (k.clone(), v.clone().into()))
171            .collect()
172    }
173
174    fn __repr__(&self) -> PyResult<String> {
175        Ok(format!("CreateExternalTable({self})"))
176    }
177
178    fn __name__(&self) -> PyResult<String> {
179        Ok("CreateExternalTable".to_string())
180    }
181}
182
183impl LogicalNode for PyCreateExternalTable {
184    fn inputs(&self) -> Vec<PyLogicalPlan> {
185        vec![]
186    }
187
188    fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
189        self.clone().into_bound_py_any(py)
190    }
191}