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