Skip to content

Commit fbd5fdc

Browse files
committed
fix correct sql gen for calendar custom gran but with common timeshift
1 parent b12d4ed commit fbd5fdc

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

rust/cubesqlplanner/cubesqlplanner/src/cube_bridge/cube_definition.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub struct CubeDefinitionStatic {
1616
pub sql_alias: Option<String>,
1717
#[serde(rename = "isView")]
1818
pub is_view: Option<bool>,
19+
#[serde(rename = "calendar")]
20+
pub is_calendar: Option<bool>,
1921
}
2022

2123
#[nativebridge::native_bridge(CubeDefinitionStatic)]

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ impl PhysicalPlanBuilderContext {
4747
dimension.calendar_time_shift_for_interval(&shift.interval)
4848
{
4949
return Either::Right((dim_key.clone(), cts.clone()));
50+
} else if let Some(calendar_pk) = dimension.time_shift_pk() {
51+
let mut shift = shift.clone();
52+
shift.interval = shift.interval.inverse();
53+
return Either::Left((calendar_pk.full_name(), shift.clone()));
5054
}
5155
}
5256
Either::Left((key.clone(), shift.clone()))

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

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -380,40 +380,52 @@ impl SymbolFactory for DimensionSymbolFactory {
380380
vec![]
381381
};
382382

383-
let time_shift_pk = if !time_shift.is_empty() {
384-
let pk_member = cube_evaluator
383+
let cube = cube_evaluator.cube_from_path(cube_name.clone())?;
384+
let is_view = cube.static_data().is_view.unwrap_or(false);
385+
let is_calendar = cube.static_data().is_calendar.unwrap_or(false);
386+
387+
// If the cube is a calendar, we need to find the primary key member
388+
// so that we can use it for time shifts processing.
389+
let time_shift_pk = if is_calendar {
390+
let pk_members = cube_evaluator
385391
.static_data()
386392
.primary_keys
387393
.get(&cube_name)
388394
.cloned()
389-
.unwrap_or_else(|| vec![])
390-
.into_iter()
391-
.map(|primary_key| -> Result<_, CubeError> {
392-
let key_dimension_name = format!("{}.{}", cube_name, primary_key);
393-
let pk_member = compiler.add_dimension_evaluator(key_dimension_name)?;
395+
.unwrap_or_else(|| vec![]);
394396

395-
Ok(pk_member)
396-
})
397-
.collect::<Result<Vec<_>, _>>()?
398-
.into_iter()
399-
.filter(|pk_member| {
400-
if let Ok(pk_dimension) = pk_member.as_dimension() {
401-
if pk_dimension.dimension_type() == "time" { // TODO: What if calendar cube is joined via non-time dimension?
402-
return true;
397+
if pk_members.iter().any(|pk| **pk == name) {
398+
// To avoid evaluation loop.
399+
None
400+
} else {
401+
let pk_member = pk_members
402+
.into_iter()
403+
.map(|primary_key| -> Result<_, CubeError> {
404+
let key_dimension_name = format!("{}.{}", cube_name, primary_key);
405+
let pk_member = compiler.add_dimension_evaluator(key_dimension_name)?;
406+
407+
Ok(pk_member)
408+
})
409+
.collect::<Result<Vec<_>, _>>()?
410+
.into_iter()
411+
.filter(|pk_member| {
412+
if let Ok(pk_dimension) = pk_member.as_dimension() {
413+
// TODO: What if calendar cube is joined via non-time dimension?
414+
if pk_dimension.dimension_type() == "time" {
415+
return true;
416+
}
403417
}
404-
}
405-
false
406-
})
407-
.collect::<Vec<_>>()
408-
.first()
409-
.cloned();
410-
pk_member
418+
false
419+
})
420+
.collect::<Vec<_>>()
421+
.first()
422+
.cloned();
423+
pk_member
424+
}
411425
} else {
412426
None
413427
};
414428

415-
let cube = cube_evaluator.cube_from_path(cube_name.clone())?;
416-
let is_view = cube.static_data().is_view.unwrap_or(false);
417429
let owned_by_cube = definition.static_data().owned_by_cube.unwrap_or(true);
418430
let is_sub_query = definition.static_data().sub_query.unwrap_or(false);
419431
let is_multi_stage = definition.static_data().multi_stage.unwrap_or(false);

0 commit comments

Comments
 (0)