Skip to main content

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