Skip to main content

datafusion_python/expr/
copy_to.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::common::file_options::file_type::FileType;
23use datafusion::logical_expr::dml::CopyTo;
24use pyo3::IntoPyObjectExt;
25use pyo3::prelude::*;
26
27use super::logical_node::LogicalNode;
28use crate::sql::logical::PyLogicalPlan;
29
30#[pyclass(
31    from_py_object,
32    frozen,
33    name = "CopyTo",
34    module = "datafusion.expr",
35    subclass
36)]
37#[derive(Clone)]
38pub struct PyCopyTo {
39    copy: CopyTo,
40}
41
42impl From<PyCopyTo> for CopyTo {
43    fn from(copy: PyCopyTo) -> Self {
44        copy.copy
45    }
46}
47
48impl From<CopyTo> for PyCopyTo {
49    fn from(copy: CopyTo) -> PyCopyTo {
50        PyCopyTo { copy }
51    }
52}
53
54impl Display for PyCopyTo {
55    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
56        write!(f, "CopyTo: {:?}", self.copy.output_url)
57    }
58}
59
60impl LogicalNode for PyCopyTo {
61    fn inputs(&self) -> Vec<PyLogicalPlan> {
62        vec![PyLogicalPlan::from((*self.copy.input).clone())]
63    }
64
65    fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
66        self.clone().into_bound_py_any(py)
67    }
68}
69
70#[pymethods]
71impl PyCopyTo {
72    #[new]
73    pub fn new(
74        input: PyLogicalPlan,
75        output_url: String,
76        partition_by: Vec<String>,
77        file_type: PyFileType,
78        options: HashMap<String, String>,
79    ) -> Self {
80        PyCopyTo {
81            copy: CopyTo::new(
82                input.plan(),
83                output_url,
84                partition_by,
85                file_type.file_type,
86                options,
87            ),
88        }
89    }
90
91    fn input(&self) -> PyLogicalPlan {
92        PyLogicalPlan::from((*self.copy.input).clone())
93    }
94
95    fn output_url(&self) -> String {
96        self.copy.output_url.clone()
97    }
98
99    fn partition_by(&self) -> Vec<String> {
100        self.copy.partition_by.clone()
101    }
102
103    fn file_type(&self) -> PyFileType {
104        PyFileType {
105            file_type: self.copy.file_type.clone(),
106        }
107    }
108
109    fn options(&self) -> HashMap<String, String> {
110        self.copy.options.clone()
111    }
112
113    fn __repr__(&self) -> PyResult<String> {
114        Ok(format!("CopyTo({self})"))
115    }
116
117    fn __name__(&self) -> PyResult<String> {
118        Ok("CopyTo".to_string())
119    }
120}
121
122#[pyclass(
123    from_py_object,
124    frozen,
125    name = "FileType",
126    module = "datafusion.expr",
127    subclass
128)]
129#[derive(Clone)]
130pub struct PyFileType {
131    file_type: Arc<dyn FileType>,
132}
133
134impl Display for PyFileType {
135    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
136        write!(f, "FileType: {}", self.file_type)
137    }
138}
139
140#[pymethods]
141impl PyFileType {
142    fn __repr__(&self) -> PyResult<String> {
143        Ok(format!("FileType({self})"))
144    }
145
146    fn __name__(&self) -> PyResult<String> {
147        Ok("FileType".to_string())
148    }
149}