Skip to content

Commit 7c9ecd6

Browse files
committed
Extract writing debug for rewrite iteration to function
1 parent 15f5a12 commit 7c9ecd6

File tree

1 file changed

+69
-64
lines changed

1 file changed

+69
-64
lines changed

rust/cubesql/cubesql/src/compile/rewrite/rewriter.rs

Lines changed: 69 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,74 @@ impl IterationData<LogicalPlanLanguage, LogicalPlanAnalysis> for IterInfo {
185185
}
186186
}
187187

188+
fn write_debug_iterations(runner: &CubeRunner, stage: &str) -> Result<(), CubeError> {
189+
let dir = format!("egraph-debug-{}", stage);
190+
let _ = fs::create_dir_all(dir.clone());
191+
let _ = fs::create_dir_all(format!("{}/public", dir));
192+
let _ = fs::create_dir_all(format!("{}/src", dir));
193+
fs::copy(
194+
"egraph-debug-template/public/index.html",
195+
format!("{}/public/index.html", dir),
196+
)?;
197+
fs::copy(
198+
"egraph-debug-template/package.json",
199+
format!("{}/package.json", dir),
200+
)?;
201+
fs::copy(
202+
"egraph-debug-template/src/index.js",
203+
format!("{}/src/index.js", dir),
204+
)?;
205+
206+
let mut iterations = Vec::new();
207+
let mut last_debug_data: Option<DebugData> = None;
208+
for i in &runner.iterations {
209+
let debug_data_clone = i.data.debug_info.as_ref().unwrap().debug_data.clone();
210+
let mut debug_data = i.data.debug_info.as_ref().unwrap().debug_data.clone();
211+
if let Some(last) = last_debug_data {
212+
debug_data
213+
.nodes
214+
.retain(|n| !last.nodes.iter().any(|ln| ln.id == n.id));
215+
debug_data.edges.retain(|n| {
216+
!last
217+
.edges
218+
.iter()
219+
.any(|ln| ln.source == n.source && ln.target == n.target)
220+
});
221+
debug_data
222+
.combos
223+
.retain(|n| !last.combos.iter().any(|ln| ln.id == n.id));
224+
225+
debug_data.removed_nodes = last.nodes.clone();
226+
debug_data
227+
.removed_nodes
228+
.retain(|n| !debug_data_clone.nodes.iter().any(|ln| ln.id == n.id));
229+
debug_data.removed_edges = last.edges.clone();
230+
debug_data.removed_edges.retain(|n| {
231+
!debug_data_clone
232+
.edges
233+
.iter()
234+
.any(|ln| ln.source == n.source && ln.target == n.target)
235+
});
236+
debug_data.removed_combos = last.combos.clone();
237+
debug_data
238+
.removed_combos
239+
.retain(|n| !debug_data_clone.combos.iter().any(|ln| ln.id == n.id));
240+
}
241+
debug_data.applied_rules = Some(i.applied.iter().map(|s| format!("{:?}", s)).collect());
242+
iterations.push(debug_data);
243+
last_debug_data = Some(debug_data_clone);
244+
}
245+
fs::write(
246+
format!("{}/src/iterations.js", dir),
247+
&format!(
248+
"export const iterations = {};",
249+
serde_json::to_string_pretty(&iterations)?
250+
),
251+
)?;
252+
253+
Ok(())
254+
}
255+
188256
impl Rewriter {
189257
pub fn new(graph: CubeEGraph, cube_context: Arc<CubeContext>) -> Self {
190258
Self {
@@ -414,70 +482,7 @@ impl Rewriter {
414482
}
415483
};
416484
if IterInfo::egraph_debug_enabled() {
417-
let dir = format!("egraph-debug-{}", stage);
418-
let _ = fs::create_dir_all(dir.clone());
419-
let _ = fs::create_dir_all(format!("{}/public", dir));
420-
let _ = fs::create_dir_all(format!("{}/src", dir));
421-
fs::copy(
422-
"egraph-debug-template/public/index.html",
423-
format!("{}/public/index.html", dir),
424-
)?;
425-
fs::copy(
426-
"egraph-debug-template/package.json",
427-
format!("{}/package.json", dir),
428-
)?;
429-
fs::copy(
430-
"egraph-debug-template/src/index.js",
431-
format!("{}/src/index.js", dir),
432-
)?;
433-
434-
let mut iterations = Vec::new();
435-
let mut last_debug_data: Option<DebugData> = None;
436-
for i in &runner.iterations {
437-
let debug_data_clone = i.data.debug_info.as_ref().unwrap().debug_data.clone();
438-
let mut debug_data = i.data.debug_info.as_ref().unwrap().debug_data.clone();
439-
if let Some(last) = last_debug_data {
440-
debug_data
441-
.nodes
442-
.retain(|n| !last.nodes.iter().any(|ln| ln.id == n.id));
443-
debug_data.edges.retain(|n| {
444-
!last
445-
.edges
446-
.iter()
447-
.any(|ln| ln.source == n.source && ln.target == n.target)
448-
});
449-
debug_data
450-
.combos
451-
.retain(|n| !last.combos.iter().any(|ln| ln.id == n.id));
452-
453-
debug_data.removed_nodes = last.nodes.clone();
454-
debug_data
455-
.removed_nodes
456-
.retain(|n| !debug_data_clone.nodes.iter().any(|ln| ln.id == n.id));
457-
debug_data.removed_edges = last.edges.clone();
458-
debug_data.removed_edges.retain(|n| {
459-
!debug_data_clone
460-
.edges
461-
.iter()
462-
.any(|ln| ln.source == n.source && ln.target == n.target)
463-
});
464-
debug_data.removed_combos = last.combos.clone();
465-
debug_data
466-
.removed_combos
467-
.retain(|n| !debug_data_clone.combos.iter().any(|ln| ln.id == n.id));
468-
}
469-
debug_data.applied_rules =
470-
Some(i.applied.iter().map(|s| format!("{:?}", s)).collect());
471-
iterations.push(debug_data);
472-
last_debug_data = Some(debug_data_clone);
473-
}
474-
fs::write(
475-
format!("{}/src/iterations.js", dir),
476-
&format!(
477-
"export const iterations = {};",
478-
serde_json::to_string_pretty(&iterations)?
479-
),
480-
)?;
485+
write_debug_iterations(&runner, stage)?;
481486
}
482487
if let Some(stop_reason) = stop_reason {
483488
return Err(CubeError::user(format!(

0 commit comments

Comments
 (0)