1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use pyo3::prelude::*;
use crate::context::PySessionContext;
use crate::errors::{py_datafusion_err, DataFusionError};
use crate::sql::logical::PyLogicalPlan;
use crate::utils::wait_for_future;
use datafusion_substrait::logical_plan::{consumer, producer};
use datafusion_substrait::serializer;
use datafusion_substrait::substrait::proto::Plan;
#[pyclass(name = "plan", module = "datafusion.substrait", subclass, unsendable)]
#[derive(Debug, Clone)]
pub(crate) struct PyPlan {
pub(crate) plan: Plan,
}
impl From<PyPlan> for Plan {
fn from(plan: PyPlan) -> Plan {
plan.plan
}
}
impl From<Plan> for PyPlan {
fn from(plan: Plan) -> PyPlan {
PyPlan { plan }
}
}
#[pyclass(name = "serde", module = "datafusion.substrait", subclass, unsendable)]
#[derive(Debug, Clone)]
pub(crate) struct PySubstraitSerializer;
#[pymethods]
impl PySubstraitSerializer {
#[staticmethod]
pub fn serialize(sql: &str, ctx: PySessionContext, path: &str, py: Python) -> PyResult<()> {
wait_for_future(py, serializer::serialize(sql, &ctx.ctx, path))
.map_err(DataFusionError::from)?;
Ok(())
}
#[staticmethod]
pub fn serialize_to_plan(sql: &str, ctx: PySessionContext, py: Python) -> PyResult<PyPlan> {
match PySubstraitSerializer::serialize_bytes(sql, ctx, py) {
Ok(proto_bytes) => PySubstraitSerializer::deserialize_bytes(proto_bytes, py),
Err(e) => Err(py_datafusion_err(e)),
}
}
#[staticmethod]
pub fn serialize_bytes(sql: &str, ctx: PySessionContext, py: Python) -> PyResult<Vec<u8>> {
let proto_bytes: Vec<u8> = wait_for_future(py, serializer::serialize_bytes(sql, &ctx.ctx))
.map_err(DataFusionError::from)?;
Ok(proto_bytes)
}
#[staticmethod]
pub fn deserialize(path: &str, py: Python) -> PyResult<PyPlan> {
let plan =
wait_for_future(py, serializer::deserialize(path)).map_err(DataFusionError::from)?;
Ok(PyPlan { plan: *plan })
}
#[staticmethod]
pub fn deserialize_bytes(proto_bytes: Vec<u8>, py: Python) -> PyResult<PyPlan> {
let plan = wait_for_future(py, serializer::deserialize_bytes(proto_bytes))
.map_err(DataFusionError::from)?;
Ok(PyPlan { plan: *plan })
}
}
#[pyclass(
name = "producer",
module = "datafusion.substrait",
subclass,
unsendable
)]
#[derive(Debug, Clone)]
pub(crate) struct PySubstraitProducer;
#[pymethods]
impl PySubstraitProducer {
#[staticmethod]
pub fn to_substrait_plan(plan: PyLogicalPlan) -> PyResult<PyPlan> {
match producer::to_substrait_plan(&plan.plan) {
Ok(plan) => Ok(PyPlan { plan: *plan }),
Err(e) => Err(py_datafusion_err(e)),
}
}
}
#[pyclass(
name = "consumer",
module = "datafusion.substrait",
subclass,
unsendable
)]
#[derive(Debug, Clone)]
pub(crate) struct PySubstraitConsumer;
#[pymethods]
impl PySubstraitConsumer {
#[staticmethod]
pub fn from_substrait_plan(
ctx: &mut PySessionContext,
plan: PyPlan,
py: Python,
) -> PyResult<PyLogicalPlan> {
let result = consumer::from_substrait_plan(&mut ctx.ctx, &plan.plan);
let logical_plan = wait_for_future(py, result).map_err(DataFusionError::from)?;
Ok(PyLogicalPlan::new(logical_plan))
}
}
pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
m.add_class::<PySubstraitConsumer>()?;
m.add_class::<PySubstraitProducer>()?;
m.add_class::<PySubstraitSerializer>()?;
Ok(())
}