Skip to content

Commit 6992e59

Browse files
committed
some refactoring to avoid copy/paste
1 parent f5c5d4f commit 6992e59

File tree

2 files changed

+68
-62
lines changed

2 files changed

+68
-62
lines changed

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

Lines changed: 67 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,7 @@ export class PreAggregations {
172172
let preAggregations: PreAggregationForQuery[] = [foundPreAggregation];
173173
if (foundPreAggregation.preAggregation.type === 'rollupJoin') {
174174
preAggregations = foundPreAggregation.preAggregationsToJoin || [];
175-
}
176-
if (foundPreAggregation.preAggregation.type === 'rollupLambda') {
175+
} else if (foundPreAggregation.preAggregation.type === 'rollupLambda') {
177176
preAggregations = foundPreAggregation.referencedPreAggregations || [];
178177
}
179178

@@ -959,6 +958,20 @@ export class PreAggregations {
959958
}
960959
}
961960

961+
private collectJoinHintsFromRollupReferences(refs: PreAggregationReferences): (string | string[])[] {
962+
if (!refs.joinTree) {
963+
return [];
964+
}
965+
966+
const hints: (string | string[])[] = [refs.joinTree.root];
967+
968+
for (const j of refs.joinTree.joins) {
969+
hints.push([j.from, j.to]);
970+
}
971+
972+
return hints;
973+
}
974+
962975
// TODO check multiplication factor didn't change
963976
private buildRollupJoin(preAggObj: PreAggregationForQuery, preAggObjsToJoin: PreAggregationForQuery[]): BuildRollupJoinResult {
964977
return this.query.cacheValue(
@@ -967,19 +980,9 @@ export class PreAggregations {
967980
// It's important to build join graph not only using the pre-agg members, but also
968981
// taking into account all explicit underlying rollup pre-aggregation joins, because
969982
// otherwise the built join tree might differ from the actual pre-aggregation.
970-
const preAggJoinsJoinHints = preAggObj.references.rollupsReferences.map(r => {
971-
if (!r.joinTree) {
972-
return [];
973-
}
974-
975-
const hints: (string | string[])[] = [r.joinTree.root];
976-
977-
for (const j of r.joinTree.joins) {
978-
hints.push([j.from, j.to]);
979-
}
980-
981-
return hints;
982-
}).flat();
983+
const preAggJoinsJoinHints = preAggObj.references.rollupsReferences.map(
984+
this.collectJoinHintsFromRollupReferences
985+
).flat();
983986

984987
const builtJoinTree = this.query.joinGraph.buildJoin(
985988
preAggJoinsJoinHints.concat(this.cubesFromPreAggregation(preAggObj))
@@ -1112,37 +1115,9 @@ export class PreAggregations {
11121115
joinsMap[j.to] = j.from;
11131116
}
11141117

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-
});
1118+
references.dimensions = this.buildMembersFullName(references.dimensions, joinsMap);
1119+
references.measures = this.buildMembersFullName(references.measures, joinsMap);
1120+
references.timeDimensions = this.buildTimeDimensionsFullName(references.timeDimensions, joinsMap);
11461121

11471122
return {
11481123
...preAggObj,
@@ -1315,11 +1290,10 @@ export class PreAggregations {
13151290
cube,
13161291
aggregation
13171292
) &&
1318-
!!references.dimensions.find((d) => {
1293+
references.dimensions.some((d) => this.query.cubeEvaluator.dimensionByPath(
13191294
// `d` can contain full join path, so we should trim it
1320-
const trimmedDimension = CubeSymbols.joinHintFromPath(d).path;
1321-
return this.query.cubeEvaluator.dimensionByPath(trimmedDimension).primaryKey;
1322-
}),
1295+
this.query.cubeEvaluator.memberShortNameFromPath(d)
1296+
).primaryKey),
13231297
});
13241298
}
13251299

@@ -1364,6 +1338,38 @@ export class PreAggregations {
13641338
.toLowerCase();
13651339
}
13661340

1341+
private enrichMembersCubeJoinPath(cubeName: string, joinsMap: Record<string, string>): string[] {
1342+
const path = [cubeName];
1343+
const parentMap = joinsMap;
1344+
while (parentMap[cubeName]) {
1345+
cubeName = parentMap[cubeName];
1346+
path.push(cubeName);
1347+
}
1348+
1349+
return path.reverse();
1350+
}
1351+
1352+
private buildMembersFullName(members: string[], joinsMap: Record<string, string>): string[] {
1353+
return members.map(d => {
1354+
const [cubeName, ...restPath] = d.split('.');
1355+
const path = this.enrichMembersCubeJoinPath(cubeName, joinsMap);
1356+
1357+
return `${path.join('.')}.${restPath.join('.')}`;
1358+
});
1359+
}
1360+
1361+
private buildTimeDimensionsFullName(members: PreAggregationTimeDimensionReference[], joinsMap: Record<string, string>): PreAggregationTimeDimensionReference[] {
1362+
return members.map(td => {
1363+
const [cubeName, ...restPath] = td.dimension.split('.');
1364+
const path = this.enrichMembersCubeJoinPath(cubeName, joinsMap);
1365+
1366+
return {
1367+
...td,
1368+
dimension: `${path.join('.')}.${restPath.join('.')}`,
1369+
};
1370+
});
1371+
}
1372+
13671373
private evaluateAllReferences(cube: string, aggregation: PreAggregationDefinition, preAggregationName: string | null = null, context: EvaluateReferencesContext = {}): PreAggregationReferences {
13681374
const evaluateReferences = () => {
13691375
const references = this.query.cubeEvaluator.evaluatePreAggregationReferences(cube, aggregation);
@@ -1374,18 +1380,18 @@ export class PreAggregations {
13741380
if (preAggQuery) {
13751381
// We need to build a join tree for all references, so they would always include full join path
13761382
// 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.
1383+
// query and preaggregation based on full join tree.
13811384
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-
}));
1385+
const joinsMap: Record<string, string> = {};
1386+
if (references.joinTree) {
1387+
for (const j of references.joinTree.joins) {
1388+
joinsMap[j.to] = j.from;
1389+
}
1390+
}
1391+
1392+
references.dimensions = this.buildMembersFullName(references.dimensions, joinsMap);
1393+
references.measures = this.buildMembersFullName(references.measures, joinsMap);
1394+
references.timeDimensions = this.buildTimeDimensionsFullName(references.timeDimensions, joinsMap);
13891395
}
13901396
}
13911397
if (aggregation.type === 'rollupLambda') {

packages/cubejs-schema-compiler/src/compiler/CubeEvaluator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ export class CubeEvaluator extends CubeSymbols {
737737
throw new UserError(`Not full member name provided: ${path[0]}`);
738738
}
739739

740-
return `${path.at(-2)}.${path.at(-1)}`;
740+
return path.slice(-2).join('.');
741741
}
742742

743743
public cubeFromPath(path: string): EvaluatedCube {

0 commit comments

Comments
 (0)