diff --git a/packages/cubejs-schema-compiler/adapter/BaseDimension.js b/packages/cubejs-schema-compiler/adapter/BaseDimension.js index e415c371aca4e..05ac1de157870 100644 --- a/packages/cubejs-schema-compiler/adapter/BaseDimension.js +++ b/packages/cubejs-schema-compiler/adapter/BaseDimension.js @@ -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); } diff --git a/packages/cubejs-schema-compiler/adapter/BaseQuery.js b/packages/cubejs-schema-compiler/adapter/BaseQuery.js index e9976379cbce8..09abe2778f8ec 100644 --- a/packages/cubejs-schema-compiler/adapter/BaseQuery.js +++ b/packages/cubejs-schema-compiler/adapter/BaseQuery.js @@ -862,6 +862,10 @@ class BaseQuery { return sql; } + wrapSegmentForDimensionSelect(sql) { + return sql; + } + pushCubeNameForCollectionIfNecessary(cubeName) { if ((this.evaluateSymbolContext || {}).cubeNames && cubeName) { this.evaluateSymbolContext.cubeNames.push(cubeName); diff --git a/packages/cubejs-schema-compiler/adapter/MssqlQuery.js b/packages/cubejs-schema-compiler/adapter/MssqlQuery.js index 3498d9632b797..20bbc34354a5b 100644 --- a/packages/cubejs-schema-compiler/adapter/MssqlQuery.js +++ b/packages/cubejs-schema-compiler/adapter/MssqlQuery.js @@ -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; diff --git a/packages/cubejs-schema-compiler/test/MSSqlPreAggregationsTest.js b/packages/cubejs-schema-compiler/test/MSSqlPreAggregationsTest.js index afcc59e7f27d5..b1eff85ecf66a 100644 --- a/packages/cubejs-schema-compiler/test/MSSqlPreAggregationsTest.js +++ b/packages/cubejs-schema-compiler/test/MSSqlPreAggregationsTest.js @@ -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 + } + ] + ); + }); + })); });