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