use std::collections::HashMap;
use std::fmt::{self, Display, Formatter};
use std::sync::Arc;
use datafusion::common::file_options::file_type::FileType;
use datafusion::logical_expr::dml::CopyTo;
use pyo3::IntoPyObjectExt;
use pyo3::prelude::*;
use super::logical_node::LogicalNode;
use crate::sql::logical::PyLogicalPlan;
#[pyclass(
from_py_object,
frozen,
name = "CopyTo",
module = "datafusion.expr",
subclass
)]
#[derive(Clone)]
pub struct PyCopyTo {
copy: CopyTo,
}
impl From<PyCopyTo> for CopyTo {
fn from(copy: PyCopyTo) -> Self {
copy.copy
}
}
impl From<CopyTo> for PyCopyTo {
fn from(copy: CopyTo) -> PyCopyTo {
PyCopyTo { copy }
}
}
impl Display for PyCopyTo {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "CopyTo: {:?}", self.copy.output_url)
}
}
impl LogicalNode for PyCopyTo {
fn inputs(&self) -> Vec<PyLogicalPlan> {
vec![PyLogicalPlan::from((*self.copy.input).clone())]
}
fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
self.clone().into_bound_py_any(py)
}
}
#[pymethods]
impl PyCopyTo {
#[new]
pub fn new(
input: PyLogicalPlan,
output_url: String,
partition_by: Vec<String>,
file_type: PyFileType,
options: HashMap<String, String>,
) -> Self {
PyCopyTo {
copy: CopyTo::new(
input.plan(),
output_url,
partition_by,
file_type.file_type,
options,
),
}
}
fn input(&self) -> PyLogicalPlan {
PyLogicalPlan::from((*self.copy.input).clone())
}
fn output_url(&self) -> String {
self.copy.output_url.clone()
}
fn partition_by(&self) -> Vec<String> {
self.copy.partition_by.clone()
}
fn file_type(&self) -> PyFileType {
PyFileType {
file_type: self.copy.file_type.clone(),
}
}
fn options(&self) -> HashMap<String, String> {
self.copy.options.clone()
}
fn __repr__(&self) -> PyResult<String> {
Ok(format!("CopyTo({self})"))
}
fn __name__(&self) -> PyResult<String> {
Ok("CopyTo".to_string())
}
}
#[pyclass(
from_py_object,
frozen,
name = "FileType",
module = "datafusion.expr",
subclass
)]
#[derive(Clone)]
pub struct PyFileType {
file_type: Arc<dyn FileType>,
}
impl Display for PyFileType {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "FileType: {}", self.file_type)
}
}
#[pymethods]
impl PyFileType {
fn __repr__(&self) -> PyResult<String> {
Ok(format!("FileType({self})"))
}
fn __name__(&self) -> PyResult<String> {
Ok("FileType".to_string())
}
}