Skip to content

Commit

Permalink
fix(schema-compiler): Fix ungrouped cumulative queries incorrect sql …
Browse files Browse the repository at this point in the history
…generation
  • Loading branch information
KSDaemon committed Nov 22, 2024
1 parent e948856 commit ca365fa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
5 changes: 4 additions & 1 deletion packages/cubejs-schema-compiler/src/adapter/BaseDimension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export class BaseDimension {
return this.dimensionSql() === this.aliasName();
}

public cumulativeSelectColumns() {
public cumulativeSelectColumns(baseQueryAliasName?: string): string[] {
if (baseQueryAliasName) {
return [`${baseQueryAliasName}.${this.aliasName()}`];
}
return [`${this.aliasName()}`];
}

Expand Down
9 changes: 5 additions & 4 deletions packages/cubejs-schema-compiler/src/adapter/BaseMeasure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ export class BaseMeasure {
return this.measureSql() === this.aliasName();
}

public cumulativeSelectColumns() {
return [`${this.cumulativeMeasureSql()} ${this.aliasName()}`];
public cumulativeSelectColumns(baseQueryAliasName?: string): string[] {
return [`${this.cumulativeMeasureSql(baseQueryAliasName)} ${this.aliasName()}`];
}

public cumulativeMeasureSql() {
public cumulativeMeasureSql(baseQueryAliasName?: string) {
return this.query.evaluateSymbolSqlWithContext(
() => this.measureSql(),
{
ungroupedAliasesForCumulative: { [this.measure]: this.aliasName() }
ungroupedAliasesForCumulative: { [this.measure]: this.aliasName() },
ungroupedBaseQueryAliasForCumulative: baseQueryAliasName
}
);
}
Expand Down
52 changes: 31 additions & 21 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,9 +789,6 @@ export class BaseQuery {
* @returns {string}
*/
fullKeyQueryAggregate() {
if (this.from) {
return this.simpleQuery();
}
const {
multipliedMeasures,
regularMeasures,
Expand Down Expand Up @@ -1426,16 +1423,17 @@ export class BaseQuery {
}

overTimeSeriesSelect(cumulativeMeasures, dateSeriesSql, baseQuery, dateJoinConditionSql, baseQueryAlias) {
const forSelect = this.overTimeSeriesForSelect(cumulativeMeasures);
const forSelect = this.overTimeSeriesForSelect(cumulativeMeasures, baseQueryAlias);
return `SELECT ${forSelect} FROM ${dateSeriesSql}` +
` LEFT JOIN (${baseQuery}) ${this.asSyntaxJoin} ${baseQueryAlias} ON ${dateJoinConditionSql}` +
this.groupByClause();
}

overTimeSeriesForSelect(cumulativeMeasures) {
return this.dimensions.map(s => s.cumulativeSelectColumns()).concat(this.dateSeriesSelect()).concat(
cumulativeMeasures.map(s => s.cumulativeSelectColumns()),
).filter(c => !!c)
overTimeSeriesForSelect(cumulativeMeasures, baseQueryAlias) {
return this.dimensions.map(s => s.cumulativeSelectColumns(baseQueryAlias))
.concat(this.dateSeriesSelect())
.concat(cumulativeMeasures.map(s => s.cumulativeSelectColumns(baseQueryAlias)))
.filter(c => !!c)
.join(', ');
}

Expand Down Expand Up @@ -1792,16 +1790,16 @@ export class BaseQuery {
}));
}

groupedUngroupedSelect(select, ungrouped, granularityOverride) {
groupedUngroupedSelect(selectFn, ungrouped, granularityOverride) {
return this.evaluateSymbolSqlWithContext(
select,
selectFn,
{ ungrouped, granularityOverride, overTimeSeriesAggregate: true }
);
}

ungroupedMeasureSelect(select) {
ungroupedMeasureSelect(selectFn) {
return this.evaluateSymbolSqlWithContext(
select,
selectFn,
{ ungrouped: true }
);
}
Expand Down Expand Up @@ -2266,17 +2264,28 @@ export class BaseQuery {
this.safeEvaluateSymbolContext().leafMeasures[this.safeEvaluateSymbolContext().currentMeasure] = true;
}
}
const primaryKeys = this.cubeEvaluator.primaryKeys[cubeName];
const orderBySql = (symbol.orderBy || []).map(o => ({ sql: this.evaluateSql(cubeName, o.sql), dir: o.dir }));

let sql;
if (symbol.type !== 'rank') {
sql = symbol.sql && this.evaluateSql(cubeName, symbol.sql) ||
primaryKeys.length && (
primaryKeys.length > 1 ?
this.concatStringsSql(primaryKeys.map((pk) => this.castToString(this.primaryKeySql(pk, cubeName))))
: this.primaryKeySql(primaryKeys[0], cubeName)
) || '*';
let orderBySql = [];

if ((this.safeEvaluateSymbolContext().ungroupedAliasesForCumulative || {})[memberPath]) {
sql = this.safeEvaluateSymbolContext().ungroupedAliasesForCumulative[memberPath];
if (this.safeEvaluateSymbolContext().ungroupedBaseQueryAliasForCumulative) {
sql = `${this.safeEvaluateSymbolContext().ungroupedBaseQueryAliasForCumulative}.${sql}`;
}
} else {
const primaryKeys = this.cubeEvaluator.primaryKeys[cubeName];
orderBySql = (symbol.orderBy || []).map(o => ({ sql: this.evaluateSql(cubeName, o.sql), dir: o.dir }));
if (symbol.type !== 'rank') {
sql = symbol.sql && this.evaluateSql(cubeName, symbol.sql) ||
primaryKeys.length && (
primaryKeys.length > 1 ?
this.concatStringsSql(primaryKeys.map((pk) => this.castToString(this.primaryKeySql(pk, cubeName))))
: this.primaryKeySql(primaryKeys[0], cubeName)
) || '*';
}
}

const result = this.renderSqlMeasure(
name,
sql && this.applyMeasureFilters(
Expand All @@ -2293,6 +2302,7 @@ export class BaseQuery {
parentMeasure,
orderBySql,
);

if (
this.safeEvaluateSymbolContext().compositeCubeMeasures ||
this.safeEvaluateSymbolContext().leafMeasures
Expand Down

0 comments on commit ca365fa

Please sign in to comment.