Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xudong963 committed Sep 14, 2024
1 parent 1a73c1f commit e0f9dcb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ use crate::binder::ColumnBindingBuilder;
use crate::binder::Visibility;
use crate::optimizer::SExpr;
use crate::planner::semantic::normalize_identifier;
use crate::plans::BoundColumnRef;
use crate::plans::EvalScalar;
use crate::plans::FunctionCall;
use crate::plans::RelOperator;
Expand Down Expand Up @@ -248,28 +247,16 @@ impl Binder {
};

if let Some(fields) = fields {
if let RelOperator::ProjectSet(plan) = (*srf_expr.plan).clone() {
if plan.srfs.len() != 1 {
if let RelOperator::EvalScalar(plan) = (*srf_expr.plan).clone() {
if plan.items.len() != 1 {
return Err(ErrorCode::Internal(format!(
"Invalid table function subquery items, expect 1, but got {}",
plan.srfs.len()
"Invalid table function subquery EvalScalar items, expect 1, but got {}",
plan.items.len()
)));
}
// Delete srf result tuple column, extract tuple inner columns instead
let _ = bind_context.columns.pop();
let column = self.metadata.read().column(plan.srfs[0].index).clone();
let column_binding = ColumnBindingBuilder::new(
column.name(),
column.index(),
Box::new(column.data_type().clone()),
Visibility::InVisible,
)
.build();

let scalar = ScalarExpr::BoundColumnRef(BoundColumnRef {
span: None,
column: column_binding,
});
let scalar = &plan.items[0].scalar;

// Add tuple inner columns
let mut items = Vec::with_capacity(fields.len());
Expand Down Expand Up @@ -303,7 +290,7 @@ impl Binder {
}
let eval_scalar = EvalScalar { items };
let new_expr =
SExpr::create_unary(Arc::new(eval_scalar.into()), Arc::new(srf_expr));
SExpr::create_unary(Arc::new(eval_scalar.into()), srf_expr.children[0].clone());

if let Some(alias) = alias {
bind_context.apply_table_alias(alias, &self.name_resolution_ctx)?;
Expand Down Expand Up @@ -393,24 +380,27 @@ impl Binder {
)
.build()
};

let eval_scalar = EvalScalar {
items: vec![ScalarItem {
scalar: srf_result,
index: column_binding.index,
}],
};
// Add srf result column
bind_context.add_column_binding(column_binding.clone());
let (mut new_expr, mut bind_context) = self
bind_context.add_column_binding(column_binding);

let flatten_expr =
SExpr::create_unary(Arc::new(eval_scalar.into()), Arc::new(srf_expr));

let (new_expr, mut bind_context) = self
.extract_srf_table_function_columns(
&mut bind_context,
span,
&func_name,
srf_expr,
flatten_expr,
alias,
)?;
let eval_scalar = EvalScalar {
items: vec![ScalarItem {
scalar: srf_result,
index: column_binding.index,
}],
};
new_expr =
SExpr::create_unary(Arc::new(eval_scalar.into()), Arc::new(new_expr));

// add left table columns.
let mut new_columns = parent_context.columns.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use crate::optimizer::RuleID;
use crate::optimizer::SExpr;
use crate::plans::RelOp;
use crate::plans::RelOperator;
use crate::plans::Scan;
use crate::plans::Sort;

/// Input: Sort
Expand All @@ -44,13 +43,25 @@ impl RulePushDownSortScan {
pub fn new() -> Self {
Self {
id: RuleID::PushDownSortScan,
matchers: vec![Matcher::MatchOp {
op_type: RelOp::Sort,
children: vec![Matcher::MatchOp {
op_type: RelOp::Scan,
children: vec![],
}],
}],
matchers: vec![
Matcher::MatchOp {
op_type: RelOp::Sort,
children: vec![Matcher::MatchOp {
op_type: RelOp::Scan,
children: vec![],
}],
},
Matcher::MatchOp {
op_type: RelOp::Sort,
children: vec![Matcher::MatchOp {
op_type: RelOp::EvalScalar,
children: vec![Matcher::MatchOp {
op_type: RelOp::Scan,
children: vec![],
}],
}],
},
],
}
}
}
Expand All @@ -63,16 +74,32 @@ impl Rule for RulePushDownSortScan {
fn apply(&self, s_expr: &SExpr, state: &mut TransformResult) -> Result<()> {
let sort: Sort = s_expr.plan().clone().try_into()?;
let child = s_expr.child(0)?;
let mut get: Scan = child.plan().clone().try_into()?;
let mut get = match child.plan() {
RelOperator::Scan(scan) => scan.clone(),
RelOperator::EvalScalar(_) => {
let child = child.child(0)?;
child.plan().clone().try_into()?
}
_ => unreachable!(),
};
if get.order_by.is_none() {
get.order_by = Some(sort.items);
}
if let Some(limit) = sort.limit {
get.limit = Some(get.limit.map_or(limit, |c| cmp::max(c, limit)));
}

let get = SExpr::create_leaf(Arc::new(RelOperator::Scan(get)));

let mut result = s_expr.replace_children(vec![Arc::new(get)]);
let mut result = match child.plan() {
RelOperator::Scan(_) => s_expr.replace_children(vec![Arc::new(get)]),
RelOperator::EvalScalar(_) => {
let child = child.replace_children(vec![Arc::new(get)]);
s_expr.replace_children(vec![Arc::new(child)])
}
_ => unreachable!(),
};

result.set_applied_rule(&self.id);
state.add_result(result);
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Limit
├── read size: 0
├── partitions total: 0
├── partitions scanned: 0
├── push downs: [filters: [], limit: NONE]
├── push downs: [filters: [], limit: 2]
└── estimated rows: 0.00

query T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Limit
├── read size: < 1 KiB
├── partitions total: 1
├── partitions scanned: 1
├── push downs: [filters: [], limit: NONE]
├── push downs: [filters: [], limit: 8]
└── estimated rows: 10.00

query T
Expand Down

0 comments on commit e0f9dcb

Please sign in to comment.