Skip to content

Commit caac28b

Browse files
authored
perf(schema-compiler): Optimize CubeToMetaTransformer compilation flow (#10150)
* get rid of ramda in CubeToMetaTransformer * optimize transform() in CubeToMetaTransformer * move CubeDictionary to ts * some types specified (CompilerInterface) * move ContextEvaluator to ts * fix unit tests (because of types) * more precise types in CubeSymbols * fix integration tests (because of types) * move CubeToMetaTransformer to ts * rewrite byId in CubeDictionary to Map<string, Cube> * move contextDefinitions in ContextEvaluator to Map<string, CompiledContext>
1 parent d6b4c21 commit caac28b

18 files changed

+627
-436
lines changed

packages/cubejs-schema-compiler/src/compiler/ContextEvaluator.js

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { CubeEvaluator } from './CubeEvaluator';
2+
import type { ErrorReporter } from './ErrorReporter';
3+
import { CompilerInterface } from './PrepareCompiler';
4+
5+
export interface ContextInput {
6+
name: string;
7+
contextMembers: any;
8+
}
9+
10+
export interface CompiledContext {
11+
name: string;
12+
contextMembers: any;
13+
}
14+
15+
export class ContextEvaluator implements CompilerInterface {
16+
private readonly cubeEvaluator: CubeEvaluator;
17+
18+
private contextDefinitions: Map<string, CompiledContext>;
19+
20+
public constructor(cubeEvaluator: CubeEvaluator) {
21+
this.cubeEvaluator = cubeEvaluator;
22+
this.contextDefinitions = new Map<string, CompiledContext>();
23+
}
24+
25+
public compile(contexts: ContextInput[], errorReporter?: ErrorReporter): void {
26+
if (contexts.length === 0) {
27+
return;
28+
}
29+
30+
this.contextDefinitions = new Map<string, CompiledContext>();
31+
for (const context of contexts) {
32+
if (errorReporter && this.contextDefinitions.has(context.name)) {
33+
errorReporter.error(`Context "${context.name}" already exists!`);
34+
} else {
35+
this.contextDefinitions.set(context.name, this.compileContext(context));
36+
}
37+
}
38+
}
39+
40+
private compileContext(context: ContextInput): CompiledContext {
41+
return {
42+
name: context.name,
43+
contextMembers: this.cubeEvaluator.evaluateReferences(null, context.contextMembers)
44+
};
45+
}
46+
47+
public get contextList(): string[] {
48+
return Array.from(this.contextDefinitions.keys());
49+
}
50+
}

packages/cubejs-schema-compiler/src/compiler/CubeDictionary.js

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { ErrorReporter } from './ErrorReporter';
2+
import { TranspilerCubeResolver } from './transpilers';
3+
import { CompilerInterface } from './PrepareCompiler';
4+
5+
export interface Cube {
6+
name: string;
7+
[key: string]: any;
8+
}
9+
10+
export class CubeDictionary implements TranspilerCubeResolver, CompilerInterface {
11+
private byId: Map<string, Cube>;
12+
13+
public constructor() {
14+
this.byId = new Map<string, Cube>();
15+
}
16+
17+
public compile(cubes: Cube[], _errorReporter?: ErrorReporter): void {
18+
this.byId = new Map<string, Cube>();
19+
for (const cube of cubes) {
20+
this.byId.set(cube.name, cube);
21+
}
22+
}
23+
24+
public resolveCube(name: string): Cube | undefined {
25+
return this.byId.get(name);
26+
}
27+
28+
public free(): void {
29+
this.byId = new Map<string, Cube>();
30+
}
31+
32+
public cubeNames(): string[] {
33+
return Array.from(this.byId.keys());
34+
}
35+
}

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import { camelizeCube } from './utils';
88

99
import type { ErrorReporter } from './ErrorReporter';
1010
import { TranspilerSymbolResolver } from './transpilers';
11+
import { CompilerInterface } from './PrepareCompiler';
1112

1213
export type ToString = { toString(): string };
1314

1415
export type GranularityDefinition = {
1516
sql?: (...args: any[]) => string;
17+
name?: string;
1618
title?: string;
1719
interval?: string;
1820
offset?: string;
@@ -139,6 +141,17 @@ export type ViewIncludedMember = {
139141
name: string;
140142
};
141143

144+
export type FolderMember = {
145+
type?: 'folder';
146+
name: string;
147+
includes?: FolderMember[];
148+
};
149+
150+
export type Folder = {
151+
name: string;
152+
includes: FolderMember[];
153+
};
154+
142155
export interface CubeDefinition {
143156
name: string;
144157
extends?: (...args: Array<unknown>) => { __cubeName: string };
@@ -158,7 +171,7 @@ export interface CubeDefinition {
158171
accessPolicy?: AccessPolicyDefinition[];
159172
// eslint-disable-next-line camelcase
160173
access_policy?: any[];
161-
folders?: any[];
174+
folders?: Folder[];
162175
includes?: any;
163176
excludes?: any;
164177
cubes?: any;
@@ -215,7 +228,7 @@ export const CONTEXT_SYMBOLS = {
215228

216229
export const CURRENT_CUBE_CONSTANTS = ['CUBE', 'TABLE'];
217230

218-
export class CubeSymbols implements TranspilerSymbolResolver {
231+
export class CubeSymbols implements TranspilerSymbolResolver, CompilerInterface {
219232
public symbols: Record<string | symbol, CubeSymbolsDefinition>;
220233

221234
private builtCubes: Record<string, CubeDefinitionExtended>;
@@ -913,9 +926,7 @@ export class CubeSymbols implements TranspilerSymbolResolver {
913926
* refactoring.
914927
*/
915928
protected evaluateContextFunction(cube: any, contextFn: any, context: any = {}) {
916-
const cubeEvaluator = this;
917-
918-
return cubeEvaluator.resolveSymbolsCall(contextFn, (name: string) => {
929+
return this.resolveSymbolsCall(contextFn, (name: string) => {
919930
const resolvedSymbol = this.resolveSymbol(cube, name);
920931
if (resolvedSymbol) {
921932
return resolvedSymbol;
@@ -930,7 +941,7 @@ export class CubeSymbols implements TranspilerSymbolResolver {
930941
});
931942
}
932943

933-
protected evaluateReferences<T extends ToString | Array<ToString>>(
944+
public evaluateReferences<T extends ToString | Array<ToString>>(
934945
cube: string | null,
935946
referencesFn: (...args: Array<unknown>) => T,
936947
options: { collectJoinHints?: boolean, originalSorting?: boolean } = {}

0 commit comments

Comments
 (0)