(
plan: LogicalPlan,
enable_subquery_sort_elimination: bool,
)
| 365 | } |
| 366 | |
| 367 | fn optimize_subquery_sort( |
| 368 | plan: LogicalPlan, |
| 369 | enable_subquery_sort_elimination: bool, |
| 370 | ) -> Result<Transformed<LogicalPlan>> { |
| 371 | if !enable_subquery_sort_elimination { |
| 372 | return Ok(Transformed::no(plan)); |
| 373 | } |
| 374 | |
| 375 | // When initializing subqueries, we examine sort options since they might be unnecessary. |
| 376 | // They are only important if the subquery result is affected by the ORDER BY statement, |
| 377 | // which can happen when we have: |
| 378 | // 1. DISTINCT ON / ARRAY_AGG ... => Handled by an `Aggregate` and its requirements. |
| 379 | // 2. RANK / ROW_NUMBER ... => Handled by a `WindowAggr` and its requirements. |
| 380 | // 3. LIMIT => Handled by a `Sort`, so we need to search for it. |
| 381 | let mut has_limit = false; |
| 382 | |
| 383 | plan.transform_down(|c| { |
| 384 | if let LogicalPlan::Limit(_) = c { |
| 385 | has_limit = true; |
| 386 | return Ok(Transformed::no(c)); |
| 387 | } |
| 388 | match c { |
| 389 | LogicalPlan::Sort(s) => { |
| 390 | if !has_limit { |
| 391 | has_limit = false; |
| 392 | return Ok(Transformed::yes(Arc::unwrap_or_clone(s.input))); |
| 393 | } |
| 394 | Ok(Transformed::no(LogicalPlan::Sort(s))) |
| 395 | } |
| 396 | _ => Ok(Transformed::no(c)), |
| 397 | } |
| 398 | }) |
| 399 | } |
no test coverage detected
searching dependent graphs…