Skip to content

Commit 5f1cabc

Browse files
fix: preserve equivalence join API compatibility
1 parent 9449194 commit 5f1cabc

2 files changed

Lines changed: 54 additions & 18 deletions

File tree

‎datafusion/physical-expr/src/equivalence/class.rs‎

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -814,12 +814,40 @@ impl EquivalenceGroup {
814814

815815
/// Combine equivalence groups of the given join children.
816816
pub fn join(
817+
&self,
818+
right_equivalences: &Self,
819+
join_type: &JoinType,
820+
left_size: usize,
821+
on: &[(PhysicalExprRef, PhysicalExprRef)],
822+
) -> Result<Self> {
823+
self.join_with_optional_schema(right_equivalences, join_type, left_size, None, on)
824+
}
825+
826+
/// Combine equivalence groups for a join with its output schema available.
827+
pub(crate) fn join_with_schema(
817828
&self,
818829
right_equivalences: &Self,
819830
join_type: &JoinType,
820831
left_size: usize,
821832
join_schema: &SchemaRef,
822833
on: &[(PhysicalExprRef, PhysicalExprRef)],
834+
) -> Result<Self> {
835+
self.join_with_optional_schema(
836+
right_equivalences,
837+
join_type,
838+
left_size,
839+
Some(join_schema),
840+
on,
841+
)
842+
}
843+
844+
fn join_with_optional_schema(
845+
&self,
846+
right_equivalences: &Self,
847+
join_type: &JoinType,
848+
left_size: usize,
849+
join_schema: Option<&SchemaRef>,
850+
on: &[(PhysicalExprRef, PhysicalExprRef)],
823851
) -> Result<Self> {
824852
let group = match join_type {
825853
JoinType::Inner | JoinType::Left | JoinType::Full | JoinType::Right => {
@@ -836,21 +864,25 @@ impl EquivalenceGroup {
836864
// null-extended row; expressions such as coalesce(a, 0) do
837865
// not, even when they were equivalent to a column before the
838866
// join.
839-
match join_type {
840-
JoinType::Left => {
841-
right = right
842-
.with_null_preserving_expressions(&null_batch(join_schema)?);
843-
}
844-
JoinType::Right => {
845-
left = left
846-
.with_null_preserving_expressions(&null_batch(join_schema)?);
867+
if let Some(join_schema) = join_schema {
868+
match join_type {
869+
JoinType::Left => {
870+
right = right.with_null_preserving_expressions(&null_batch(
871+
join_schema,
872+
)?);
873+
}
874+
JoinType::Right => {
875+
left = left.with_null_preserving_expressions(&null_batch(
876+
join_schema,
877+
)?);
878+
}
879+
JoinType::Full => {
880+
let batch = null_batch(join_schema)?;
881+
left = left.with_null_preserving_expressions(&batch);
882+
right = right.with_null_preserving_expressions(&batch);
883+
}
884+
_ => {}
847885
}
848-
JoinType::Full => {
849-
let batch = null_batch(join_schema)?;
850-
left = left.with_null_preserving_expressions(&batch);
851-
right = right.with_null_preserving_expressions(&batch);
852-
}
853-
_ => {}
854886
}
855887

856888
let mut result =
@@ -1033,7 +1065,7 @@ mod tests {
10331065
let left = EquivalenceGroup::new(vec![]);
10341066
let right = EquivalenceGroup::new(vec![]);
10351067

1036-
let result = left.join(&right, &JoinType::Full, 0, &schema, &[])?;
1068+
let result = left.join_with_schema(&right, &JoinType::Full, 0, &schema, &[])?;
10371069

10381070
assert_eq!(result.iter().count(), 0);
10391071
Ok(())

‎datafusion/physical-expr/src/equivalence/properties/joins.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ pub fn join_equivalence_properties(
3232
on: &[(PhysicalExprRef, PhysicalExprRef)],
3333
) -> Result<EquivalenceProperties> {
3434
let left_size = left.schema.fields.len();
35-
let equivalences =
36-
left.eq_group()
37-
.join(right.eq_group(), join_type, left_size, &join_schema, on)?;
35+
let equivalences = left.eq_group().join_with_schema(
36+
right.eq_group(),
37+
join_type,
38+
left_size,
39+
&join_schema,
40+
on,
41+
)?;
3842
let mut result = EquivalenceProperties::new(join_schema);
3943
result.add_equivalence_group(equivalences)?;
4044

0 commit comments

Comments
 (0)