Skip to content

Commit bed4289

Browse files
committed
refactor(cubesql): Simplify CrossJoin to CubeScan rewrite
1 parent 51a12f4 commit bed4289

File tree

1 file changed

+53
-142
lines changed
  • rust/cubesql/cubesql/src/compile/rewrite/rules

1 file changed

+53
-142
lines changed

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

Lines changed: 53 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ use crate::{
55
agg_fun_expr, aggregate, alias_expr, all_members,
66
analysis::{ConstantFolding, LogicalPlanData, Member, MemberNamesToExpr, OriginalExpr},
77
binary_expr, cast_expr, change_user_expr, column_expr, cross_join, cube_scan,
8-
cube_scan_filters_empty_tail, cube_scan_members, cube_scan_members_empty_tail,
9-
cube_scan_order_empty_tail, dimension_expr, distinct, expr_column_name, fun_expr, join,
10-
like_expr, limit, list_concat_pushdown_replacer, list_concat_pushup_replacer,
11-
literal_expr, literal_member, measure_expr, member_pushdown_replacer, member_replacer,
12-
original_expr_name, projection, referenced_columns, rewrite,
8+
cube_scan_filters, cube_scan_filters_empty_tail, cube_scan_members,
9+
cube_scan_members_empty_tail, cube_scan_order_empty_tail, dimension_expr, distinct,
10+
expr_column_name, fun_expr, join, like_expr, limit, list_concat_pushdown_replacer,
11+
list_concat_pushup_replacer, literal_expr, literal_member, measure_expr,
12+
member_pushdown_replacer, member_replacer, original_expr_name, projection,
13+
referenced_columns, rewrite,
1314
rewriter::{CubeEGraph, CubeRewrite, RewriteRules},
1415
rules::{
1516
replacer_flat_push_down_node_substitute_rules, replacer_push_down_node,
@@ -31,6 +32,7 @@ use crate::{
3132
},
3233
},
3334
config::ConfigObj,
35+
singular_eclass,
3436
sql::ColumnType,
3537
transport::{MetaContext, V1CubeMetaDimensionExt, V1CubeMetaExt, V1CubeMetaMeasureExt},
3638
var, var_iter, var_list_iter, CubeError,
@@ -2565,159 +2567,76 @@ impl MemberRules {
25652567
left_alias_to_cube_var: &'static str,
25662568
right_alias_to_cube_var: &'static str,
25672569
joined_alias_to_cube_var: &'static str,
2568-
left_members_var: &'static str,
2569-
right_members_var: &'static str,
2570-
joined_members_var: &'static str,
2571-
left_filters_var: &'static str,
2572-
right_filters_var: &'static str,
2573-
new_filters_var: &'static str,
25742570
left_order_var: &'static str,
25752571
right_order_var: &'static str,
25762572
new_order_var: &'static str,
2577-
left_limit_var: &'static str,
2578-
right_limit_var: &'static str,
2579-
new_limit_var: &'static str,
25802573
left_ungrouped_var: &'static str,
25812574
right_ungrouped_var: &'static str,
25822575
new_ungrouped_var: &'static str,
25832576
) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool {
25842577
let left_alias_to_cube_var = var!(left_alias_to_cube_var);
25852578
let right_alias_to_cube_var = var!(right_alias_to_cube_var);
25862579
let joined_alias_to_cube_var = var!(joined_alias_to_cube_var);
2587-
let left_members_var = var!(left_members_var);
2588-
let right_members_var = var!(right_members_var);
2589-
let joined_members_var = var!(joined_members_var);
2590-
let left_filters_var = var!(left_filters_var);
2591-
let right_filters_var = var!(right_filters_var);
2592-
let new_filters_var = var!(new_filters_var);
25932580
let left_order_var = var!(left_order_var);
25942581
let right_order_var = var!(right_order_var);
25952582
let new_order_var = var!(new_order_var);
2596-
let left_limit_var = var!(left_limit_var);
2597-
let right_limit_var = var!(right_limit_var);
2598-
let new_limit_var = var!(new_limit_var);
25992583
let left_ungrouped_var = var!(left_ungrouped_var);
26002584
let right_ungrouped_var = var!(right_ungrouped_var);
26012585
let new_ungrouped_var = var!(new_ungrouped_var);
26022586
move |egraph, subst| {
2587+
let Some(left_ungrouped) =
2588+
singular_eclass!(egraph[subst[left_ungrouped_var]], CubeScanUngrouped).copied()
2589+
else {
2590+
return false;
2591+
};
2592+
let Some(right_ungrouped) =
2593+
singular_eclass!(egraph[subst[right_ungrouped_var]], CubeScanUngrouped).copied()
2594+
else {
2595+
return false;
2596+
};
2597+
26032598
for left_alias_to_cube in
26042599
var_iter!(egraph[subst[left_alias_to_cube_var]], CubeScanAliasToCube).cloned()
26052600
{
26062601
for right_alias_to_cube in
26072602
var_iter!(egraph[subst[right_alias_to_cube_var]], CubeScanAliasToCube).cloned()
26082603
{
2609-
for left_members in
2610-
var_list_iter!(egraph[subst[left_members_var]], CubeScanMembers).cloned()
2611-
{
2612-
// This should not happen. If it did - investigate how
2613-
if left_members.is_empty() {
2614-
continue;
2615-
}
2616-
2617-
for right_members in
2618-
var_list_iter!(egraph[subst[right_members_var]], CubeScanMembers)
2619-
.cloned()
2620-
{
2621-
// This should not happen. If it did - investigate how
2622-
if right_members.is_empty() {
2623-
continue;
2624-
}
2625-
2626-
let left_limit =
2627-
match var_iter!(egraph[subst[left_limit_var]], CubeScanLimit)
2628-
.cloned()
2629-
.next()
2630-
{
2631-
Some(limit) => limit,
2632-
None => continue,
2633-
};
2634-
2635-
let right_limit =
2636-
match var_iter!(egraph[subst[right_limit_var]], CubeScanLimit)
2637-
.cloned()
2638-
.next()
2639-
{
2640-
Some(limit) => limit,
2641-
None => continue,
2642-
};
2643-
2644-
// TODO handle the case when limit set on non multiplied cube. It's possible to push down the limit in this case.
2645-
if left_limit.is_some() || right_limit.is_some() {
2646-
continue;
2647-
}
2648-
2649-
let is_left_order_empty =
2650-
Some(true) == egraph[subst[left_order_var]].data.is_empty_list;
2651-
2652-
let is_right_order_empty =
2653-
Some(true) == egraph[subst[right_order_var]].data.is_empty_list;
2654-
2655-
if !is_left_order_empty && !is_right_order_empty {
2656-
continue;
2657-
}
2658-
2659-
for left_ungrouped in
2660-
var_iter!(egraph[subst[left_ungrouped_var]], CubeScanUngrouped)
2661-
.cloned()
2662-
{
2663-
for right_ungrouped in
2664-
var_iter!(egraph[subst[right_ungrouped_var]], CubeScanUngrouped)
2665-
.cloned()
2666-
{
2667-
subst.insert(
2668-
joined_alias_to_cube_var,
2669-
egraph.add(LogicalPlanLanguage::CubeScanAliasToCube(
2670-
CubeScanAliasToCube(
2671-
left_alias_to_cube
2672-
.into_iter()
2673-
.chain(right_alias_to_cube.into_iter())
2674-
.collect(),
2675-
),
2676-
)),
2677-
);
2678-
2679-
let joined_members =
2680-
egraph.add(LogicalPlanLanguage::CubeScanMembers(vec![
2681-
subst[left_members_var],
2682-
subst[right_members_var],
2683-
]));
2604+
let is_left_order_empty =
2605+
Some(true) == egraph[subst[left_order_var]].data.is_empty_list;
26842606

2685-
subst.insert(joined_members_var, joined_members);
2607+
let is_right_order_empty =
2608+
Some(true) == egraph[subst[right_order_var]].data.is_empty_list;
26862609

2687-
subst.insert(
2688-
new_filters_var,
2689-
egraph.add(LogicalPlanLanguage::CubeScanFilters(vec![
2690-
subst[left_filters_var],
2691-
subst[right_filters_var],
2692-
])),
2693-
);
2610+
if !is_left_order_empty && !is_right_order_empty {
2611+
continue;
2612+
}
26942613

2695-
let orders = if is_left_order_empty {
2696-
subst[right_order_var]
2697-
} else {
2698-
subst[left_order_var]
2699-
};
2614+
subst.insert(
2615+
joined_alias_to_cube_var,
2616+
egraph.add(LogicalPlanLanguage::CubeScanAliasToCube(
2617+
CubeScanAliasToCube(
2618+
left_alias_to_cube
2619+
.into_iter()
2620+
.chain(right_alias_to_cube.into_iter())
2621+
.collect(),
2622+
),
2623+
)),
2624+
);
27002625

2701-
subst.insert(
2702-
new_limit_var,
2703-
egraph.add(LogicalPlanLanguage::CubeScanLimit(
2704-
CubeScanLimit(None),
2705-
)),
2706-
);
2626+
let orders = if is_left_order_empty {
2627+
subst[right_order_var]
2628+
} else {
2629+
subst[left_order_var]
2630+
};
27072631

2708-
subst.insert(new_order_var, orders);
2632+
subst.insert(new_order_var, orders);
27092633

2710-
let joined_ungrouped =
2711-
egraph.add(LogicalPlanLanguage::CubeScanUngrouped(
2712-
CubeScanUngrouped(left_ungrouped && right_ungrouped),
2713-
));
2714-
subst.insert(new_ungrouped_var, joined_ungrouped);
2634+
let joined_ungrouped = egraph.add(LogicalPlanLanguage::CubeScanUngrouped(
2635+
CubeScanUngrouped(left_ungrouped && right_ungrouped),
2636+
));
2637+
subst.insert(new_ungrouped_var, joined_ungrouped);
27152638

2716-
return true;
2717-
}
2718-
}
2719-
}
2720-
}
2639+
return true;
27212640
}
27222641
}
27232642

@@ -2756,6 +2675,7 @@ impl MemberRules {
27562675
left_members: &'static str,
27572676
right_members: &'static str,
27582677
) -> CubeRewrite {
2678+
// TODO handle the case when limit set on non multiplied cube. It's possible to push down the limit in this case.
27592679
transforming_rewrite(
27602680
&format!("push-down-cross-join-to-cube-scan-{}", name),
27612681
cross_join(
@@ -2764,7 +2684,7 @@ impl MemberRules {
27642684
left_members_expr,
27652685
"?left_filters",
27662686
"?left_order",
2767-
"?left_limit",
2687+
"CubeScanLimit:None",
27682688
"CubeScanOffset:None",
27692689
"CubeScanSplit:false",
27702690
"CubeScanCanPushdownJoin:true",
@@ -2776,7 +2696,7 @@ impl MemberRules {
27762696
right_members_expr,
27772697
"?right_filters",
27782698
"?right_order",
2779-
"?right_limit",
2699+
"CubeScanLimit:None",
27802700
"CubeScanOffset:None",
27812701
"CubeScanSplit:false",
27822702
"CubeScanCanPushdownJoin:true",
@@ -2786,10 +2706,10 @@ impl MemberRules {
27862706
),
27872707
cube_scan(
27882708
"?joined_alias_to_cube",
2789-
"?joined_members",
2790-
"?joined_filters",
2709+
cube_scan_members(left_members, right_members),
2710+
cube_scan_filters("?left_filters", "?right_filters"),
27912711
"?new_order",
2792-
"?new_limit",
2712+
"CubeScanLimit:None",
27932713
"CubeScanOffset:None",
27942714
"CubeScanSplit:false",
27952715
"CubeScanCanPushdownJoin:true",
@@ -2800,18 +2720,9 @@ impl MemberRules {
28002720
"?left_alias_to_cube",
28012721
"?right_alias_to_cube",
28022722
"?joined_alias_to_cube",
2803-
left_members,
2804-
right_members,
2805-
"?joined_members",
2806-
"?left_filters",
2807-
"?right_filters",
2808-
"?joined_filters",
28092723
"?left_order",
28102724
"?right_order",
28112725
"?new_order",
2812-
"?left_limit",
2813-
"?right_limit",
2814-
"?new_limit",
28152726
"?left_ungrouped",
28162727
"?right_ungrouped",
28172728
"?new_ungrouped",

0 commit comments

Comments
 (0)