@@ -42,6 +42,7 @@ use itertools::Itertools;
42
42
43
43
use mz_ore:: collections:: CollectionExt ;
44
44
use mz_ore:: stack:: maybe_grow;
45
+ use mz_repr:: explain:: tracing:: { dbg_misc, dbg_plan, ContextHash } ;
45
46
use mz_repr:: RelationType ;
46
47
use mz_repr:: * ;
47
48
@@ -179,7 +180,11 @@ impl HirRelationExpr {
179
180
col_map : & ColumnMap ,
180
181
cte_map : & mut CteMap ,
181
182
) -> 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 ( || {
183
188
use self :: HirRelationExpr :: * ;
184
189
use mz_expr:: MirRelationExpr as SR ;
185
190
if let mz_expr:: MirRelationExpr :: Get { .. } = & get_outer {
@@ -397,6 +402,9 @@ impl HirRelationExpr {
397
402
) ?;
398
403
input = with_subqueries;
399
404
405
+ dbg_misc ( format ! ( "applied_to[{}].scalars" , h) , scalars. len ( ) ) ;
406
+ dbg_misc ( format ! ( "applied_to[{}].old_arity" , h) , old_arity) ;
407
+
400
408
// We will proceed sequentially through the scalar expressions, for each transforming
401
409
// the decorrelated `input` into a relation with potentially more columns capable of
402
410
// addressing the needs of the scalar expression.
@@ -733,7 +741,12 @@ impl HirRelationExpr {
733
741
. threshold ( )
734
742
}
735
743
} )
736
- } )
744
+ } ) ;
745
+
746
+ if let Ok ( result) = result. as_ref ( ) {
747
+ dbg_plan ( format ! ( "applied_to[{}].result" , h) , result) ;
748
+ }
749
+ result
737
750
}
738
751
}
739
752
@@ -1355,6 +1368,12 @@ impl HirScalarExpr {
1355
1368
cte_map : & mut CteMap ,
1356
1369
inner : mz_expr:: MirRelationExpr ,
1357
1370
) -> 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
+
1358
1377
let mut subquery_map = BTreeMap :: new ( ) ;
1359
1378
let output = inner. let_in_fallible ( id_gen, |id_gen, get_inner| {
1360
1379
let mut subqueries = Vec :: new ( ) ;
@@ -1442,6 +1461,8 @@ impl HirScalarExpr {
1442
1461
) )
1443
1462
}
1444
1463
} ) ?;
1464
+
1465
+ dbg_plan ( format ! ( "lower_subqueries[{:03}].result" , h) , & output) ;
1445
1466
Ok ( ( output, subquery_map) )
1446
1467
}
1447
1468
@@ -1518,6 +1539,9 @@ where
1518
1539
& mut CteMap ,
1519
1540
) -> Result < mz_expr:: MirRelationExpr , PlanError > ,
1520
1541
{
1542
+ let h = ContextHash :: of ( & inner) ;
1543
+ dbg_plan ( format ! ( "branch[{}].inner" , h) , & inner) ;
1544
+
1521
1545
// TODO: It would be nice to have a version of this code w/o optimizations,
1522
1546
// at the least for purposes of understanding. It was difficult for one reader
1523
1547
// to understand the required properties of `outer` and `col_map`.
@@ -1654,7 +1678,8 @@ where
1654
1678
} ) ) ;
1655
1679
}
1656
1680
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| {
1658
1683
let keyed_outer = if key. is_empty ( ) {
1659
1684
// Don't depend on outer at all if the branch is not correlated,
1660
1685
// which yields vastly better query plans. Note that this is a bit
@@ -1681,7 +1706,12 @@ where
1681
1706
. project ( ( 0 ..oa) . chain ( ( oa + key. len ( ) ) ..( oa + ba) ) . collect ( ) ) ;
1682
1707
Ok ( joined)
1683
1708
} )
1684
- } )
1709
+ } ) ;
1710
+
1711
+ if let Ok ( result) = result. as_ref ( ) {
1712
+ dbg_plan ( format ! ( "branch[{}].result" , h) , result) ;
1713
+ }
1714
+ result
1685
1715
}
1686
1716
1687
1717
fn apply_scalar_subquery (
0 commit comments