Skip to content

Commit 8425c17

Browse files
committed
Update ordering.rs
1 parent 0b18967 commit 8425c17

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

datafusion/physical-expr/src/equivalence/ordering.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,40 @@ impl OrderingEquivalenceClass {
237237
None
238238
}
239239

240-
/// Checks whether the given expression remains constant across all variations of [`SortOptions`].
240+
/// Checks whether the given expression is constant according to `self`'s ordering equivalence class.
241241
///
242-
/// This function determines if `expr` appears with every possible combination of `descending`
243-
/// and `nulls_first` options. If `expr` meets this condition, it is effectively constant.
242+
/// This function determines whether `expr` appears in at least one combination of `descending`
243+
/// and `nulls_first` options that indicate constantness. Specifically, an expression is
244+
/// considered constant if it satisfies either of the following conditions:
245+
/// - `descending & nulls_first` and `ascending & nulls_last`
246+
/// - `descending & nulls_last` and `ascending & nulls_first`
244247
///
245-
/// We primarily use `ConstExpr` to represent globally constant expressions. However, if an
246-
/// expression is only constant within secondary ordering constraints, this function helps
247-
/// identify such cases.
248+
/// We primarily use `ConstExpr` to represent globally constant expressions. However, some expressions
249+
/// may only be constant within secondary ordering constraints. This function helps identify such cases.
250+
/// If an expression is constant within a prefix ordering, it is added as a constant during
251+
/// `ordering_satisfy_requirement()` iterations after the corresponding prefix requirement is satisfied.
248252
///
249-
/// TODO: If [`SortOptions`] eventually supports encoding constantness information, this function
250-
/// may become obsolete.
253+
/// ### Example Scenarios (Assuming All Expressions Share the Same Sort Properties)
254+
///
255+
/// #### Case 1: Sort Requirement `[a, c]`
256+
/// - **Existing Orderings:** `[[a, b, c], [a, d]]`, **Constants:** `[]`
257+
/// 1. `ordering_satisfy_single()` returns `true` because the requirement `[a]` is satisfied by `[a, b, c].first()`
258+
/// 2. `[a]` is added as a constant to the existing orderings
259+
/// 3. The normalized orderings become `[[b, c], [d]]`
260+
/// 4. `ordering_satisfy_single()` returns `false` for `[c]`, as neither `[b, c]` nor `[d]` satisfies `[c]`
261+
///
262+
/// #### Case 2: Sort Requirement `[a, d]`
263+
/// - **Existing Orderings:** `[[a, b, c], [a, d]]`, **Constants:** `[]`
264+
/// 1. `ordering_satisfy_single()` returns `true` because `[a]` is satisfied by `[a, b, c].first()`
265+
/// 2. `[a]` is added as a constant to the existing orderings
266+
/// 3. The normalized orderings become `[[b, c], [d]]`
267+
/// 4. `ordering_satisfy_single()` returns `true` for `[d]`, as `[d]` satisfies `[d]`
268+
///
269+
/// ### Future Improvements
270+
/// This function may become unnecessary if any of the following improvements are implemented:
271+
/// 1. `SortOptions` supports encoding constantness information.
272+
/// 2. `EquivalenceProperties` gains `FunctionalDependency` awareness, eliminating the need for
273+
/// `Constant` and `Constraints`.
251274
pub fn is_expr_partial_const(&self, expr: &Arc<dyn PhysicalExpr>) -> bool {
252275
let mut constantness_defining_pairs = [
253276
HashSet::from([(false, false), (true, true)]),

0 commit comments

Comments
 (0)