Skip to content

Commit

Permalink
continued hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Sep 29, 2024
1 parent 7d19e41 commit 6333c3f
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions datafusion/physical-expr/src/equivalence/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ impl EquivalenceProperties {
// add version with no constants
self.oeq_class.add_new_ordering(ordering.clone());
}
println!("** result of orderings with constants: {self}");
println!("** result of orderings with constants:");
for o in self.oeq_class.iter() {
println!(" [{}]", PhysicalSortExpr::format_list(o));
}
println!("All: {self}");
self
}

Expand Down Expand Up @@ -1686,11 +1690,12 @@ impl Hash for ExprWrapper {
/// Calculates the union (in the sense of `UnionExec`) `EquivalenceProperties`
/// of `lhs` and `rhs` according to the schema of `lhs`.
///
/// Rules: The UnionExec does not interleave its inputs: instead it passes each input
/// partition from the children as its own output.
/// Rules: The UnionExec does not interleave its inputs: instead it passes each
/// input partition from the children as its own output.
///
/// Since the output equivalence properties are propertires that are true for
/// *all* output partitions, that is the same as being true for all *input* partitions
/// Since the output equivalence properties are properties that are true for
/// *all* output partitions, that is the same as being true for all *input*
/// partitions
///
fn calculate_union_binary(
mut lhs: EquivalenceProperties,
Expand All @@ -1707,16 +1712,18 @@ fn calculate_union_binary(
rhs = rhs.with_new_schema(Arc::clone(&lhs.schema))?;
}

// First, calculate valid constants for the union. A quantity is constant
// after the union if it is constant in both sides.
// First, calculate valid constants for the union. An expression is constant
// at the output of the union if it is constant in both sides.
let constants = lhs
.constants()
.iter()
.filter(|const_expr| const_exprs_contains(rhs.constants(), const_expr.expr()))
.map(|const_expr| {
// TODO: When both sides have a constant column, and the constant is the
// same, then the output properties could also reflect that the
// output is that constant as well.
// TODO: When both sides have a constant column, and the constant is
// the same, then the output properties could reflect the constant
// is valid across all partitions. However we don't track the actual
// value that the ConstExpr takes on, so we can't determine that yet

ConstExpr::new(Arc::clone(const_expr.expr())).with_across_partitions(false)
})
.collect();
Expand All @@ -1736,24 +1743,31 @@ fn calculate_union_binary(
// in both sides.
println!("Considering orderings:");
let mut orderings = vec![];
println!("** Begin lhs. Compare with {rhs}");
println!("** Begin LHS. Compare with {rhs}");
for mut ordering in lhs.normalized_oeq_class().orderings {
// Progressively shorten the ordering to search for a satisfied prefix:
println!(" ordering: {}", PhysicalSortExpr::format_list(&ordering));
println!(
" input ordering: {}",
PhysicalSortExpr::format_list(&ordering)
);
while !rhs.ordering_satisfy(&ordering) {
println!(
" Ordering {} did not satisfy {rhs}, trying suffix",
PhysicalSortExpr::format_list(&ordering)
);
ordering.pop();
}
// There is a non-trivial satisfied prefix, add it as a valid ordering:
if !ordering.is_empty() {
println!(
" FOUND ORDERING: {}",
" FOUND ORDERING (LHS): {}",
PhysicalSortExpr::format_list(&ordering)
);

orderings.push(ordering);
}
}
println!("** Begin rhs. Compare with {lhs}");
println!("** Begin RHS. Compare with {lhs}");
for mut ordering in rhs.normalized_oeq_class().orderings {
// Progressively shorten the ordering to search for a satisfied prefix:
println!(" ordering: {}", PhysicalSortExpr::format_list(&ordering));
Expand All @@ -1763,7 +1777,7 @@ fn calculate_union_binary(
// There is a non-trivial satisfied prefix, add it as a valid ordering:
if !ordering.is_empty() {
println!(
" FOUND ORDERING: {}",
" FOUND ORDERING (RHS): {}",
PhysicalSortExpr::format_list(&ordering)
);
orderings.push(ordering);
Expand Down

0 comments on commit 6333c3f

Please sign in to comment.