This repository was archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcodec.rs
211 lines (201 loc) · 8.55 KB
/
codec.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
// 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.
use crate::protobuf::ray_sql_exec_node::PlanType;
use crate::protobuf::{
RayShuffleReaderExecNode, RayShuffleWriterExecNode, RaySqlExecNode, ShuffleReaderExecNode,
ShuffleWriterExecNode,
};
use crate::shuffle::{
RayShuffleReaderExec, RayShuffleWriterExec, ShuffleReaderExec, ShuffleWriterExec,
};
use datafusion::arrow::datatypes::SchemaRef;
use datafusion::common::{DataFusionError, Result};
use datafusion::execution::runtime_env::RuntimeEnv;
use datafusion::execution::FunctionRegistry;
use datafusion::logical_expr::{AggregateUDF, ScalarUDF, WindowUDF};
use datafusion::physical_plan::{ExecutionPlan, Partitioning};
use datafusion_proto::physical_plan::from_proto::parse_protobuf_hash_partitioning;
use datafusion_proto::physical_plan::AsExecutionPlan;
use datafusion_proto::physical_plan::PhysicalExtensionCodec;
use datafusion_proto::protobuf;
use datafusion_proto::protobuf::{PhysicalHashRepartition, PhysicalPlanNode};
use prost::Message;
use std::collections::HashSet;
use std::sync::Arc;
#[derive(Debug)]
pub struct ShuffleCodec {}
impl PhysicalExtensionCodec for ShuffleCodec {
fn try_decode(
&self,
buf: &[u8],
_inputs: &[Arc<dyn ExecutionPlan>],
registry: &dyn FunctionRegistry,
) -> Result<Arc<dyn ExecutionPlan>, DataFusionError> {
// decode bytes to protobuf struct
let node = RaySqlExecNode::decode(buf)
.map_err(|e| DataFusionError::Internal(format!("failed to decode plan: {e:?}")))?;
match node.plan_type {
Some(PlanType::ShuffleReader(reader)) => {
let schema = reader.schema.as_ref().unwrap();
let schema: SchemaRef = Arc::new(schema.try_into().unwrap());
let hash_part = parse_protobuf_hash_partitioning(
reader.partitioning.as_ref(),
registry,
&schema,
)?;
Ok(Arc::new(ShuffleReaderExec::new(
reader.stage_id as usize,
schema,
hash_part.unwrap(),
&reader.shuffle_dir,
)))
}
Some(PlanType::ShuffleWriter(writer)) => {
let plan = writer.plan.unwrap().try_into_physical_plan(
registry,
&RuntimeEnv::default(),
self,
)?;
let hash_part = parse_protobuf_hash_partitioning(
writer.partitioning.as_ref(),
registry,
plan.schema().as_ref(),
)?;
Ok(Arc::new(ShuffleWriterExec::new(
writer.stage_id as usize,
plan,
hash_part.unwrap(),
&writer.shuffle_dir,
)))
}
Some(PlanType::RayShuffleReader(reader)) => {
let schema = reader.schema.as_ref().unwrap();
let schema: SchemaRef = Arc::new(schema.try_into().unwrap());
let hash_part = parse_protobuf_hash_partitioning(
reader.partitioning.as_ref(),
registry,
&schema,
)?;
Ok(Arc::new(RayShuffleReaderExec::new(
reader.stage_id as usize,
schema,
hash_part.unwrap(),
)))
}
Some(PlanType::RayShuffleWriter(writer)) => {
let plan = writer.plan.unwrap().try_into_physical_plan(
registry,
&RuntimeEnv::default(),
self,
)?;
let hash_part = parse_protobuf_hash_partitioning(
writer.partitioning.as_ref(),
registry,
plan.schema().as_ref(),
)?;
Ok(Arc::new(RayShuffleWriterExec::new(
writer.stage_id as usize,
plan,
hash_part.unwrap(),
)))
}
_ => unreachable!(),
}
}
fn try_encode(
&self,
node: Arc<dyn ExecutionPlan>,
buf: &mut Vec<u8>,
) -> Result<(), DataFusionError> {
let plan = if let Some(reader) = node.as_any().downcast_ref::<ShuffleReaderExec>() {
let schema: protobuf::Schema = reader.schema().try_into().unwrap();
let partitioning = encode_partitioning_scheme(&reader.output_partitioning())?;
let reader = ShuffleReaderExecNode {
stage_id: reader.stage_id as u32,
schema: Some(schema),
partitioning: Some(partitioning),
shuffle_dir: reader.shuffle_dir.clone(),
};
PlanType::ShuffleReader(reader)
} else if let Some(writer) = node.as_any().downcast_ref::<ShuffleWriterExec>() {
let plan = PhysicalPlanNode::try_from_physical_plan(writer.plan.clone(), self)?;
let partitioning = encode_partitioning_scheme(&writer.output_partitioning())?;
let writer = ShuffleWriterExecNode {
stage_id: writer.stage_id as u32,
plan: Some(plan),
partitioning: Some(partitioning),
shuffle_dir: writer.shuffle_dir.clone(),
};
PlanType::ShuffleWriter(writer)
} else if let Some(reader) = node.as_any().downcast_ref::<RayShuffleReaderExec>() {
let schema: protobuf::Schema = reader.schema().try_into().unwrap();
let partitioning = encode_partitioning_scheme(&reader.output_partitioning())?;
let reader = RayShuffleReaderExecNode {
stage_id: reader.stage_id as u32,
schema: Some(schema),
partitioning: Some(partitioning),
};
PlanType::RayShuffleReader(reader)
} else if let Some(writer) = node.as_any().downcast_ref::<RayShuffleWriterExec>() {
let plan = PhysicalPlanNode::try_from_physical_plan(writer.plan.clone(), self)?;
let partitioning = encode_partitioning_scheme(&writer.output_partitioning())?;
let writer = RayShuffleWriterExecNode {
stage_id: writer.stage_id as u32,
plan: Some(plan),
partitioning: Some(partitioning),
};
PlanType::RayShuffleWriter(writer)
} else {
unreachable!()
};
plan.encode(buf);
Ok(())
}
}
fn encode_partitioning_scheme(partitioning: &Partitioning) -> Result<PhysicalHashRepartition> {
match partitioning {
Partitioning::Hash(expr, partition_count) => Ok(protobuf::PhysicalHashRepartition {
hash_expr: expr
.iter()
.map(|expr| expr.clone().try_into())
.collect::<Result<Vec<_>, DataFusionError>>()?,
partition_count: *partition_count as u64,
}),
Partitioning::UnknownPartitioning(n) => Ok(protobuf::PhysicalHashRepartition {
hash_expr: vec![],
partition_count: *n as u64,
}),
other => Err(DataFusionError::Plan(format!(
"Unsupported shuffle partitioning scheme: {other:?}"
))),
}
}
struct RaySqlFunctionRegistry {}
impl FunctionRegistry for RaySqlFunctionRegistry {
fn udfs(&self) -> HashSet<String> {
HashSet::new()
}
fn udf(&self, name: &str) -> datafusion::common::Result<Arc<ScalarUDF>> {
Err(DataFusionError::Plan(format!("Invalid UDF: {name}")))
}
fn udaf(&self, name: &str) -> datafusion::common::Result<Arc<AggregateUDF>> {
Err(DataFusionError::Plan(format!("Invalid UDAF: {name}")))
}
fn udwf(&self, name: &str) -> datafusion::common::Result<Arc<WindowUDF>> {
Err(DataFusionError::Plan(format!("Invalid UDAWF: {name}")))
}
}