adriangb commented on code in PR #17337:
URL: https://github.com/apache/datafusion/pull/17337#discussion_r2330795001


##########
datafusion/optimizer/src/push_down_sort.rs:
##########
@@ -0,0 +1,580 @@
+// 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.
+
+//! [`PushDownSort`] pushes sort expressions into table scans to enable
+//! sort pushdown optimizations by table providers
+
+use std::sync::Arc;
+
+use crate::optimizer::ApplyOrder;
+use crate::{OptimizerConfig, OptimizerRule};
+
+use datafusion_common::tree_node::Transformed;
+use datafusion_common::Result;
+use datafusion_expr::logical_plan::{LogicalPlan, TableScan};
+use datafusion_expr::{Expr, SortExpr};
+
+/// Optimization rule that pushes sort expressions down to table scans
+/// when the sort can potentially be optimized by the table provider.
+///
+/// This rule looks for `Sort -> TableScan` patterns and moves the sort
+/// expressions into the `TableScan.preferred_ordering` field, allowing
+/// table providers to potentially optimize the scan based on sort 
requirements.
+///
+/// # Behavior
+///
+/// The optimizer preserves the original `Sort` node as a fallback while 
passing
+/// the ordering preference to the `TableScan` as an optimization hint. This 
ensures
+/// correctness even if the table provider cannot satisfy the requested 
ordering.
+///
+/// # Supported Sort Expressions
+///
+/// Currently, only simple column references are supported for pushdown because
+/// table providers typically cannot optimize complex expressions in sort 
operations.
+/// Complex expressions like `col("a") + col("b")` or function calls are not 
pushed down.
+///
+/// # Examples
+///
+/// ```text
+/// Before optimization:
+/// Sort: test.a ASC NULLS LAST
+///   TableScan: test
+///
+/// After optimization:
+/// Sort: test.a ASC NULLS LAST  -- Preserved as fallback
+///   TableScan: test            -- Now includes preferred_ordering hint
+/// ```
+#[derive(Default, Debug)]
+pub struct PushDownSort {}
+
+impl PushDownSort {

Review Comment:
   @alamb do you have any guidance on pushing down sorts in logical plans? I 
don't see anything which is a bit surprising, I thought it would basically 
already be implemented.



-- 
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

Reply via email to