Skip to content

Commit

Permalink
fix(@cubejs-schema-compilter): MSSQL rollingWindow with granularity (#…
Browse files Browse the repository at this point in the history
…1169) Thanks to @JoshMentzer!

Co-authored-by: Joshua D. Mentzer <mentzerj@trinity-health.org>
  • Loading branch information
JoshMentzer and mentzerj committed Oct 2, 2020
1 parent f82b84d commit 16e6a9e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
22 changes: 22 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,21 @@ class MssqlQuery extends BaseQuery {
return dimensionColumns.length ? ` GROUP BY ${dimensionColumns.join(', ')}` : '';
}

overTimeSeriesSelect(cumulativeMeasures, dateSeriesSql, baseQuery, dateJoinConditionSql, baseQueryAlias) {
const forGroupBy = this.timeDimensions.map(
(t) => `${t.dateSeriesAliasName() + '.' + this.escapeColumnName('date_from')}`
);
const forSelect = this.dateSeriesSelect()
.concat(this.dimensions.concat(cumulativeMeasures).map((s) => s.cumulativeSelectColumns()))
.filter((c) => !!c)
.join(', ');
return (
`SELECT ${forSelect} FROM ${dateSeriesSql}` +
` LEFT JOIN (${baseQuery}) ${this.asSyntaxJoin} ${baseQueryAlias} ON ${dateJoinConditionSql}` +
` GROUP BY ${forGroupBy}`
);
}

nowTimestampSql() {
return `CURRENT_TIMESTAMP`;
}
Expand All @@ -111,6 +126,13 @@ class MssqlQuery extends BaseQuery {
wrapSegmentForDimensionSelect(sql) {
return `CAST((CASE WHEN ${sql} THEN 1 ELSE 0 END) AS BIT)`;
}

seriesSql(timeDimension) {
const values = timeDimension.timeSeries().map(([from, to]) => `('${from}', '${to}')`);
return `SELECT ${this.dateTimeCast('date_from')} date_from, ${this.dateTimeCast(
'date_to'
)} date_to FROM (VALUES ${values}) ${this.asSyntaxTable} dates (date_from, date_to)`;
}
}

module.exports = MssqlQuery;
69 changes: 69 additions & 0 deletions packages/cubejs-schema-compiler/test/unit/MssqlQueryTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* globals it, describe, after */
/* eslint-disable quote-props */
const MssqlQuery = require('../../adapter/MssqlQuery');
const PrepareCompiler = require('./PrepareCompiler');
require('should');

const { prepareCompiler } = PrepareCompiler;

describe('MssqlQuery', () => {
const { compiler, joinGraph, cubeEvaluator } = prepareCompiler(`
cube(\`visitors\`, {
sql: \`
select * from visitors
\`,
measures: {
count: {
type: 'count'
},
unboundedCount: {
type: 'count',
rollingWindow: {
trailing: 'unbounded'
}
}
},
dimensions: {
createdAt: {
type: 'time',
sql: 'created_at'
}
}
});
`);

it('group by the date_from field on unbounded trailing windows', () =>
compiler.compile().then(() => {
const query = new MssqlQuery(
{ joinGraph, cubeEvaluator, compiler },
{
measures: ['visitors.count', 'visitors.unboundedCount'],
timeDimensions: [
{
dimension: 'visitors.createdAt',
granularity: 'week',
dateRange: ['2017-01-01', '2017-01-30'],
},
],
timezone: 'America/Los_Angeles',
order: [
{
id: 'visitors.createdAt',
},
],
}
);

const queryAndParams = query.buildSqlAndParams();

const queryString = queryAndParams[0];
const lastGroupByIdx = queryString.lastIndexOf('GROUP BY');
const queryCloseIdx = queryString.indexOf(')', lastGroupByIdx + 1);
const finalGroupBy = queryString.substring(lastGroupByIdx, queryCloseIdx);

finalGroupBy.should.equal('GROUP BY "visitors.createdAt_series"."date_from"');
}));
});

0 comments on commit 16e6a9e

Please sign in to comment.