Skip to content

Commit

Permalink
simplify partial constantness
Browse files Browse the repository at this point in the history
  • Loading branch information
berkaysynnada committed Feb 24, 2025
1 parent 38e057f commit 0b18967
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
13 changes: 9 additions & 4 deletions datafusion/physical-expr/src/equivalence/ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,10 @@ impl OrderingEquivalenceClass {
/// TODO: If [`SortOptions`] eventually supports encoding constantness information, this function
/// may become obsolete.
pub fn is_expr_partial_const(&self, expr: &Arc<dyn PhysicalExpr>) -> bool {
let mut variants =
HashSet::from([(false, false), (false, true), (true, false), (true, true)]);
let mut constantness_defining_pairs = [
HashSet::from([(false, false), (true, true)]),
HashSet::from([(false, true), (true, false)]),
];

for ordering in self.iter() {
if let Some(leading_ordering) = ordering.first() {
Expand All @@ -259,12 +261,15 @@ impl OrderingEquivalenceClass {
leading_ordering.options.descending,
leading_ordering.options.nulls_first,
);
variants.remove(&opt);
constantness_defining_pairs[0].remove(&opt);
constantness_defining_pairs[1].remove(&opt);
}
}
}

variants.is_empty()
constantness_defining_pairs
.iter()
.any(|pair| pair.is_empty())
}
}

Expand Down
10 changes: 4 additions & 6 deletions datafusion/physical-plan/src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ pub(crate) fn window_equivalence_properties(
let mut partition_by_orders = vec![];
for pb_order in partitioning_exprs {
let all_orders_at_current_level =
all_possible_sort_options(Arc::clone(pb_order));
sort_options_resolving_constant(Arc::clone(pb_order));
partition_by_orders.push(all_orders_at_current_level);
}
let all_orders_cartesian =
Expand Down Expand Up @@ -400,7 +400,7 @@ pub(crate) fn window_equivalence_properties(
.flat_map(|lex| {
let orderings = lex.to_vec();
let new_partial_consts =
all_possible_sort_options(Arc::new(window_col.clone()));
sort_options_resolving_constant(Arc::new(window_col.clone()));

new_partial_consts.into_iter().map(move |partial| {
let mut existing = orderings.clone();
Expand Down Expand Up @@ -475,7 +475,7 @@ pub(crate) fn window_equivalence_properties(
sliding_expr.get_aggregate_expr().expressions().to_vec();
let args_all_lex_combinations = window_fn_args
.into_iter()
.map(all_possible_sort_options)
.map(sort_options_resolving_constant)
.collect::<Vec<_>>();
let mut args_all_lexs = args_all_lex_combinations
.into_iter()
Expand Down Expand Up @@ -645,11 +645,9 @@ pub fn get_window_mode(
None
}

fn all_possible_sort_options(expr: Arc<dyn PhysicalExpr>) -> Vec<PhysicalSortExpr> {
fn sort_options_resolving_constant(expr: Arc<dyn PhysicalExpr>) -> Vec<PhysicalSortExpr> {
vec![
PhysicalSortExpr::new(Arc::clone(&expr), SortOptions::new(false, false)),
PhysicalSortExpr::new(Arc::clone(&expr), SortOptions::new(false, true)),
PhysicalSortExpr::new(Arc::clone(&expr), SortOptions::new(true, false)),
PhysicalSortExpr::new(expr, SortOptions::new(true, true)),
]
}
Expand Down

0 comments on commit 0b18967

Please sign in to comment.