MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / lower_subqueries

Method lower_subqueries

src/sql/src/plan/lowering.rs:1571–1666  ·  view source on GitHub ↗

Applies the subqueries in the given list of scalar expressions to every distinct value of the given relation and returns a join of the given relation with all the subqueries found, and the mapping of scalar expressions with columns projected by the returned join that will hold their results.

(
        exprs: &[Self],
        id_gen: &mut mz_ore::id_gen::IdGen,
        col_map: &ColumnMap,
        cte_map: &mut CteMap,
        inner: MirRelationExpr,
        context: &Context,
    )

Source from the content-addressed store, hash-verified

1569 /// the subqueries found, and the mapping of scalar expressions with columns projected
1570 /// by the returned join that will hold their results.
1571 fn lower_subqueries(
1572 exprs: &[Self],
1573 id_gen: &mut mz_ore::id_gen::IdGen,
1574 col_map: &ColumnMap,
1575 cte_map: &mut CteMap,
1576 inner: MirRelationExpr,
1577 context: &Context,
1578 ) -> Result<(MirRelationExpr, BTreeMap<HirScalarExpr, usize>), PlanError> {
1579 let mut subquery_map = BTreeMap::new();
1580 let output = inner.let_in(id_gen, |id_gen, get_inner| {
1581 let mut subqueries = Vec::new();
1582 let distinct_inner = get_inner.clone().distinct();
1583 for expr in exprs.iter() {
1584 expr.visit_pre_post(
1585 &mut |e| match e {
1586 // For simplicity, subqueries within a conditional statement will be
1587 // lowered when lowering the conditional expression.
1588 HirScalarExpr::If { .. } => Some(vec![]),
1589 _ => None,
1590 },
1591 &mut |e| match e {
1592 HirScalarExpr::Select(expr, _name) => {
1593 let apply_requires_distinct_outer = false;
1594 let subquery = apply_scalar_subquery(
1595 id_gen,
1596 distinct_inner.clone(),
1597 col_map,
1598 cte_map,
1599 (**expr).clone(),
1600 apply_requires_distinct_outer,
1601 context,
1602 )
1603 .unwrap();
1604
1605 subqueries.push((e.clone(), subquery));
1606 }
1607 HirScalarExpr::Exists(expr, _name) => {
1608 let apply_requires_distinct_outer = false;
1609 let subquery = apply_existential_subquery(
1610 id_gen,
1611 distinct_inner.clone(),
1612 col_map,
1613 cte_map,
1614 (**expr).clone(),
1615 apply_requires_distinct_outer,
1616 context,
1617 )
1618 .unwrap();
1619 subqueries.push((e.clone(), subquery));
1620 }
1621 _ => {}
1622 },
1623 );
1624 }
1625
1626 if subqueries.is_empty() {
1627 Ok::<MirRelationExpr, PlanError>(get_inner)
1628 } else {

Callers

nothing calls this directly

Calls 15

apply_scalar_subqueryFunction · 0.85
let_inMethod · 0.80
visit_pre_postMethod · 0.80
unwrapMethod · 0.80
contains_keyMethod · 0.80
enumerateMethod · 0.80
map_column_to_globalMethod · 0.80
distinctMethod · 0.45
cloneMethod · 0.45
iterMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected