Skip to content

Commit 27ac5bc

Browse files
committed
fix(schema-compiler): Fix pre-agg matching for 'rollupJoin' / 'rollupLambda' pre-aggregations
1 parent 97c62dc commit 27ac5bc

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,18 @@ export class PreAggregations {
509509
filterDimensionsSingleValueEqual =
510510
allValuesEq1(filterDimensionsSingleValueEqual) ? new Set(filterDimensionsSingleValueEqual?.keys()) : null;
511511

512+
// Build reverse query joins map, which is used for
513+
// rollupLambda and rollupJoin pre-aggs matching later
514+
const joinsMap: Record<string, string> = {};
515+
if (query.join) {
516+
for (const j of query.join.joins) {
517+
joinsMap[j.to] = j.from;
518+
}
519+
}
520+
512521
return {
513-
joinGraph: query.join,
522+
joinGraphRoot: query.join?.root,
523+
joinsMap,
514524
sortedDimensions,
515525
sortedTimeDimensions,
516526
timeDimensions,
@@ -736,11 +746,33 @@ export class PreAggregations {
736746
if (references.rollups.length > 0) {
737747
// In 'rollupJoin' / 'rollupLambda' pre-aggregations fullName members will be empty, because there are
738748
// no connections in the joinTree between cubes from different datasources
749+
// but joinGraph of the query has all the connections, necessary for serving the query,
750+
// so we use this information to complete the full paths of members from the root of the query
751+
// up to the pre-agg cube.
739752
dimsToMatch = references.dimensions;
740753
timeDimsToMatch = references.timeDimensions;
741754

755+
const buildPath = (cube: string): string[] => {
756+
const path = [cube];
757+
const parentMap = transformedQuery.joinsMap;
758+
while (parentMap[cube]) {
759+
cube = parentMap[cube];
760+
path.push(cube);
761+
}
762+
return path.reverse();
763+
};
764+
742765
dimensionsMatch = (dimensions, doBackAlias) => {
743-
const target = doBackAlias ? backAlias(dimsToMatch) : dimsToMatch;
766+
let target = doBackAlias ? backAlias(dimsToMatch) : dimsToMatch;
767+
target = target.map(dim => {
768+
const [cube, field] = dim.split('.');
769+
if (cube === transformedQuery.joinGraphRoot) {
770+
return dim;
771+
}
772+
const path = buildPath(cube);
773+
return `${path.join('.')}.${field}`;
774+
});
775+
744776
return dimensions.every(d => target.includes(d));
745777
};
746778
} else {

0 commit comments

Comments
 (0)