Skip to content

Commit 87de073

Browse files
authored
perf(schema-compiler): Add Server Core shared caches for model compilation (#10144)
* chore(server-core): move driver deps to ts * move logger to ts * move CompilerApi to ts # Conflicts: # packages/cubejs-schema-compiler/src/compiler/CubeSymbols.ts * add top compile cache in the server core * fix tsc errors
1 parent 6a0c398 commit 87de073

File tree

17 files changed

+476
-236
lines changed

17 files changed

+476
-236
lines changed

packages/cubejs-api-gateway/src/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
Query,
2222
NormalizedQueryFilter,
2323
NormalizedQuery,
24+
MemberExpression,
2425
} from './types/query';
2526

2627
import {
@@ -69,6 +70,7 @@ export {
6970
Query,
7071
NormalizedQueryFilter,
7172
NormalizedQuery,
73+
MemberExpression,
7274
JWTOptions,
7375
CheckAuthFn,
7476
CheckSQLAuthSuccessResponse,

packages/cubejs-api-gateway/src/types/query.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,10 @@ interface Query {
146146
cache?: CacheMode; // Used in public interface
147147
ungrouped?: boolean;
148148
responseFormat?: ResultType;
149-
150149
// TODO incoming query, query with parsed exprs and query with evaluated exprs are all different types
151-
subqueryJoins?: Array<SubqueryJoins>,
152-
153-
joinHints?: Array<JoinHint>
150+
subqueryJoins?: Array<SubqueryJoins>;
151+
joinHints?: Array<JoinHint>;
152+
requestId?: string;
154153
}
155154

156155
/**

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export * from './PostgresQuery';
1818
export * from './MssqlQuery';
1919
export * from './PrestodbQuery';
2020

21+
export { PreAggregationReferences } from '../compiler/CubeEvaluator';
22+
2123
// Candidates to move from this package to drivers packages
2224
// export * from './RedshiftQuery';
2325
// export * from './SnowflakeQuery';

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
JoinDefinition,
1010
PreAggregationDefinition,
1111
PreAggregationDefinitionRollup,
12-
type ToString
12+
type ToString,
13+
ViewIncludedMember
1314
} from './CubeSymbols';
1415
import { UserError } from './UserError';
1516
import { BaseQuery, PreAggregationDefinitionExtended } from '../adapter';
@@ -100,7 +101,7 @@ export type PreAggregationReferences = {
100101
export type PreAggregationInfo = {
101102
id: string,
102103
preAggregationName: string,
103-
preAggregation: unknown,
104+
preAggregation: any,
104105
cube: string,
105106
references: PreAggregationReferences,
106107
refreshKey: unknown,
@@ -124,6 +125,7 @@ export type EvaluatedFolder = {
124125
};
125126

126127
export type EvaluatedCube = {
128+
name: string;
127129
measures: Record<string, MeasureDefinition>;
128130
dimensions: Record<string, DimensionDefinition>;
129131
segments: Record<string, SegmentDefinition>;
@@ -136,6 +138,8 @@ export type EvaluatedCube = {
136138
sql?: (...args: any[]) => string;
137139
sqlTable?: (...args: any[]) => string;
138140
accessPolicy?: AccessPolicyDefinition[];
141+
isView?: boolean;
142+
includedMembers?: ViewIncludedMember[];
139143
};
140144

141145
export class CubeEvaluator extends CubeSymbols {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ export type AccessPolicyDefinition = {
133133
includesMembers?: string[];
134134
excludesMembers?: string[];
135135
};
136+
conditions?: {
137+
if: Function;
138+
}[]
136139
};
137140

138141
export type ViewIncludedMember = {
@@ -233,7 +236,7 @@ export class CubeSymbols implements TranspilerSymbolResolver, CompilerInterface
233236

234237
private builtCubes: Record<string, CubeDefinitionExtended>;
235238

236-
private cubeDefinitions: Record<string, CubeDefinition>;
239+
public cubeDefinitions: Record<string, CubeDefinition>;
237240

238241
private funcArgumentsValues: Record<string, string[]>;
239242

@@ -925,7 +928,7 @@ export class CubeSymbols implements TranspilerSymbolResolver, CompilerInterface
925928
* resolveSymbolsCall are sync. Async support may be added later with deeper
926929
* refactoring.
927930
*/
928-
protected evaluateContextFunction(cube: any, contextFn: any, context: any = {}) {
931+
public evaluateContextFunction(cube: any, contextFn: any, context: any = {}) {
929932
return this.resolveSymbolsCall(contextFn, (name: string) => {
930933
const resolvedSymbol = this.resolveSymbol(cube, name);
931934
if (resolvedSymbol) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export class CubeToMetaTransformer implements CompilerInterface {
135135

136136
private readonly cubeSymbols: CubeEvaluator;
137137

138-
private readonly cubeEvaluator: CubeEvaluator;
138+
public readonly cubeEvaluator: CubeEvaluator;
139139

140140
private readonly contextEvaluator: ContextEvaluator;
141141

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,18 @@ export interface CompilerInterface {
4545
compile: (cubes: any[], errorReporter: ErrorReporter) => void;
4646
}
4747

48-
export const prepareCompiler = (repo: SchemaFileRepository, options: PrepareCompilerOptions = {}) => {
48+
export type Compiler = {
49+
compiler: DataSchemaCompiler;
50+
metaTransformer: CubeToMetaTransformer;
51+
cubeEvaluator: CubeEvaluator;
52+
contextEvaluator: ContextEvaluator;
53+
joinGraph: JoinGraph;
54+
compilerCache: CompilerCache;
55+
headCommitId?: string;
56+
compilerId: string;
57+
};
58+
59+
export const prepareCompiler = (repo: SchemaFileRepository, options: PrepareCompilerOptions = {}): Compiler => {
4960
const nativeInstance = options.nativeInstance || new NativeInstance();
5061
const cubeDictionary = new CubeDictionary();
5162
const cubeSymbols = new CubeSymbols();
@@ -118,9 +129,8 @@ export const prepareCompiler = (repo: SchemaFileRepository, options: PrepareComp
118129
};
119130
};
120131

121-
export const compile = (repo: SchemaFileRepository, options?: PrepareCompilerOptions) => {
132+
export const compile = async (repo: SchemaFileRepository, options?: PrepareCompilerOptions): Promise<Compiler> => {
122133
const compilers = prepareCompiler(repo, options);
123-
return compilers.compiler.compile().then(
124-
() => compilers
125-
);
134+
await compilers.compiler.compile();
135+
return compilers;
126136
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
export * from './PrepareCompiler';
22
export * from './UserError';
33
export * from './converters';
4+
export {
5+
CubeDefinition,
6+
AccessPolicyDefinition,
7+
ViewIncludedMember,
8+
} from './CubeSymbols';
9+
export {
10+
PreAggregationFilters,
11+
PreAggregationInfo,
12+
EvaluatedCube,
13+
} from './CubeEvaluator';

packages/cubejs-server-core/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { CubejsServerCore } = require('./dist/src/core/server');
33

44
/**
55
* After 5 years working with TypeScript, now I know
6-
* that commonjs and nodejs require is not compatibility with using export default
6+
* that commonjs and Node.js require is not compatible with using export default
77
*/
88
const toExport = CubejsServerCore;
99

packages/cubejs-server-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"codesandbox-import-utils": "^2.1.12",
4242
"cross-spawn": "^7.0.1",
4343
"fs-extra": "^8.1.0",
44+
"graphql": "^15.8.0",
4445
"http-proxy-agent": "^7.0.2",
4546
"https-proxy-agent": "^7.0.6",
4647
"is-docker": "^2.1.1",

0 commit comments

Comments
 (0)