Skip to content

Commit 07d00f8

Browse files
committed
fix: Querying empty Postgres table with 'time' dimension in a cube results in null value
Fixes #639
1 parent 6d0096e commit 07d00f8

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

packages/cubejs-schema-compiler/test/PreAggregationsTest.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,19 @@ describe('PreAggregations', function test() {
204204
incremental: true,
205205
updateWindow: '1 day'
206206
}
207+
},
208+
emptyPartitioned: {
209+
type: 'rollup',
210+
measureReferences: [count],
211+
timeDimensionReference: EmptyHourVisitors.createdAt,
212+
granularity: 'hour',
213+
partitionGranularity: 'month',
214+
scheduledRefresh: true,
215+
refreshKey: {
216+
every: '1 hour',
217+
incremental: true,
218+
updateWindow: '1 day'
219+
}
207220
}
208221
}
209222
})
@@ -245,6 +258,11 @@ describe('PreAggregations', function test() {
245258
}
246259
}
247260
})
261+
262+
cube('EmptyHourVisitors', {
263+
extends: EveryHourVisitors,
264+
sql: \`select v.* from \${visitors.sql()} v where created_at < '2000-01-01'\`
265+
})
248266
`);
249267

250268
function replaceTableName(query, preAggregation, suffix) {
@@ -536,6 +554,40 @@ describe('PreAggregations', function test() {
536554
});
537555
});
538556

557+
it('empty scheduled refresh', () => {
558+
return compiler.compile().then(async () => {
559+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
560+
measures: [
561+
'visitor_checkins.count'
562+
],
563+
timeDimensions: [{
564+
dimension: 'EmptyHourVisitors.createdAt',
565+
granularity: 'hour',
566+
dateRange: ['2017-01-01', '2017-01-25']
567+
}],
568+
timezone: 'UTC',
569+
order: [{
570+
id: 'EmptyHourVisitors.createdAt'
571+
}],
572+
preAggregationsSchema: ''
573+
});
574+
575+
const preAggregations = cubeEvaluator.scheduledPreAggregations();
576+
const partitionedPreAgg =
577+
preAggregations.find(p => p.preAggregationName === 'emptyPartitioned' && p.cube === 'visitor_checkins');
578+
579+
const minMaxQueries = query.preAggregationStartEndQueries('visitor_checkins', partitionedPreAgg.preAggregation);
580+
581+
console.log(minMaxQueries);
582+
583+
const res = await dbRunner.testQueries(minMaxQueries);
584+
585+
res.should.be.deepEqual(
586+
[{ max: null }]
587+
);
588+
});
589+
});
590+
539591
it('mutable partition default refreshKey', () => {
540592
return compiler.compile().then(() => {
541593
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {

packages/cubejs-server-core/core/RefreshScheduler.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ class RefreshScheduler {
3131
return data[0] && data[0][Object.keys(data[0])[0]];
3232
};
3333

34+
const dateRange = [extractDate(startDate), extractDate(endDate)];
35+
if (!dateRange[0] || !dateRange[1]) {
36+
// Empty table. Nothing to refresh.
37+
return [];
38+
}
39+
3440
const baseQuery = {
3541
...queryingOptions,
3642
...preAggregation.references,
3743
timeDimensions: [{
3844
...preAggregation.references.timeDimensions[0],
39-
dateRange: [extractDate(startDate), extractDate(endDate)]
45+
dateRange
4046
}]
4147
};
4248
const partitionQuery = compilerApi.createQuery(compilers, dbType, baseQuery);

0 commit comments

Comments
 (0)