Skip to content

Commit f970937

Browse files
authored
fix(cubesql): Incorrect CASE WHEN generated during ungrouped filtered count (#7859)
Fixes #7858
1 parent 696c035 commit f970937

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

packages/cubejs-schema-compiler/src/adapter/BaseQuery.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,8 @@ export class BaseQuery {
20052005
}
20062006
if (this.ungrouped) {
20072007
if (symbol.type === 'count' || symbol.type === 'countDistinct' || symbol.type === 'countDistinctApprox') {
2008-
return evaluateSql === '*' ? '1' : this.caseWhenStatement([{ sql: `(${evaluateSql}) IS NOT NULL`, label: `1` }]);
2008+
const sql = symbol.type === 'countDistinct' || symbol.type === 'countDistinctApprox' ? evaluateSql : this.caseWhenStatement([{ sql: `(${evaluateSql}) IS NOT NULL`, label: `1` }]);
2009+
return evaluateSql === '*' ? '1' : sql;
20092010
} else {
20102011
return evaluateSql;
20112012
}

packages/cubejs-schema-compiler/test/integration/postgres/sql-generation.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ describe('SQL Generation', () => {
212212
sql: \`\${visitors}.source = 'google'\`
213213
}]
214214
},
215+
unique_google_sourced_checkins: {
216+
type: 'countDistinct',
217+
sql: 'id',
218+
filters: [{
219+
sql: \`\${visitors}.source = 'google'\`
220+
}]
221+
},
215222
minDate: {
216223
type: 'min',
217224
sql: 'created_at'
@@ -1459,6 +1466,30 @@ describe('SQL Generation', () => {
14591466
{ visitor_checkins__created_at_day: '2017-01-05T00:00:00.000Z', visitor_checkins__google_sourced_checkins: 1 },
14601467
]));
14611468

1469+
it('ungrouped filtered distinct count', () => runQueryTest({
1470+
measures: [
1471+
'visitor_checkins.unique_google_sourced_checkins',
1472+
],
1473+
timezone: 'America/Los_Angeles',
1474+
order: [{
1475+
id: 'visitor_checkins.created_at',
1476+
}],
1477+
timeDimensions: [{
1478+
dimension: 'visitor_checkins.created_at',
1479+
granularity: 'day',
1480+
dateRange: ['2016-12-01', '2017-03-30'],
1481+
}],
1482+
ungrouped: true,
1483+
allowUngroupedWithoutPrimaryKey: true,
1484+
}, [
1485+
{ visitor_checkins__created_at_day: '2017-01-02T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
1486+
{ visitor_checkins__created_at_day: '2017-01-03T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
1487+
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
1488+
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
1489+
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
1490+
{ visitor_checkins__created_at_day: '2017-01-05T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: 6 },
1491+
]));
1492+
14621493
it('builds geo dimension', () => runQueryTest({
14631494
dimensions: [
14641495
'visitors.location'

0 commit comments

Comments
 (0)