xudong963 commented on code in PR #14569: URL: https://github.com/apache/datafusion/pull/14569#discussion_r1948528835
########## datafusion/physical-optimizer/src/enforce_sorting/mod.rs: ########## @@ -373,9 +373,10 @@ pub fn ensure_sorting( return adjust_window_sort_removal(requirements).map(Transformed::yes); } else if is_sort_preserving_merge(plan) && child_node.plan.output_partitioning().partition_count() <= 1 + && plan.fetch().is_none() { // This `SortPreservingMergeExec` is unnecessary, input already has a - // single partition. + // single partition and no fetch is required. let child_node = requirements.children.swap_remove(0); return Ok(Transformed::yes(child_node)); Review Comment: Another possible fix: ```rust let mut child_node = requirements.children.swap_remove(0); if let Some(fetch) = plan.fetch() { // Add the limit exec if the spm has a fetch child_node.plan = Arc::new(LocalLimitExec::new( Arc::clone(&child_node.plan), fetch, )); } return Ok(Transformed::yes(child_node)) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org