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

Function plan_map_subquery

src/sql/src/plan/query.rs:4806–4910  ·  view source on GitHub ↗
(
    ecx: &ExprContext,
    query: &Query<Aug>,
)

Source from the content-addressed store, hash-verified

4804}
4805
4806fn plan_map_subquery(
4807 ecx: &ExprContext,
4808 query: &Query<Aug>,
4809) -> Result<CoercibleScalarExpr, PlanError> {
4810 if !ecx.allow_subqueries {
4811 sql_bail!("{} does not allow subqueries", ecx.name)
4812 }
4813
4814 let mut qcx = ecx.derived_query_context();
4815 let mut query = plan_query(&mut qcx, query)?;
4816 if query.limit.is_some()
4817 || !query
4818 .offset
4819 .clone()
4820 .try_into_literal_int64()
4821 .is_ok_and(|offset| offset == 0)
4822 {
4823 query.expr = HirRelationExpr::top_k(
4824 query.expr,
4825 vec![],
4826 query.order_by.clone(),
4827 query.limit,
4828 query.offset,
4829 query.group_size_hints.limit_input_group_size,
4830 );
4831 }
4832 if query.project.len() != 2 {
4833 sql_bail!(
4834 "expected map subquery to return 2 columns, got {} columns",
4835 query.project.len()
4836 );
4837 }
4838
4839 let query_types = qcx.relation_type(&query.expr).column_types;
4840 let key_column = query.project[0];
4841 let key_type = query_types[key_column].clone().scalar_type();
4842 let value_column = query.project[1];
4843 let value_type = query_types[value_column].clone().scalar_type();
4844
4845 if key_type != SqlScalarType::String {
4846 sql_bail!("cannot build map from subquery because first column is not of type text");
4847 }
4848
4849 let aggregation_exprs: Vec<_> = iter::once(HirScalarExpr::call_variadic(
4850 RecordCreate {
4851 field_names: vec![ColumnName::from("key"), ColumnName::from("value")],
4852 },
4853 vec![
4854 HirScalarExpr::column(key_column),
4855 HirScalarExpr::column(value_column),
4856 ],
4857 ))
4858 .chain(
4859 query
4860 .order_by
4861 .iter()
4862 .map(|co| HirScalarExpr::column(co.column)),
4863 )

Callers 1

plan_expr_innerFunction · 0.85

Calls 13

plan_queryFunction · 0.85
derived_query_contextMethod · 0.80
is_someMethod · 0.80
relation_typeMethod · 0.80
cloneMethod · 0.45
lenMethod · 0.45
scalar_typeMethod · 0.45
collectMethod · 0.45
mapMethod · 0.45
iterMethod · 0.45
projectMethod · 0.45

Tested by

no test coverage detected