Skip to content

Commit 174e6c2

Browse files
committed
fix(tesseract): Fix rolling window external pre-aggregation
1 parent 81a1ae9 commit 174e6c2

File tree

13 files changed

+472
-129
lines changed

13 files changed

+472
-129
lines changed

packages/cubejs-schema-compiler/src/adapter/BaseQuery.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,11 @@ export class BaseQuery {
330330
*/
331331
this.customSubQueryJoins = this.options.subqueryJoins ?? [];
332332
this.useNativeSqlPlanner = this.options.useNativeSqlPlanner ?? getEnv('nativeSqlPlanner');
333-
this.canUseNativeSqlPlannerPreAggregation = false;
334-
if (this.useNativeSqlPlanner && !this.neverUseSqlPlannerPreaggregation()) {
333+
this.canUseNativeSqlPlannerPreAggregation = true;
334+
/* if (this.useNativeSqlPlanner && !this.neverUseSqlPlannerPreaggregation()) {
335335
const hasMultiStageMeasures = this.fullKeyQueryAggregateMeasures({ hasMultipliedForPreAggregation: true }).multiStageMembers.length > 0;
336336
this.canUseNativeSqlPlannerPreAggregation = hasMultiStageMeasures;
337-
}
337+
} */
338338
this.queryLevelJoinHints = this.options.joinHints ?? [];
339339
this.prebuildJoin();
340340

@@ -775,6 +775,13 @@ export class BaseQuery {
775775
);
776776
}
777777

778+
driverTools(external) {
779+
if (external && this.options.disableExternalPreAggregations && this.externalQueryClass) {
780+
return this.externalQuery();
781+
}
782+
return this;
783+
}
784+
778785
buildSqlAndParamsRust(exportAnnotatedSql) {
779786
const order = this.options.order && R.pipe(
780787
R.map((hash) => ((!hash || !hash.id) ? null : hash)),

rust/cubesqlplanner/cubesqlplanner/src/cube_bridge/base_tools.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::filter_params::{FilterParams, NativeFilterParams};
44
use super::pre_aggregation_obj::{NativePreAggregationObj, PreAggregationObj};
55
use super::security_context::{NativeSecurityContext, SecurityContext};
66
use super::sql_templates_render::{NativeSqlTemplatesRender, SqlTemplatesRender};
7+
use super::driver_tools::{NativeDriverTools, DriverTools};
78
use super::sql_utils::{NativeSqlUtils, SqlUtils};
89
use cubenativeutils::wrappers::serializer::{
910
NativeDeserialize, NativeDeserializer, NativeSerialize,
@@ -16,6 +17,7 @@ use std::rc::Rc;
1617

1718
#[nativebridge::native_bridge]
1819
pub trait BaseTools {
20+
fn driver_tools(&self, external: bool) -> Result<Rc<dyn DriverTools>, CubeError>;
1921
fn convert_tz(&self, field: String) -> Result<String, CubeError>;
2022
fn time_grouped_column(
2123
&self,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use super::base_query_options::FilterItem;
2+
use super::filter_group::{FilterGroup, NativeFilterGroup};
3+
use super::filter_params::{FilterParams, NativeFilterParams};
4+
use super::pre_aggregation_obj::{NativePreAggregationObj, PreAggregationObj};
5+
use super::security_context::{NativeSecurityContext, SecurityContext};
6+
use super::sql_templates_render::{NativeSqlTemplatesRender, SqlTemplatesRender};
7+
use super::sql_utils::{NativeSqlUtils, SqlUtils};
8+
use cubenativeutils::wrappers::serializer::{
9+
NativeDeserialize, NativeDeserializer, NativeSerialize,
10+
};
11+
use cubenativeutils::wrappers::NativeContextHolder;
12+
use cubenativeutils::wrappers::NativeObjectHandle;
13+
use cubenativeutils::CubeError;
14+
use std::any::Any;
15+
use std::rc::Rc;
16+
17+
#[nativebridge::native_bridge]
18+
pub trait DriverTools {
19+
fn convert_tz(&self, field: String) -> Result<String, CubeError>;
20+
fn time_grouped_column(
21+
&self,
22+
granularity: String,
23+
dimension: String,
24+
) -> Result<String, CubeError>;
25+
fn sql_templates(&self) -> Result<Rc<dyn SqlTemplatesRender>, CubeError>;
26+
fn timestamp_precision(&self) -> Result<u32, CubeError>;
27+
fn time_stamp_cast(&self, field: String) -> Result<String, CubeError>; //TODO move to templates
28+
fn date_time_cast(&self, field: String) -> Result<String, CubeError>; //TODO move to templates
29+
fn in_db_time_zone(&self, date: String) -> Result<String, CubeError>;
30+
fn get_allocated_params(&self) -> Result<Vec<String>, CubeError>;
31+
fn subtract_interval(&self, date: String, interval: String) -> Result<String, CubeError>;
32+
fn add_interval(&self, date: String, interval: String) -> Result<String, CubeError>;
33+
fn add_timestamp_interval(&self, date: String, interval: String) -> Result<String, CubeError>;
34+
fn interval_and_minimal_time_unit(&self, interval: String) -> Result<Vec<String>, CubeError>;
35+
fn hll_init(&self, sql: String) -> Result<String, CubeError>;
36+
fn hll_merge(&self, sql: String) -> Result<String, CubeError>;
37+
fn hll_cardinality_merge(&self, sql: String) -> Result<String, CubeError>;
38+
fn count_distinct_approx(&self, sql: String) -> Result<String, CubeError>;
39+
fn date_bin(
40+
&self,
41+
interval: String,
42+
source: String,
43+
origin: String,
44+
) -> Result<String, CubeError>;
45+
}

rust/cubesqlplanner/cubesqlplanner/src/cube_bridge/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ pub mod segment_definition;
2828
pub mod sql_templates_render;
2929
pub mod sql_utils;
3030
pub mod struct_with_sql_member;
31+
pub mod driver_tools;

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@ impl PhysicalPlanBuilderContext {
4141

4242
pub struct PhysicalPlanBuilder {
4343
query_tools: Rc<QueryTools>,
44-
_plan_sql_templates: PlanSqlTemplates,
44+
plan_sql_templates: PlanSqlTemplates,
4545
}
4646

4747
impl PhysicalPlanBuilder {
48-
pub fn new(query_tools: Rc<QueryTools>) -> Self {
49-
let plan_sql_templates = query_tools.plan_sql_templates();
48+
pub fn new(query_tools: Rc<QueryTools>, plan_sql_templates: PlanSqlTemplates) -> Self {
5049
Self {
5150
query_tools,
52-
_plan_sql_templates: plan_sql_templates,
51+
plan_sql_templates,
5352
}
5453
}
5554

@@ -970,7 +969,7 @@ impl PhysicalPlanBuilder {
970969
));
971970
};
972971

973-
let templates = self.query_tools.plan_sql_templates();
972+
let templates = self.query_tools.plan_sql_templates(false)?;
974973

975974
let ts_date_range = if templates.supports_generated_time_series()
976975
&& granularity_obj.is_predefined_granularity()

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ impl RegularRollingWindowJoinCondition {
5252
};
5353

5454
let trailing_start = if let Some(trailing_interval) = &self.trailing_interval {
55-
templates
56-
.base_tools()
57-
.subtract_interval(start_date, trailing_interval.clone())?
55+
templates.subtract_interval(start_date, trailing_interval.clone())?
5856
} else {
5957
start_date
6058
};
@@ -72,9 +70,7 @@ impl RegularRollingWindowJoinCondition {
7270
};
7371

7472
let leading_end = if let Some(leading_interval) = &self.leading_interval {
75-
templates
76-
.base_tools()
77-
.add_interval(end_date, leading_interval.clone())?
73+
templates.add_interval(end_date, leading_interval.clone())?
7874
} else {
7975
end_date
8076
};

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ impl TimeSeries {
4444
&& self.granularity.is_predefined_granularity()
4545
{
4646
let interval_description = templates
47-
.base_tools()
4847
.interval_and_minimal_time_unit(self.granularity.granularity_interval().clone())?;
4948
if interval_description.len() != 2 {
5049
return Err(CubeError::internal(

rust/cubesqlplanner/cubesqlplanner/src/planner/base_query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ impl<IT: InnerTypes> BaseQuery<IT> {
7878
}
7979

8080
fn build_sql_and_params_impl(&self) -> Result<NativeObjectHandle<IT>, CubeError> {
81-
let templates = self.query_tools.plan_sql_templates();
81+
let templates = self.query_tools.plan_sql_templates(false)?;
8282
let query_planner = QueryPlanner::new(self.request.clone(), self.query_tools.clone());
8383
let logical_plan = query_planner.plan()?;
8484

8585
let (optimized_plan, used_pre_aggregations) =
8686
self.try_pre_aggregations(logical_plan.clone())?;
8787

88-
let physical_plan_builder = PhysicalPlanBuilder::new(self.query_tools.clone());
88+
let physical_plan_builder = PhysicalPlanBuilder::new(self.query_tools.clone(), templates.clone());
8989
let original_sql_pre_aggregations = if !self.request.is_pre_aggregation_query() {
9090
OriginalSqlCollector::new(self.query_tools.clone()).collect(&optimized_plan)?
9191
} else {

0 commit comments

Comments
 (0)