Skip to content

Commit b5a9736

Browse files
committed
in work
1 parent e19e16c commit b5a9736

File tree

7 files changed

+112
-87
lines changed

7 files changed

+112
-87
lines changed

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/logical_node.rs

Lines changed: 33 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -34,58 +34,6 @@ pub enum PlanNode {
3434
LogicalMultiStageMember(Rc<LogicalMultiStageMember>),
3535
}
3636

37-
impl PlanNode {
38-
pub fn node_name(&self) -> &'static str {
39-
match self {
40-
PlanNode::Query(node) => node.node_name(),
41-
PlanNode::LogicalJoin(node) => node.node_name(),
42-
PlanNode::FullKeyAggregate(node) => node.node_name(),
43-
PlanNode::PreAggregation(node) => node.node_name(),
44-
PlanNode::ResolveMultipliedMeasures(node) => node.node_name(),
45-
PlanNode::AggregateMultipliedSubquery(node) => node.node_name(),
46-
PlanNode::Cube(node) => node.node_name(),
47-
PlanNode::MeasureSubquery(node) => node.node_name(),
48-
PlanNode::DimensionSubQuery(node) => node.node_name(),
49-
PlanNode::KeysSubQuery(node) => node.node_name(),
50-
PlanNode::MultiStageGetDateRange(node) => node.node_name(),
51-
PlanNode::MultiStageLeafMeasure(node) => node.node_name(),
52-
PlanNode::MultiStageMeasureCalculation(node) => node.node_name(),
53-
PlanNode::MultiStageTimeSeries(node) => node.node_name(),
54-
PlanNode::MultiStageRollingWindow(node) => node.node_name(),
55-
PlanNode::LogicalMultiStageMember(node) => node.node_name(),
56-
}
57-
}
58-
59-
pub fn into_logical_node<T: LogicalNode>(self) -> Result<Rc<T>, CubeError> {
60-
T::try_from_plan_node(self)
61-
}
62-
}
63-
64-
pub(super) fn cast_error(plan_node: &PlanNode, target_type: &str) -> CubeError {
65-
CubeError::internal(format!(
66-
"Can't cast {} PlanNode into {}",
67-
plan_node.node_name(),
68-
target_type,
69-
))
70-
}
71-
72-
pub(super) fn check_inputs_len(
73-
inputs: &Vec<PlanNode>,
74-
expected: usize,
75-
node_type: &str,
76-
) -> Result<(), CubeError> {
77-
if inputs.len() == expected {
78-
Ok(())
79-
} else {
80-
Err(CubeError::internal(format!(
81-
"For node {} expected {} inputs but received {}",
82-
node_type,
83-
expected,
84-
inputs.len()
85-
)))
86-
}
87-
}
88-
8937
// Macro for applying a block to all enum variants
9038
macro_rules! match_plan_node {
9139
($self:expr, $node:ident => $block:block) => {
@@ -110,31 +58,52 @@ macro_rules! match_plan_node {
11058
};
11159
}
11260

113-
impl LogicalNode for PlanNode {
114-
fn inputs(&self) -> Vec<PlanNode> {
61+
impl PlanNode {
62+
pub fn into_logical_node<T: LogicalNode>(self) -> Result<Rc<T>, CubeError> {
63+
T::try_from_plan_node(self)
64+
}
65+
66+
pub fn inputs(&self) -> Vec<PlanNode> {
11567
match_plan_node!(self, node => {
11668
node.inputs()
11769
})
11870
}
11971

120-
fn node_name(&self) -> &'static str {
72+
pub fn node_name(&self) -> &'static str {
12173
match_plan_node!(self, node => {
12274
node.node_name()
12375
})
12476
}
12577

126-
fn with_inputs(self: Rc<Self>, inputs: Vec<PlanNode>) -> Result<Rc<Self>, CubeError> {
127-
let result = match_plan_node!(self.as_ref(), node => {
128-
node.clone().with_inputs(inputs)?.as_plan_node()
78+
pub fn with_inputs(self, inputs: Vec<PlanNode>) -> Result<Self, CubeError> {
79+
let result = match_plan_node!(self, node => {
80+
node.with_inputs(inputs)?.as_plan_node()
12981
});
130-
Ok(Rc::new(result))
82+
Ok(result)
13183
}
84+
}
13285

133-
fn try_from_plan_node(plan_node: PlanNode) -> Result<Rc<Self>, CubeError> {
134-
Ok(Rc::new(plan_node))
135-
}
86+
pub(super) fn cast_error(plan_node: &PlanNode, target_type: &str) -> CubeError {
87+
CubeError::internal(format!(
88+
"Can't cast {} PlanNode into {}",
89+
plan_node.node_name(),
90+
target_type,
91+
))
92+
}
13693

137-
fn as_plan_node(self: &Rc<Self>) -> PlanNode {
138-
(**self).clone()
94+
pub(super) fn check_inputs_len(
95+
inputs: &Vec<PlanNode>,
96+
expected: usize,
97+
node_type: &str,
98+
) -> Result<(), CubeError> {
99+
if inputs.len() == expected {
100+
Ok(())
101+
} else {
102+
Err(CubeError::internal(format!(
103+
"For node {} expected {} inputs but received {}",
104+
node_type,
105+
expected,
106+
inputs.len()
107+
)))
139108
}
140109
}

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/optimizers/pre_aggregation/dimension_matcher.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::CompiledPreAggregation;
2-
use super::MatchState;
32
use crate::plan::filter::FilterGroupOperator;
43
use crate::plan::FilterItem;
54
use crate::planner::filter::BaseFilter;
@@ -12,6 +11,25 @@ use cubenativeutils::CubeError;
1211
use std::collections::HashMap;
1312
use std::rc::Rc;
1413

14+
#[derive(Clone, Debug, PartialEq)]
15+
pub enum MatchState {
16+
Partial,
17+
Full,
18+
NotMatched,
19+
}
20+
21+
impl MatchState {
22+
pub fn combine(&self, other: &MatchState) -> MatchState {
23+
if matches!(self, MatchState::NotMatched) || matches!(other, MatchState::NotMatched) {
24+
return MatchState::NotMatched;
25+
}
26+
if matches!(self, MatchState::Partial) || matches!(other, MatchState::Partial) {
27+
return MatchState::Partial;
28+
}
29+
MatchState::Full
30+
}
31+
}
32+
1533
pub struct DimensionMatcher<'a> {
1634
query_tools: Rc<QueryTools>,
1735
pre_aggregation: &'a CompiledPreAggregation,

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/optimizers/pre_aggregation/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
mod compiled_pre_aggregation;
2-
//mod dimension_matcher;
2+
mod dimension_matcher;
33
//mod measure_matcher;
44
//mod optimizer;
55
//mod original_sql_collector;
66
//mod original_sql_optimizer;
77
//mod pre_aggregations_compiler;
88

99
pub use compiled_pre_aggregation::*;
10-
//use dimension_matcher::*;
10+
use dimension_matcher::*;
1111
//use measure_matcher::*;
1212
//pub use optimizer::*;
1313
//pub use original_sql_collector::*;

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/optimizers/pre_aggregation/optimizer.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,6 @@ use cubenativeutils::CubeError;
88
use std::collections::{HashMap, HashSet};
99
use std::rc::Rc;
1010

11-
#[derive(Clone, Debug, PartialEq)]
12-
pub enum MatchState {
13-
Partial,
14-
Full,
15-
NotMatched,
16-
}
17-
18-
impl MatchState {
19-
pub fn combine(&self, other: &MatchState) -> MatchState {
20-
if matches!(self, MatchState::NotMatched) || matches!(other, MatchState::NotMatched) {
21-
return MatchState::NotMatched;
22-
}
23-
if matches!(self, MatchState::Partial) || matches!(other, MatchState::Partial) {
24-
return MatchState::Partial;
25-
}
26-
MatchState::Full
27-
}
28-
}
29-
3011
pub struct PreAggregationOptimizer {
3112
query_tools: Rc<QueryTools>,
3213
allow_multi_stage: bool,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
mod rewriter;
12
mod visitor;
3+
pub use rewriter::*;
24
pub use visitor::*;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use crate::logical_plan::{LogicalNode, PlanNode};
2+
use cubenativeutils::CubeError;
3+
use std::rc::Rc;
4+
5+
pub trait LogicalNodeRewriter {
6+
fn process_node(&mut self, node: &PlanNode) -> Result<Option<PlanNode>, CubeError>;
7+
}
8+
9+
pub struct LogicalPlanRewriter {}
10+
11+
impl LogicalPlanRewriter {
12+
pub fn new() -> Self {
13+
Self {}
14+
}
15+
16+
pub fn rewrite_top_down<T: LogicalNodeRewriter, N: LogicalNode>(
17+
&self,
18+
node_visitor: &mut T,
19+
node: Rc<N>,
20+
) -> Result<Rc<N>, CubeError> {
21+
let res = if let Some(rewrited) =
22+
self.rewrite_top_down_impl(node_visitor, node.as_plan_node())?
23+
{
24+
rewrited.into_logical_node()?
25+
} else {
26+
node
27+
};
28+
Ok(res)
29+
}
30+
31+
fn rewrite_top_down_impl<T: LogicalNodeRewriter>(
32+
&self,
33+
node_visitor: &mut T,
34+
node: PlanNode,
35+
) -> Result<Option<PlanNode>, CubeError> {
36+
if let Some(rewrited) = node_visitor.process_node(&node)? {
37+
return Ok(Some(rewrited));
38+
}
39+
let mut has_changes = false;
40+
let mut inputs = node.inputs();
41+
for input in inputs.iter_mut() {
42+
if let Some(rewrited) = self.rewrite_top_down_impl(node_visitor, input.clone())? {
43+
*input = rewrited;
44+
has_changes = true;
45+
}
46+
}
47+
let res = if has_changes {
48+
Some(node.with_inputs(inputs)?)
49+
} else {
50+
None
51+
};
52+
53+
Ok(res)
54+
}
55+
}

rust/cubesqlplanner/cubesqlplanner/src/plan/builder/select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl SelectBuilder {
167167
}
168168
pub fn add_projection_reference_member(
169169
&mut self,
170-
member: &Rc<dyn BaseMember>,
170+
member: &Rc<MemberSymbol>,
171171
reference: QualifiedColumnName,
172172
alias: Option<String>,
173173
) {

0 commit comments

Comments
 (0)