-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sqlplanner): Extract alias logic from the symbols (#8919)
- Loading branch information
1 parent
fe62933
commit d1b07f1
Showing
69 changed files
with
2,397 additions
and
1,233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
rust/cubesqlplanner/cubesqlplanner/src/plan/aggregation.rs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
125 changes: 125 additions & 0 deletions
125
rust/cubesqlplanner/cubesqlplanner/src/plan/builder/join.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use crate::plan::{Join, JoinCondition, JoinItem, QueryPlan, Schema, Select, SingleAliasedSource}; | ||
use crate::planner::BaseCube; | ||
use std::rc::Rc; | ||
|
||
pub struct JoinBuilder { | ||
root: SingleAliasedSource, | ||
joins: Vec<JoinItem>, | ||
} | ||
|
||
impl JoinBuilder { | ||
pub fn new(root: SingleAliasedSource) -> Self { | ||
Self { | ||
root, | ||
joins: vec![], | ||
} | ||
} | ||
|
||
pub fn new_from_cube(cube: Rc<BaseCube>, alias: Option<String>) -> Self { | ||
Self::new(SingleAliasedSource::new_from_cube(cube, alias)) | ||
} | ||
|
||
pub fn new_from_table_reference( | ||
reference: String, | ||
schema: Rc<Schema>, | ||
alias: Option<String>, | ||
) -> Self { | ||
Self::new(SingleAliasedSource::new_from_table_reference( | ||
reference, schema, alias, | ||
)) | ||
} | ||
|
||
pub fn new_from_subquery(plan: Rc<QueryPlan>, alias: String) -> Self { | ||
Self::new(SingleAliasedSource::new_from_subquery(plan, alias)) | ||
} | ||
|
||
pub fn new_from_subselect(plan: Rc<Select>, alias: String) -> Self { | ||
Self::new(SingleAliasedSource::new_from_subquery( | ||
Rc::new(QueryPlan::Select(plan)), | ||
alias, | ||
)) | ||
} | ||
|
||
pub fn left_join_subselect(&mut self, subquery: Rc<Select>, alias: String, on: JoinCondition) { | ||
self.join_subselect(subquery, alias, on, false) | ||
} | ||
|
||
pub fn inner_join_subselect(&mut self, subquery: Rc<Select>, alias: String, on: JoinCondition) { | ||
self.join_subselect(subquery, alias, on, true) | ||
} | ||
|
||
pub fn left_join_cube(&mut self, cube: Rc<BaseCube>, alias: Option<String>, on: JoinCondition) { | ||
self.join_cube(cube, alias, on, false) | ||
} | ||
|
||
pub fn inner_join_cube( | ||
&mut self, | ||
cube: Rc<BaseCube>, | ||
alias: Option<String>, | ||
on: JoinCondition, | ||
) { | ||
self.join_cube(cube, alias, on, true) | ||
} | ||
|
||
pub fn left_join_table_reference( | ||
&mut self, | ||
reference: String, | ||
schema: Rc<Schema>, | ||
alias: Option<String>, | ||
on: JoinCondition, | ||
) { | ||
self.join_table_reference(reference, schema, alias, on, false) | ||
} | ||
|
||
pub fn inner_join_table_reference( | ||
&mut self, | ||
reference: String, | ||
schema: Rc<Schema>, | ||
alias: Option<String>, | ||
on: JoinCondition, | ||
) { | ||
self.join_table_reference(reference, schema, alias, on, true) | ||
} | ||
|
||
pub fn build(self) -> Rc<Join> { | ||
Rc::new(Join { | ||
root: self.root, | ||
joins: self.joins, | ||
}) | ||
} | ||
|
||
fn join_subselect( | ||
&mut self, | ||
subquery: Rc<Select>, | ||
alias: String, | ||
on: JoinCondition, | ||
is_inner: bool, | ||
) { | ||
let subquery = Rc::new(QueryPlan::Select(subquery)); | ||
let from = SingleAliasedSource::new_from_subquery(subquery, alias); | ||
self.joins.push(JoinItem { from, on, is_inner }) | ||
} | ||
|
||
fn join_cube( | ||
&mut self, | ||
cube: Rc<BaseCube>, | ||
alias: Option<String>, | ||
on: JoinCondition, | ||
is_inner: bool, | ||
) { | ||
let from = SingleAliasedSource::new_from_cube(cube, alias); | ||
self.joins.push(JoinItem { from, on, is_inner }) | ||
} | ||
|
||
fn join_table_reference( | ||
&mut self, | ||
reference: String, | ||
schema: Rc<Schema>, | ||
alias: Option<String>, | ||
on: JoinCondition, | ||
is_inner: bool, | ||
) { | ||
let from = SingleAliasedSource::new_from_table_reference(reference, schema, alias); | ||
self.joins.push(JoinItem { from, on, is_inner }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod join; | ||
pub mod select; | ||
|
||
pub use join::JoinBuilder; | ||
pub use select::SelectBuilder; |
107 changes: 107 additions & 0 deletions
107
rust/cubesqlplanner/cubesqlplanner/src/plan/builder/select.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use crate::plan::{ | ||
AliasedExpr, Cte, Expr, Filter, From, MemberExpression, OrderBy, Schema, Select, | ||
}; | ||
use crate::planner::{BaseMember, VisitorContext}; | ||
use std::rc::Rc; | ||
|
||
pub struct SelectBuilder { | ||
projection_columns: Vec<AliasedExpr>, | ||
from: From, | ||
filter: Option<Filter>, | ||
group_by: Vec<Expr>, | ||
having: Option<Filter>, | ||
order_by: Vec<OrderBy>, | ||
context: Rc<VisitorContext>, | ||
ctes: Vec<Rc<Cte>>, | ||
is_distinct: bool, | ||
limit: Option<usize>, | ||
offset: Option<usize>, | ||
input_schema: Rc<Schema>, | ||
} | ||
|
||
impl SelectBuilder { | ||
pub fn new(from: From, context: VisitorContext) -> Self { | ||
let input_schema = from.schema.clone(); | ||
Self { | ||
projection_columns: vec![], | ||
from, | ||
filter: None, | ||
group_by: vec![], | ||
having: None, | ||
order_by: vec![], | ||
context: Rc::new(context), | ||
ctes: vec![], | ||
is_distinct: false, | ||
limit: None, | ||
offset: None, | ||
input_schema, | ||
} | ||
} | ||
|
||
pub fn add_projection_member( | ||
&mut self, | ||
member: &Rc<dyn BaseMember>, | ||
source: Option<String>, | ||
alias: Option<String>, | ||
) { | ||
let alias = if let Some(alias) = alias { | ||
alias | ||
} else { | ||
self.input_schema.resolve_member_alias(&member, &source) | ||
}; | ||
let expr = Expr::Member(MemberExpression::new(member.clone(), source)); | ||
let aliased_expr = AliasedExpr { | ||
expr, | ||
alias: alias.clone(), | ||
}; | ||
|
||
self.projection_columns.push(aliased_expr); | ||
} | ||
|
||
pub fn set_filter(&mut self, filter: Option<Filter>) { | ||
self.filter = filter; | ||
} | ||
|
||
pub fn set_group_by(&mut self, group_by: Vec<Expr>) { | ||
self.group_by = group_by; | ||
} | ||
|
||
pub fn set_having(&mut self, having: Option<Filter>) { | ||
self.having = having; | ||
} | ||
|
||
pub fn set_order_by(&mut self, order_by: Vec<OrderBy>) { | ||
self.order_by = order_by; | ||
} | ||
|
||
pub fn set_distinct(&mut self) { | ||
self.is_distinct = true; | ||
} | ||
|
||
pub fn set_limit(&mut self, limit: Option<usize>) { | ||
self.limit = limit; | ||
} | ||
|
||
pub fn set_offset(&mut self, offset: Option<usize>) { | ||
self.offset = offset; | ||
} | ||
pub fn set_ctes(&mut self, ctes: Vec<Rc<Cte>>) { | ||
self.ctes = ctes; | ||
} | ||
|
||
pub fn build(self) -> Select { | ||
Select { | ||
projection_columns: self.projection_columns, | ||
from: self.from, | ||
filter: self.filter, | ||
group_by: self.group_by, | ||
having: self.having, | ||
order_by: self.order_by, | ||
context: self.context.clone(), | ||
ctes: self.ctes, | ||
is_distinct: self.is_distinct, | ||
limit: self.limit, | ||
offset: self.offset, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use super::{QueryPlan, Schema, Select}; | ||
use crate::planner::sql_templates::PlanSqlTemplates; | ||
use cubenativeutils::CubeError; | ||
|
||
use std::rc::Rc; | ||
|
||
#[derive(Clone)] | ||
pub struct Cte { | ||
query: Rc<QueryPlan>, | ||
name: String, | ||
} | ||
|
||
impl Cte { | ||
pub fn new(query: Rc<QueryPlan>, name: String) -> Self { | ||
Self { query, name } | ||
} | ||
|
||
pub fn new_from_select(select: Rc<Select>, name: String) -> Self { | ||
Self { | ||
query: Rc::new(QueryPlan::Select(select)), | ||
name, | ||
} | ||
} | ||
|
||
pub fn make_schema(&self) -> Schema { | ||
self.query.make_schema(Some(self.name().clone())) | ||
} | ||
|
||
pub fn query(&self) -> &Rc<QueryPlan> { | ||
&self.query | ||
} | ||
|
||
pub fn name(&self) -> &String { | ||
&self.name | ||
} | ||
|
||
pub fn to_sql(&self, templates: &PlanSqlTemplates) -> Result<String, CubeError> { | ||
let sql = format!("({})", self.query.to_sql(templates)?); | ||
Ok(sql) | ||
} | ||
} |
Oops, something went wrong.