Skip to content

Commit

Permalink
fix: Querying empty Postgres table with 'time' dimension in a cube re…
Browse files Browse the repository at this point in the history
…sults in null value

Fixes #639
  • Loading branch information
paveltiunov committed Jun 29, 2020
1 parent 6d0096e commit 07d00f8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
52 changes: 52 additions & 0 deletions packages/cubejs-schema-compiler/test/PreAggregationsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ describe('PreAggregations', function test() {
incremental: true,
updateWindow: '1 day'
}
},
emptyPartitioned: {
type: 'rollup',
measureReferences: [count],
timeDimensionReference: EmptyHourVisitors.createdAt,
granularity: 'hour',
partitionGranularity: 'month',
scheduledRefresh: true,
refreshKey: {
every: '1 hour',
incremental: true,
updateWindow: '1 day'
}
}
}
})
Expand Down Expand Up @@ -245,6 +258,11 @@ describe('PreAggregations', function test() {
}
}
})
cube('EmptyHourVisitors', {
extends: EveryHourVisitors,
sql: \`select v.* from \${visitors.sql()} v where created_at < '2000-01-01'\`
})
`);

function replaceTableName(query, preAggregation, suffix) {
Expand Down Expand Up @@ -536,6 +554,40 @@ describe('PreAggregations', function test() {
});
});

it('empty scheduled refresh', () => {
return compiler.compile().then(async () => {
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: [
'visitor_checkins.count'
],
timeDimensions: [{
dimension: 'EmptyHourVisitors.createdAt',
granularity: 'hour',
dateRange: ['2017-01-01', '2017-01-25']
}],
timezone: 'UTC',
order: [{
id: 'EmptyHourVisitors.createdAt'
}],
preAggregationsSchema: ''
});

const preAggregations = cubeEvaluator.scheduledPreAggregations();
const partitionedPreAgg =
preAggregations.find(p => p.preAggregationName === 'emptyPartitioned' && p.cube === 'visitor_checkins');

const minMaxQueries = query.preAggregationStartEndQueries('visitor_checkins', partitionedPreAgg.preAggregation);

console.log(minMaxQueries);

const res = await dbRunner.testQueries(minMaxQueries);

res.should.be.deepEqual(
[{ max: null }]
);
});
});

it('mutable partition default refreshKey', () => {
return compiler.compile().then(() => {
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
Expand Down
8 changes: 7 additions & 1 deletion packages/cubejs-server-core/core/RefreshScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ class RefreshScheduler {
return data[0] && data[0][Object.keys(data[0])[0]];
};

const dateRange = [extractDate(startDate), extractDate(endDate)];
if (!dateRange[0] || !dateRange[1]) {
// Empty table. Nothing to refresh.
return [];
}

const baseQuery = {
...queryingOptions,
...preAggregation.references,
timeDimensions: [{
...preAggregation.references.timeDimensions[0],
dateRange: [extractDate(startDate), extractDate(endDate)]
dateRange
}]
};
const partitionQuery = compilerApi.createQuery(compilers, dbType, baseQuery);
Expand Down

1 comment on commit 07d00f8

@SamShiSS
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for fixing this!

Please sign in to comment.