Skip to content

Commit ed5c593

Browse files
committed
in work
1 parent 42434d8 commit ed5c593

File tree

6 files changed

+95
-6
lines changed

6 files changed

+95
-6
lines changed

packages/cubejs-schema-compiler/test/integration/postgres/sql-generation.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ describe('SQL Generation', () => {
316316
}
317317
}
318318
},
319+
created_month: {
320+
type: 'time',
321+
sql: \`\${created_at.month}\`
322+
},
319323
updated_at: {
320324
type: 'time',
321325
sql: 'updated_at'
@@ -1343,6 +1347,33 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
13431347
});
13441348
}
13451349

1350+
if (getEnv('nativeSqlPlanner')) {
1351+
it('rolling count proxy time dimension', async () => {
1352+
await runQueryTest({
1353+
measures: [
1354+
'visitors.countRollingThreeMonth'
1355+
],
1356+
dimensions: [
1357+
'visitors.created_month'
1358+
],
1359+
order: [{
1360+
id: 'visitors.created_month'
1361+
}],
1362+
timezone: 'America/Los_Angeles'
1363+
}, [
1364+
{ visitors__created_month: '2016-09-01T00:00:00.000Z', visitors__count_rolling_three_month: '1' },
1365+
{ visitors__created_month: '2016-10-01T00:00:00.000Z', visitors__count_rolling_three_month: '1' },
1366+
{ visitors__created_month: '2016-11-01T00:00:00.000Z', visitors__count_rolling_three_month: '1' },
1367+
{ visitors__created_month: '2016-12-01T00:00:00.000Z', visitors__count_rolling_three_month: null },
1368+
{ visitors__created_month: '2017-01-01T00:00:00.000Z', visitors__count_rolling_three_month: '5' },
1369+
]);
1370+
});
1371+
} else {
1372+
it.skip('rolling count without date range', () => {
1373+
// Skipping because it works only in Tesseract
1374+
});
1375+
}
1376+
13461377
it('rolling qtd', async () => runQueryTest({
13471378
measures: [
13481379
'visitors.revenue_qtd'

rust/cubesqlplanner/cubesqlplanner/src/plan/time_series.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
use super::{Schema, SchemaColumn};
2-
use crate::planner::{
3-
query_tools::QueryTools,
4-
sql_templates::PlanSqlTemplates,
5-
Granularity,
6-
};
2+
use crate::planner::{query_tools::QueryTools, sql_templates::PlanSqlTemplates, Granularity};
73
use cubenativeutils::CubeError;
84
use std::rc::Rc;
95

rust/cubesqlplanner/cubesqlplanner/src/planner/base_time_dimension.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,36 @@ impl BaseMember for BaseTimeDimension {
6363
}
6464

6565
impl BaseTimeDimension {
66+
pub fn try_new_from_td_symbol(
67+
query_tools: Rc<QueryTools>,
68+
td_symbol: &TimeDimensionSymbol,
69+
) -> Result<Rc<Self>, CubeError> {
70+
let dimension =
71+
BaseDimension::try_new_required(td_symbol.base_symbol().clone(), query_tools.clone())?;
72+
let granularity = td_symbol.granularity().clone();
73+
let granularity_obj = td_symbol.granularity_obj().clone();
74+
let date_range = td_symbol.date_range_vec();
75+
let alias_suffix = td_symbol.alias_suffix();
76+
let default_alias = BaseMemberHelper::default_alias(
77+
&dimension.cube_name(),
78+
&dimension.name(),
79+
&Some(alias_suffix.clone()),
80+
query_tools.clone(),
81+
)?;
82+
let member_evaluator = Rc::new(MemberSymbol::TimeDimension(td_symbol.clone()));
83+
84+
Ok(Rc::new(Self {
85+
dimension,
86+
query_tools,
87+
granularity,
88+
granularity_obj,
89+
date_range,
90+
alias_suffix,
91+
default_alias,
92+
member_evaluator,
93+
}))
94+
}
95+
6696
pub fn try_new_required(
6797
query_tools: Rc<QueryTools>,
6898
member_evaluator: Rc<MemberSymbol>,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ impl MultiStageAppliedState {
117117
self.time_dimensions = time_dimensions;
118118
}
119119

120+
pub fn set_dimensions(&mut self, dimensions: Vec<Rc<BaseDimension>>) {
121+
self.dimensions = dimensions;
122+
}
123+
120124
pub fn remove_filter_for_member(&mut self, member_name: &String) {
121125
self.time_dimensions_filters =
122126
self.extract_filters_exclude_member(member_name, &self.time_dimensions_filters);

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,18 @@ impl RollingWindowPlanner {
5151
}
5252
_ => false,
5353
};
54-
let time_dimensions = self.query_properties.time_dimensions();
54+
let mut time_dimensions = self.query_properties.time_dimensions().clone();
55+
for dim in self.query_properties.dimension_symbols() {
56+
let dim = dim.resolve_reference_chain();
57+
if let Ok(time_dimension_symbol) = dim.as_time_dimension() {
58+
let time_dimension = BaseTimeDimension::try_new_from_td_symbol(
59+
self.query_tools.clone(),
60+
time_dimension_symbol,
61+
)?;
62+
time_dimensions.push(time_dimension);
63+
}
64+
}
65+
5566
if time_dimensions.len() == 0 {
5667
let rolling_base = self.add_rolling_window_base(
5768
member.clone(),
@@ -358,6 +369,19 @@ impl RollingWindowPlanner {
358369
//We keep only one time_dimension in the leaf query because, even if time_dimension values have different granularity, in the leaf query we need to group by the lowest granularity.
359370
new_state.set_time_dimensions(vec![new_time_dimension.clone()]);
360371

372+
let dimensions = new_state
373+
.dimensions()
374+
.clone()
375+
.into_iter()
376+
.filter(|d| {
377+
d.member_evaluator()
378+
.resolve_reference_chain()
379+
.as_time_dimension()
380+
.is_err()
381+
})
382+
.collect_vec();
383+
new_state.set_dimensions(dimensions);
384+
361385
if let Some(granularity) = self.get_to_date_rolling_granularity(rolling_window)? {
362386
new_state.replace_to_date_date_range_filter(&time_dimension_base_name, &granularity);
363387
} else {

rust/cubesqlplanner/cubesqlplanner/src/planner/sql_evaluator/symbols/time_dimension_symbol.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ impl TimeDimensionSymbol {
6868
self.base_symbol.owned_by_cube()
6969
}
7070

71+
pub fn date_range_vec(&self) -> Option<Vec<String>> {
72+
self.date_range.clone().map(|(from, to)| vec![from, to])
73+
}
74+
7175
pub fn get_dependencies_as_time_dimensions(&self) -> Vec<Rc<MemberSymbol>> {
7276
self.get_dependencies()
7377
.into_iter()

0 commit comments

Comments
 (0)