Skip to content

Commit cfb8594

Browse files
committed
refactor(cubesql): Simplify is_proper_cube_join_condition
1 parent f8918e7 commit cfb8594

File tree

1 file changed

+29
-65
lines changed
  • rust/cubesql/cubesql/src/compile/rewrite/rules

1 file changed

+29
-65
lines changed

rust/cubesql/cubesql/src/compile/rewrite/rules/members.rs

Lines changed: 29 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,18 +2830,26 @@ pub fn min_granularity(granularity_a: &String, granularity_b: &String) -> Option
28302830
}
28312831
}
28322832

2833-
fn find_column_by_alias<'mn>(
2834-
column_name: &str,
2835-
member_names_to_expr: &'mn mut MemberNamesToExpr,
2836-
cube_alias: &str,
2837-
) -> Option<&'mn str> {
2838-
if let Some((tuple, _)) = LogicalPlanData::do_find_member_by_alias(
2839-
member_names_to_expr,
2840-
&format!("{}.{}", cube_alias, column_name),
2841-
) {
2842-
return tuple.0.as_deref();
2833+
fn is_join_on_cube_join_field(
2834+
egraph: &mut CubeEGraph,
2835+
subst: &Subst,
2836+
cube_members_var: Var,
2837+
join_on: &[Column],
2838+
) -> bool {
2839+
if join_on.len() != 1 {
2840+
return false;
28432841
}
2844-
None
2842+
let join_on = &join_on[0];
2843+
let Some(((_, join_member, _), _)) = egraph[subst[cube_members_var]]
2844+
.data
2845+
.find_member_by_column(join_on)
2846+
else {
2847+
return false;
2848+
};
2849+
let Member::VirtualField { name, .. } = join_member else {
2850+
return false;
2851+
};
2852+
name == "__cubeJoinField"
28452853
}
28462854

28472855
fn is_proper_cube_join_condition(
@@ -2878,63 +2886,19 @@ fn is_proper_cube_join_condition(
28782886
// For now this allows only exact left.__cubeJoinField = right.__cubeJoinField
28792887
// TODO implement more complex conditions
28802888

2881-
for left_join_on in &left_join_ons {
2882-
if left_join_on.len() != 1 {
2883-
continue;
2884-
}
2885-
2886-
let left_join_on = &left_join_on[0];
2887-
2888-
let left_member_names_to_expr = &mut egraph[subst[left_cube_members_var]]
2889-
.data
2890-
.member_name_to_expr
2891-
.as_mut()
2892-
.unwrap();
2893-
2894-
let mut left_column_name = left_join_on.name.as_str();
2895-
if let Some(name) = find_column_by_alias(
2896-
left_column_name,
2897-
left_member_names_to_expr,
2898-
left_join_on.relation.as_deref().unwrap_or_default(),
2899-
) {
2900-
left_column_name = name.rsplit_once(".").unwrap().1;
2901-
}
2902-
2903-
if left_column_name != "__cubeJoinField" {
2904-
continue;
2905-
}
2906-
2907-
for right_join_on in &right_join_ons {
2908-
if right_join_on.len() != 1 {
2909-
continue;
2910-
}
2911-
2912-
let right_join_on = &right_join_on[0];
2913-
2914-
let right_member_names_to_expr = &mut egraph[subst[right_cube_members_var]]
2915-
.data
2916-
.member_name_to_expr
2917-
.as_mut()
2918-
.unwrap();
2919-
2920-
let mut right_column_name = right_join_on.name.as_str();
2921-
if let Some(name) = find_column_by_alias(
2922-
right_column_name,
2923-
right_member_names_to_expr,
2924-
right_join_on.relation.as_deref().unwrap_or_default(),
2925-
) {
2926-
right_column_name = name.rsplit_once(".").unwrap().1;
2927-
}
2928-
2929-
if right_column_name != "__cubeJoinField" {
2930-
continue;
2931-
}
2889+
if left_join_ons.iter().all(|left_join_on| {
2890+
!is_join_on_cube_join_field(egraph, subst, left_cube_members_var, left_join_on)
2891+
}) {
2892+
return false;
2893+
}
29322894

2933-
return true;
2934-
}
2895+
if right_join_ons.iter().all(|right_join_on| {
2896+
!is_join_on_cube_join_field(egraph, subst, right_cube_members_var, right_join_on)
2897+
}) {
2898+
return false;
29352899
}
29362900

2937-
false
2901+
true
29382902
}
29392903

29402904
#[cfg(test)]

0 commit comments

Comments
 (0)