Skip to content

Commit

Permalink
fix: combine_operators.rs deletes the temp table alias of subquery
Browse files Browse the repository at this point in the history
  • Loading branch information
KKould committed Mar 16, 2024
1 parent 3b71e7f commit b74fff1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
11 changes: 6 additions & 5 deletions src/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<'a, T: Transaction> Binder<'a, T> {
// Resolve scalar function call.
// TODO support SRF(Set-Returning Function).

let mut select_list = self.normalize_select_item(&select.projection)?;
let mut select_list = self.normalize_select_item(&select.projection, &plan)?;

if let Some(predicate) = &select.selection {
plan = self.bind_where(plan, predicate)?;
Expand Down Expand Up @@ -341,6 +341,7 @@ impl<'a, T: Transaction> Binder<'a, T> {
fn normalize_select_item(
&mut self,
items: &[SelectItem],
plan: &LogicalPlan,
) -> Result<Vec<ScalarExpression>, DatabaseError> {
let mut select_items = vec![];

Expand All @@ -359,6 +360,9 @@ impl<'a, T: Transaction> Binder<'a, T> {
});
}
SelectItem::Wildcard(_) => {
if let Operator::Project(op) = &plan.operator {
return Ok(op.exprs.clone());
}
for (table_name, _) in self.context.bind_table.keys() {
self.bind_table_column_refs(&mut select_items, table_name.clone())?;
}
Expand Down Expand Up @@ -722,10 +726,7 @@ impl<'a, T: Transaction> Binder<'a, T> {
} => {
match op {
BinaryOperator::Eq => {
match (
left_expr.unpack_alias_inner_ref().unpack_alias_ref(),
right_expr.unpack_alias_inner_ref().unpack_alias_ref(),
) {
match (left_expr.unpack_alias_ref(), right_expr.unpack_alias_ref()) {
// example: foo = bar
(ScalarExpression::ColumnRef(l), ScalarExpression::ColumnRef(r)) => {
// reorder left and right joins keys to pattern: (left, right)
Expand Down
22 changes: 11 additions & 11 deletions src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,35 +121,35 @@ pub enum ScalarExpression {

impl ScalarExpression {
pub fn unpack_alias(self) -> ScalarExpression {
if let ScalarExpression::Alias { expr, .. } = self {
if let ScalarExpression::Alias {
alias: AliasType::Expr(expr),
..
} = self
{
expr.unpack_alias()
} else if let ScalarExpression::Alias { expr, .. } = self {
expr.unpack_alias()
} else {
self
}
}

pub fn unpack_alias_ref(&self) -> &ScalarExpression {
if let ScalarExpression::Alias { expr, .. } = self {
expr.unpack_alias_ref()
} else {
self
}
}

pub fn unpack_alias_inner_ref(&self) -> &ScalarExpression {
if let ScalarExpression::Alias {
alias: AliasType::Expr(expr),
..
} = self
{
expr.unpack_alias_inner_ref()
expr.unpack_alias_ref()
} else if let ScalarExpression::Alias { expr, .. } = self {
expr.unpack_alias_ref()
} else {
self
}
}

pub fn try_reference(&mut self, output_exprs: &[ScalarExpression]) {
let fn_output_column = |expr: &ScalarExpression| expr.unpack_alias_ref().output_column();
let fn_output_column = |expr: &ScalarExpression| expr.output_column();
let self_column = fn_output_column(self);
if let Some((pos, _)) = output_exprs
.iter()
Expand Down
2 changes: 0 additions & 2 deletions src/optimizer/rule/normalization/combine_operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ impl NormalizationRule for CollapseProject {
if let Operator::Project(child_op) = graph.operator(child_id) {
if is_subset_exprs(&op.exprs, &child_op.exprs) {
graph.remove_node(child_id, false);
} else {
graph.remove_node(node_id, false);
}
}
}
Expand Down

0 comments on commit b74fff1

Please sign in to comment.