Generate a logical plan from an SQL query/subquery
(
&self,
query: Query,
outer_planner_context: &mut PlannerContext,
)
| 37 | impl<S: ContextProvider> SqlToRel<'_, S> { |
| 38 | /// Generate a logical plan from an SQL query/subquery |
| 39 | pub(crate) fn query_to_plan( |
| 40 | &self, |
| 41 | query: Query, |
| 42 | outer_planner_context: &mut PlannerContext, |
| 43 | ) -> Result<LogicalPlan> { |
| 44 | // Each query has its own planner context, including CTEs that are visible within that query. |
| 45 | // It also inherits the CTEs from the outer query by cloning the outer planner context. |
| 46 | let mut query_plan_context = outer_planner_context.clone(); |
| 47 | let planner_context = &mut query_plan_context; |
| 48 | |
| 49 | let Query { |
| 50 | with, |
| 51 | body, |
| 52 | order_by, |
| 53 | limit_clause, |
| 54 | fetch, |
| 55 | locks: _, |
| 56 | for_clause: _, |
| 57 | settings: _, |
| 58 | format_clause: _, |
| 59 | pipe_operators, |
| 60 | } = query; |
| 61 | |
| 62 | if fetch.is_some() { |
| 63 | return not_impl_err!("FETCH clause is not supported yet"); |
| 64 | } |
| 65 | |
| 66 | if let Some(with) = with { |
| 67 | self.plan_with_clause(with, planner_context)?; |
| 68 | } |
| 69 | |
| 70 | let set_expr = *body; |
| 71 | let plan = match set_expr { |
| 72 | SetExpr::Select(mut select) => { |
| 73 | let select_into = select.into.take(); |
| 74 | let plan = |
| 75 | self.select_to_plan(*select, order_by.clone(), planner_context)?; |
| 76 | let plan = self.limit(plan, limit_clause.clone(), planner_context)?; |
| 77 | // Process the `SELECT INTO` after `LIMIT`. |
| 78 | self.select_into(plan, select_into) |
| 79 | } |
| 80 | other => { |
| 81 | // The functions called from `set_expr_to_plan()` need more than 128KB |
| 82 | // stack in debug builds as investigated in: |
| 83 | // https://github.com/apache/datafusion/pull/13310#discussion_r1836813902 |
| 84 | let plan = { |
| 85 | // scope for dropping _guard |
| 86 | let _guard = StackGuard::new(256 * 1024); |
| 87 | self.set_expr_to_plan(other, planner_context) |
| 88 | }?; |
| 89 | let oby_exprs = to_order_by_exprs(order_by)?; |
| 90 | let order_by_rex = self.order_by_to_sort_expr( |
| 91 | oby_exprs, |
| 92 | plan.schema(), |
| 93 | planner_context, |
| 94 | true, |
| 95 | None, |
| 96 | )?; |
no test coverage detected