kosiew commented on code in PR #26736: URL: https://github.com/apache/datafusion/pull/26736#discussion_r4551049938
########## datafusion/sqllogictest/src/test_context/exact_sort_pushdown.rs: ########## @@ -0,0 +1,141 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::fmt::{self, Formatter}; +use std::sync::Arc; + +use arrow::array::record_batch; +use arrow::datatypes::{self as arrow_schema, SchemaRef}; +use async_trait::async_trait; +use datafusion::catalog::{Session, TableProvider}; +use datafusion::common::Result; +use datafusion::common::tree_node::TreeNodeRecursion; +use datafusion::execution::{SendableRecordBatchStream, TaskContext}; +use datafusion::logical_expr::{Expr, TableType}; +use datafusion::physical_expr::expressions::Column; +use datafusion::physical_expr::{LexOrdering, PhysicalExpr, PhysicalSortExpr}; +use datafusion::physical_plan::test::TestMemoryExec; +use datafusion::physical_plan::{ + DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties, SortOrderPushdownResult, +}; +use datafusion::prelude::SessionContext; + +pub(super) fn register_exact_sort_source(ctx: &SessionContext) -> Result<()> { + let partitions = (0..4) + .map(|i| { + Ok(vec![record_batch!( + ("group_id", Int32, [0, 0, 1, 1]), + ("value", Int32, [i, i + 4, i + 8, i + 12]) + )?]) + }) + .collect::<Result<Vec<_>>>()?; + let input = TestMemoryExec::try_new(&partitions, partitions[0][0].schema(), None)?; + ctx.register_table("exact_sort_source", Arc::new(ExactSortSource(input)))?; + Ok(()) +} + +fn ordering() -> LexOrdering { + [ + PhysicalSortExpr::new_default(Arc::new(Column::new("group_id", 0))).nulls_last(), + PhysicalSortExpr::new_default(Arc::new(Column::new("value", 1))).nulls_last(), + ] + .into() +} + +/// Accepts exact sort pushdown without supporting fetch pushdown. Its rows are +/// already sorted within each partition; the hook makes that ordering explicit. +#[derive(Debug, Clone)] +struct ExactSortSource(TestMemoryExec); + +#[async_trait] +impl TableProvider for ExactSortSource { + fn schema(&self) -> SchemaRef { + self.0.schema() + } + + fn table_type(&self) -> TableType { + TableType::Base + } + + async fn scan( + &self, + _: &dyn Session, + projection: Option<&[usize]>, + _: &[Expr], + _: Option<usize>, + ) -> Result<Arc<dyn ExecutionPlan>> { + assert!(projection.is_none_or(|indices| indices == [0, 1])); + Ok(Arc::new(self.clone())) + } +} + +impl DisplayAs for ExactSortSource { + fn fmt_as(&self, _: DisplayFormatType, f: &mut Formatter) -> fmt::Result { + write!(f, "ExactSortSource") + } +} + +impl ExecutionPlan for ExactSortSource { + fn name(&self) -> &str { + "ExactSortSource" + } + + fn properties(&self) -> &Arc<PlanProperties> { + self.0.properties() + } + + fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> { + vec![] + } + + fn with_new_children( + self: Arc<Self>, + children: Vec<Arc<dyn ExecutionPlan>>, + ) -> Result<Arc<dyn ExecutionPlan>> { + assert!(children.is_empty()); + Ok(self) + } + + fn apply_expressions( + &self, + _: &mut dyn FnMut(&Arc<dyn PhysicalExpr>) -> Result<TreeNodeRecursion>, + ) -> Result<TreeNodeRecursion> { + Ok(TreeNodeRecursion::Continue) + } + + fn execute( + &self, + partition: usize, + context: Arc<TaskContext>, + ) -> Result<SendableRecordBatchStream> { + self.0.execute(partition, context) + } + + fn try_pushdown_sort( Review Comment: Low-priority follow-up: consider testing this source with a standalone partition-preserving sort without fetch, asserting sort elimination, no limit wrapper, and all 16 rows preserved. Existing Parquet tests already cover exact elimination without fetch, so this would only add fixture-specific coverage; an optimizer-level test may avoid an intervening `SortPreservingMergeExec`. ########## datafusion/physical-optimizer/src/pushdown_sort.rs: ########## @@ -167,11 +167,15 @@ impl PhysicalOptimizerRule for PushdownSort { // // If the SortExec carried a fetch (LIMIT), we must preserve it. // First try pushing the limit into the source via `with_fetch()`. - // If the source doesn't support `with_fetch`, fall back to - // wrapping with GlobalLimitExec. + // If the source doesn't support `with_fetch`, preserve whether + // the limit applies to each partition or to the entire input. if let Some(fetch) = sort_exec.fetch() { let inner = inner.with_fetch(Some(fetch)).unwrap_or_else(|| { - Arc::new(GlobalLimitExec::new(inner, 0, Some(fetch))) + if sort_exec.preserve_partitioning() { + Arc::new(LocalLimitExec::new(inner, fetch)) + } else { + Arc::new(GlobalLimitExec::new(inner, 0, Some(fetch))) Review Comment: Optional follow-up: could we test a standalone non-partition-preserving sort with fetch over a single-partition exact source that declines `with_fetch`? Assert that elimination retains `GlobalLimitExec` and returns exactly k rows; the plan assertion matters because `LocalLimitExec` produces identical results on one partition. An optimizer-level test may be simplest. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
