datafusion_python/sql/
logical.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::sync::Arc;
19
20use datafusion::logical_expr::{DdlStatement, LogicalPlan, Statement};
21use datafusion_proto::logical_plan::{AsLogicalPlan, DefaultLogicalExtensionCodec};
22use prost::Message;
23use pyo3::exceptions::PyRuntimeError;
24use pyo3::prelude::*;
25use pyo3::types::PyBytes;
26
27use crate::context::PySessionContext;
28use crate::errors::PyDataFusionResult;
29use crate::expr::aggregate::PyAggregate;
30use crate::expr::analyze::PyAnalyze;
31use crate::expr::copy_to::PyCopyTo;
32use crate::expr::create_catalog::PyCreateCatalog;
33use crate::expr::create_catalog_schema::PyCreateCatalogSchema;
34use crate::expr::create_external_table::PyCreateExternalTable;
35use crate::expr::create_function::PyCreateFunction;
36use crate::expr::create_index::PyCreateIndex;
37use crate::expr::create_memory_table::PyCreateMemoryTable;
38use crate::expr::create_view::PyCreateView;
39use crate::expr::describe_table::PyDescribeTable;
40use crate::expr::distinct::PyDistinct;
41use crate::expr::dml::PyDmlStatement;
42use crate::expr::drop_catalog_schema::PyDropCatalogSchema;
43use crate::expr::drop_function::PyDropFunction;
44use crate::expr::drop_table::PyDropTable;
45use crate::expr::drop_view::PyDropView;
46use crate::expr::empty_relation::PyEmptyRelation;
47use crate::expr::explain::PyExplain;
48use crate::expr::extension::PyExtension;
49use crate::expr::filter::PyFilter;
50use crate::expr::join::PyJoin;
51use crate::expr::limit::PyLimit;
52use crate::expr::logical_node::LogicalNode;
53use crate::expr::projection::PyProjection;
54use crate::expr::recursive_query::PyRecursiveQuery;
55use crate::expr::repartition::PyRepartition;
56use crate::expr::sort::PySort;
57use crate::expr::statement::{
58    PyDeallocate, PyExecute, PyPrepare, PySetVariable, PyTransactionEnd, PyTransactionStart,
59};
60use crate::expr::subquery::PySubquery;
61use crate::expr::subquery_alias::PySubqueryAlias;
62use crate::expr::table_scan::PyTableScan;
63use crate::expr::union::PyUnion;
64use crate::expr::unnest::PyUnnest;
65use crate::expr::values::PyValues;
66use crate::expr::window::PyWindowExpr;
67
68#[pyclass(frozen, name = "LogicalPlan", module = "datafusion", subclass)]
69#[derive(Debug, Clone)]
70pub struct PyLogicalPlan {
71    pub(crate) plan: Arc<LogicalPlan>,
72}
73
74impl PyLogicalPlan {
75    /// creates a new PyLogicalPlan
76    pub fn new(plan: LogicalPlan) -> Self {
77        Self {
78            plan: Arc::new(plan),
79        }
80    }
81
82    pub fn plan(&self) -> Arc<LogicalPlan> {
83        self.plan.clone()
84    }
85}
86
87#[pymethods]
88impl PyLogicalPlan {
89    /// Return the specific logical operator
90    pub fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
91        match self.plan.as_ref() {
92            LogicalPlan::Aggregate(plan) => PyAggregate::from(plan.clone()).to_variant(py),
93            LogicalPlan::Analyze(plan) => PyAnalyze::from(plan.clone()).to_variant(py),
94            LogicalPlan::Distinct(plan) => PyDistinct::from(plan.clone()).to_variant(py),
95            LogicalPlan::EmptyRelation(plan) => PyEmptyRelation::from(plan.clone()).to_variant(py),
96            LogicalPlan::Explain(plan) => PyExplain::from(plan.clone()).to_variant(py),
97            LogicalPlan::Extension(plan) => PyExtension::from(plan.clone()).to_variant(py),
98            LogicalPlan::Filter(plan) => PyFilter::from(plan.clone()).to_variant(py),
99            LogicalPlan::Join(plan) => PyJoin::from(plan.clone()).to_variant(py),
100            LogicalPlan::Limit(plan) => PyLimit::from(plan.clone()).to_variant(py),
101            LogicalPlan::Projection(plan) => PyProjection::from(plan.clone()).to_variant(py),
102            LogicalPlan::Sort(plan) => PySort::from(plan.clone()).to_variant(py),
103            LogicalPlan::TableScan(plan) => PyTableScan::from(plan.clone()).to_variant(py),
104            LogicalPlan::Subquery(plan) => PySubquery::from(plan.clone()).to_variant(py),
105            LogicalPlan::SubqueryAlias(plan) => PySubqueryAlias::from(plan.clone()).to_variant(py),
106            LogicalPlan::Unnest(plan) => PyUnnest::from(plan.clone()).to_variant(py),
107            LogicalPlan::Window(plan) => PyWindowExpr::from(plan.clone()).to_variant(py),
108            LogicalPlan::Repartition(plan) => PyRepartition::from(plan.clone()).to_variant(py),
109            LogicalPlan::Union(plan) => PyUnion::from(plan.clone()).to_variant(py),
110            LogicalPlan::Statement(plan) => match plan {
111                Statement::TransactionStart(plan) => {
112                    PyTransactionStart::from(plan.clone()).to_variant(py)
113                }
114                Statement::TransactionEnd(plan) => {
115                    PyTransactionEnd::from(plan.clone()).to_variant(py)
116                }
117                Statement::SetVariable(plan) => PySetVariable::from(plan.clone()).to_variant(py),
118                Statement::Prepare(plan) => PyPrepare::from(plan.clone()).to_variant(py),
119                Statement::Execute(plan) => PyExecute::from(plan.clone()).to_variant(py),
120                Statement::Deallocate(plan) => PyDeallocate::from(plan.clone()).to_variant(py),
121            },
122            LogicalPlan::Values(plan) => PyValues::from(plan.clone()).to_variant(py),
123            LogicalPlan::Dml(plan) => PyDmlStatement::from(plan.clone()).to_variant(py),
124            LogicalPlan::Ddl(plan) => match plan {
125                DdlStatement::CreateExternalTable(plan) => {
126                    PyCreateExternalTable::from(plan.clone()).to_variant(py)
127                }
128                DdlStatement::CreateMemoryTable(plan) => {
129                    PyCreateMemoryTable::from(plan.clone()).to_variant(py)
130                }
131                DdlStatement::CreateView(plan) => PyCreateView::from(plan.clone()).to_variant(py),
132                DdlStatement::CreateCatalogSchema(plan) => {
133                    PyCreateCatalogSchema::from(plan.clone()).to_variant(py)
134                }
135                DdlStatement::CreateCatalog(plan) => {
136                    PyCreateCatalog::from(plan.clone()).to_variant(py)
137                }
138                DdlStatement::CreateIndex(plan) => PyCreateIndex::from(plan.clone()).to_variant(py),
139                DdlStatement::DropTable(plan) => PyDropTable::from(plan.clone()).to_variant(py),
140                DdlStatement::DropView(plan) => PyDropView::from(plan.clone()).to_variant(py),
141                DdlStatement::DropCatalogSchema(plan) => {
142                    PyDropCatalogSchema::from(plan.clone()).to_variant(py)
143                }
144                DdlStatement::CreateFunction(plan) => {
145                    PyCreateFunction::from(plan.clone()).to_variant(py)
146                }
147                DdlStatement::DropFunction(plan) => {
148                    PyDropFunction::from(plan.clone()).to_variant(py)
149                }
150            },
151            LogicalPlan::Copy(plan) => PyCopyTo::from(plan.clone()).to_variant(py),
152            LogicalPlan::DescribeTable(plan) => PyDescribeTable::from(plan.clone()).to_variant(py),
153            LogicalPlan::RecursiveQuery(plan) => {
154                PyRecursiveQuery::from(plan.clone()).to_variant(py)
155            }
156        }
157    }
158
159    /// Get the inputs to this plan
160    fn inputs(&self) -> Vec<PyLogicalPlan> {
161        let mut inputs = vec![];
162        for input in self.plan.inputs() {
163            inputs.push(input.to_owned().into());
164        }
165        inputs
166    }
167
168    fn __repr__(&self) -> PyResult<String> {
169        Ok(format!("{:?}", self.plan))
170    }
171
172    fn display(&self) -> String {
173        format!("{}", self.plan.display())
174    }
175
176    fn display_indent(&self) -> String {
177        format!("{}", self.plan.display_indent())
178    }
179
180    fn display_indent_schema(&self) -> String {
181        format!("{}", self.plan.display_indent_schema())
182    }
183
184    fn display_graphviz(&self) -> String {
185        format!("{}", self.plan.display_graphviz())
186    }
187
188    pub fn to_proto<'py>(&'py self, py: Python<'py>) -> PyDataFusionResult<Bound<'py, PyBytes>> {
189        let codec = DefaultLogicalExtensionCodec {};
190        let proto =
191            datafusion_proto::protobuf::LogicalPlanNode::try_from_logical_plan(&self.plan, &codec)?;
192
193        let bytes = proto.encode_to_vec();
194        Ok(PyBytes::new(py, &bytes))
195    }
196
197    #[staticmethod]
198    pub fn from_proto(
199        ctx: PySessionContext,
200        proto_msg: Bound<'_, PyBytes>,
201    ) -> PyDataFusionResult<Self> {
202        let bytes: &[u8] = proto_msg.extract()?;
203        let proto_plan =
204            datafusion_proto::protobuf::LogicalPlanNode::decode(bytes).map_err(|e| {
205                PyRuntimeError::new_err(format!(
206                    "Unable to decode logical node from serialized bytes: {e}"
207                ))
208            })?;
209
210        let codec = DefaultLogicalExtensionCodec {};
211        let plan = proto_plan.try_into_logical_plan(&ctx.ctx.task_ctx(), &codec)?;
212        Ok(Self::new(plan))
213    }
214}
215
216impl From<PyLogicalPlan> for LogicalPlan {
217    fn from(logical_plan: PyLogicalPlan) -> LogicalPlan {
218        logical_plan.plan.as_ref().clone()
219    }
220}
221
222impl From<LogicalPlan> for PyLogicalPlan {
223    fn from(logical_plan: LogicalPlan) -> PyLogicalPlan {
224        PyLogicalPlan {
225            plan: Arc::new(logical_plan),
226        }
227    }
228}