Skip to content

Commit deaae68

Browse files
committed
implement baseQuery's dimensionTimeGroupedColumn() in Granularity object
1 parent f987713 commit deaae68

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

rust/cubesqlplanner/cubesqlplanner/src/planner/sql_templates/plan.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ impl PlanSqlTemplates {
6262
.time_grouped_column(granularity, dimension)
6363
}
6464

65+
pub fn date_bin(
66+
&self,
67+
interval: String,
68+
source: String,
69+
origin: String,
70+
) -> Result<String, CubeError> {
71+
self.driver_tools.date_bin(interval, source, origin)
72+
}
73+
6574
pub fn timestamp_precision(&self) -> Result<u32, CubeError> {
6675
self.driver_tools.timestamp_precision()
6776
}
@@ -121,14 +130,6 @@ impl PlanSqlTemplates {
121130
self.driver_tools.count_distinct_approx(sql)
122131
}
123132

124-
pub fn date_bin(
125-
&self,
126-
interval: String,
127-
source: String,
128-
origin: String,
129-
) -> Result<String, CubeError> {
130-
self.driver_tools.date_bin(interval, source, origin)
131-
}
132133
pub fn alias_name(name: &str) -> String {
133134
let res = name
134135
.with_boundaries(&[

rust/cubesqlplanner/cubesqlplanner/src/planner/time_dimension/granularity.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{GranularityHelper, QueryDateTime, SqlInterval};
22
use crate::planner::sql_evaluator::SqlCall;
3+
use crate::planner::sql_templates::PlanSqlTemplates;
34
use chrono_tz::Tz;
45
use cubenativeutils::CubeError;
56
use itertools::Itertools;
@@ -166,4 +167,29 @@ impl Granularity {
166167
fn default_origin(timezone: Tz) -> Result<QueryDateTime, CubeError> {
167168
Ok(QueryDateTime::now(timezone)?.start_of_year())
168169
}
170+
171+
pub fn apply_to_input_sql(
172+
&self,
173+
templates: &PlanSqlTemplates,
174+
input: String,
175+
) -> Result<String, CubeError> {
176+
let res = if self.is_natural_aligned {
177+
if let Some(offset) = &self.granularity_offset {
178+
let mut res = templates.subtract_interval(input.clone(), offset.clone())?;
179+
res = templates.time_grouped_column(self.granularity_from_interval()?, res)?;
180+
res = templates.add_interval(res, offset.clone())?;
181+
res
182+
} else {
183+
templates.time_grouped_column(self.granularity_from_interval()?, input)?
184+
}
185+
} else {
186+
templates.date_bin(
187+
self.granularity_interval.clone(),
188+
input,
189+
self.origin_local_formatted(),
190+
)?
191+
};
192+
193+
Ok(res)
194+
}
169195
}

0 commit comments

Comments
 (0)