Function mz_sql::plan::transform_expr::split_subquery_predicates

source ยท
pub fn split_subquery_predicates(expr: &mut HirRelationExpr)
Expand description

Rewrites predicates that contain subqueries so that the subqueries appear in their own later predicate when possible.

For example, this function rewrites this expression

Filter {
    predicates: [a = b AND EXISTS (<subquery 1>) AND c = d AND (<subquery 2>) = e]
}

like so:

Filter {
    predicates: [
        a = b AND c = d,
        EXISTS (<subquery>),
        (<subquery 2>) = e,
    ]
}

The rewrite causes decorrelation to incorporate prior predicates into the outer relation upon which the subquery is evaluated. In the above rewritten example, the EXISTS (<subquery>) will only be evaluated for outer rows where a = b AND c = d. The second subquery, (<subquery 2>) = e, will be further restricted to outer rows that match A = b AND c = d AND EXISTS(<subquery>). This can vastly reduce the cost of the subquery, especially when the original conjunction contains join keys.