Skip to content

Commit

Permalink
Update Cube Evaluator to support nested properties access
Browse files Browse the repository at this point in the history
  • Loading branch information
KSDaemon committed Aug 7, 2024
1 parent 8913e63 commit b26e3b9
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions packages/cubejs-schema-compiler/src/compiler/CubeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class CubeEvaluator extends CubeSymbols {
for (const cube of validCubes) {
this.evaluatedCubes[cube.name] = this.prepareCube(cube, errorReporter);
}

this.byFileName = R.groupBy(v => v.fileName, validCubes);
this.primaryKeys = R.fromPairs(
validCubes.map((v) => {
Expand Down Expand Up @@ -128,21 +128,21 @@ export class CubeEvaluator extends CubeSymbols {
if (cube.isView && (cube.includedMembers || []).length) {
const includedCubeNames: string[] = R.uniq(cube.includedMembers.map(it => it.memberPath.split('.')[0]));
const includedMemberPaths: string[] = R.uniq(cube.includedMembers.map(it => it.memberPath));

if (!cube.hierarchies) {
for (const cubeName of includedCubeNames) {
const { hierarchies } = this.evaluatedCubes[cubeName] || {};

if (Array.isArray(hierarchies) && hierarchies.length) {
const filteredHierarchies = hierarchies.map(it => {
const levels = it.levels.filter(level => includedMemberPaths.includes(level));

return {
...it,
levels
};
}).filter(it => it.levels.length);

cube.hierarchies = [...(cube.hierarchies || []), ...filteredHierarchies];
}
}
Expand Down Expand Up @@ -499,7 +499,28 @@ export class CubeEvaluator extends CubeSymbols {
throw new UserError(`'${cubeAndName[1]}' not found for path '${path}'`);
}

return this.evaluatedCubes[cubeAndName[0]][type][cubeAndName[1]];
if (cubeAndName.length === 2) {
return this.evaluatedCubes[cubeAndName[0]][type][cubeAndName[1]];
}

// length > 2 means that some nested property is requested.
if (cubeAndName.length === 3) {
if (!this.evaluatedCubes[cubeAndName[0]][type][cubeAndName[1]][cubeAndName[2]]) {
throw new UserError(`'${cubeAndName[1]}.${cubeAndName[2]}' not found for path '${path}'`);
}

return this.evaluatedCubes[cubeAndName[0]][type][cubeAndName[1]][cubeAndName[2]];
}

if (cubeAndName.length === 4) {
if (!this.evaluatedCubes[cubeAndName[0]][type][cubeAndName[1]][cubeAndName[2]][cubeAndName[3]]) {
throw new UserError(`'${cubeAndName[1]}.${cubeAndName[2]}.${cubeAndName[3]}' not found for path '${path}'`);
}

return this.evaluatedCubes[cubeAndName[0]][type][cubeAndName[1]][cubeAndName[2]][cubeAndName[3]];
}

throw new UserError(`Unknown requested path: '${path}'`);
}

public parsePath(type, path) {
Expand Down

0 comments on commit b26e3b9

Please sign in to comment.