Skip to content

Commit

Permalink
fix: MS SQL segment pre-aggregations support
Browse files Browse the repository at this point in the history
Fixes #186
  • Loading branch information
paveltiunov committed Aug 21, 2019
1 parent 3b42899 commit f8e37bf
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/cubejs-schema-compiler/adapter/BaseDimension.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class BaseDimension {
}

dimensionSql() {
if (this.query.cubeEvaluator.isSegment(this.dimension)) {
return this.query.wrapSegmentForDimensionSelect(this.query.dimensionSql(this));
}
return this.query.dimensionSql(this);
}

Expand Down
4 changes: 4 additions & 0 deletions packages/cubejs-schema-compiler/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,10 @@ class BaseQuery {
return sql;
}

wrapSegmentForDimensionSelect(sql) {
return sql;
}

pushCubeNameForCollectionIfNecessary(cubeName) {
if ((this.evaluateSymbolContext || {}).cubeNames && cubeName) {
this.evaluateSymbolContext.cubeNames.push(cubeName);
Expand Down
4 changes: 4 additions & 0 deletions packages/cubejs-schema-compiler/adapter/MssqlQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ class MssqlQuery extends BaseQuery {
const sqlAndParams = this.preAggregationSql(cube, preAggregation);
return [`SELECT * INTO ${tableName} FROM (${sqlAndParams[0]}) AS PreAggregation`, sqlAndParams[1]];
}

wrapSegmentForDimensionSelect(sql) {
return `CAST((CASE WHEN ${sql} THEN 1 ELSE 0 END) AS BIT)`;
}
}

module.exports = MssqlQuery;
43 changes: 43 additions & 0 deletions packages/cubejs-schema-compiler/test/MSSqlPreAggregationsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,47 @@ describe('MSSqlPreAggregations', function test() {
);
});
}));

it('segment', () => compiler.compile().then(() => {
const query = new MSSqlQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: [
'visitors.checkinsTotal'
],
dimensions: [],
segments: ['visitors.google'],
timezone: 'UTC',
preAggregationsSchema: '',
timeDimensions: [{
dimension: 'visitors.createdAt',
granularity: 'date',
dateRange: ['2016-12-30', '2017-01-06']
}],
order: [{
id: 'visitors.createdAt'
}],
});

const queryAndParams = query.buildSqlAndParams();
console.log(queryAndParams);
const preAggregationsDescription = query.preAggregations.preAggregationsDescription();
console.log(preAggregationsDescription);

const queries = tempTablePreAggregations(preAggregationsDescription);

console.log(JSON.stringify(queries.concat(queryAndParams)));

return dbRunner.testQueries(
queries.concat([queryAndParams]).map(q => replaceTableName(q, preAggregationsDescription, 142))
).then(res => {
console.log(JSON.stringify(res));
res.should.be.deepEqual(
[
{
"visitors__created_at_date": "2017-01-06T00:00:00.000",
"visitors__checkins_total": 1
}
]
);
});
}));
});

0 comments on commit f8e37bf

Please sign in to comment.