Skip to content

Commit 0d605e2

Browse files
committed
Use Granularity obj in ToDateRollingWindowJoinCondition
1 parent deaae68 commit 0d605e2

File tree

5 files changed

+40
-35
lines changed

5 files changed

+40
-35
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::logical_plan::*;
22
use crate::planner::query_properties::OrderByItem;
33
use crate::planner::sql_evaluator::MemberSymbol;
4+
use crate::planner::Granularity;
45
use std::rc::Rc;
56

67
pub struct MultiStageRegularRollingWindow {
@@ -24,14 +25,17 @@ impl PrettyPrint for MultiStageRegularRollingWindow {
2425
}
2526

2627
pub struct MultiStageToDateRollingWindow {
27-
pub granularity: String,
28+
pub granularity_obj: Rc<Granularity>,
2829
}
2930

3031
impl PrettyPrint for MultiStageToDateRollingWindow {
3132
fn pretty_print(&self, result: &mut PrettyPrintResult, state: &PrettyPrintState) {
3233
result.println("ToDate Rolling Window", state);
3334
let state = state.new_level();
34-
result.println(&format!("granularity: {}", self.granularity), &state);
35+
result.println(
36+
&format!("granularity: {}", self.granularity_obj.granularity()),
37+
&state,
38+
);
3539
}
3640
}
3741

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ impl PhysicalPlanBuilder {
10611061
MultiStageRollingWindowType::ToDate(to_date_rolling_window) => {
10621062
JoinCondition::new_to_date_rolling_join(
10631063
root_alias.clone(),
1064-
to_date_rolling_window.granularity.clone(),
1064+
to_date_rolling_window.granularity_obj.clone(),
10651065
Expr::Reference(QualifiedColumnName::new(
10661066
Some(measure_input_alias.clone()),
10671067
base_time_dimension_alias,
@@ -1092,7 +1092,7 @@ impl PhysicalPlanBuilder {
10921092
let mut render_references = HashMap::new();
10931093
let mut select_builder = SelectBuilder::new(from.clone());
10941094

1095-
//We insert render reference for main time dimension (with the some granularity as in time series to avoid unnecessary date_tranc)
1095+
//We insert render reference for main time dimension (with some granularity as in time series to avoid unnecessary date_tranc)
10961096
render_references.insert(
10971097
time_dimension.full_name(),
10981098
QualifiedColumnName::new(Some(root_alias.clone()), format!("date_from")),

rust/cubesqlplanner/cubesqlplanner/src/plan/join.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{Expr, SingleAliasedSource};
22
use crate::planner::query_tools::QueryTools;
33
use crate::planner::sql_templates::PlanSqlTemplates;
4-
use crate::planner::{BaseJoinCondition, VisitorContext};
4+
use crate::planner::{BaseJoinCondition, Granularity, VisitorContext};
55
use cubenativeutils::CubeError;
66
use lazy_static::lazy_static;
77

@@ -118,15 +118,15 @@ impl RollingTotalJoinCondition {
118118
}
119119
pub struct ToDateRollingWindowJoinCondition {
120120
time_series_source: String,
121-
granularity: String,
121+
granularity: Rc<Granularity>,
122122
time_dimension: Expr,
123123
_query_tools: Rc<QueryTools>,
124124
}
125125

126126
impl ToDateRollingWindowJoinCondition {
127127
pub fn new(
128128
time_series_source: String,
129-
granularity: String,
129+
granularity: Rc<Granularity>,
130130
time_dimension: Expr,
131131
query_tools: Rc<QueryTools>,
132132
) -> Self {
@@ -151,7 +151,7 @@ impl ToDateRollingWindowJoinCondition {
151151
templates.column_reference(&Some(self.time_series_source.clone()), "date_from")?;
152152
let date_from = templates.rolling_window_expr_timestamp_cast(&date_from)?;
153153
let date_to = templates.rolling_window_expr_timestamp_cast(&date_to)?;
154-
let grouped_from = templates.time_grouped_column(self.granularity.clone(), date_from)?;
154+
let grouped_from = self.granularity.apply_to_input_sql(templates, date_from)?;
155155
let result = format!("{date_column} >= {grouped_from} and {date_column} <= {date_to}");
156156
Ok(result)
157157
}
@@ -243,7 +243,7 @@ impl JoinCondition {
243243

244244
pub fn new_to_date_rolling_join(
245245
time_series_source: String,
246-
granularity: String,
246+
granularity: Rc<Granularity>,
247247
time_dimension: Expr,
248248
query_tools: Rc<QueryTools>,
249249
) -> Self {

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use crate::logical_plan::*;
66
use crate::planner::planners::{multi_stage::RollingWindowType, QueryPlanner, SimpleQueryPlanner};
77
use crate::planner::query_tools::QueryTools;
88
use crate::planner::sql_evaluator::MemberSymbol;
9-
use crate::planner::{BaseDimension, BaseMeasure, BaseMember, BaseMemberHelper, BaseTimeDimension};
9+
use crate::planner::{
10+
BaseDimension, BaseMeasure, BaseMember, BaseMemberHelper, BaseTimeDimension, GranularityHelper,
11+
};
1012
use crate::planner::{OrderByItem, QueryProperties};
1113

1214
use cubenativeutils::CubeError;
@@ -126,8 +128,30 @@ impl MultiStageMemberQueryPlanner {
126128
})
127129
}
128130
RollingWindowType::ToDate(to_date_rolling_window) => {
131+
let time_dimension = &rolling_window_desc.time_dimension;
132+
let query_granularity = to_date_rolling_window.granularity.clone();
133+
134+
let evaluator_compiler_cell = self.query_tools.evaluator_compiler().clone();
135+
let mut evaluator_compiler = evaluator_compiler_cell.borrow_mut();
136+
137+
let Some(granularity_obj) = GranularityHelper::make_granularity_obj(
138+
self.query_tools.cube_evaluator().clone(),
139+
&mut evaluator_compiler,
140+
self.query_tools.timezone().clone(),
141+
time_dimension.cube_name(),
142+
time_dimension.name(),
143+
Some(query_granularity.clone()),
144+
)?
145+
else {
146+
return Err(CubeError::internal(format!(
147+
"Rolling window granularity '{}' is not found in time dimension '{}'",
148+
query_granularity,
149+
time_dimension.name()
150+
)));
151+
};
152+
129153
MultiStageRollingWindowType::ToDate(MultiStageToDateRollingWindow {
130-
granularity: to_date_rolling_window.granularity.clone(),
154+
granularity_obj: Rc::new(granularity_obj),
131155
})
132156
}
133157
RollingWindowType::RunningTotal => MultiStageRollingWindowType::RunningTotal,

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

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,7 @@ impl SqlNode for TimeDimensionNode {
6262
templates.convert_tz(input_sql)?
6363
};
6464

65-
let res = if granularity_obj.is_natural_aligned() {
66-
if let Some(granularity_offset) = granularity_obj.granularity_offset() {
67-
let dt = templates
68-
.subtract_interval(converted_tz, granularity_offset.clone())?;
69-
let dt = templates.time_grouped_column(
70-
granularity_obj.granularity_from_interval()?,
71-
dt,
72-
)?;
73-
templates.add_interval(dt, granularity_offset.clone())?
74-
} else {
75-
templates.time_grouped_column(
76-
granularity_obj.granularity().clone(),
77-
converted_tz,
78-
)?
79-
}
80-
} else {
81-
templates.date_bin(
82-
granularity_obj.granularity_interval().clone(),
83-
converted_tz,
84-
granularity_obj.origin_local_formatted(),
85-
)?
86-
};
87-
88-
res
65+
granularity_obj.apply_to_input_sql(templates, converted_tz)?
8966
} else {
9067
input_sql
9168
};

0 commit comments

Comments
 (0)