Skip to content

Commit 12a58aa

Browse files
committed
some refactoring to avoid copy/paste
1 parent a223c36 commit 12a58aa

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

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

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,37 +1112,9 @@ export class PreAggregations {
11121112
joinsMap[j.to] = j.from;
11131113
}
11141114

1115-
const buildPath = (cubeName: string): string[] => {
1116-
const path = [cubeName];
1117-
const parentMap = joinsMap;
1118-
while (parentMap[cubeName]) {
1119-
cubeName = parentMap[cubeName];
1120-
path.push(cubeName);
1121-
}
1122-
return path.reverse();
1123-
};
1124-
1125-
references.dimensions = references.dimensions.map(d => {
1126-
const [cubeName, ...restPath] = d.split('.');
1127-
const path = buildPath(cubeName);
1128-
1129-
return `${path.join('.')}.${restPath.join('.')}`;
1130-
});
1131-
references.measures = references.measures.map(m => {
1132-
const [cubeName, ...restPath] = m.split('.');
1133-
const path = buildPath(cubeName);
1134-
1135-
return `${path.join('.')}.${restPath.join('.')}`;
1136-
});
1137-
references.timeDimensions = references.timeDimensions.map(td => {
1138-
const [cubeName, ...restPath] = td.dimension.split('.');
1139-
const path = buildPath(cubeName);
1140-
1141-
return {
1142-
...td,
1143-
dimension: `${path.join('.')}.${restPath.join('.')}`,
1144-
};
1145-
});
1115+
references.dimensions = this.buildMembersFullName(references.dimensions, joinsMap);
1116+
references.measures = this.buildMembersFullName(references.measures, joinsMap);
1117+
references.timeDimensions = this.buildTimeDimensionsFullName(references.timeDimensions, joinsMap);
11461118

11471119
return {
11481120
...preAggObj,
@@ -1364,6 +1336,38 @@ export class PreAggregations {
13641336
.toLowerCase();
13651337
}
13661338

1339+
private enrichMembersCubeJoinPath(cubeName: string, joinsMap: Record<string, string>): string[] {
1340+
const path = [cubeName];
1341+
const parentMap = joinsMap;
1342+
while (parentMap[cubeName]) {
1343+
cubeName = parentMap[cubeName];
1344+
path.push(cubeName);
1345+
}
1346+
1347+
return path.reverse();
1348+
}
1349+
1350+
private buildMembersFullName(members: string[], joinsMap: Record<string, string>): string[] {
1351+
return members.map(d => {
1352+
const [cubeName, ...restPath] = d.split('.');
1353+
const path = this.enrichMembersCubeJoinPath(cubeName, joinsMap);
1354+
1355+
return `${path.join('.')}.${restPath.join('.')}`;
1356+
});
1357+
}
1358+
1359+
private buildTimeDimensionsFullName(members: PreAggregationTimeDimensionReference[], joinsMap: Record<string, string>): PreAggregationTimeDimensionReference[] {
1360+
return members.map(td => {
1361+
const [cubeName, ...restPath] = td.dimension.split('.');
1362+
const path = this.enrichMembersCubeJoinPath(cubeName, joinsMap);
1363+
1364+
return {
1365+
...td,
1366+
dimension: `${path.join('.')}.${restPath.join('.')}`,
1367+
};
1368+
});
1369+
}
1370+
13671371
private evaluateAllReferences(cube: string, aggregation: PreAggregationDefinition, preAggregationName: string | null = null, context: EvaluateReferencesContext = {}): PreAggregationReferences {
13681372
const evaluateReferences = () => {
13691373
const references = this.query.cubeEvaluator.evaluatePreAggregationReferences(cube, aggregation);
@@ -1374,18 +1378,18 @@ export class PreAggregations {
13741378
if (preAggQuery) {
13751379
// We need to build a join tree for all references, so they would always include full join path
13761380
// even for preaggregation references without join path. It is necessary to be able to match
1377-
// query and preaggregation based on full join tree. But we can not update
1378-
// references.{dimensions,measures,timeDimensions} directly, because it will break
1379-
// evaluation of references in the query on later stages.
1380-
// So we store full named members separately and use them in canUsePreAggregation functions.
1381+
// query and preaggregation based on full join tree.
13811382
references.joinTree = preAggQuery.join;
1382-
const root = references.joinTree?.root || '';
1383-
references.measures = references.measures.map(m => (m.startsWith(root) ? m : `${root}.${m}`));
1384-
references.dimensions = references.dimensions.map(d => (d.startsWith(root) ? d : `${root}.${d}`));
1385-
references.timeDimensions = references.timeDimensions.map(d => ({
1386-
dimension: (d.dimension.startsWith(root) ? d.dimension : `${root}.${d.dimension}`),
1387-
granularity: d.granularity,
1388-
}));
1383+
const joinsMap: Record<string, string> = {};
1384+
if (references.joinTree) {
1385+
for (const j of references.joinTree.joins) {
1386+
joinsMap[j.to] = j.from;
1387+
}
1388+
}
1389+
1390+
references.dimensions = this.buildMembersFullName(references.dimensions, joinsMap);
1391+
references.measures = this.buildMembersFullName(references.measures, joinsMap);
1392+
references.timeDimensions = this.buildTimeDimensionsFullName(references.timeDimensions, joinsMap);
13891393
}
13901394
}
13911395
if (aggregation.type === 'rollupLambda') {

0 commit comments

Comments
 (0)