Skip to content

Commit 12880ec

Browse files
committed
feat(tesseract): Catch up with features recently added to BaseQuery
1 parent aae3b05 commit 12880ec

File tree

16 files changed

+171
-88
lines changed

16 files changed

+171
-88
lines changed

rust/cubesqlplanner/cubesqlplanner/src/cube_bridge/measure_definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct TimeShiftReference {
1919
#[serde(rename = "type")]
2020
pub shift_type: Option<String>,
2121
#[serde(rename = "timeDimension")]
22-
pub time_dimension: String,
22+
pub time_dimension: Option<String>,
2323
}
2424

2525
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ impl PrettyPrint for MultiStageAppliedState {
5353
}
5454

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

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use crate::logical_plan::*;
2-
use crate::planner::sql_evaluator::MeasureTimeShift;
2+
use crate::planner::planners::multi_stage::TimeShiftState;
33
use crate::planner::sql_evaluator::MemberSymbol;
4-
use std::collections::HashMap;
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, MeasureTimeShift>,
10+
pub time_shifts: TimeShiftState,
1211
pub query: Rc<Query>,
1312
}
1413

@@ -23,10 +22,13 @@ impl PrettyPrint for MultiStageLeafMeasure {
2322
if self.render_measure_for_ungrouped {
2423
result.println("render_measure_for_ungrouped: true", &state);
2524
}
26-
if !self.time_shifts.is_empty() {
25+
if !self.time_shifts.dimensions_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+
if let Some(common) = &self.time_shifts.common_time_shift {
29+
result.println(&format!("- common: {}", common.to_sql()), &details_state);
30+
}
31+
for (_, time_shift) in self.time_shifts.dimensions_shifts.iter() {
3032
result.println(
3133
&format!(
3234
"- {}: {}",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::logical_plan::*;
22
use crate::plan::schema::QualifiedColumnName;
33
use crate::plan::*;
4+
use crate::planner::planners::multi_stage::TimeShiftState;
45
use crate::planner::query_properties::OrderByItem;
56
use crate::planner::query_tools::QueryTools;
67
use crate::planner::sql_evaluator::sql_nodes::SqlNodesFactory;
7-
use crate::planner::sql_evaluator::MeasureTimeShift;
88
use crate::planner::sql_evaluator::MemberSymbol;
99
use crate::planner::sql_evaluator::ReferencesBuilder;
1010
use crate::planner::sql_templates::PlanSqlTemplates;
@@ -24,7 +24,7 @@ struct PhysicalPlanBuilderContext {
2424
pub alias_prefix: Option<String>,
2525
pub render_measure_as_state: bool, //Render measure as state, for example hll state for count_approx
2626
pub render_measure_for_ungrouped: bool,
27-
pub time_shifts: HashMap<String, MeasureTimeShift>,
27+
pub time_shifts: TimeShiftState,
2828
pub original_sql_pre_aggregations: HashMap<String, String>,
2929
}
3030

rust/cubesqlplanner/cubesqlplanner/src/planner/base_measure.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::query_tools::QueryTools;
2-
use super::sql_evaluator::{MeasureTimeShift, MemberExpressionSymbol, MemberSymbol, SqlCall};
2+
use super::sql_evaluator::{MeasureTimeShifts, MemberExpressionSymbol, MemberSymbol, SqlCall};
33
use super::{evaluate_with_context, BaseMember, BaseMemberHelper, VisitorContext};
44
use crate::cube_bridge::measure_definition::{MeasureDefinition, RollingWindow};
55
use crate::planner::sql_templates::PlanSqlTemplates;
@@ -208,10 +208,10 @@ impl BaseMeasure {
208208
}
209209
}
210210

211-
pub fn time_shifts(&self) -> Vec<MeasureTimeShift> {
211+
pub fn time_shift(&self) -> Option<MeasureTimeShifts> {
212212
match self.member_evaluator.as_ref() {
213-
MemberSymbol::Measure(measure_symbol) => measure_symbol.time_shifts().clone(),
214-
_ => vec![],
213+
MemberSymbol::Measure(measure_symbol) => measure_symbol.time_shift().clone(),
214+
_ => None,
215215
}
216216
}
217217

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

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
use crate::plan::{FilterGroup, FilterItem};
22
use crate::planner::filter::FilterOperator;
3-
use crate::planner::sql_evaluator::{MeasureTimeShift, MemberSymbol};
4-
use crate::planner::{BaseDimension, BaseMember, BaseTimeDimension};
3+
use crate::planner::sql_evaluator::{DimensionTimeShift, MeasureTimeShifts, MemberSymbol};
4+
use crate::planner::{BaseDimension, BaseMember, BaseTimeDimension, SqlInterval};
55
use itertools::Itertools;
66
use std::cmp::PartialEq;
77
use std::collections::HashMap;
88
use std::fmt::Debug;
99
use std::rc::Rc;
1010

11+
#[derive(Clone, Default, Debug)]
12+
pub struct TimeShiftState {
13+
pub dimensions_shifts: HashMap<String, DimensionTimeShift>,
14+
pub common_time_shift: Option<SqlInterval>,
15+
}
16+
17+
impl TimeShiftState {
18+
pub fn is_empty(&self) -> bool {
19+
self.dimensions_shifts.is_empty() && self.common_time_shift.is_none()
20+
}
21+
}
22+
1123
#[derive(Clone)]
1224
pub struct MultiStageAppliedState {
1325
time_dimensions: Vec<Rc<BaseTimeDimension>>,
@@ -16,7 +28,7 @@ pub struct MultiStageAppliedState {
1628
dimensions_filters: Vec<FilterItem>,
1729
measures_filters: Vec<FilterItem>,
1830
segments: Vec<FilterItem>,
19-
time_shifts: HashMap<String, MeasureTimeShift>,
31+
time_shifts: TimeShiftState,
2032
}
2133

2234
impl MultiStageAppliedState {
@@ -35,7 +47,7 @@ impl MultiStageAppliedState {
3547
dimensions_filters,
3648
measures_filters,
3749
segments,
38-
time_shifts: HashMap::new(),
50+
time_shifts: TimeShiftState::default(),
3951
})
4052
}
4153

@@ -61,17 +73,34 @@ impl MultiStageAppliedState {
6173
.collect_vec();
6274
}
6375

64-
pub fn add_time_shifts(&mut self, time_shifts: Vec<MeasureTimeShift>) {
65-
for ts in time_shifts.into_iter() {
66-
if let Some(exists) = self.time_shifts.get_mut(&ts.dimension.full_name()) {
67-
exists.interval += ts.interval;
68-
} else {
69-
self.time_shifts.insert(ts.dimension.full_name(), ts);
76+
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+
}
7099
}
71100
}
72101
}
73102

74-
pub fn time_shifts(&self) -> &HashMap<String, MeasureTimeShift> {
103+
pub fn time_shifts(&self) -> &TimeShiftState {
75104
&self.time_shifts
76105
}
77106

@@ -314,7 +343,8 @@ impl PartialEq for MultiStageAppliedState {
314343
&& self.time_dimensions_filters == other.time_dimensions_filters
315344
&& self.dimensions_filters == other.dimensions_filters
316345
&& self.measures_filters == other.measures_filters
317-
&& self.time_shifts == other.time_shifts
346+
&& self.time_shifts.common_time_shift == other.time_shifts.common_time_shift
347+
&& self.time_shifts.dimensions_shifts == other.time_shifts.dimensions_shifts
318348
}
319349
}
320350

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::planner::sql_evaluator::{MeasureTimeShift, MemberSymbol};
1+
use crate::planner::sql_evaluator::{MeasureTimeShifts, MemberSymbol};
22
use crate::planner::BaseTimeDimension;
33
use std::rc::Rc;
44

@@ -99,7 +99,7 @@ pub struct MultiStageInodeMember {
9999
reduce_by: Vec<Rc<MemberSymbol>>,
100100
add_group_by: Vec<Rc<MemberSymbol>>,
101101
group_by: Option<Vec<Rc<MemberSymbol>>>,
102-
time_shifts: Vec<MeasureTimeShift>,
102+
time_shift: Option<MeasureTimeShifts>,
103103
}
104104

105105
impl MultiStageInodeMember {
@@ -108,14 +108,14 @@ impl MultiStageInodeMember {
108108
reduce_by: Vec<Rc<MemberSymbol>>,
109109
add_group_by: Vec<Rc<MemberSymbol>>,
110110
group_by: Option<Vec<Rc<MemberSymbol>>>,
111-
time_shifts: Vec<MeasureTimeShift>,
111+
time_shift: Option<MeasureTimeShifts>,
112112
) -> Self {
113113
Self {
114114
inode_type,
115115
reduce_by,
116116
add_group_by,
117117
group_by,
118-
time_shifts,
118+
time_shift,
119119
}
120120
}
121121

@@ -149,8 +149,8 @@ impl MultiStageInodeMember {
149149
&self.group_by
150150
}
151151

152-
pub fn time_shifts(&self) -> &Vec<MeasureTimeShift> {
153-
&self.time_shifts
152+
pub fn time_shift(&self) -> &Option<MeasureTimeShifts> {
153+
&self.time_shift
154154
}
155155
}
156156

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod multi_stage_query_planner;
55
mod query_description;
66
mod rolling_window_planner;
77

8-
pub use applied_state::MultiStageAppliedState;
8+
pub use applied_state::*;
99
pub use member::*;
1010
pub use member_query_planner::MultiStageMemberQueryPlanner;
1111
pub use multi_stage_query_planner::MultiStageQueryPlanner;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl MultiStageQueryPlanner {
124124
MultiStageInodeMemberType::Calculate
125125
};
126126

127-
let time_shifts = measure.time_shifts();
127+
let time_shift = measure.time_shift();
128128

129129
let is_ungrupped = match &member_type {
130130
MultiStageInodeMemberType::Rank | MultiStageInodeMemberType::Calculate => true,
@@ -146,7 +146,7 @@ impl MultiStageQueryPlanner {
146146
reduce_by,
147147
add_group_by,
148148
group_by,
149-
time_shifts,
149+
time_shift,
150150
),
151151
is_ungrupped,
152152
)
@@ -157,7 +157,7 @@ impl MultiStageQueryPlanner {
157157
vec![],
158158
vec![],
159159
None,
160-
vec![],
160+
None,
161161
),
162162
self.query_properties.ungrouped(),
163163
)
@@ -222,15 +222,15 @@ impl MultiStageQueryPlanner {
222222
.collect::<Result<Vec<_>, _>>()?;
223223

224224
let new_state = if !dimensions_to_add.is_empty()
225-
|| !multi_stage_member.time_shifts().is_empty()
225+
|| multi_stage_member.time_shift().is_some()
226226
|| state.has_filters_for_member(&member_name)
227227
{
228228
let mut new_state = state.clone_state();
229229
if !dimensions_to_add.is_empty() {
230230
new_state.add_dimensions(dimensions_to_add);
231231
}
232-
if !multi_stage_member.time_shifts().is_empty() {
233-
new_state.add_time_shifts(multi_stage_member.time_shifts().clone());
232+
if let Some(time_shift) = multi_stage_member.time_shift() {
233+
new_state.add_time_shifts(time_shift.clone());
234234
}
235235
if state.has_filters_for_member(&member_name) {
236236
new_state.remove_filter_for_member(&member_name);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl RollingWindowPlanner {
127127
vec![],
128128
vec![],
129129
None,
130-
vec![],
130+
None,
131131
);
132132

133133
let description = MultiStageQueryDescription::new(

0 commit comments

Comments
 (0)