Skip to content

Commit 4b0fb66

Browse files
committed
done
1 parent 86c5ab1 commit 4b0fb66

File tree

7 files changed

+36
-64
lines changed

7 files changed

+36
-64
lines changed

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/multistage/common.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ impl PrettyPrint for MultiStageAppliedState {
5353
}
5454

5555
result.println("time_shifts:", &state);
56-
if let Some(common) = &self.time_shifts().common_time_shift {
57-
result.println(&format!("- common: {}", common.to_sql()), &details_state);
58-
}
5956
for (_, time_shift) in self.time_shifts().dimensions_shifts.iter() {
6057
result.println(
6158
&format!(

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/multistage/leaf_measure.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use crate::logical_plan::*;
22
use crate::planner::planners::multi_stage::TimeShiftState;
3-
use crate::planner::sql_evaluator::{DimensionTimeShift, MemberSymbol};
4-
use std::collections::HashMap;
3+
use crate::planner::sql_evaluator::MemberSymbol;
54
use std::rc::Rc;
65

76
pub struct MultiStageLeafMeasure {
87
pub measure: Rc<MemberSymbol>,
98
pub render_measure_as_state: bool, //Render measure as state, for example hll state for count_approx
109
pub render_measure_for_ungrouped: bool,
11-
pub time_shifts: HashMap<String, DimensionTimeShift>,
10+
pub time_shifts: TimeShiftState,
1211
pub query: Rc<Query>,
1312
}
1413

@@ -26,7 +25,7 @@ impl PrettyPrint for MultiStageLeafMeasure {
2625
if !self.time_shifts.is_empty() {
2726
result.println("time_shifts:", &state);
2827
let details_state = state.new_level();
29-
for (_, time_shift) in self.time_shifts.iter() {
28+
for (_, time_shift) in self.time_shifts.dimensions_shifts.iter() {
3029
result.println(
3130
&format!(
3231
"- {}: {}",

rust/cubesqlplanner/cubesqlplanner/src/physical_plan_builder/builder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::planner::planners::multi_stage::TimeShiftState;
55
use crate::planner::query_properties::OrderByItem;
66
use crate::planner::query_tools::QueryTools;
77
use crate::planner::sql_evaluator::sql_nodes::SqlNodesFactory;
8-
use crate::planner::sql_evaluator::DimensionTimeShift;
98
use crate::planner::sql_evaluator::MemberSymbol;
109
use crate::planner::sql_evaluator::ReferencesBuilder;
1110
use crate::planner::sql_templates::PlanSqlTemplates;
@@ -25,7 +24,7 @@ struct PhysicalPlanBuilderContext {
2524
pub alias_prefix: Option<String>,
2625
pub render_measure_as_state: bool, //Render measure as state, for example hll state for count_approx
2726
pub render_measure_for_ungrouped: bool,
28-
pub time_shifts: HashMap<String, DimensionTimeShift>,
27+
pub time_shifts: TimeShiftState,
2928
pub original_sql_pre_aggregations: HashMap<String, String>,
3029
}
3130

rust/cubesqlplanner/cubesqlplanner/src/planner/planners/multi_stage/applied_state.rs

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::plan::{FilterGroup, FilterItem};
22
use crate::planner::filter::FilterOperator;
33
use crate::planner::sql_evaluator::{DimensionTimeShift, MeasureTimeShifts, MemberSymbol};
4-
use crate::planner::{BaseDimension, BaseMember, BaseTimeDimension, SqlInterval};
4+
use crate::planner::{BaseDimension, BaseMember, BaseTimeDimension};
55
use itertools::Itertools;
66
use std::cmp::PartialEq;
77
use std::collections::HashMap;
@@ -11,12 +11,11 @@ use std::rc::Rc;
1111
#[derive(Clone, Default, Debug)]
1212
pub struct TimeShiftState {
1313
pub dimensions_shifts: HashMap<String, DimensionTimeShift>,
14-
pub common_time_shift: Option<SqlInterval>,
1514
}
1615

1716
impl TimeShiftState {
1817
pub fn is_empty(&self) -> bool {
19-
self.dimensions_shifts.is_empty() && self.common_time_shift.is_none()
18+
self.dimensions_shifts.is_empty()
2019
}
2120
}
2221

@@ -74,28 +73,28 @@ impl MultiStageAppliedState {
7473
}
7574

7675
pub fn add_time_shifts(&mut self, time_shifts: MeasureTimeShifts) {
77-
match time_shifts {
78-
MeasureTimeShifts::Dimensions(dimensions) => {
79-
for ts in dimensions.into_iter() {
80-
if let Some(exists) = self
81-
.time_shifts
82-
.dimensions_shifts
83-
.get_mut(&ts.dimension.full_name())
84-
{
85-
exists.interval += ts.interval;
86-
} else {
87-
self.time_shifts
88-
.dimensions_shifts
89-
.insert(ts.dimension.full_name(), ts);
90-
}
91-
}
92-
}
93-
MeasureTimeShifts::Common(interval) => {
94-
if let Some(common) = self.time_shifts.common_time_shift.as_mut() {
95-
*common += interval;
96-
} else {
97-
self.time_shifts.common_time_shift = Some(interval);
98-
}
76+
let resolved_shifts = match time_shifts {
77+
MeasureTimeShifts::Dimensions(dimensions) => dimensions,
78+
MeasureTimeShifts::Common(interval) => self
79+
.all_time_members()
80+
.into_iter()
81+
.map(|m| DimensionTimeShift {
82+
interval: interval.clone(),
83+
dimension: m,
84+
})
85+
.collect_vec(),
86+
};
87+
for ts in resolved_shifts.into_iter() {
88+
if let Some(exists) = self
89+
.time_shifts
90+
.dimensions_shifts
91+
.get_mut(&ts.dimension.full_name())
92+
{
93+
exists.interval += ts.interval;
94+
} else {
95+
self.time_shifts
96+
.dimensions_shifts
97+
.insert(ts.dimension.full_name(), ts);
9998
}
10099
}
101100
}
@@ -104,24 +103,6 @@ impl MultiStageAppliedState {
104103
&self.time_shifts
105104
}
106105

107-
pub fn resolved_time_shifts(&self) -> HashMap<String, DimensionTimeShift> {
108-
let mut resolved_time_shifts = self.time_shifts.dimensions_shifts.clone();
109-
if let Some(common) = &self.time_shifts.common_time_shift {
110-
for member in self.all_time_members() {
111-
if let Some(exists) = resolved_time_shifts.get_mut(&member.full_name()) {
112-
exists.interval += common;
113-
} else {
114-
let time_shift = DimensionTimeShift {
115-
interval: common.clone(),
116-
dimension: member.clone(),
117-
};
118-
resolved_time_shifts.insert(member.full_name(), time_shift);
119-
}
120-
}
121-
}
122-
resolved_time_shifts
123-
}
124-
125106
fn all_time_members(&self) -> Vec<Rc<MemberSymbol>> {
126107
let mut filter_symbols = self.all_dimensions_symbols();
127108
for filter_item in self
@@ -151,6 +132,7 @@ impl MultiStageAppliedState {
151132
None
152133
}
153134
})
135+
.unique_by(|s| s.full_name())
154136
.collect_vec();
155137
time_symbols
156138
}
@@ -402,7 +384,6 @@ impl PartialEq for MultiStageAppliedState {
402384
&& self.time_dimensions_filters == other.time_dimensions_filters
403385
&& self.dimensions_filters == other.dimensions_filters
404386
&& self.measures_filters == other.measures_filters
405-
&& self.time_shifts.common_time_shift == other.time_shifts.common_time_shift
406387
&& self.time_shifts.dimensions_shifts == other.time_shifts.dimensions_shifts
407388
}
408389
}

rust/cubesqlplanner/cubesqlplanner/src/planner/planners/multi_stage/member_query_planner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ impl MultiStageMemberQueryPlanner {
301301
measure: member_node.clone(),
302302
query,
303303
render_measure_as_state: self.description.member().has_aggregates_on_top(),
304-
time_shifts: self.description.state().resolved_time_shifts(),
304+
time_shifts: self.description.state().time_shifts().clone(),
305305
render_measure_for_ungrouped: self.description.member().is_ungrupped(),
306306
};
307307
let result = LogicalMultiStageMember {

rust/cubesqlplanner/cubesqlplanner/src/planner/sql_evaluator/sql_nodes/factory.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ use super::{
77
};
88
use crate::plan::schema::QualifiedColumnName;
99
use crate::planner::planners::multi_stage::TimeShiftState;
10-
use crate::planner::sql_evaluator::DimensionTimeShift;
1110
use std::collections::{HashMap, HashSet};
1211
use std::rc::Rc;
1312

1413
#[derive(Clone)]
1514
pub struct SqlNodesFactory {
16-
time_shifts: HashMap<String, DimensionTimeShift>,
15+
time_shifts: TimeShiftState,
1716
ungrouped: bool,
1817
ungrouped_measure: bool,
1918
count_approx_as_state: bool,
@@ -34,7 +33,7 @@ pub struct SqlNodesFactory {
3433
impl SqlNodesFactory {
3534
pub fn new() -> Self {
3635
Self {
37-
time_shifts: HashMap::new(),
36+
time_shifts: TimeShiftState::default(),
3837
ungrouped: false,
3938
ungrouped_measure: false,
4039
count_approx_as_state: false,
@@ -53,7 +52,7 @@ impl SqlNodesFactory {
5352
}
5453
}
5554

56-
pub fn set_time_shifts(&mut self, time_shifts: HashMap<String, DimensionTimeShift>) {
55+
pub fn set_time_shifts(&mut self, time_shifts: TimeShiftState) {
5756
self.time_shifts = time_shifts;
5857
}
5958

rust/cubesqlplanner/cubesqlplanner/src/planner/sql_evaluator/sql_nodes/time_shift.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
use super::SqlNode;
22
use crate::planner::planners::multi_stage::TimeShiftState;
33
use crate::planner::query_tools::QueryTools;
4-
use crate::planner::sql_evaluator::DimensionTimeShift;
54
use crate::planner::sql_evaluator::MemberSymbol;
65
use crate::planner::sql_evaluator::SqlEvaluatorVisitor;
76
use crate::planner::sql_templates::PlanSqlTemplates;
8-
use crate::planner::SqlInterval;
97
use cubenativeutils::CubeError;
108
use std::any::Any;
11-
use std::collections::HashMap;
129
use std::rc::Rc;
1310

1411
pub struct TimeShiftSqlNode {
15-
shifts: HashMap<String, DimensionTimeShift>,
12+
shifts: TimeShiftState,
1613
input: Rc<dyn SqlNode>,
1714
}
1815

1916
impl TimeShiftSqlNode {
20-
pub fn new(shifts: HashMap<String, DimensionTimeShift>, input: Rc<dyn SqlNode>) -> Rc<Self> {
17+
pub fn new(shifts: TimeShiftState, input: Rc<dyn SqlNode>) -> Rc<Self> {
2118
Rc::new(Self { shifts, input })
2219
}
2320

@@ -45,7 +42,7 @@ impl SqlNode for TimeShiftSqlNode {
4542
let res = match node.as_ref() {
4643
MemberSymbol::Dimension(ev) => {
4744
if !ev.is_reference() && ev.dimension_type() == "time" {
48-
if let Some(shift) = self.shifts.get(&ev.full_name()) {
45+
if let Some(shift) = self.shifts.dimensions_shifts.get(&ev.full_name()) {
4946
let shift = shift.interval.to_sql();
5047
let res = templates.add_timestamp_interval(input, shift)?;
5148
format!("({})", res)

0 commit comments

Comments
 (0)