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