forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposed_extension_codec.rs
More file actions
235 lines (204 loc) · 6.76 KB
/
composed_extension_codec.rs
File metadata and controls
235 lines (204 loc) · 6.76 KB
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
// 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.
//! See `main.rs` for how to run it.
//!
//! This example demonstrates how to compose multiple PhysicalExtensionCodecs
//!
//! This can be helpful when an Execution plan tree has different nodes from different crates
//! that need to be serialized.
//!
//! For example if your plan has `ShuffleWriterExec` from `datafusion-ballista` and `DeltaScan` from `deltalake`
//! both crates both provide PhysicalExtensionCodec and this example shows how to combine them together
//!
//! ```text
//! ShuffleWriterExec
//! ProjectionExec
//! ...
//! DeltaScan
//! ```
use std::any::Any;
use std::fmt::Debug;
use std::sync::Arc;
use datafusion::common::Result;
use datafusion::common::internal_err;
use datafusion::execution::TaskContext;
use datafusion::physical_plan::{DisplayAs, ExecutionPlan};
use datafusion::prelude::SessionContext;
use datafusion_proto::physical_plan::{
AsExecutionPlan, ComposedPhysicalExtensionCodec, PhysicalExtensionCodec,
};
use datafusion_proto::protobuf;
/// Example of using multiple extension codecs for serialization / deserialization
pub async fn composed_extension_codec() -> Result<()> {
// build execution plan that has both types of nodes
//
// Note each node requires a different `PhysicalExtensionCodec` to decode
let exec_plan = Arc::new(ParentExec {
input: Arc::new(ChildExec {}),
});
let ctx = SessionContext::new();
// Position in this list is important as it will be used for decoding.
// If new codec is added it should go to last position.
let composed_codec = ComposedPhysicalExtensionCodec::new(vec![
Arc::new(ParentPhysicalExtensionCodec {}),
Arc::new(ChildPhysicalExtensionCodec {}),
]);
// serialize execution plan to proto
let proto: protobuf::PhysicalPlanNode =
protobuf::PhysicalPlanNode::try_from_physical_plan(
exec_plan.clone(),
&composed_codec,
)?;
// deserialize proto back to execution plan
let result_exec_plan: Arc<dyn ExecutionPlan> =
proto.try_into_physical_plan(&ctx.task_ctx(), &composed_codec)?;
// assert that the original and deserialized execution plans are equal
assert_eq!(format!("{exec_plan:?}"), format!("{result_exec_plan:?}"));
Ok(())
}
/// This example has two types of nodes: `ParentExec` and `ChildExec` which can only
/// be serialized with different `PhysicalExtensionCodec`s
#[derive(Debug)]
struct ParentExec {
input: Arc<dyn ExecutionPlan>,
}
impl DisplayAs for ParentExec {
fn fmt_as(
&self,
_t: datafusion::physical_plan::DisplayFormatType,
f: &mut std::fmt::Formatter,
) -> std::fmt::Result {
write!(f, "ParentExec")
}
}
impl ExecutionPlan for ParentExec {
fn name(&self) -> &str {
"ParentExec"
}
fn as_any(&self) -> &dyn Any {
self
}
fn properties(&self) -> &Arc<datafusion::physical_plan::PlanProperties> {
unreachable!()
}
fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
vec![&self.input]
}
fn with_new_children(
self: Arc<Self>,
_children: Vec<Arc<dyn ExecutionPlan>>,
) -> Result<Arc<dyn ExecutionPlan>> {
unreachable!()
}
fn execute(
&self,
_partition: usize,
_context: Arc<TaskContext>,
) -> Result<datafusion::physical_plan::SendableRecordBatchStream> {
unreachable!()
}
}
/// A PhysicalExtensionCodec that can serialize and deserialize ParentExec
#[derive(Debug)]
struct ParentPhysicalExtensionCodec;
impl PhysicalExtensionCodec for ParentPhysicalExtensionCodec {
fn try_decode(
&self,
buf: &[u8],
inputs: &[Arc<dyn ExecutionPlan>],
_ctx: &TaskContext,
) -> Result<Arc<dyn ExecutionPlan>> {
if buf == "ParentExec".as_bytes() {
Ok(Arc::new(ParentExec {
input: inputs[0].clone(),
}))
} else {
internal_err!("Not supported")
}
}
fn try_encode(&self, node: Arc<dyn ExecutionPlan>, buf: &mut Vec<u8>) -> Result<()> {
if node.as_any().downcast_ref::<ParentExec>().is_some() {
buf.extend_from_slice("ParentExec".as_bytes());
Ok(())
} else {
internal_err!("Not supported")
}
}
}
#[derive(Debug)]
struct ChildExec {}
impl DisplayAs for ChildExec {
fn fmt_as(
&self,
_t: datafusion::physical_plan::DisplayFormatType,
f: &mut std::fmt::Formatter,
) -> std::fmt::Result {
write!(f, "ChildExec")
}
}
impl ExecutionPlan for ChildExec {
fn name(&self) -> &str {
"ChildExec"
}
fn as_any(&self) -> &dyn Any {
self
}
fn properties(&self) -> &Arc<datafusion::physical_plan::PlanProperties> {
unreachable!()
}
fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
vec![]
}
fn with_new_children(
self: Arc<Self>,
_children: Vec<Arc<dyn ExecutionPlan>>,
) -> Result<Arc<dyn ExecutionPlan>> {
unreachable!()
}
fn execute(
&self,
_partition: usize,
_context: Arc<TaskContext>,
) -> Result<datafusion::physical_plan::SendableRecordBatchStream> {
unreachable!()
}
}
/// A PhysicalExtensionCodec that can serialize and deserialize ChildExec
#[derive(Debug)]
struct ChildPhysicalExtensionCodec;
impl PhysicalExtensionCodec for ChildPhysicalExtensionCodec {
fn try_decode(
&self,
buf: &[u8],
_inputs: &[Arc<dyn ExecutionPlan>],
_ctx: &TaskContext,
) -> Result<Arc<dyn ExecutionPlan>> {
if buf == "ChildExec".as_bytes() {
Ok(Arc::new(ChildExec {}))
} else {
internal_err!("Not supported")
}
}
fn try_encode(&self, node: Arc<dyn ExecutionPlan>, buf: &mut Vec<u8>) -> Result<()> {
if node.as_any().downcast_ref::<ChildExec>().is_some() {
buf.extend_from_slice("ChildExec".as_bytes());
Ok(())
} else {
internal_err!("Not supported")
}
}
}