jayzhan211 commented on code in PR #26593:
URL: https://github.com/apache/datafusion/pull/26593#discussion_r4616087748


##########
datafusion/physical-expr/src/partitioning.rs:
##########
@@ -285,9 +309,100 @@ impl RangePartitioning {
             ordering,
             samples,
             split_points,
+            start_bound: None,
+            end_bound: None,
         })
     }
 
+    /// Sets the inclusive start and exclusive end of the covered key space.
+    ///
+    /// `None` leaves that end unbounded. Bounds use the same lexicographic 
order
+    /// as the split points, including descending and null ordering. Every
+    /// retained sample must lie between the bounds, inclusive. Equality 
permits
+    /// empty first or last partitions; equal start and end bounds describe an
+    /// entirely empty range.
+    ///
+    /// This changes metadata only. The caller must ensure that every row falls
+    /// within the declared bounds and in the partition described by the split 
points.
+    ///
+    /// # Example
+    ///
+    /// Split `[10, 20, 30]` across two tasks without losing the boundary at 
`20`:
+    ///
+    /// ```
+    /// # use arrow::datatypes::{DataType, Field, Schema};
+    /// # use datafusion_common::{Result, ScalarValue};
+    /// # use datafusion_physical_expr::expressions::col;
+    /// # use datafusion_physical_expr::{LexOrdering, PhysicalSortExpr, 
RangePartitioning, SplitPoint};
+    /// # fn main() -> Result<()> {
+    /// let schema = Schema::new(vec![Field::new("x", DataType::Int64, 
false)]);
+    /// let ordering: LexOrdering =
+    ///     [PhysicalSortExpr::new_default(col("x", &schema)?)].into();
+    /// let point = |v| SplitPoint::new(vec![ScalarValue::Int64(Some(v))]);
+    ///
+    /// // x < 10 and 10 <= x < 20
+    /// let first = RangePartitioning::try_new(ordering.clone(), 
vec![point(10)])?
+    ///     .try_with_bounds(None, Some(point(20)))?;
+    /// // 20 <= x < 30 and 30 <= x
+    /// let second = RangePartitioning::try_new(ordering.clone(), 
vec![point(30)])?
+    ///     .try_with_bounds(Some(point(20)), None)?;
+    /// assert_eq!(first.end_bound(), Some(&point(20)));
+    /// assert_eq!(first.end_bound(), second.start_bound());
+    /// assert_eq!(first.start_bound(), None);
+    /// assert_eq!(second.end_bound(), None);
+    ///
+    /// // A bound must not exclude any retained samples.
+    /// assert!(RangePartitioning::try_new(ordering, vec![point(30)])?
+    ///     .try_with_bounds(Some(point(35)), None)
+    ///     .is_err());
+    /// # Ok(())
+    /// # }
+    /// ```
+    pub fn try_with_bounds(
+        mut self,
+        start_bound: Option<SplitPoint>,
+        end_bound: Option<SplitPoint>,
+    ) -> Result<Self> {
+        let sort_options = self
+            .ordering
+            .iter()
+            .map(|expr| expr.options)
+            .collect::<Vec<_>>();
+        // Samples are already ordered. Only compare each bound with the 
nearest
+        // sample, including samples omitted from the effective split points.
+        if let (Some(first), Some(last)) = (self.samples.first(), 
self.samples.last()) {
+            if let Some(start) = &start_bound {
+                validate_range_split_points(
+                    &[start.clone(), first.clone()],
+                    &sort_options,
+                )?;
+            }
+            if let Some(end) = &end_bound {
+                validate_range_split_points(&[last.clone(), end.clone()], 
&sort_options)?;
+            }
+        } else {
+            let bounds = start_bound
+                .iter()
+                .chain(end_bound.iter())
+                .cloned()
+                .collect::<Vec<_>>();
+            validate_range_split_points(&bounds, &sort_options)?;

Review Comment:
   @jayshrivastava thanks for splitting out #27384 and rebasing! Optional 
cleanup: an invalid bound currently reports `split point 0 ((35)) must be less 
than or equal to split point 1 ((30))`, for example `try_with_bounds(Some(35), 
None)` over samples `[30]`, or a bad proto payload. That names split points the 
caller never passed. Adding context keeps the detail and says which bound 
failed:
   
   ```suggestion
                   validate_range_split_points(
                       &[start.clone(), first.clone()],
                       &sort_options,
                   )
                   .map_err(|e| e.context("Invalid range partitioning start 
bound"))?;
               }
               if let Some(end) = &end_bound {
                   validate_range_split_points(&[last.clone(), end.clone()], 
&sort_options)
                       .map_err(|e| e.context("Invalid range partitioning end 
bound"))?;
               }
           } else {
               let bounds = start_bound
                   .iter()
                   .chain(end_bound.iter())
                   .cloned()
                   .collect::<Vec<_>>();
               validate_range_split_points(&bounds, &sort_options)
                   .map_err(|e| e.context("Invalid range partitioning 
bounds"))?;
   ```
   



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