datafusion_python/expr/
create_view.rs1use std::fmt::{self, Display, Formatter};
19
20use datafusion::logical_expr::{CreateView, DdlStatement, LogicalPlan};
21use pyo3::prelude::*;
22use pyo3::IntoPyObjectExt;
23
24use super::logical_node::LogicalNode;
25use crate::errors::py_type_err;
26use crate::sql::logical::PyLogicalPlan;
27
28#[pyclass(frozen, name = "CreateView", module = "datafusion.expr", subclass)]
29#[derive(Clone)]
30pub struct PyCreateView {
31 create: CreateView,
32}
33
34impl From<PyCreateView> for CreateView {
35 fn from(create: PyCreateView) -> Self {
36 create.create
37 }
38}
39
40impl From<CreateView> for PyCreateView {
41 fn from(create: CreateView) -> PyCreateView {
42 PyCreateView { create }
43 }
44}
45
46impl Display for PyCreateView {
47 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
48 write!(
49 f,
50 "CreateView
51 name: {:?}
52 input: {:?}
53 or_replace: {:?}
54 definition: {:?}",
55 &self.create.name, &self.create.input, &self.create.or_replace, &self.create.definition,
56 )
57 }
58}
59
60#[pymethods]
61impl PyCreateView {
62 fn name(&self) -> PyResult<String> {
63 Ok(self.create.name.to_string())
64 }
65
66 fn input(&self) -> PyResult<Vec<PyLogicalPlan>> {
67 Ok(Self::inputs(self))
68 }
69
70 fn or_replace(&self) -> bool {
71 self.create.or_replace
72 }
73
74 fn definition(&self) -> PyResult<Option<String>> {
75 Ok(self.create.definition.clone())
76 }
77
78 fn __repr__(&self) -> PyResult<String> {
79 Ok(format!("CreateView({self})"))
80 }
81
82 fn __name__(&self) -> PyResult<String> {
83 Ok("CreateView".to_string())
84 }
85}
86
87impl LogicalNode for PyCreateView {
88 fn inputs(&self) -> Vec<PyLogicalPlan> {
89 vec![PyLogicalPlan::from((*self.create.input).clone())]
90 }
91
92 fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
93 self.clone().into_bound_py_any(py)
94 }
95}
96
97impl TryFrom<LogicalPlan> for PyCreateView {
98 type Error = PyErr;
99
100 fn try_from(logical_plan: LogicalPlan) -> Result<Self, Self::Error> {
101 match logical_plan {
102 LogicalPlan::Ddl(DdlStatement::CreateView(create)) => Ok(PyCreateView { create }),
103 _ => Err(py_type_err("unexpected plan")),
104 }
105 }
106}