Skip to content

Commit 709a38f

Browse files
committed
fix(cubesql): Replace alias to cube during wrapper pull up
1 parent 1d0bc78 commit 709a38f

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ impl MemberRules {
14371437
}
14381438
}
14391439

1440-
fn replace_alias(
1440+
pub fn replace_alias(
14411441
alias_to_cube: &Vec<(String, String)>,
14421442
projection_alias: &Option<String>,
14431443
) -> Vec<(String, String)> {

rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/wrapper_pull_up.rs

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ use crate::{
22
compile::rewrite::{
33
cube_scan_wrapper,
44
rewriter::{CubeEGraph, CubeRewrite},
5-
rules::wrapper::WrapperRules,
5+
rules::{members::MemberRules, wrapper::WrapperRules},
66
transforming_rewrite, wrapped_select, wrapped_select_having_expr_empty_tail,
77
wrapped_select_joins_empty_tail, wrapper_pullup_replacer, wrapper_replacer_context,
8-
WrappedSelectSelectType, WrappedSelectType,
8+
LogicalPlanLanguage, WrappedSelectAlias, WrappedSelectSelectType, WrappedSelectType,
9+
WrapperReplacerContextAliasToCube,
910
},
1011
var, var_iter, var_list_iter,
1112
};
12-
use egg::Subst;
13+
use egg::{Subst, Var};
1314

1415
impl WrapperRules {
1516
pub fn wrapper_pull_up_rules(&self, rules: &mut Vec<CubeRewrite>) {
@@ -150,7 +151,7 @@ impl WrapperRules {
150151
"?select_ungrouped_scan",
151152
),
152153
wrapper_replacer_context(
153-
"?alias_to_cube",
154+
"?alias_to_cube_out",
154155
// This is fixed to false for any LHS because we should only allow to push to Cube when from is ungrouped CubeScan
155156
// And after pulling replacer over this node it will be WrappedSelect(from=CubeScan), so it should not allow to push for whatever LP is on top of it
156157
"WrapperReplacerContextPushToCube:false",
@@ -162,7 +163,12 @@ impl WrapperRules {
162163
),
163164
"CubeScanWrapperFinalized:false",
164165
),
165-
self.transform_pull_up_wrapper_select("?cube_scan_input"),
166+
self.transform_pull_up_wrapper_select(
167+
"?cube_scan_input",
168+
"?alias_to_cube",
169+
"?select_alias",
170+
"?alias_to_cube_out",
171+
),
166172
),
167173
transforming_rewrite(
168174
"wrapper-pull-up-to-cube-scan-non-trivial-wrapped-select",
@@ -338,7 +344,7 @@ impl WrapperRules {
338344
"?select_ungrouped_scan",
339345
),
340346
wrapper_replacer_context(
341-
"?alias_to_cube",
347+
"?alias_to_cube_out",
342348
// This is fixed to false for any LHS because we should only allow to push to Cube when from is ungrouped CubeSCan
343349
// And after pulling replacer over this node it will be WrappedSelect(from=WrappedSelect), so it should not allow to push for whatever LP is on top of it
344350
"WrapperReplacerContextPushToCube:false",
@@ -359,20 +365,66 @@ impl WrapperRules {
359365
"?inner_projection_expr",
360366
"?inner_group_expr",
361367
"?inner_aggr_expr",
368+
"?alias_to_cube",
369+
"?select_alias",
370+
"?alias_to_cube_out",
362371
),
363372
),
364373
]);
365374
}
366375

376+
fn replace_aliases(
377+
egraph: &mut CubeEGraph,
378+
subst: &mut Subst,
379+
alias_to_cube_var: Var,
380+
select_alias_var: Var,
381+
alias_to_cube_out_var: Var,
382+
) -> bool {
383+
for alias_to_cube in var_iter!(
384+
egraph[subst[alias_to_cube_var]],
385+
WrapperReplacerContextAliasToCube
386+
) {
387+
for projection_alias in var_iter!(egraph[subst[select_alias_var]], WrappedSelectAlias) {
388+
let replaced_alias_to_cube =
389+
MemberRules::replace_alias(&alias_to_cube, &projection_alias);
390+
let new_alias_to_cube =
391+
egraph.add(LogicalPlanLanguage::WrapperReplacerContextAliasToCube(
392+
WrapperReplacerContextAliasToCube(replaced_alias_to_cube),
393+
));
394+
subst.insert(alias_to_cube_out_var, new_alias_to_cube);
395+
return true;
396+
}
397+
}
398+
399+
false
400+
}
401+
367402
fn transform_pull_up_wrapper_select(
368403
&self,
369404
cube_scan_input_var: &'static str,
405+
alias_to_cube_var: &'static str,
406+
select_alias_var: &'static str,
407+
alias_to_cube_out_var: &'static str,
370408
) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool {
371409
let cube_scan_input_var = var!(cube_scan_input_var);
410+
let alias_to_cube_var = var!(alias_to_cube_var);
411+
let select_alias_var = var!(select_alias_var);
412+
let alias_to_cube_out_var = var!(alias_to_cube_out_var);
372413
move |egraph, subst| {
373414
for _ in var_list_iter!(egraph[subst[cube_scan_input_var]], WrappedSelect).cloned() {
374415
return false;
375416
}
417+
418+
if !Self::replace_aliases(
419+
egraph,
420+
subst,
421+
alias_to_cube_var,
422+
select_alias_var,
423+
alias_to_cube_out_var,
424+
) {
425+
return false;
426+
}
427+
376428
true
377429
}
378430
}
@@ -387,12 +439,28 @@ impl WrapperRules {
387439
inner_projection_expr_var: &'static str,
388440
_inner_group_expr_var: &'static str,
389441
_inner_aggr_expr_var: &'static str,
442+
alias_to_cube_var: &'static str,
443+
select_alias_var: &'static str,
444+
alias_to_cube_out_var: &'static str,
390445
) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool {
391446
let select_type_var = var!(select_type_var);
392447
let projection_expr_var = var!(projection_expr_var);
393448
let inner_select_type_var = var!(inner_select_type_var);
394449
let inner_projection_expr_var = var!(inner_projection_expr_var);
450+
let alias_to_cube_var = var!(alias_to_cube_var);
451+
let select_alias_var = var!(select_alias_var);
452+
let alias_to_cube_out_var = var!(alias_to_cube_out_var);
395453
move |egraph, subst| {
454+
if !Self::replace_aliases(
455+
egraph,
456+
subst,
457+
alias_to_cube_var,
458+
select_alias_var,
459+
alias_to_cube_out_var,
460+
) {
461+
return false;
462+
}
463+
396464
for select_type in
397465
var_iter!(egraph[subst[select_type_var]], WrappedSelectSelectType).cloned()
398466
{

0 commit comments

Comments
 (0)