Skip to content

Commit 493963e

Browse files
committed
in work
1 parent 32a380a commit 493963e

File tree

9 files changed

+277
-1341
lines changed

9 files changed

+277
-1341
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ export class PreAggregations {
14041404
if (tables.length === 1) {
14051405
return tables[0].tableName;
14061406
}
1407-
const union = tables.map(table => `SELECT ${table.columns.join(', ')} FROM ${table.tableName}`).join(' UNION ALL ');
1407+
const union = tables.map(table => `SELECT ${table.columns.join(', ')} FROM ${table.tableName} un`).join(' UNION ALL ');
14081408
return `(${union})`;
14091409
}
14101410

packages/cubejs-schema-compiler/test/integration/postgres/pre-aggregations.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ describe('PreAggregations', () => {
577577
});
578578
`);
579579

580-
it('simple pre-aggregation', async () => {
580+
it('simple pre-aggregation 1', async () => {
581581
await compiler.compile();
582582

583583
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
@@ -2194,7 +2194,7 @@ describe('PreAggregations', () => {
21942194
});
21952195
});
21962196

2197-
if (getEnv('nativeSqlPlanner') && getEnv('nativeSqlPlannerPreAggregations')) {
2197+
if (true) { //getEnv('nativeSqlPlanner') && getEnv('nativeSqlPlannerPreAggregations')) {
21982198
it('rollup lambda', async () => {
21992199
await compiler.compile();
22002200

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct PreAggregationUnion {
2525
#[derive(Clone)]
2626
pub struct PreAggregationTable {
2727
pub cube_name: String,
28+
pub cube_alias: String,
2829
pub name: String,
2930
pub alias: Option<String>,
3031
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,18 @@ impl PreAggregationsCompiler {
157157
let source = if static_data.pre_aggregation_type == "rollupJoin" {
158158
PreAggregationSource::Join(self.build_join_source(&measures, &dimensions, &rollups)?)
159159
} else {
160+
let cube = self
161+
.query_tools
162+
.cube_evaluator()
163+
.cube_from_path(name.cube_name.clone())?;
164+
let cube_alias = if let Some(alias) = &cube.static_data().sql_alias {
165+
alias.clone()
166+
} else {
167+
name.cube_name.clone()
168+
};
160169
PreAggregationSource::Single(PreAggregationTable {
161170
cube_name: name.cube_name.clone(),
171+
cube_alias,
162172
name: name.name.clone(),
163173
alias: static_data.sql_alias.clone(),
164174
})

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::*;
2-
use crate::planner::sql_evaluator::MemberSymbol;
2+
use crate::{plan::QualifiedColumnName, planner::sql_evaluator::MemberSymbol};
33
use cubenativeutils::CubeError;
44
use itertools::Itertools;
5-
use std::rc::Rc;
5+
use std::{collections::HashMap, rc::Rc};
66

77
pub struct PreAggregation {
88
pub name: String,
@@ -42,6 +42,63 @@ impl LogicalNode for PreAggregation {
4242
}
4343
}
4444

45+
impl PreAggregation {
46+
pub fn all_dimensions_refererences(&self) -> HashMap<String, QualifiedColumnName> {
47+
let mut res = HashMap::new();
48+
49+
for dim in self.dimensions.iter() {
50+
let alias = dim.alias();
51+
res.insert(
52+
dim.full_name(),
53+
QualifiedColumnName::new(None, alias.clone()),
54+
);
55+
}
56+
for (dim, granularity) in self.time_dimensions.iter() {
57+
let base_symbol = if let Ok(td) = dim.as_time_dimension() {
58+
td.base_symbol().clone()
59+
} else {
60+
dim.clone()
61+
};
62+
let suffix = if let Some(granularity) = &granularity {
63+
format!("_{}", granularity.clone())
64+
} else {
65+
"".to_string()
66+
};
67+
let alias = format!("{}{}", base_symbol.alias(), suffix);
68+
res.insert(
69+
dim.full_name(),
70+
QualifiedColumnName::new(None, alias.clone()),
71+
);
72+
}
73+
74+
if let PreAggregationSource::Join(join) = self.source.as_ref() {
75+
for item in join.items.iter() {
76+
for member in item.from_members.iter().chain(item.to_members.iter()) {
77+
let alias = member.alias();
78+
res.insert(
79+
member.full_name(),
80+
QualifiedColumnName::new(None, alias.clone()),
81+
);
82+
}
83+
}
84+
}
85+
86+
res
87+
}
88+
pub fn all_measures_refererences(&self) -> HashMap<String, QualifiedColumnName> {
89+
self.measures
90+
.iter()
91+
.map(|measure| {
92+
let alias = measure.alias();
93+
(
94+
measure.full_name(),
95+
QualifiedColumnName::new(None, alias.clone()),
96+
)
97+
})
98+
.collect()
99+
}
100+
}
101+
45102
impl PrettyPrint for PreAggregation {
46103
fn pretty_print(&self, result: &mut PrettyPrintResult, state: &PrettyPrintState) {
47104
result.println("PreAggregation: ", state);

0 commit comments

Comments
 (0)