Skip to content

Commit 18237b0

Browse files
committed
chore(schema-compiler): move preAggregations class to typescript
1 parent 862b00a commit 18237b0

File tree

14 files changed

+437
-248
lines changed

14 files changed

+437
-248
lines changed

packages/cubejs-ksql-driver/src/KsqlQuery.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ export class KsqlQuery extends BaseQuery {
8383
return dimensionColumns.length ? ` GROUP BY ${dimensionColumns.join(', ')}` : '';
8484
}
8585

86-
public partitionInvalidateKeyQueries(cube: string, preAggregation: any) {
87-
return [];
88-
}
89-
9086
public preAggregationStartEndQueries(cube: string, preAggregation: any) {
9187
if (preAggregation.partitionGranularity) {
9288
if (!preAggregation.refreshRangeStart) {
@@ -97,7 +93,7 @@ export class KsqlQuery extends BaseQuery {
9793
}
9894
}
9995
const res = this.evaluateSymbolSqlWithContext(() => [
100-
96+
10197
preAggregation.refreshRangeStart && [this.evaluateSql(cube, preAggregation.refreshRangeStart.sql, {}), [], { external: true }],
10298
preAggregation.refreshRangeEnd && [this.evaluateSql(cube, preAggregation.refreshRangeEnd.sql, {}), [], { external: true }]
10399
], { preAggregationQuery: true });

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class BaseDimension {
126126
return this.dimensionDefinition().fieldType;
127127
}
128128

129-
public path() {
129+
public path(): string[] | null {
130130
if (this.expression) {
131131
return null;
132132
}
@@ -138,10 +138,10 @@ export class BaseDimension {
138138
return this.query.cubeEvaluator.parsePath('dimensions', this.dimension);
139139
}
140140

141-
public expressionPath() {
141+
public expressionPath(): string {
142142
if (this.expression) {
143143
return `expr:${this.expression.expressionName}`;
144144
}
145-
return this.query.cubeEvaluator.pathFromArray(this.path());
145+
return this.query.cubeEvaluator.pathFromArray(this.path() as string[]);
146146
}
147147
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ export class BaseFilter extends BaseDimension {
4747
}
4848

4949
// Evaluates filters on measures to whole where statement in query
50-
// It used in drill downs
50+
// It used in drill-downs
5151
public measureFilterToWhere() {
5252
const measureDefinition = this.measureDefinition();
53-
if (measureDefinition.filters && measureDefinition.filters.length ||
54-
measureDefinition.drillFilters && measureDefinition.drillFilters.length) {
53+
if (measureDefinition.filters?.length || measureDefinition.drillFilters?.length) {
5554
return this.query.evaluateFiltersArray(
5655
(measureDefinition.filters || []).concat(measureDefinition.drillFilters || []),
5756
this.query.cubeEvaluator.cubeNameFromPath(this.measure)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ export class BaseGroupFilter {
33

44
protected readonly operator: any;
55

6-
protected readonly measure: any;
6+
public readonly measure: any;
77

8-
protected readonly dimension: any;
8+
public readonly dimension: any;
99

1010
public constructor(filter: any) {
1111
this.values = filter.values;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,6 @@ export class BaseMeasure {
331331
if (this.expression) {
332332
return `expr:${this.expression.expressionName}`;
333333
}
334-
return this.query.cubeEvaluator.pathFromArray(this.path());
334+
return this.query.cubeEvaluator.pathFromArray(this.path() as string[]);
335335
}
336336
}

packages/cubejs-schema-compiler/src/adapter/BaseQuery.js

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,33 @@ const SecondsDurations = {
101101
* and {@code CompilerApi} configuration.
102102
*/
103103
export class BaseQuery {
104+
/** @type {import('./PreAggregations').PreAggregations} */
105+
preAggregations;
106+
107+
/** @type {import('./BaseMeasure').BaseMeasure[]} */
108+
measures;
109+
110+
/** @type {import('./BaseDimension').BaseDimension[]} */
111+
dimensions;
112+
113+
/** @type {import('./BaseDimension').BaseDimension[]} */
114+
multiStageDimensions;
115+
116+
/** @type {import('./BaseTimeDimension').BaseTimeDimension[]} */
117+
multiStageTimeDimensions;
118+
119+
/** @type {import('./BaseSegment').BaseSegment[]} */
120+
segments;
121+
122+
/** @type {(BaseFilter|BaseGroupFilter)[]} */
123+
filters;
124+
125+
/** @type {(BaseFilter|BaseGroupFilter)[]} */
126+
measureFilters;
127+
128+
/** @type {import('./BaseTimeDimension').BaseTimeDimension[]} */
129+
timeDimensions;
130+
104131
/**
105132
* BaseQuery class constructor.
106133
* @param {Compilers|*} compilers
@@ -237,7 +264,7 @@ export class BaseQuery {
237264
rowLimit: this.options.rowLimit,
238265
preAggregationsSchema: this.options.preAggregationsSchema,
239266
className: this.constructor.name,
240-
externalClassName: this.options.externalQueryClass && this.options.externalQueryClass.name,
267+
externalClassName: this.options.externalQueryClass?.name,
241268
preAggregationQuery: this.options.preAggregationQuery,
242269
disableExternalPreAggregations: this.options.disableExternalPreAggregations,
243270
useOriginalSqlPreAggregationsInPreAggregation: this.options.useOriginalSqlPreAggregationsInPreAggregation,
@@ -258,20 +285,28 @@ export class BaseQuery {
258285
this.timezone = this.options.timezone;
259286
this.rowLimit = this.options.rowLimit;
260287
this.offset = this.options.offset;
288+
/** @type {import('./PreAggregations').PreAggregations} */
261289
this.preAggregations = this.newPreAggregations();
290+
/** @type {import('./BaseMeasure').BaseMeasure[]} */
262291
this.measures = (this.options.measures || []).map(this.newMeasure.bind(this));
292+
/** @type {import('./BaseDimension').BaseDimension[]} */
263293
this.dimensions = (this.options.dimensions || []).map(this.newDimension.bind(this));
294+
/** @type {import('./BaseDimension').BaseDimension[]} */
264295
this.multiStageDimensions = (this.options.multiStageDimensions || []).map(this.newDimension.bind(this));
296+
/** @type {import('./BaseTimeDimension').BaseTimeDimension[]} */
265297
this.multiStageTimeDimensions = (this.options.multiStageTimeDimensions || []).map(this.newTimeDimension.bind(this));
298+
/** @type {import('./BaseSegment').BaseSegment[]} */
266299
this.segments = (this.options.segments || []).map(this.newSegment.bind(this));
267300

268301
const filters = this.extractFiltersAsTree(this.options.filters || []);
269302

270303
// measure_filter (the one extracted from filters parameter on measure and
271-
// used in drill downs) should go to WHERE instead of HAVING
304+
// used in drill-downs) should go to WHERE instead of HAVING
272305
/** @type {(BaseFilter|BaseGroupFilter)[]} */
273306
this.filters = filters.filter(f => f.dimensionGroup || f.dimension || f.operator === 'measure_filter' || f.operator === 'measureFilter').map(this.initFilter.bind(this));
307+
/** @type {(BaseFilter|BaseGroupFilter)[]} */
274308
this.measureFilters = filters.filter(f => (f.measureGroup || f.measure) && f.operator !== 'measure_filter' && f.operator !== 'measureFilter').map(this.initFilter.bind(this));
309+
/** @type {import('./BaseTimeDimension').BaseTimeDimension[]} */
275310
this.timeDimensions = (this.options.timeDimensions || []).map(dimension => {
276311
if (!dimension.dimension) {
277312
const join = this.joinGraph.buildJoin(this.collectJoinHints(true));
@@ -466,14 +501,29 @@ export class BaseQuery {
466501
return res;
467502
}
468503

504+
/**
505+
*
506+
* @param measurePath
507+
* @returns {BaseMeasure}
508+
*/
469509
newMeasure(measurePath) {
470510
return new BaseMeasure(this, measurePath);
471511
}
472512

513+
/**
514+
*
515+
* @param dimensionPath
516+
* @returns {BaseDimension}
517+
*/
473518
newDimension(dimensionPath) {
474519
return new BaseDimension(this, dimensionPath);
475520
}
476521

522+
/**
523+
*
524+
* @param segmentPath
525+
* @returns {BaseSegment}
526+
*/
477527
newSegment(segmentPath) {
478528
return new BaseSegment(this, segmentPath);
479529
}
@@ -497,6 +547,11 @@ export class BaseQuery {
497547
return new BaseFilter(this, filter);
498548
}
499549

550+
/**
551+
*
552+
* @param filter
553+
* @returns {BaseGroupFilter}
554+
*/
500555
newGroupFilter(filter) {
501556
return new BaseGroupFilter(filter);
502557
}
@@ -509,10 +564,19 @@ export class BaseQuery {
509564
return new BaseTimeDimension(this, timeDimension);
510565
}
511566

567+
/**
568+
*
569+
* @param expressionParams
570+
* @returns {ParamAllocator}
571+
*/
512572
newParamAllocator(expressionParams) {
513573
return new ParamAllocator(expressionParams);
514574
}
515575

576+
/**
577+
*
578+
* @returns {PreAggregations}
579+
*/
516580
newPreAggregations() {
517581
return new PreAggregations(this, this.options.historyQueries || [], this.options.cubeLatticeCache);
518582
}
@@ -540,7 +604,7 @@ export class BaseQuery {
540604
if (!this.options.preAggregationQuery) {
541605
preAggForQuery =
542606
this.preAggregations.findPreAggregationForQuery();
543-
if (this.options.disableExternalPreAggregations && preAggForQuery && preAggForQuery.preAggregation.external) {
607+
if (this.options.disableExternalPreAggregations && preAggForQuery?.preAggregation.external) {
544608
preAggForQuery = undefined;
545609
}
546610
}
@@ -2201,9 +2265,13 @@ export class BaseQuery {
22012265
}
22022266
}
22032267

2204-
collectCubeNames(excludeTimeDimensions) {
2268+
/**
2269+
*
2270+
* @returns {Array<string>}
2271+
*/
2272+
collectCubeNames() {
22052273
return this.collectFromMembers(
2206-
excludeTimeDimensions,
2274+
[],
22072275
this.collectCubeNamesFor.bind(this),
22082276
'collectCubeNamesFor'
22092277
);
@@ -2323,6 +2391,11 @@ export class BaseQuery {
23232391
return this.rollupGroupByClause(dimensionNames);
23242392
}
23252393

2394+
/**
2395+
* XXX: String as return value is added because of HiveQuery.getFieldIndex()
2396+
* @param id
2397+
* @returns {number|string|null}
2398+
*/
23262399
getFieldIndex(id) {
23272400
const equalIgnoreCase = (a, b) => (
23282401
typeof a === 'string' && typeof b === 'string' && a.toUpperCase() === b.toUpperCase()
@@ -2794,6 +2867,11 @@ export class BaseQuery {
27942867
);
27952868
}
27962869

2870+
/**
2871+
*
2872+
* @param fn
2873+
* @returns {Array<string>}
2874+
*/
27972875
collectCubeNamesFor(fn) {
27982876
const context = { cubeNames: [] };
27992877
this.evaluateSymbolSqlWithContext(
@@ -2813,6 +2891,11 @@ export class BaseQuery {
28132891
return context.joinHints;
28142892
}
28152893

2894+
/**
2895+
*
2896+
* @param fn
2897+
* @returns {Array<string>}
2898+
*/
28162899
collectMemberNamesFor(fn) {
28172900
const context = { memberNames: [] };
28182901
this.evaluateSymbolSqlWithContext(
@@ -3226,6 +3309,11 @@ export class BaseQuery {
32263309
return inflection.underscore(name).replace(/\./g, isPreAggregationName ? '_' : '__');
32273310
}
32283311

3312+
/**
3313+
*
3314+
* @param options
3315+
* @returns {BaseQuery}
3316+
*/
32293317
newSubQuery(options) {
32303318
const QueryClass = this.constructor;
32313319
return new QueryClass(this.compilers, this.subQueryOptions(options));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,6 @@ export class BaseSegment {
9292
if (this.expression) {
9393
return `expr:${this.expression.expressionName}`;
9494
}
95-
return this.query.cubeEvaluator.pathFromArray(this.path());
95+
return this.query.cubeEvaluator.pathFromArray(this.path() as string[]);
9696
}
9797
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { parseSqlInterval } from '@cubejs-backend/shared';
33
import { BaseQuery } from './BaseQuery';
44
import { BaseFilter } from './BaseFilter';
55
import { BaseMeasure } from './BaseMeasure';
6+
import { BaseSegment } from './BaseSegment';
7+
import { BaseGroupFilter } from './BaseGroupFilter';
68

79
const GRANULARITY_TO_INTERVAL: Record<string, string> = {
810
day: 'day',
@@ -192,9 +194,9 @@ export class CubeStoreQuery extends BaseQuery {
192194
const maxRollingWindow = cumulativeMeasuresWithoutMultiplied.reduce((a, b) => this.maxRollingWindow(a, b.rollingWindowDefinition()), <RollingWindow><unknown>null);
193195
const commonDateCondition =
194196
this.rollingWindowDateJoinCondition(maxRollingWindow.trailing, maxRollingWindow.leading, maxRollingWindow.offset);
195-
const filters = this.segments.concat(this.filters).concat(
197+
const filters: (BaseSegment | BaseFilter | BaseGroupFilter)[] = [...this.segments, ...this.filters, ...(
196198
timeDimension?.dateRange && this.dateFromStartToEndConditionSql(commonDateCondition, true, true) || []
197-
);
199+
)];
198200
const rollupGranularity = this.preAggregations?.castGranularity(preAggregationForQuery.preAggregation.granularity) || 'day';
199201
const granularityOverride = timeDimensionWithGranularity &&
200202
cumulativeMeasuresWithoutMultiplied.reduce((a, b) => this.minGranularity(a, b.windowGranularity()), timeDimensionWithGranularity.granularity) || rollupGranularity;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class HiveFilter extends BaseFilter {
2323

2424
export class HiveQuery extends BaseQuery {
2525
public newFilter(filter) {
26-
return new HiveFilter(this, filter);
26+
return new HiveFilter(this as BaseQuery, filter);
2727
}
2828

2929
public convertTz(field) {
@@ -67,11 +67,11 @@ export class HiveQuery extends BaseQuery {
6767
const select = this.evaluateSymbolSqlWithContext(
6868
() => this.dimensionsForSelect().map(
6969
d => d.aliasName()
70-
).concat(this.measures.map(m => m.selectColumns())).filter(s => !!s), {
70+
).concat(this.measures.flatMap(m => m.selectColumns())).filter(s => !!s), {
7171
ungroupedAliases: R.fromPairs(this.forSelect().map((m: any) => [m.measure || m.dimension, m.aliasName()]))
7272
}
7373
);
74-
return `SELECT ${select} FROM (${ungrouped}) AS ${this.escapeColumnName('hive_wrapper')}
74+
return `SELECT ${select} FROM (${ungrouped}) AS ${this.escapeColumnName('hive_wrapper')}
7575
${this.groupByClause()}${this.baseHaving(this.measureFilters)}${this.orderBy()}${this.groupByDimensionLimit()}`;
7676
}
7777

0 commit comments

Comments
 (0)