Skip to content

Commit 2a1152e

Browse files
committed
fix loadBuildRange() to use inDbTimeZone()
1 parent 685383f commit 2a1152e

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

packages/cubejs-backend-shared/src/time.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ export const BUILD_RANGE_START_LOCAL = '__BUILD_RANGE_START_LOCAL';
192192

193193
export const BUILD_RANGE_END_LOCAL = '__BUILD_RANGE_END_LOCAL';
194194

195-
export const inDbTimeZone = (timezone: string, timestampFormat: string, timestamp: string): string => {
195+
export const inDbTimeZone = (timezone: string, timestampFormat: string, timestamp: string): string | null => {
196+
if (!timestamp) {
197+
return null;
198+
}
196199
if (timestamp.length === 23 || timestamp.length === 26) {
197200
const zone = moment.tz.zone(timezone);
198201
if (!zone) {

packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationPartitionRangeLoader.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,9 @@ export class PreAggregationPartitionRangeLoader {
136136
return queryValues?.map(
137137
param => {
138138
if (param === BUILD_RANGE_START_LOCAL) {
139-
// buildRangeStart was already localized in loadBuildRange() but the preAggregation.timestampFormat
140-
// might be different from 'YYYY-MM-DDTHH:mm:ss.SSS' so we need to convert.
141-
// Same applies to buildRangeEnd 2 below.
142-
return utcToLocalTimeZoneInUtc('UTC', this.preAggregation.timestampFormat, buildRangeStart);
139+
return PreAggregationPartitionRangeLoader.inDbTimeZone(this.preAggregation, buildRangeStart);
143140
} else if (param === BUILD_RANGE_END_LOCAL) {
144-
return utcToLocalTimeZoneInUtc('UTC', this.preAggregation.timestampFormat, buildRangeEnd);
141+
return PreAggregationPartitionRangeLoader.inDbTimeZone(this.preAggregation, buildRangeEnd);
145142
} else {
146143
return param;
147144
}
@@ -410,7 +407,7 @@ export class PreAggregationPartitionRangeLoader {
410407
const { preAggregationStartEndQueries } = this.preAggregation;
411408
const [startDate, endDate] = await Promise.all(
412409
preAggregationStartEndQueries.map(
413-
async rangeQuery => utcToLocalTimeZoneInUtc(
410+
async rangeQuery => inDbTimeZone(
414411
this.preAggregation.timezone,
415412
'YYYY-MM-DDTHH:mm:ss.SSS',
416413
PreAggregationPartitionRangeLoader.extractDate(await this.loadRangeQuery(rangeQuery)),
@@ -430,7 +427,7 @@ export class PreAggregationPartitionRangeLoader {
430427
);
431428
const [rangeStart, rangeEnd] = await Promise.all(
432429
preAggregationStartEndQueries.map(
433-
async (rangeQuery, i) => utcToLocalTimeZoneInUtc(
430+
async (rangeQuery, i) => inDbTimeZone(
434431
this.preAggregation.timezone,
435432
'YYYY-MM-DDTHH:mm:ss.SSS',
436433
PreAggregationPartitionRangeLoader.extractDate(
@@ -445,7 +442,7 @@ export class PreAggregationPartitionRangeLoader {
445442
}
446443

447444
private now() {
448-
return utcToLocalTimeZoneInUtc(this.preAggregation.timezone, 'YYYY-MM-DDTHH:mm:ss.SSS', new Date().toJSON().substring(0, 23));
445+
return inDbTimeZone(this.preAggregation.timezone, 'YYYY-MM-DDTHH:mm:ss.SSS', new Date().toJSON().substring(0, 23));
449446
}
450447

451448
private orNowIfEmpty(dateRange: QueryDateRange): QueryDateRange {

packages/cubejs-server-core/test/unit/RefreshScheduler.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -698,63 +698,63 @@ describe('Refresh Scheduler', () => {
698698
{ tableName: 'stb_pre_aggregations.foo_second20201231', timezone: 'UTC', fromTable: 'foo_tenant1' },
699699
{ tableName: 'stb_pre_aggregations.bar_first20201231', timezone: 'UTC', fromTable: 'bar' },
700700
{
701-
tableName: 'stb_pre_aggregations.foo_first20201230',
701+
tableName: 'stb_pre_aggregations.foo_first20201231',
702702
timezone: 'America/Los_Angeles',
703703
fromTable: 'foo_tenant1',
704704
},
705-
{ tableName: 'stb_pre_aggregations.foo_orphaned20201230', timezone: 'America/Los_Angeles', fromTable: 'foo_tenant1' },
705+
{ tableName: 'stb_pre_aggregations.foo_orphaned20201231', timezone: 'America/Los_Angeles', fromTable: 'foo_tenant1' },
706706
{
707-
tableName: 'stb_pre_aggregations.foo_second20201230',
707+
tableName: 'stb_pre_aggregations.foo_second20201231',
708708
timezone: 'America/Los_Angeles',
709709
fromTable: 'foo_tenant1',
710710
},
711-
{ tableName: 'stb_pre_aggregations.bar_first20201230', timezone: 'America/Los_Angeles', fromTable: 'bar' },
711+
{ tableName: 'stb_pre_aggregations.bar_first20201231', timezone: 'America/Los_Angeles', fromTable: 'bar' },
712712

713713
{ tableName: 'stb_pre_aggregations.foo_first20201230', timezone: 'UTC', fromTable: 'foo_tenant1' },
714714
{ tableName: 'stb_pre_aggregations.foo_orphaned20201230', timezone: 'UTC', fromTable: 'foo_tenant1' },
715715
{ tableName: 'stb_pre_aggregations.foo_second20201230', timezone: 'UTC', fromTable: 'foo_tenant1' },
716716
{ tableName: 'stb_pre_aggregations.bar_first20201230', timezone: 'UTC', fromTable: 'bar' },
717717
{
718-
tableName: 'stb_pre_aggregations.foo_first20201229',
718+
tableName: 'stb_pre_aggregations.foo_first20201230',
719719
timezone: 'America/Los_Angeles',
720720
fromTable: 'foo_tenant1',
721721
},
722-
{ tableName: 'stb_pre_aggregations.foo_orphaned20201229', timezone: 'America/Los_Angeles', fromTable: 'foo_tenant1' },
722+
{ tableName: 'stb_pre_aggregations.foo_orphaned20201230', timezone: 'America/Los_Angeles', fromTable: 'foo_tenant1' },
723723
{
724-
tableName: 'stb_pre_aggregations.foo_second20201229',
724+
tableName: 'stb_pre_aggregations.foo_second20201230',
725725
timezone: 'America/Los_Angeles',
726726
fromTable: 'foo_tenant1',
727727
},
728-
{ tableName: 'stb_pre_aggregations.bar_first20201229', timezone: 'America/Los_Angeles', fromTable: 'bar' },
728+
{ tableName: 'stb_pre_aggregations.bar_first20201230', timezone: 'America/Los_Angeles', fromTable: 'bar' },
729729

730730
{ tableName: 'stb_pre_aggregations.foo_first20201229', timezone: 'UTC', fromTable: 'foo_tenant1' },
731731
{ tableName: 'stb_pre_aggregations.foo_orphaned20201229', timezone: 'UTC', fromTable: 'foo_tenant1' },
732732
{ tableName: 'stb_pre_aggregations.foo_second20201229', timezone: 'UTC', fromTable: 'foo_tenant1' },
733733
{ tableName: 'stb_pre_aggregations.bar_first20201229', timezone: 'UTC', fromTable: 'bar' },
734734
{
735-
tableName: 'stb_pre_aggregations.foo_first20201228',
735+
tableName: 'stb_pre_aggregations.foo_first20201229',
736736
timezone: 'America/Los_Angeles',
737737
fromTable: 'foo_tenant1',
738738
},
739-
{ tableName: 'stb_pre_aggregations.foo_orphaned20201228', timezone: 'America/Los_Angeles', fromTable: 'foo_tenant1' },
739+
{ tableName: 'stb_pre_aggregations.foo_orphaned20201229', timezone: 'America/Los_Angeles', fromTable: 'foo_tenant1' },
740740
{
741-
tableName: 'stb_pre_aggregations.foo_second20201228',
741+
tableName: 'stb_pre_aggregations.foo_second20201229',
742742
timezone: 'America/Los_Angeles',
743743
fromTable: 'foo_tenant1',
744744
},
745-
{ tableName: 'stb_pre_aggregations.bar_first20201228', timezone: 'America/Los_Angeles', fromTable: 'bar' },
745+
{ tableName: 'stb_pre_aggregations.bar_first20201229', timezone: 'America/Los_Angeles', fromTable: 'bar' },
746746

747747
{ tableName: 'stb_pre_aggregations.foo_first20201228', timezone: 'UTC', fromTable: 'foo_tenant1' },
748748
{ tableName: 'stb_pre_aggregations.foo_orphaned20201228', timezone: 'UTC', fromTable: 'foo_tenant1' },
749749
{ tableName: 'stb_pre_aggregations.foo_second20201228', timezone: 'UTC', fromTable: 'foo_tenant1' },
750750
{
751-
tableName: 'stb_pre_aggregations.foo_first20201227',
751+
tableName: 'stb_pre_aggregations.foo_first20201228',
752752
timezone: 'America/Los_Angeles',
753753
fromTable: 'foo_tenant1',
754754
},
755-
{ tableName: 'stb_pre_aggregations.foo_orphaned20201227', timezone: 'America/Los_Angeles', fromTable: 'foo_tenant1' },
755+
{ tableName: 'stb_pre_aggregations.foo_orphaned20201228', timezone: 'America/Los_Angeles', fromTable: 'foo_tenant1' },
756756
{
757-
tableName: 'stb_pre_aggregations.foo_second20201227',
757+
tableName: 'stb_pre_aggregations.foo_second20201228',
758758
timezone: 'America/Los_Angeles',
759759
fromTable: 'foo_tenant1',
760760
},
@@ -763,13 +763,13 @@ describe('Refresh Scheduler', () => {
763763
{ tableName: 'stb_pre_aggregations.foo_orphaned20201227', timezone: 'UTC', fromTable: 'foo_tenant1' },
764764
{ tableName: 'stb_pre_aggregations.foo_second20201227', timezone: 'UTC', fromTable: 'foo_tenant1' },
765765
{
766-
tableName: 'stb_pre_aggregations.foo_first20201226',
766+
tableName: 'stb_pre_aggregations.foo_first20201227',
767767
timezone: 'America/Los_Angeles',
768768
fromTable: 'foo_tenant1',
769769
},
770-
{ tableName: 'stb_pre_aggregations.foo_orphaned20201226', timezone: 'America/Los_Angeles', fromTable: 'foo_tenant1' },
770+
{ tableName: 'stb_pre_aggregations.foo_orphaned20201227', timezone: 'America/Los_Angeles', fromTable: 'foo_tenant1' },
771771
{
772-
tableName: 'stb_pre_aggregations.foo_second20201226',
772+
tableName: 'stb_pre_aggregations.foo_second20201227',
773773
timezone: 'America/Los_Angeles',
774774
fromTable: 'foo_tenant1',
775775
},

0 commit comments

Comments
 (0)