-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
mod.rs
506 lines (441 loc) · 18.2 KB
/
mod.rs
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Serialization / Deserialization to Bytes
use crate::logical_plan::{
self, AsLogicalPlan, DefaultLogicalExtensionCodec, LogicalExtensionCodec,
};
use crate::physical_plan::{
AsExecutionPlan, DefaultPhysicalExtensionCodec, PhysicalExtensionCodec,
};
use crate::protobuf;
use datafusion::physical_plan::functions::make_scalar_function;
use datafusion_common::{DataFusionError, Result};
use datafusion_expr::{
create_udaf, create_udf, create_udwf, AggregateUDF, Expr, LogicalPlan, Volatility,
WindowUDF,
};
use prost::{
bytes::{Bytes, BytesMut},
Message,
};
use std::sync::Arc;
// Reexport Bytes which appears in the API
use datafusion::execution::registry::FunctionRegistry;
use datafusion::physical_plan::ExecutionPlan;
use datafusion::prelude::SessionContext;
mod registry;
/// Encodes something (such as [`Expr`]) to/from a stream of
/// bytes.
///
/// ```
/// use datafusion_expr::{col, lit, Expr};
/// use datafusion_proto::bytes::Serializeable;
///
/// // Create a new `Expr` a < 32
/// let expr = col("a").lt(lit(5i32));
///
/// // Convert it to an opaque form
/// let bytes = expr.to_bytes().unwrap();
///
/// // Decode bytes from somewhere (over network, etc.)
/// let decoded_expr = Expr::from_bytes(&bytes).unwrap();
/// assert_eq!(expr, decoded_expr);
/// ```
pub trait Serializeable: Sized {
/// Convert `self` to an opaque byte stream
fn to_bytes(&self) -> Result<Bytes>;
/// Convert `bytes` (the output of [`to_bytes`]) back into an
/// object. This will error if the serialized bytes contain any
/// user defined functions, in which case use
/// [`from_bytes_with_registry`]
///
/// [`to_bytes`]: Self::to_bytes
/// [`from_bytes_with_registry`]: Self::from_bytes_with_registry
fn from_bytes(bytes: &[u8]) -> Result<Self> {
Self::from_bytes_with_registry(bytes, ®istry::NoRegistry {})
}
/// Convert `bytes` (the output of [`to_bytes`]) back into an
/// object resolving user defined functions with the specified
/// `registry`
///
/// [`to_bytes`]: Self::to_bytes
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}"))
})?;
let bytes: Bytes = buffer.into();
// the produced byte stream may lead to "recursion limit" errors, see
// https://github.com/apache/arrow-datafusion/issues/3968
// Until the underlying prost issue ( https://github.com/tokio-rs/prost/issues/736 ) is fixed, we try to
// deserialize the data here and check for errors.
//
// Need to provide some placeholder registry because the stream may contain UDFs
struct PlaceHolderRegistry;
impl FunctionRegistry for PlaceHolderRegistry {
fn udfs(&self) -> std::collections::HashSet<String> {
std::collections::HashSet::default()
}
fn udf(&self, name: &str) -> Result<Arc<datafusion_expr::ScalarUDF>> {
Ok(Arc::new(create_udf(
name,
vec![],
Arc::new(arrow::datatypes::DataType::Null),
Volatility::Immutable,
make_scalar_function(|_| unimplemented!()),
)))
}
fn udaf(&self, name: &str) -> Result<Arc<AggregateUDF>> {
Ok(Arc::new(create_udaf(
name,
arrow::datatypes::DataType::Null,
Arc::new(arrow::datatypes::DataType::Null),
Volatility::Immutable,
Arc::new(|_| unimplemented!()),
Arc::new(vec![]),
)))
}
fn udwf(&self, name: &str) -> Result<Arc<WindowUDF>> {
Ok(Arc::new(create_udwf(
name,
arrow::datatypes::DataType::Null,
Arc::new(arrow::datatypes::DataType::Null),
Volatility::Immutable,
Arc::new(|| unimplemented!()),
)))
}
}
Expr::from_bytes_with_registry(&bytes, &PlaceHolderRegistry)?;
Ok(bytes)
}
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}"))
})?;
logical_plan::from_proto::parse_expr(&protobuf, registry).map_err(|e| {
DataFusionError::Plan(format!("Error parsing protobuf into Expr: {e}"))
})
}
}
/// Serialize a LogicalPlan as bytes
pub fn logical_plan_to_bytes(plan: &LogicalPlan) -> Result<Bytes> {
let extension_codec = DefaultLogicalExtensionCodec {};
logical_plan_to_bytes_with_extension_codec(plan, &extension_codec)
}
/// Serialize a LogicalPlan as JSON
#[cfg(feature = "json")]
pub fn logical_plan_to_json(plan: &LogicalPlan) -> Result<String> {
let extension_codec = DefaultLogicalExtensionCodec {};
let protobuf =
protobuf::LogicalPlanNode::try_from_logical_plan(plan, &extension_codec)
.map_err(|e| DataFusionError::Plan(format!("Error serializing plan: {e}")))?;
serde_json::to_string(&protobuf)
.map_err(|e| DataFusionError::Plan(format!("Error serializing plan: {e}")))
}
/// Serialize a LogicalPlan as bytes, using the provided 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())
}
/// Deserialize a LogicalPlan from JSON
#[cfg(feature = "json")]
pub fn logical_plan_from_json(json: &str, ctx: &SessionContext) -> Result<LogicalPlan> {
let back: protobuf::LogicalPlanNode = serde_json::from_str(json)
.map_err(|e| DataFusionError::Plan(format!("Error serializing plan: {e}")))?;
let extension_codec = DefaultLogicalExtensionCodec {};
back.try_into_logical_plan(ctx, &extension_codec)
}
/// Deserialize a LogicalPlan from bytes
pub fn logical_plan_from_bytes(
bytes: &[u8],
ctx: &SessionContext,
) -> Result<LogicalPlan> {
let extension_codec = DefaultLogicalExtensionCodec {};
logical_plan_from_bytes_with_extension_codec(bytes, ctx, &extension_codec)
}
/// Deserialize a LogicalPlan from bytes
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)
}
/// Serialize a PhysicalPlan as bytes
pub fn physical_plan_to_bytes(plan: Arc<dyn ExecutionPlan>) -> Result<Bytes> {
let extension_codec = DefaultPhysicalExtensionCodec {};
physical_plan_to_bytes_with_extension_codec(plan, &extension_codec)
}
/// Serialize a PhysicalPlan as JSON
#[cfg(feature = "json")]
pub fn physical_plan_to_json(plan: Arc<dyn ExecutionPlan>) -> Result<String> {
let extension_codec = DefaultPhysicalExtensionCodec {};
let protobuf =
protobuf::PhysicalPlanNode::try_from_physical_plan(plan, &extension_codec)
.map_err(|e| DataFusionError::Plan(format!("Error serializing plan: {e}")))?;
serde_json::to_string(&protobuf)
.map_err(|e| DataFusionError::Plan(format!("Error serializing plan: {e}")))
}
/// Serialize a PhysicalPlan as bytes, using the provided extension codec
pub fn physical_plan_to_bytes_with_extension_codec(
plan: Arc<dyn ExecutionPlan>,
extension_codec: &dyn PhysicalExtensionCodec,
) -> Result<Bytes> {
let protobuf =
protobuf::PhysicalPlanNode::try_from_physical_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())
}
/// Deserialize a PhysicalPlan from JSON
#[cfg(feature = "json")]
pub fn physical_plan_from_json(
json: &str,
ctx: &SessionContext,
) -> Result<Arc<dyn ExecutionPlan>> {
let back: protobuf::PhysicalPlanNode = serde_json::from_str(json)
.map_err(|e| DataFusionError::Plan(format!("Error serializing plan: {e}")))?;
let extension_codec = DefaultPhysicalExtensionCodec {};
back.try_into_physical_plan(ctx, &ctx.runtime_env(), &extension_codec)
}
/// Deserialize a PhysicalPlan from bytes
pub fn physical_plan_from_bytes(
bytes: &[u8],
ctx: &SessionContext,
) -> Result<Arc<dyn ExecutionPlan>> {
let extension_codec = DefaultPhysicalExtensionCodec {};
physical_plan_from_bytes_with_extension_codec(bytes, ctx, &extension_codec)
}
/// Deserialize a PhysicalPlan from bytes
pub fn physical_plan_from_bytes_with_extension_codec(
bytes: &[u8],
ctx: &SessionContext,
extension_codec: &dyn PhysicalExtensionCodec,
) -> Result<Arc<dyn ExecutionPlan>> {
let protobuf = protobuf::PhysicalPlanNode::decode(bytes).map_err(|e| {
DataFusionError::Plan(format!("Error decoding expr as protobuf: {e}"))
})?;
protobuf.try_into_physical_plan(ctx, &ctx.runtime_env(), extension_codec)
}
#[cfg(test)]
mod test {
use super::*;
use arrow::{array::ArrayRef, datatypes::DataType};
use datafusion::physical_plan::functions::make_scalar_function;
use datafusion::prelude::SessionContext;
use datafusion_expr::{col, create_udf, 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]
#[cfg(feature = "json")]
fn plan_to_json() {
use datafusion_common::DFSchema;
use datafusion_expr::logical_plan::EmptyRelation;
let plan = LogicalPlan::EmptyRelation(EmptyRelation {
produce_one_row: false,
schema: Arc::new(DFSchema::empty()),
});
let actual = logical_plan_to_json(&plan).unwrap();
let expected = r#"{"emptyRelation":{}}"#.to_string();
assert_eq!(actual, expected);
}
#[test]
#[cfg(feature = "json")]
fn json_to_plan() {
let input = r#"{"emptyRelation":{}}"#.to_string();
let ctx = SessionContext::new();
let actual = logical_plan_from_json(&input, &ctx).unwrap();
let result = matches!(actual, LogicalPlan::EmptyRelation(_));
assert!(result, "Should parse empty relation");
}
#[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();
// should explode
Expr::from_bytes(&bytes).unwrap();
}
fn roundtrip_expr(expr: &Expr) -> Expr {
let bytes = expr.to_bytes().unwrap();
Expr::from_bytes(&bytes).unwrap()
}
#[test]
fn exact_roundtrip_linearized_binary_expr() {
// (((A AND B) AND C) AND D)
let expr_ordered = col("A").and(col("B")).and(col("C")).and(col("D"));
assert_eq!(expr_ordered, roundtrip_expr(&expr_ordered));
// Ensure that no other variation becomes equal
let other_variants = vec![
// (((B AND A) AND C) AND D)
col("B").and(col("A")).and(col("C")).and(col("D")),
// (((A AND C) AND B) AND D)
col("A").and(col("C")).and(col("B")).and(col("D")),
// (((A AND B) AND D) AND C)
col("A").and(col("B")).and(col("D")).and(col("C")),
// A AND (B AND (C AND D)))
col("A").and(col("B").and(col("C").and(col("D")))),
];
for case in other_variants {
// Each variant is still equal to itself
assert_eq!(case, roundtrip_expr(&case));
// But non of them is equal to the original
assert_ne!(expr_ordered, roundtrip_expr(&case));
assert_ne!(roundtrip_expr(&expr_ordered), roundtrip_expr(&case));
}
}
#[test]
fn roundtrip_deeply_nested_binary_expr() {
// We need more stack space so this doesn't overflow in dev builds
std::thread::Builder::new()
.stack_size(10_000_000)
.spawn(|| {
let n = 100;
// a < 5
let basic_expr = col("a").lt(lit(5i32));
// (a < 5) OR (a < 5) OR (a < 5) OR ...
let or_chain = (0..n)
.fold(basic_expr.clone(), |expr, _| expr.or(basic_expr.clone()));
// (a < 5) OR (a < 5) AND (a < 5) OR (a < 5) AND (a < 5) AND (a < 5) OR ...
let expr =
(0..n).fold(or_chain.clone(), |expr, _| expr.and(or_chain.clone()));
// Should work fine.
let bytes = expr.to_bytes().unwrap();
let decoded_expr = Expr::from_bytes(&bytes).expect(
"serialization worked, so deserialization should work as well",
);
assert_eq!(decoded_expr, expr);
})
.expect("spawning thread")
.join()
.expect("joining thread");
}
#[test]
fn roundtrip_deeply_nested_binary_expr_reverse_order() {
// We need more stack space so this doesn't overflow in dev builds
std::thread::Builder::new()
.stack_size(10_000_000)
.spawn(|| {
let n = 100;
// a < 5
let expr_base = col("a").lt(lit(5i32));
// ((a < 5 AND a < 5) AND a < 5) AND ...
let and_chain =
(0..n).fold(expr_base.clone(), |expr, _| expr.and(expr_base.clone()));
// a < 5 AND (a < 5 AND (a < 5 AND ...))
let expr = expr_base.and(and_chain);
// Should work fine.
let bytes = expr.to_bytes().unwrap();
let decoded_expr = Expr::from_bytes(&bytes).expect(
"serialization worked, so deserialization should work as well",
);
assert_eq!(decoded_expr, expr);
})
.expect("spawning thread")
.join()
.expect("joining thread");
}
#[test]
fn roundtrip_deeply_nested() {
// we need more stack space so this doesn't overflow in dev builds
std::thread::Builder::new().stack_size(10_000_000).spawn(|| {
// don't know what "too much" is, so let's slowly try to increase complexity
let n_max = 100;
for n in 1..n_max {
println!("testing: {n}");
let expr_base = col("a").lt(lit(5i32));
// Generate a tree of AND and OR expressions (no subsequent ANDs or ORs).
let expr = (0..n).fold(expr_base.clone(), |expr, n| if n % 2 == 0 { expr.and(expr_base.clone()) } else { expr.or(expr_base.clone()) });
// Convert it to an opaque form
let bytes = match expr.to_bytes() {
Ok(bytes) => bytes,
Err(_) => {
// found expression that is too deeply nested
return;
}
};
// Decode bytes from somewhere (over network, etc.
let decoded_expr = Expr::from_bytes(&bytes).expect("serialization worked, so deserialization should work as well");
assert_eq!(expr, decoded_expr);
}
panic!("did not find a 'too deeply nested' expression, tested up to a depth of {n_max}")
}).expect("spawning thread").join().expect("joining thread");
}
/// return a `SessionContext` with a `dummy` function registered as a UDF
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 ctx = SessionContext::new();
ctx.register_udf(udf);
ctx
}
}