Function mz_sql::plan::lowering::variadic_left::attempt_left_join_magic

source ·
pub(crate) fn attempt_left_join_magic(
    left: &HirRelationExpr,
    rights: Vec<(&HirRelationExpr, &HirScalarExpr)>,
    id_gen: &mut IdGen,
    get_outer: MirRelationExpr,
    col_map: &ColumnMap,
    cte_map: &mut BTreeMap<LocalId, CteDesc>,
    context: &Context<'_>,
) -> Result<Option<MirRelationExpr>, PlanError>
Expand description

Attempt to render a stack of left joins as an inner join against “enriched” right relations.

This optimization applies for a contiguous block of left joins where the right term is not correlated, and where the on constraints equate columns in right to expressions over some single prior joined relation (left, or a prior right).

The plan is to enrich each right with any missing key values, extracted by applying the equated expressions to the source collection and then introducing them to an “augmented” right relation. The introduced records are augmented with null values where missing, and an additional column that indicates whether the data are original or augmented (important for masking out introduced keys).

Importantly, we need to introduce the constraints that equate columns and expressions in the Join, as a Filter will still use SQL’s equality, which treats NULL as unequal (we want them to match). We could replace each (col = expr) with (col = expr OR (col IS NULL AND expr IS NULL)).