Skip to content

Commit 2ce9322

Browse files
committed
sql: instrument HirRelationExpr::lower for debugging
1 parent 85c9385 commit 2ce9322

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/sql/src/plan/lowering.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use itertools::Itertools;
4242

4343
use mz_ore::collections::CollectionExt;
4444
use mz_ore::stack::maybe_grow;
45+
use mz_repr::explain::tracing::{dbg_misc, dbg_plan, ContextHash};
4546
use mz_repr::RelationType;
4647
use mz_repr::*;
4748

@@ -179,7 +180,11 @@ impl HirRelationExpr {
179180
col_map: &ColumnMap,
180181
cte_map: &mut CteMap,
181182
) -> Result<mz_expr::MirRelationExpr, PlanError> {
182-
maybe_grow(|| {
183+
let h = ContextHash::of(&self);
184+
dbg_plan(format!("applied_to[{}].outer", h), &get_outer);
185+
dbg_plan(format!("applied_to[{}].inner", h), &self);
186+
187+
let result = maybe_grow(|| {
183188
use self::HirRelationExpr::*;
184189
use mz_expr::MirRelationExpr as SR;
185190
if let mz_expr::MirRelationExpr::Get { .. } = &get_outer {
@@ -397,6 +402,9 @@ impl HirRelationExpr {
397402
)?;
398403
input = with_subqueries;
399404

405+
dbg_misc(format!("applied_to[{}].scalars", h), scalars.len());
406+
dbg_misc(format!("applied_to[{}].old_arity", h), old_arity);
407+
400408
// We will proceed sequentially through the scalar expressions, for each transforming
401409
// the decorrelated `input` into a relation with potentially more columns capable of
402410
// addressing the needs of the scalar expression.
@@ -733,7 +741,12 @@ impl HirRelationExpr {
733741
.threshold()
734742
}
735743
})
736-
})
744+
});
745+
746+
if let Ok(result) = result.as_ref() {
747+
dbg_plan(format!("applied_to[{}].result", h), result);
748+
}
749+
result
737750
}
738751
}
739752

@@ -1355,6 +1368,12 @@ impl HirScalarExpr {
13551368
cte_map: &mut CteMap,
13561369
inner: mz_expr::MirRelationExpr,
13571370
) -> Result<(mz_expr::MirRelationExpr, BTreeMap<HirScalarExpr, usize>), PlanError> {
1371+
let h = ContextHash::of(&inner);
1372+
dbg_plan(format!("lower_subqueries[{}].inner", h), &inner);
1373+
for (i, expr) in exprs.iter().enumerate() {
1374+
dbg_plan(format!("lower_subqueries[{}].expr[{i}]", h), expr);
1375+
}
1376+
13581377
let mut subquery_map = BTreeMap::new();
13591378
let output = inner.let_in_fallible(id_gen, |id_gen, get_inner| {
13601379
let mut subqueries = Vec::new();
@@ -1442,6 +1461,8 @@ impl HirScalarExpr {
14421461
))
14431462
}
14441463
})?;
1464+
1465+
dbg_plan(format!("lower_subqueries[{:03}].result", h), &output);
14451466
Ok((output, subquery_map))
14461467
}
14471468

@@ -1518,6 +1539,9 @@ where
15181539
&mut CteMap,
15191540
) -> Result<mz_expr::MirRelationExpr, PlanError>,
15201541
{
1542+
let h = ContextHash::of(&inner);
1543+
dbg_plan(format!("branch[{}].inner", h), &inner);
1544+
15211545
// TODO: It would be nice to have a version of this code w/o optimizations,
15221546
// at the least for purposes of understanding. It was difficult for one reader
15231547
// to understand the required properties of `outer` and `col_map`.
@@ -1654,7 +1678,8 @@ where
16541678
}));
16551679
}
16561680
let new_col_map = ColumnMap::new(new_col_map);
1657-
outer.let_in_fallible(id_gen, |id_gen, get_outer| {
1681+
1682+
let result = outer.let_in_fallible(id_gen, |id_gen, get_outer| {
16581683
let keyed_outer = if key.is_empty() {
16591684
// Don't depend on outer at all if the branch is not correlated,
16601685
// which yields vastly better query plans. Note that this is a bit
@@ -1681,7 +1706,12 @@ where
16811706
.project((0..oa).chain((oa + key.len())..(oa + ba)).collect());
16821707
Ok(joined)
16831708
})
1684-
})
1709+
});
1710+
1711+
if let Ok(result) = result.as_ref() {
1712+
dbg_plan(format!("branch[{}].result", h), result);
1713+
}
1714+
result
16851715
}
16861716

16871717
fn apply_scalar_subquery(

0 commit comments

Comments
 (0)