alamb commented on code in PR #27833:
URL: https://github.com/apache/datafusion/pull/27833#discussion_r4760157399


##########
datafusion/physical-optimizer/src/output_requirements.rs:
##########
@@ -36,39 +32,64 @@ use datafusion_physical_plan::{
     SendableRecordBatchStream, StatisticsArgs,
 };
 
-/// This rule either adds or removes [`OutputRequirements`]s to/from the 
physical
-/// plan according to its `mode` attribute, which is set by the constructors
-/// `new_add_mode` and `new_remove_mode`. With this rule, we can keep track of
-/// the global requirements (ordering and distribution) across rules.
+/// OptimizerRule that preserves the final query output ordering and
+/// distribution requirements across physical plan rewrites.
 ///
-/// The primary use case of this node and rule is to specify and preserve the 
desired output
-/// ordering and distribution the entire plan. When sending to a single 
client, a single partition may
-/// be desirable, but when sending to a multi-partitioned writer, keeping 
multiple partitions may be
-/// better.
+/// This rule is needed because the optimizer may rewrite the plan in ways 
that lose
+/// the original ordering and distribution requirements.
+///
+/// # Example
+///
+/// For example, the query `SELECT a FROM t ORDER BY a` requires that the final
+/// output be sorted by `a`.
+///
+/// If a single partition data source is already sorted by `a`, the simplified
+/// physical plan starts with a sort above the scan:
+///
+/// ```text
+/// SortExec: expr=[a@0 ASC NULLS LAST]
+///   DataSourceExec: projection=[a], output_ordering=[a@0 ASC NULLS LAST]
+/// ```
+///
+/// The optimizer can remove the redundant sort because the scan already
+/// produces the requested order.
+/// ```text
+/// DataSourceExec: projection=[a], output_ordering=[a@0 ASC NULLS LAST]
+/// ```
+///
+/// However, other rewrites might split the scan across multiple partitions,
+/// which could produce a final output that is not globally sorted. In order to
+/// prevent such a rewrite, this rule adds a [`OutputRequirementExec`] above 
the
+/// original sort to record the output requirements. The optimizer can then
+/// remove the sort, and the output requirements remain encoded in the plan.

Review Comment:
   Right, I was trying to keep the description generic rather than describe 
that one particular case. 
   
   I think your proposal is still pretty general though, so 👍 



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

Reply via email to