Skip to content

Commit

Permalink
fix(cubesql): Incorrect CASE WHEN generated during ungrouped filtered…
Browse files Browse the repository at this point in the history
… count (#7859)

Fixes #7858
  • Loading branch information
paveltiunov committed Mar 1, 2024
1 parent 696c035 commit f970937
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Expand Up @@ -2005,7 +2005,8 @@ export class BaseQuery {
}
if (this.ungrouped) {
if (symbol.type === 'count' || symbol.type === 'countDistinct' || symbol.type === 'countDistinctApprox') {
return evaluateSql === '*' ? '1' : this.caseWhenStatement([{ sql: `(${evaluateSql}) IS NOT NULL`, label: `1` }]);
const sql = symbol.type === 'countDistinct' || symbol.type === 'countDistinctApprox' ? evaluateSql : this.caseWhenStatement([{ sql: `(${evaluateSql}) IS NOT NULL`, label: `1` }]);
return evaluateSql === '*' ? '1' : sql;
} else {
return evaluateSql;
}
Expand Down
Expand Up @@ -212,6 +212,13 @@ describe('SQL Generation', () => {
sql: \`\${visitors}.source = 'google'\`
}]
},
unique_google_sourced_checkins: {
type: 'countDistinct',
sql: 'id',
filters: [{
sql: \`\${visitors}.source = 'google'\`
}]
},
minDate: {
type: 'min',
sql: 'created_at'
Expand Down Expand Up @@ -1459,6 +1466,30 @@ describe('SQL Generation', () => {
{ visitor_checkins__created_at_day: '2017-01-05T00:00:00.000Z', visitor_checkins__google_sourced_checkins: 1 },
]));

it('ungrouped filtered distinct count', () => runQueryTest({
measures: [
'visitor_checkins.unique_google_sourced_checkins',
],
timezone: 'America/Los_Angeles',
order: [{
id: 'visitor_checkins.created_at',
}],
timeDimensions: [{
dimension: 'visitor_checkins.created_at',
granularity: 'day',
dateRange: ['2016-12-01', '2017-03-30'],
}],
ungrouped: true,
allowUngroupedWithoutPrimaryKey: true,
}, [
{ visitor_checkins__created_at_day: '2017-01-02T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-03T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-05T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: 6 },
]));

it('builds geo dimension', () => runQueryTest({
dimensions: [
'visitors.location'
Expand Down

0 comments on commit f970937

Please sign in to comment.