(
ecx: &ExprContext,
query: &Query<Aug>,
)
| 4804 | } |
| 4805 | |
| 4806 | fn 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 | ) |
no test coverage detected