Skip to content

Commit c54aa36

Browse files
committed
chore(schema-compiler): move preAggregations class to typescript
1 parent ba59f59 commit c54aa36

File tree

14 files changed

+438
-249
lines changed

14 files changed

+438
-249
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));
@@ -471,10 +506,20 @@ export class BaseQuery {
471506
return res;
472507
}
473508

509+
/**
510+
*
511+
* @param measurePath
512+
* @returns {BaseMeasure}
513+
*/
474514
newMeasure(measurePath) {
475515
return new BaseMeasure(this, measurePath);
476516
}
477517

518+
/**
519+
*
520+
* @param dimensionPath
521+
* @returns {BaseDimension}
522+
*/
478523
newDimension(dimensionPath) {
479524
if (typeof dimensionPath === 'string') {
480525
const memberArr = dimensionPath.split('.');
@@ -492,6 +537,11 @@ export class BaseQuery {
492537
return new BaseDimension(this, dimensionPath);
493538
}
494539

540+
/**
541+
*
542+
* @param segmentPath
543+
* @returns {BaseSegment}
544+
*/
495545
newSegment(segmentPath) {
496546
return new BaseSegment(this, segmentPath);
497547
}
@@ -515,6 +565,11 @@ export class BaseQuery {
515565
return new BaseFilter(this, filter);
516566
}
517567

568+
/**
569+
*
570+
* @param filter
571+
* @returns {BaseGroupFilter}
572+
*/
518573
newGroupFilter(filter) {
519574
return new BaseGroupFilter(filter);
520575
}
@@ -527,10 +582,19 @@ export class BaseQuery {
527582
return new BaseTimeDimension(this, timeDimension);
528583
}
529584

585+
/**
586+
*
587+
* @param expressionParams
588+
* @returns {ParamAllocator}
589+
*/
530590
newParamAllocator(expressionParams) {
531591
return new ParamAllocator(expressionParams);
532592
}
533593

594+
/**
595+
*
596+
* @returns {PreAggregations}
597+
*/
534598
newPreAggregations() {
535599
return new PreAggregations(this, this.options.historyQueries || [], this.options.cubeLatticeCache);
536600
}
@@ -558,7 +622,7 @@ export class BaseQuery {
558622
if (!this.options.preAggregationQuery) {
559623
preAggForQuery =
560624
this.preAggregations.findPreAggregationForQuery();
561-
if (this.options.disableExternalPreAggregations && preAggForQuery && preAggForQuery.preAggregation.external) {
625+
if (this.options.disableExternalPreAggregations && preAggForQuery?.preAggregation.external) {
562626
preAggForQuery = undefined;
563627
}
564628
}
@@ -2287,9 +2351,13 @@ export class BaseQuery {
22872351
}
22882352
}
22892353

2290-
collectCubeNames(excludeTimeDimensions) {
2354+
/**
2355+
*
2356+
* @returns {Array<string>}
2357+
*/
2358+
collectCubeNames() {
22912359
return this.collectFromMembers(
2292-
excludeTimeDimensions,
2360+
[],
22932361
this.collectCubeNamesFor.bind(this),
22942362
'collectCubeNamesFor'
22952363
);
@@ -2409,6 +2477,11 @@ export class BaseQuery {
24092477
return this.rollupGroupByClause(dimensionNames);
24102478
}
24112479

2480+
/**
2481+
* XXX: String as return value is added because of HiveQuery.getFieldIndex()
2482+
* @param id
2483+
* @returns {number|string|null}
2484+
*/
24122485
getFieldIndex(id) {
24132486
const equalIgnoreCase = (a, b) => (
24142487
typeof a === 'string' && typeof b === 'string' && a.toUpperCase() === b.toUpperCase()
@@ -2880,6 +2953,11 @@ export class BaseQuery {
28802953
);
28812954
}
28822955

2956+
/**
2957+
*
2958+
* @param fn
2959+
* @returns {Array<string>}
2960+
*/
28832961
collectCubeNamesFor(fn) {
28842962
const context = { cubeNames: [] };
28852963
this.evaluateSymbolSqlWithContext(
@@ -2899,6 +2977,11 @@ export class BaseQuery {
28992977
return context.joinHints;
29002978
}
29012979

2980+
/**
2981+
*
2982+
* @param fn
2983+
* @returns {Array<string>}
2984+
*/
29022985
collectMemberNamesFor(fn) {
29032986
const context = { memberNames: [] };
29042987
this.evaluateSymbolSqlWithContext(
@@ -3312,6 +3395,11 @@ export class BaseQuery {
33123395
return inflection.underscore(name).replace(/\./g, isPreAggregationName ? '_' : '__');
33133396
}
33143397

3398+
/**
3399+
*
3400+
* @param options
3401+
* @returns {BaseQuery}
3402+
*/
33153403
newSubQuery(options) {
33163404
const QueryClass = this.constructor;
33173405
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)