-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution.rs
More file actions
281 lines (256 loc) · 9.5 KB
/
Copy pathexecution.rs
File metadata and controls
281 lines (256 loc) · 9.5 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
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
/// Implementation of PlanVisitor for DataFusion ExecutionPlan nodes.
use crate::visitor::PlanVisitor;
use crate::{JoinAlgorithm, LeafNode, PlanNodeMetadata};
use datafusion::arrow::datatypes::Schema;
use datafusion::execution::context::SessionContext;
use datafusion::physical_plan::ExecutionPlan;
use datafusion::physical_plan::joins::{HashJoinExec, NestedLoopJoinExec};
use datafusion::physical_plan::test::exec::MockExec;
use datafusion::physical_plan::{ExecutionPlanVisitor, accept};
use std::collections::HashSet;
use std::sync::Arc;
use tracing::{debug, trace, warn};
pub struct DFExecutionPlanVisitor {
metadata: Vec<PlanNodeMetadata>,
ctx: Arc<SessionContext>,
}
impl DFExecutionPlanVisitor {
pub fn new(ctx: Arc<SessionContext>) -> Self {
DFExecutionPlanVisitor {
metadata: Vec::new(),
ctx,
}
}
// Doesn't work if tables have same name
async fn _find_table_name_by_schema(
&self,
target_schema: &Schema,
) -> Option<String> {
for catalog_name in self.ctx.catalog_names() {
let catalog = self.ctx.catalog(&catalog_name)?;
for schema_name in catalog.schema_names() {
let schema_provider = catalog.schema(&schema_name)?;
for table_name in schema_provider.table_names() {
if let Some(table) =
schema_provider.table(&table_name).await.ok().unwrap()
{
// Create a set of tables columns
let schema = table.schema();
let a_cols = schema
.fields()
.iter()
.map(|f| f.name())
.collect::<HashSet<_>>()
.clone();
let b_cols = target_schema
.fields()
.iter()
.map(|f| f.name())
.collect::<Vec<_>>();
// Check if the table's schema matches the target schema
let mut is_match = true;
for col in b_cols {
if !a_cols.contains(col) {
is_match = false;
break;
}
}
if is_match {
return Some(table_name.clone());
}
}
}
}
}
None
}
}
impl PlanVisitor<dyn ExecutionPlan> for DFExecutionPlanVisitor {
fn visit(&mut self, plan: &dyn ExecutionPlan) -> Vec<PlanNodeMetadata> {
// TODO: Making another temp visitor isn't the best way to do this, try something else later
let mut visitor = DFExecutionPlanVisitor::new(self.ctx.clone());
debug!("stats: {:?}", plan.partition_statistics(None));
// Visit the plan and collect metadata
accept(plan, &mut visitor).unwrap();
// print!("{:?}", visitor.metadata);
visitor.metadata
}
}
impl ExecutionPlanVisitor for DFExecutionPlanVisitor {
type Error = datafusion::error::DataFusionError;
fn pre_visit<'a>(
&mut self,
_plan: &'a dyn ExecutionPlan,
) -> Result<bool, Self::Error> {
// We don't use previsit, but must implement
Ok(true)
}
/// Visit all nodes of a plan in post order, saving information related to joins.
///
/// Consider the following join tree:
/// ```text
///
/// HashJoin
/// / \
/// HashJoin NestedLoopJoin
/// / \ / \
/// A B C D
///```
/// When we visit the nodes in post order, we will visit A, B, HashJoin(AB), C, D, NestedLoopJoin(CD), HashJoin(AB, CD)
///
/// We implement the post_visit to collect the required metadata from each node and save it to output
///
fn post_visit(
&mut self,
plan: &dyn ExecutionPlan,
) -> Result<bool, Self::Error> {
// TODO: Replace this with a trait that maps nodes to NodeMetadatas
match plan.name() {
"NestedLoopJoinExec" => {
trace!("Found NestedLoopJoinExec");
if let Some(_exec) =
plan.as_any().downcast_ref::<NestedLoopJoinExec>()
{
self.metadata.push(PlanNodeMetadata::Join {
join_algorithm: JoinAlgorithm::NestedLoopJoin,
join_card: None,
memoize: None,
});
}
}
"HashJoinExec" => {
trace!("Found HashJoinExec");
if let Some(_exec) =
plan.as_any().downcast_ref::<HashJoinExec>()
{
self.metadata.push(PlanNodeMetadata::Join {
join_algorithm: JoinAlgorithm::HashJoin,
join_card: None,
memoize: None,
});
}
}
"DataSourceExec" => {
trace!("Found DataSourceExec");
// println!("{:?}", plan);
// let target_schema = plan.schema();
// let table_name_future = self.find_table_name_by_schema(target_schema.as_ref());
// let table_name = block_on(table_name_future);
// Extract table name from csv
let file_name = plan
.as_any()
.downcast_ref::<datafusion::datasource::memory::DataSourceExec>()
.unwrap()
.data_source()
.as_any()
.downcast_ref::<datafusion::datasource::physical_plan::FileScanConfig>()
.unwrap()
.file_groups
.get(0)
.unwrap()
.files()
.get(0)
.unwrap()
.path()
.filename()
.unwrap();
trace!("File name: {:?}", file_name);
let mut table_name: Option<String> = None;
if file_name.ends_with(".csv") {
table_name =
Some(file_name.replace(".csv", "").to_string());
}
if let Some(name) = table_name {
self.metadata
.push(PlanNodeMetadata::Leaf(LeafNode::new(name)));
} else {
warn!(
"Could not find table name for schema: {:?}",
plan.schema()
);
}
}
"MockExec" => {
trace!("Found MockExec");
let table_name = plan
.as_any()
.downcast_ref::<MockExec>()
.unwrap()
.schema()
.metadata()
.get("table_name")
.unwrap()
.clone();
self.metadata
.push(PlanNodeMetadata::Leaf(LeafNode::new(table_name)));
}
_ => {
/* No hint for other plan types */
trace!("Skipping plan node: {}", plan.name());
}
}
Ok(true)
}
}
#[cfg(test)]
mod tests {
use super::*;
use datafusion::arrow::datatypes::{DataType, Field, Schema};
use datafusion::common::NullEquality;
use datafusion::physical_expr::expressions::Column;
use datafusion::physical_plan::joins::{HashJoinExec, PartitionMode};
use datafusion::prelude::JoinType;
use std::collections::HashMap;
use std::sync::Arc;
use std::vec;
#[test]
fn basic_post_visit_test() {
let ctx = Arc::new(SessionContext::new());
let mut visitor = DFExecutionPlanVisitor::new(ctx.clone());
// Init a very simple DF execution plan
// ```text
// HashJoin
// / \
// A B
// ```
let a = MockExec::new(
vec![],
Arc::new(Schema::new_with_metadata(
vec![Field::new("A", DataType::Utf8, false)],
HashMap::from([("table_name".to_string(), "A".to_string())]),
)),
);
let b = MockExec::new(
vec![],
Arc::new(Schema::new_with_metadata(
vec![Field::new("B", DataType::Utf8, false)],
HashMap::from([("table_name".to_string(), "B".to_string())]),
)),
);
let hash_join = HashJoinExec::try_new(
Arc::new(a),
Arc::new(b),
vec![(
Arc::new(Column::new("A", 0)),
Arc::new(Column::new("B", 0)),
)],
None,
&JoinType::Inner,
None,
PartitionMode::Auto,
NullEquality::NullEqualsNull,
)
.unwrap();
let nodes = visitor.visit(&hash_join);
let expected_nodes = vec![
PlanNodeMetadata::Leaf(LeafNode::new("A".to_string())),
PlanNodeMetadata::Leaf(LeafNode::new("B".to_string())),
PlanNodeMetadata::Join {
join_algorithm: JoinAlgorithm::HashJoin,
join_card: None,
memoize: None,
},
];
assert_eq!(nodes, expected_nodes);
}
}