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