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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::{
from_proto::parse_expr,
logical_plan::{AsLogicalPlan, LogicalExtensionCodec},
protobuf,
};
use datafusion_common::{DataFusionError, Result};
use datafusion_expr::{Expr, LogicalPlan};
use prost::{
bytes::{Bytes, BytesMut},
Message,
};
use datafusion::logical_plan::FunctionRegistry;
use datafusion::prelude::SessionContext;
use datafusion_expr::logical_plan::Extension;
mod registry;
pub trait Serializeable: Sized {
fn to_bytes(&self) -> Result<Bytes>;
fn from_bytes(bytes: &[u8]) -> Result<Self> {
Self::from_bytes_with_registry(bytes, ®istry::NoRegistry {})
}
fn from_bytes_with_registry(
bytes: &[u8],
registry: &dyn FunctionRegistry,
) -> Result<Self>;
}
impl Serializeable for Expr {
fn to_bytes(&self) -> Result<Bytes> {
let mut buffer = BytesMut::new();
let protobuf: protobuf::LogicalExprNode = self.try_into().map_err(|e| {
DataFusionError::Plan(format!("Error encoding expr as protobuf: {}", e))
})?;
protobuf.encode(&mut buffer).map_err(|e| {
DataFusionError::Plan(format!("Error encoding protobuf as bytes: {}", e))
})?;
Ok(buffer.into())
}
fn from_bytes_with_registry(
bytes: &[u8],
registry: &dyn FunctionRegistry,
) -> Result<Self> {
let protobuf = protobuf::LogicalExprNode::decode(bytes).map_err(|e| {
DataFusionError::Plan(format!("Error decoding expr as protobuf: {}", e))
})?;
parse_expr(&protobuf, registry).map_err(|e| {
DataFusionError::Plan(format!("Error parsing protobuf into Expr: {}", e))
})
}
}
pub fn logical_plan_to_bytes(plan: &LogicalPlan) -> Result<Bytes> {
let extension_codec = DefaultExtensionCodec {};
logical_plan_to_bytes_with_extension_codec(plan, &extension_codec)
}
pub fn logical_plan_to_bytes_with_extension_codec(
plan: &LogicalPlan,
extension_codec: &dyn LogicalExtensionCodec,
) -> Result<Bytes> {
let protobuf =
protobuf::LogicalPlanNode::try_from_logical_plan(plan, extension_codec)?;
let mut buffer = BytesMut::new();
protobuf.encode(&mut buffer).map_err(|e| {
DataFusionError::Plan(format!("Error encoding protobuf as bytes: {}", e))
})?;
Ok(buffer.into())
}
pub fn logical_plan_from_bytes(
bytes: &[u8],
ctx: &SessionContext,
) -> Result<LogicalPlan> {
let extension_codec = DefaultExtensionCodec {};
logical_plan_from_bytes_with_extension_codec(bytes, ctx, &extension_codec)
}
pub fn logical_plan_from_bytes_with_extension_codec(
bytes: &[u8],
ctx: &SessionContext,
extension_codec: &dyn LogicalExtensionCodec,
) -> Result<LogicalPlan> {
let protobuf = protobuf::LogicalPlanNode::decode(bytes).map_err(|e| {
DataFusionError::Plan(format!("Error decoding expr as protobuf: {}", e))
})?;
protobuf.try_into_logical_plan(ctx, extension_codec)
}
#[derive(Debug)]
struct DefaultExtensionCodec {}
impl LogicalExtensionCodec for DefaultExtensionCodec {
fn try_decode(
&self,
_buf: &[u8],
_inputs: &[LogicalPlan],
_ctx: &SessionContext,
) -> Result<Extension> {
Err(DataFusionError::NotImplemented(
"No extension codec provided".to_string(),
))
}
fn try_encode(&self, _node: &Extension, _buf: &mut Vec<u8>) -> Result<()> {
Err(DataFusionError::NotImplemented(
"No extension codec provided".to_string(),
))
}
}
#[cfg(test)]
mod test {
use super::*;
use arrow::{array::ArrayRef, datatypes::DataType};
use datafusion::prelude::SessionContext;
use datafusion::{
logical_plan::create_udf, physical_plan::functions::make_scalar_function,
};
use datafusion_expr::{lit, Volatility};
use std::sync::Arc;
#[test]
#[should_panic(
expected = "Error decoding expr as protobuf: failed to decode Protobuf message"
)]
fn bad_decode() {
Expr::from_bytes(b"Leet").unwrap();
}
#[test]
fn udf_roundtrip_with_registry() {
let ctx = context_with_udf();
let expr = ctx
.udf("dummy")
.expect("could not find udf")
.call(vec![lit("")]);
let bytes = expr.to_bytes().unwrap();
let deserialized_expr = Expr::from_bytes_with_registry(&bytes, &ctx).unwrap();
assert_eq!(expr, deserialized_expr);
}
#[test]
#[should_panic(
expected = "No function registry provided to deserialize, so can not deserialize User Defined Function 'dummy'"
)]
fn udf_roundtrip_without_registry() {
let ctx = context_with_udf();
let expr = ctx
.udf("dummy")
.expect("could not find udf")
.call(vec![lit("")]);
let bytes = expr.to_bytes().unwrap();
Expr::from_bytes(&bytes).unwrap();
}
fn context_with_udf() -> SessionContext {
let fn_impl = |args: &[ArrayRef]| Ok(Arc::new(args[0].clone()) as ArrayRef);
let scalar_fn = make_scalar_function(fn_impl);
let udf = create_udf(
"dummy",
vec![DataType::Utf8],
Arc::new(DataType::Utf8),
Volatility::Immutable,
scalar_fn,
);
let mut ctx = SessionContext::new();
ctx.register_udf(udf);
ctx
}
}