MCPcopy Create free account
hub / github.com/apache/datafusion / optimize_subquery_sort

Function optimize_subquery_sort

datafusion/sql/src/relation/mod.rs:367–399  ·  view source on GitHub ↗
(
    plan: LogicalPlan,
    enable_subquery_sort_elimination: bool,
)

Source from the content-addressed store, hash-verified

365}
366
367fn 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}

Callers 1

create_relationMethod · 0.85

Calls 2

transform_downMethod · 0.80
SortClass · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…