Skip to content

Commit 7b2874e

Browse files
MazterQyouKSDaemon
andauthored
fix(schema-compiler): Do not collect disabled external pre-aggregations (#10014)
* fix(schema-compiler): Do not collect disabled external pre-aggregations Signed-off-by: Alex Qyoun-ae <[email protected]> * code polish * add a comment --------- Signed-off-by: Alex Qyoun-ae <[email protected]> Co-authored-by: Konstantin Burkalev <[email protected]>
1 parent d2e67ad commit 7b2874e

File tree

9 files changed

+41
-9
lines changed

9 files changed

+41
-9
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,8 @@ export class BaseQuery {
897897
preAggregationQuery: this.options.preAggregationQuery,
898898
totalQuery: this.options.totalQuery,
899899
joinHints: this.options.joinHints,
900-
cubestoreSupportMultistage: this.options.cubestoreSupportMultistage ?? getEnv('cubeStoreRollingWindowJoin')
900+
cubestoreSupportMultistage: this.options.cubestoreSupportMultistage ?? getEnv('cubeStoreRollingWindowJoin'),
901+
disableExternalPreAggregations: !!this.options.disableExternalPreAggregations,
901902
};
902903

903904
try {
@@ -945,7 +946,8 @@ export class BaseQuery {
945946
ungrouped: this.options.ungrouped,
946947
exportAnnotatedSql: false,
947948
preAggregationQuery: this.options.preAggregationQuery,
948-
cubestoreSupportMultistage: this.options.cubestoreSupportMultistage ?? getEnv('cubeStoreRollingWindowJoin')
949+
cubestoreSupportMultistage: this.options.cubestoreSupportMultistage ?? getEnv('cubeStoreRollingWindowJoin'),
950+
disableExternalPreAggregations: !!this.options.disableExternalPreAggregations,
949951
};
950952

951953
const buildResult = nativeBuildSqlAndParams(queryParams);

packages/cubejs-schema-compiler/src/adapter/PreAggregations.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,19 @@ export class PreAggregations {
104104
* It returns full pre-aggregation object (with keyQueries, previewSql, loadSql, and so on.
105105
*/
106106
public preAggregationsDescription(): FullPreAggregationDescription[] {
107+
const disableExternalPreAggregations = this.query.options?.disableExternalPreAggregations;
107108
const preAggregations = [this.preAggregationsDescriptionLocal()].concat(
108109
this.query.subQueryDimensions.map(d => this.query.subQueryDescription(d).subQuery)
109110
.map(q => q.preAggregations.preAggregationsDescription())
110111
);
111112

112113
return R.pipe(
113114
R.unnest as (list: any[][]) => any[],
115+
// TODO: Move this to somewhere BEFORE pre-agg matching, possibly to rollupMatchResults()
116+
// to avoid constly matching and then throwing it away.
117+
R.filter((agg: FullPreAggregationDescription) => !(disableExternalPreAggregations && agg.external)),
114118
R.uniqBy(desc => desc.tableName)
115-
)(
116-
preAggregations
117-
);
119+
)(preAggregations);
118120
}
119121

120122
private preAggregationsDescriptionLocal(): FullPreAggregationDescription[] {

rust/cubesqlplanner/cubesqlplanner/src/cube_bridge/base_query_options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ pub struct BaseQueryOptionsStatic {
6868
pub total_query: Option<bool>,
6969
#[serde(rename = "cubestoreSupportMultistage")]
7070
pub cubestore_support_multistage: Option<bool>,
71+
#[serde(rename = "disableExternalPreAggregations")]
72+
pub disable_external_pre_aggregations: bool,
7173
}
7274

7375
#[nativebridge::native_bridge(BaseQueryOptionsStatic)]

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/optimizers/pre_aggregation/optimizer.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ impl PreAggregationOptimizer {
2424
}
2525
}
2626

27-
pub fn try_optimize(&mut self, plan: Rc<Query>) -> Result<Option<Rc<Query>>, CubeError> {
27+
pub fn try_optimize(
28+
&mut self,
29+
plan: Rc<Query>,
30+
disable_external_pre_aggregations: bool,
31+
) -> Result<Option<Rc<Query>>, CubeError> {
2832
let cube_names = collect_cube_names_from_node(&plan)?;
2933
let mut compiler = PreAggregationsCompiler::try_new(self.query_tools.clone(), &cube_names)?;
3034

31-
let compiled_pre_aggregations = compiler.compile_all_pre_aggregations()?;
35+
let compiled_pre_aggregations =
36+
compiler.compile_all_pre_aggregations(disable_external_pre_aggregations)?;
3237

3338
for pre_aggregation in compiled_pre_aggregations.iter() {
3439
let new_query = self.try_rewrite_query(plan.clone(), pre_aggregation)?;

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/optimizers/pre_aggregation/pre_aggregations_compiler.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,14 @@ impl PreAggregationsCompiler {
406406

407407
pub fn compile_all_pre_aggregations(
408408
&mut self,
409+
disable_external_pre_aggregations: bool,
409410
) -> Result<Vec<Rc<CompiledPreAggregation>>, CubeError> {
410411
let mut result = Vec::new();
411412
for (name, _) in self.descriptions.clone().iter() {
412-
result.push(self.compile_pre_aggregation(&name)?);
413+
let pre_aggregation = self.compile_pre_aggregation(name)?;
414+
if !(disable_external_pre_aggregations && pre_aggregation.external == Some(true)) {
415+
result.push(pre_aggregation);
416+
}
413417
}
414418
Ok(result)
415419
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ impl<IT: InnerTypes> BaseQuery<IT> {
124124
self.query_tools.clone(),
125125
self.cubestore_support_multistage,
126126
);
127-
if let Some(result) = pre_aggregation_optimizer.try_optimize(plan.clone())? {
127+
let disable_external_pre_aggregations =
128+
self.request.disable_external_pre_aggregations();
129+
if let Some(result) = pre_aggregation_optimizer
130+
.try_optimize(plan.clone(), disable_external_pre_aggregations)?
131+
{
128132
if pre_aggregation_optimizer.get_used_pre_aggregations().len() == 1 {
129133
(
130134
result,

rust/cubesqlplanner/cubesqlplanner/src/planner/planners/dimension_subquery_planner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ impl DimensionSubqueryPlanner {
123123
false,
124124
Rc::new(vec![]),
125125
true,
126+
self.query_properties.disable_external_pre_aggregations(),
126127
)?;
127128
let query_planner = QueryPlanner::new(sub_query_properties, self.query_tools.clone());
128129
let sub_query = query_planner.plan()?;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl MultiStageMemberQueryPlanner {
7676
false,
7777
Rc::new(vec![]),
7878
true,
79+
self.query_properties.disable_external_pre_aggregations(),
7980
)?;
8081

8182
let simple_query_planer =
@@ -377,6 +378,7 @@ impl MultiStageMemberQueryPlanner {
377378
false,
378379
self.query_properties.query_join_hints().clone(),
379380
false,
381+
self.query_properties.disable_external_pre_aggregations(),
380382
)?;
381383

382384
let query_planner =

rust/cubesqlplanner/cubesqlplanner/src/planner/query_properties.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub struct QueryProperties {
107107
total_query: bool,
108108
query_join_hints: Rc<Vec<JoinHintItem>>,
109109
allow_multi_stage: bool,
110+
disable_external_pre_aggregations: bool,
110111
}
111112

112113
impl QueryProperties {
@@ -405,6 +406,8 @@ impl QueryProperties {
405406

406407
let pre_aggregation_query = options.static_data().pre_aggregation_query.unwrap_or(false);
407408
let total_query = options.static_data().total_query.unwrap_or(false);
409+
let disable_external_pre_aggregations =
410+
options.static_data().disable_external_pre_aggregations;
408411

409412
let mut res = Self {
410413
measures,
@@ -425,6 +428,7 @@ impl QueryProperties {
425428
total_query,
426429
query_join_hints,
427430
allow_multi_stage: true,
431+
disable_external_pre_aggregations,
428432
};
429433
res.apply_static_filters()?;
430434
Ok(Rc::new(res))
@@ -448,6 +452,7 @@ impl QueryProperties {
448452
total_query: bool,
449453
query_join_hints: Rc<Vec<JoinHintItem>>,
450454
allow_multi_stage: bool,
455+
disable_external_pre_aggregations: bool,
451456
) -> Result<Rc<Self>, CubeError> {
452457
let order_by = if order_by.is_empty() {
453458
Self::default_order(&dimensions, &time_dimensions, &measures)
@@ -474,6 +479,7 @@ impl QueryProperties {
474479
total_query,
475480
query_join_hints,
476481
allow_multi_stage,
482+
disable_external_pre_aggregations,
477483
};
478484
res.apply_static_filters()?;
479485

@@ -721,6 +727,10 @@ impl QueryProperties {
721727
self.pre_aggregation_query
722728
}
723729

730+
pub fn disable_external_pre_aggregations(&self) -> bool {
731+
self.disable_external_pre_aggregations
732+
}
733+
724734
pub fn all_filters(&self) -> Option<Filter> {
725735
let items = self
726736
.time_dimensions_filters

0 commit comments

Comments
 (0)