Skip to content

chore(schema-compiler): move preAggregations class to typescript #9538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions packages/cubejs-ksql-driver/src/KsqlQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ export class KsqlQuery extends BaseQuery {
return dimensionColumns.length ? ` GROUP BY ${dimensionColumns.join(', ')}` : '';
}

public partitionInvalidateKeyQueries(cube: string, preAggregation: any) {
return [];
}

public preAggregationStartEndQueries(cube: string, preAggregation: any) {
if (preAggregation.partitionGranularity) {
if (!preAggregation.refreshRangeStart) {
Expand All @@ -97,7 +93,7 @@ export class KsqlQuery extends BaseQuery {
}
}
const res = this.evaluateSymbolSqlWithContext(() => [

preAggregation.refreshRangeStart && [this.evaluateSql(cube, preAggregation.refreshRangeStart.sql, {}), [], { external: true }],
preAggregation.refreshRangeEnd && [this.evaluateSql(cube, preAggregation.refreshRangeEnd.sql, {}), [], { external: true }]
], { preAggregationQuery: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class BaseDimension {
return this.dimensionDefinition().fieldType;
}

public path() {
public path(): string[] | null {
if (this.expression) {
return null;
}
Expand All @@ -138,10 +138,10 @@ export class BaseDimension {
return this.query.cubeEvaluator.parsePath('dimensions', this.dimension);
}

public expressionPath() {
public expressionPath(): string {
if (this.expression) {
return `expr:${this.expression.expressionName}`;
}
return this.query.cubeEvaluator.pathFromArray(this.path());
return this.query.cubeEvaluator.pathFromArray(this.path() as string[]);
}
}
5 changes: 2 additions & 3 deletions packages/cubejs-schema-compiler/src/adapter/BaseFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ export class BaseFilter extends BaseDimension {
}

// Evaluates filters on measures to whole where statement in query
// It used in drill downs
// It used in drill-downs
public measureFilterToWhere() {
const measureDefinition = this.measureDefinition();
if (measureDefinition.filters && measureDefinition.filters.length ||
measureDefinition.drillFilters && measureDefinition.drillFilters.length) {
if (measureDefinition.filters?.length || measureDefinition.drillFilters?.length) {
return this.query.evaluateFiltersArray(
(measureDefinition.filters || []).concat(measureDefinition.drillFilters || []),
this.query.cubeEvaluator.cubeNameFromPath(this.measure)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ export class BaseGroupFilter {

protected readonly operator: any;

protected readonly measure: any;
public readonly measure: any;

protected readonly dimension: any;
public readonly dimension: any;

public constructor(filter: any) {
this.values = filter.values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,6 @@ export class BaseMeasure {
if (this.expression) {
return `expr:${this.expression.expressionName}`;
}
return this.query.cubeEvaluator.pathFromArray(this.path());
return this.query.cubeEvaluator.pathFromArray(this.path() as string[]);
}
}
106 changes: 99 additions & 7 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,33 @@ const SecondsDurations = {
* and {@code CompilerApi} configuration.
*/
export class BaseQuery {
/** @type {import('./PreAggregations').PreAggregations} */
preAggregations;

/** @type {import('./BaseMeasure').BaseMeasure[]} */
measures;

/** @type {import('./BaseDimension').BaseDimension[]} */
dimensions;

/** @type {import('./BaseDimension').BaseDimension[]} */
multiStageDimensions;

/** @type {import('./BaseTimeDimension').BaseTimeDimension[]} */
multiStageTimeDimensions;

/** @type {import('./BaseSegment').BaseSegment[]} */
segments;

/** @type {(BaseFilter|BaseGroupFilter)[]} */
filters;

/** @type {(BaseFilter|BaseGroupFilter)[]} */
measureFilters;

/** @type {import('./BaseTimeDimension').BaseTimeDimension[]} */
timeDimensions;

/**
* BaseQuery class constructor.
* @param {Compilers|*} compilers
Expand Down Expand Up @@ -237,7 +264,7 @@ export class BaseQuery {
rowLimit: this.options.rowLimit,
preAggregationsSchema: this.options.preAggregationsSchema,
className: this.constructor.name,
externalClassName: this.options.externalQueryClass && this.options.externalQueryClass.name,
externalClassName: this.options.externalQueryClass?.name,
preAggregationQuery: this.options.preAggregationQuery,
disableExternalPreAggregations: this.options.disableExternalPreAggregations,
useOriginalSqlPreAggregationsInPreAggregation: this.options.useOriginalSqlPreAggregationsInPreAggregation,
Expand All @@ -258,20 +285,28 @@ export class BaseQuery {
this.timezone = this.options.timezone;
this.rowLimit = this.options.rowLimit;
this.offset = this.options.offset;
/** @type {import('./PreAggregations').PreAggregations} */
this.preAggregations = this.newPreAggregations();
/** @type {import('./BaseMeasure').BaseMeasure[]} */
this.measures = (this.options.measures || []).map(this.newMeasure.bind(this));
/** @type {import('./BaseDimension').BaseDimension[]} */
this.dimensions = (this.options.dimensions || []).map(this.newDimension.bind(this));
/** @type {import('./BaseDimension').BaseDimension[]} */
this.multiStageDimensions = (this.options.multiStageDimensions || []).map(this.newDimension.bind(this));
/** @type {import('./BaseTimeDimension').BaseTimeDimension[]} */
this.multiStageTimeDimensions = (this.options.multiStageTimeDimensions || []).map(this.newTimeDimension.bind(this));
/** @type {import('./BaseSegment').BaseSegment[]} */
this.segments = (this.options.segments || []).map(this.newSegment.bind(this));

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

// measure_filter (the one extracted from filters parameter on measure and
// used in drill downs) should go to WHERE instead of HAVING
// used in drill-downs) should go to WHERE instead of HAVING
/** @type {(BaseFilter|BaseGroupFilter)[]} */
this.filters = filters.filter(f => f.dimensionGroup || f.dimension || f.operator === 'measure_filter' || f.operator === 'measureFilter').map(this.initFilter.bind(this));
/** @type {(BaseFilter|BaseGroupFilter)[]} */
this.measureFilters = filters.filter(f => (f.measureGroup || f.measure) && f.operator !== 'measure_filter' && f.operator !== 'measureFilter').map(this.initFilter.bind(this));
/** @type {import('./BaseTimeDimension').BaseTimeDimension[]} */
this.timeDimensions = (this.options.timeDimensions || []).map(dimension => {
if (!dimension.dimension) {
const join = this.joinGraph.buildJoin(this.collectJoinHints(true));
Expand Down Expand Up @@ -471,10 +506,20 @@ export class BaseQuery {
return res;
}

/**
*
* @param measurePath
* @returns {BaseMeasure}
*/
newMeasure(measurePath) {
return new BaseMeasure(this, measurePath);
}

/**
*
* @param dimensionPath
* @returns {BaseDimension}
*/
newDimension(dimensionPath) {
if (typeof dimensionPath === 'string') {
const memberArr = dimensionPath.split('.');
Expand All @@ -492,6 +537,11 @@ export class BaseQuery {
return new BaseDimension(this, dimensionPath);
}

/**
*
* @param segmentPath
* @returns {BaseSegment}
*/
newSegment(segmentPath) {
return new BaseSegment(this, segmentPath);
}
Expand All @@ -515,6 +565,11 @@ export class BaseQuery {
return new BaseFilter(this, filter);
}

/**
*
* @param filter
* @returns {BaseGroupFilter}
*/
newGroupFilter(filter) {
return new BaseGroupFilter(filter);
}
Expand All @@ -527,10 +582,19 @@ export class BaseQuery {
return new BaseTimeDimension(this, timeDimension);
}

/**
*
* @param expressionParams
* @returns {ParamAllocator}
*/
newParamAllocator(expressionParams) {
return new ParamAllocator(expressionParams);
}

/**
*
* @returns {PreAggregations}
*/
newPreAggregations() {
return new PreAggregations(this, this.options.historyQueries || [], this.options.cubeLatticeCache);
}
Expand Down Expand Up @@ -558,7 +622,7 @@ export class BaseQuery {
if (!this.options.preAggregationQuery) {
preAggForQuery =
this.preAggregations.findPreAggregationForQuery();
if (this.options.disableExternalPreAggregations && preAggForQuery && preAggForQuery.preAggregation.external) {
if (this.options.disableExternalPreAggregations && preAggForQuery?.preAggregation.external) {
preAggForQuery = undefined;
}
}
Expand Down Expand Up @@ -1199,7 +1263,7 @@ export class BaseQuery {
}

fullKeyQueryAggregateMeasures(context) {
const measureToHierarchy = this.collectRootMeasureToHieararchy(context);
const measureToHierarchy = this.collectRootMeasureToHierarchy(context);
const allMemberChildren = this.collectAllMemberChildren(context);
const memberToIsMultiStage = this.collectAllMultiStageMembers(allMemberChildren);

Expand Down Expand Up @@ -1855,7 +1919,7 @@ export class BaseQuery {
}]];
}

collectRootMeasureToHieararchy(context) {
collectRootMeasureToHierarchy(context) {
const notAddedMeasureFilters = R.flatten(this.measureFilters.map(f => f.getMembers()))
.filter(f => R.none(m => m.measure === f.measure, this.measures));

Expand Down Expand Up @@ -2287,9 +2351,13 @@ export class BaseQuery {
}
}

collectCubeNames(excludeTimeDimensions) {
/**
*
* @returns {Array<string>}
*/
collectCubeNames() {
return this.collectFromMembers(
excludeTimeDimensions,
[],
this.collectCubeNamesFor.bind(this),
'collectCubeNamesFor'
);
Expand Down Expand Up @@ -2409,6 +2477,11 @@ export class BaseQuery {
return this.rollupGroupByClause(dimensionNames);
}

/**
* XXX: String as return value is added because of HiveQuery.getFieldIndex()
* @param id
* @returns {number|string|null}
*/
getFieldIndex(id) {
const equalIgnoreCase = (a, b) => (
typeof a === 'string' && typeof b === 'string' && a.toUpperCase() === b.toUpperCase()
Expand Down Expand Up @@ -2880,6 +2953,11 @@ export class BaseQuery {
);
}

/**
*
* @param fn
* @returns {Array<string>}
*/
collectCubeNamesFor(fn) {
const context = { cubeNames: [] };
this.evaluateSymbolSqlWithContext(
Expand All @@ -2899,6 +2977,11 @@ export class BaseQuery {
return context.joinHints;
}

/**
*
* @param fn
* @returns {Array<string>}
*/
collectMemberNamesFor(fn) {
const context = { memberNames: [] };
this.evaluateSymbolSqlWithContext(
Expand Down Expand Up @@ -3178,6 +3261,10 @@ export class BaseQuery {
);
}

/**
* @param cubeName
* @returns Boolean
*/
multipliedJoinRowResult(cubeName) {
// this.join not initialized on collectCubeNamesForSql
return this.join && this.join.multiplicationFactor[cubeName];
Expand Down Expand Up @@ -3308,6 +3395,11 @@ export class BaseQuery {
return inflection.underscore(name).replace(/\./g, isPreAggregationName ? '_' : '__');
}

/**
*
* @param options
* @returns {BaseQuery}
*/
newSubQuery(options) {
const QueryClass = this.constructor;
return new QueryClass(this.compilers, this.subQueryOptions(options));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ export class BaseSegment {
if (this.expression) {
return `expr:${this.expression.expressionName}`;
}
return this.query.cubeEvaluator.pathFromArray(this.path());
return this.query.cubeEvaluator.pathFromArray(this.path() as string[]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { parseSqlInterval } from '@cubejs-backend/shared';
import { BaseQuery } from './BaseQuery';
import { BaseFilter } from './BaseFilter';
import { BaseMeasure } from './BaseMeasure';
import { BaseSegment } from './BaseSegment';
import { BaseGroupFilter } from './BaseGroupFilter';

const GRANULARITY_TO_INTERVAL: Record<string, string> = {
day: 'day',
Expand Down Expand Up @@ -192,9 +194,9 @@ export class CubeStoreQuery extends BaseQuery {
const maxRollingWindow = cumulativeMeasuresWithoutMultiplied.reduce((a, b) => this.maxRollingWindow(a, b.rollingWindowDefinition()), <RollingWindow><unknown>null);
const commonDateCondition =
this.rollingWindowDateJoinCondition(maxRollingWindow.trailing, maxRollingWindow.leading, maxRollingWindow.offset);
const filters = this.segments.concat(this.filters).concat(
const filters: (BaseSegment | BaseFilter | BaseGroupFilter)[] = [...this.segments, ...this.filters, ...(
timeDimension?.dateRange && this.dateFromStartToEndConditionSql(commonDateCondition, true, true) || []
);
)];
const rollupGranularity = this.preAggregations?.castGranularity(preAggregationForQuery.preAggregation.granularity) || 'day';
const granularityOverride = timeDimensionWithGranularity &&
cumulativeMeasuresWithoutMultiplied.reduce((a, b) => this.minGranularity(a, b.windowGranularity()), timeDimensionWithGranularity.granularity) || rollupGranularity;
Expand Down
6 changes: 3 additions & 3 deletions packages/cubejs-schema-compiler/src/adapter/HiveQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HiveFilter extends BaseFilter {

export class HiveQuery extends BaseQuery {
public newFilter(filter) {
return new HiveFilter(this, filter);
return new HiveFilter(this as BaseQuery, filter);
}

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

Expand Down
Loading