Skip to content

Commit 5ef6e08

Browse files
committed
simplify builder by moving extract_time_shifts to time_shift_state
1 parent f88e934 commit 5ef6e08

File tree

4 files changed

+71
-52
lines changed

4 files changed

+71
-52
lines changed

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

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,46 +33,7 @@ impl PhysicalPlanBuilderContext {
3333
pub fn make_sql_nodes_factory(&self) -> Result<SqlNodesFactory, CubeError> {
3434
let mut factory = SqlNodesFactory::new();
3535

36-
let mut time_shifts = HashMap::new();
37-
let mut calendar_time_shifts = HashMap::new();
38-
39-
for (key, shift) in self.time_shifts.dimensions_shifts.iter() {
40-
if let Ok(dimension) = shift.dimension.as_dimension() {
41-
// 1. Shift might be referenced by name or by interval
42-
// 2. Shift body might be defined in calendar dimension as:
43-
// * sql reference
44-
// * interval + type
45-
46-
if let Some(dim_shift_name) = &shift.name {
47-
if let Some((dim_key, cts)) =
48-
dimension.calendar_time_shift_for_named_interval(dim_shift_name)
49-
{
50-
calendar_time_shifts.insert(dim_key.clone(), cts.clone());
51-
} else if let Some(_calendar_pk) = dimension.time_shift_pk_full_name() {
52-
return Err(CubeError::internal(format!(
53-
"Time shift with name {} not found for dimension {}",
54-
dim_shift_name,
55-
dimension.full_name()
56-
)));
57-
}
58-
} else if let Some(dim_shift_interval) = &shift.interval {
59-
if let Some((dim_key, cts)) =
60-
dimension.calendar_time_shift_for_interval(dim_shift_interval)
61-
{
62-
calendar_time_shifts.insert(dim_key.clone(), cts.clone());
63-
} else if let Some(calendar_pk) = dimension.time_shift_pk_full_name() {
64-
let mut shift = shift.clone();
65-
shift.interval = Some(dim_shift_interval.inverse());
66-
time_shifts.insert(calendar_pk, shift.clone());
67-
} else {
68-
time_shifts.insert(key.clone(), shift.clone());
69-
}
70-
}
71-
} else {
72-
time_shifts.insert(key.clone(), shift.clone());
73-
}
74-
}
75-
36+
let (time_shifts, calendar_time_shifts) = self.time_shifts.extract_time_shifts()?;
7637
let common_time_shifts = TimeShiftState {
7738
dimensions_shifts: time_shifts,
7839
};

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
use crate::plan::{FilterGroup, FilterItem};
22
use crate::planner::filter::FilterOperator;
3+
use crate::planner::planners::multi_stage::time_shift_state::TimeShiftState;
34
use crate::planner::sql_evaluator::{DimensionTimeShift, MeasureTimeShifts, MemberSymbol};
45
use crate::planner::{BaseDimension, BaseMember, BaseTimeDimension};
56
use cubenativeutils::CubeError;
67
use itertools::Itertools;
78
use std::cmp::PartialEq;
8-
use std::collections::HashMap;
99
use std::fmt::Debug;
1010
use std::rc::Rc;
1111

12-
#[derive(Clone, Default, Debug)]
13-
pub struct TimeShiftState {
14-
pub dimensions_shifts: HashMap<String, DimensionTimeShift>,
15-
}
16-
17-
impl TimeShiftState {
18-
pub fn is_empty(&self) -> bool {
19-
self.dimensions_shifts.is_empty()
20-
}
21-
}
22-
2312
#[derive(Clone)]
2413
pub struct MultiStageAppliedState {
2514
time_dimensions: Vec<Rc<BaseTimeDimension>>,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ mod member;
33
mod member_query_planner;
44
mod multi_stage_query_planner;
55
mod query_description;
6+
mod time_shift_state;
67

78
pub use applied_state::*;
89
pub use member::*;
910
pub use member_query_planner::MultiStageMemberQueryPlanner;
1011
pub use multi_stage_query_planner::MultiStageQueryPlanner;
1112
pub use query_description::MultiStageQueryDescription;
13+
pub use time_shift_state::TimeShiftState;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use crate::planner::sql_evaluator::symbols::CalendarDimensionTimeShift;
2+
use crate::planner::sql_evaluator::DimensionTimeShift;
3+
use cubenativeutils::CubeError;
4+
use std::collections::HashMap;
5+
6+
#[derive(Clone, Default, Debug)]
7+
pub struct TimeShiftState {
8+
pub dimensions_shifts: HashMap<String, DimensionTimeShift>,
9+
}
10+
11+
impl TimeShiftState {
12+
pub fn is_empty(&self) -> bool {
13+
self.dimensions_shifts.is_empty()
14+
}
15+
16+
pub fn extract_time_shifts(
17+
&self,
18+
) -> Result<
19+
(
20+
HashMap<String, DimensionTimeShift>,
21+
HashMap<String, CalendarDimensionTimeShift>,
22+
),
23+
CubeError,
24+
> {
25+
let mut time_shifts = HashMap::new();
26+
let mut calendar_time_shifts = HashMap::new();
27+
28+
for (key, shift) in self.dimensions_shifts.iter() {
29+
if let Ok(dimension) = shift.dimension.as_dimension() {
30+
// 1. Shift might be referenced by name or by interval
31+
// 2. Shift body might be defined in calendar dimension as:
32+
// * sql reference
33+
// * interval + type
34+
35+
if let Some(dim_shift_name) = &shift.name {
36+
if let Some((dim_key, cts)) =
37+
dimension.calendar_time_shift_for_named_interval(dim_shift_name)
38+
{
39+
calendar_time_shifts.insert(dim_key.clone(), cts.clone());
40+
} else if let Some(_calendar_pk) = dimension.time_shift_pk_full_name() {
41+
return Err(CubeError::internal(format!(
42+
"Time shift with name {} not found for dimension {}",
43+
dim_shift_name,
44+
dimension.full_name()
45+
)));
46+
}
47+
} else if let Some(dim_shift_interval) = &shift.interval {
48+
if let Some((dim_key, cts)) =
49+
dimension.calendar_time_shift_for_interval(dim_shift_interval)
50+
{
51+
calendar_time_shifts.insert(dim_key.clone(), cts.clone());
52+
} else if let Some(calendar_pk) = dimension.time_shift_pk_full_name() {
53+
let mut shift = shift.clone();
54+
shift.interval = Some(dim_shift_interval.inverse());
55+
time_shifts.insert(calendar_pk, shift.clone());
56+
} else {
57+
time_shifts.insert(key.clone(), shift.clone());
58+
}
59+
}
60+
} else {
61+
time_shifts.insert(key.clone(), shift.clone());
62+
}
63+
}
64+
65+
Ok((time_shifts, calendar_time_shifts))
66+
}
67+
}

0 commit comments

Comments
 (0)