mingmwang commented on code in PR #5772:
URL: https://github.com/apache/arrow-datafusion/pull/5772#discussion_r1151727664
##########
datafusion/physical-expr/src/sort_expr.rs:
##########
@@ -69,4 +62,73 @@ impl PhysicalSortExpr {
options: Some(self.options),
})
}
+
+ /// Check whether sort expression satisfies `PhysicalSortRequirement`.
+ // If sort options is Some in `PhysicalSortRequirement`, `expr` and
`options` field are compared for equality.
+ // If sort options is None in `PhysicalSortRequirement`, only `expr` is
compared for equality.
+ pub fn satisfy(&self, requirement: &PhysicalSortRequirement) -> bool {
+ self.expr.eq(&requirement.expr)
+ && requirement
+ .options
+ .map_or(true, |opts| self.options == opts)
+ }
+}
+
+/// Represents sort requirement associated with a plan
+#[derive(Clone, Debug)]
+pub struct PhysicalSortRequirement {
+ /// Physical expression representing the column to sort
+ pub expr: Arc<dyn PhysicalExpr>,
+ /// Option to specify how the given column should be sorted.
+ /// If unspecified, there is no constraint on sort options.
+ pub options: Option<SortOptions>,
+}
+
+impl From<PhysicalSortExpr> for PhysicalSortRequirement {
+ fn from(value: PhysicalSortExpr) -> Self {
+ Self {
+ expr: value.expr,
+ options: Some(value.options),
+ }
+ }
+}
+
+impl PartialEq for PhysicalSortRequirement {
+ fn eq(&self, other: &PhysicalSortRequirement) -> bool {
+ self.options == other.options && self.expr.eq(&other.expr)
+ }
+}
+
+impl std::fmt::Display for PhysicalSortRequirement {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ let opts_string = self.options.as_ref().map_or("NA", to_str);
+ write!(f, "{} {}", self.expr, opts_string)
+ }
+}
+
+impl PhysicalSortRequirement {
+ /// Returns whether this requirement is equal or more specific than
`other`.
+ pub fn compatible(&self, other: &PhysicalSortRequirement) -> bool {
+ self.expr.eq(&other.expr)
+ && other.options.map_or(true, |other_opts| {
+ self.options.map_or(false, |opts| opts == other_opts)
+ })
+ }
+}
+
Review Comment:
👍
--
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]