Skip to content

Commit 4552791

Browse files
committed
first version of CubeJoinsResolver
1 parent 4432c9f commit 4552791

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { CompilerInterface } from './PrepareCompiler';
2+
import { CubeDefinitionExtended, CubeSymbols, JoinDefinition } from './CubeSymbols';
3+
import type { ErrorReporter } from './ErrorReporter';
4+
5+
export class CubeJoinsResolver extends CubeSymbols implements CompilerInterface {
6+
// key: cubeName with joins defined
7+
private cubeJoins: Record<string, JoinDefinition[]>;
8+
9+
// key: cubeName with joins defined
10+
// 1st level value: join alias
11+
// 2nd level value: join definition
12+
private cubeJoinAliases: Record<string, Record<string, JoinDefinition>>;
13+
14+
// key: cubeName with joins defined
15+
// 1st level value: target cube name
16+
// 2nd level value: join definition
17+
private cubeJoinTargets: Record<string, Record<string, JoinDefinition>>;
18+
19+
public constructor() {
20+
super(false); // It seems that we don't need to evaluate views
21+
this.cubeJoins = {};
22+
this.cubeJoinAliases = {};
23+
this.cubeJoinTargets = {};
24+
}
25+
26+
public compile(cubes: CubeDefinitionExtended[], errorReporter: ErrorReporter) {
27+
super.compile(cubes, errorReporter);
28+
29+
this.cubeList.forEach(cube => {
30+
if (!cube.joins) {
31+
return;
32+
}
33+
34+
this.cubeJoins[cube.name] = this.cubeJoins[cube.name] || [];
35+
this.cubeJoinAliases[cube.name] = this.cubeJoinAliases[cube.name] || {};
36+
this.cubeJoinTargets[cube.name] = this.cubeJoinTargets[cube.name] || {};
37+
38+
const er = errorReporter.inContext(`${cube.name} cube`);
39+
40+
cube.joins.forEach(join => {
41+
this.cubeJoins[cube.name].push(join);
42+
this.cubeJoinTargets[cube.name][join.name] = join;
43+
44+
if (join.alias) {
45+
if (this.cubeJoinAliases[cube.name][join.alias]) {
46+
er.error(
47+
`Join alias "${join.alias}" is already defined in cube "${cube.name}".`,
48+
cube.fileName
49+
);
50+
51+
return;
52+
}
53+
this.cubeJoinAliases[cube.name][join.alias] = join;
54+
}
55+
});
56+
});
57+
}
58+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ export type PreAggregationDefinitionRollup = BasePreAggregationDefinition & {
116116
export type PreAggregationDefinition = PreAggregationDefinitionRollup;
117117

118118
export type JoinDefinition = {
119-
name: string,
120-
relationship: string,
121-
sql: (...args: any[]) => string,
119+
name: string;
120+
relationship: string;
121+
sql: (...args: any[]) => string;
122+
alias?: string;
122123
};
123124

124125
export type Filter =

0 commit comments

Comments
 (0)