Skip to content

Commit 104f2d6

Browse files
committed
fix(schema-compiler): fix time dimension granularity origin formatting in local timezone (#9071)
* fix integration custom granularities tests * fix postgres drivers tests related to custom granularities * fix granularity origin formatting in local timezone * fix spelling # Conflicts: # packages/cubejs-schema-compiler/src/adapter/Granularity.ts
1 parent 6d30458 commit 104f2d6

File tree

8 files changed

+29
-13
lines changed

8 files changed

+29
-13
lines changed

.github/pull_request_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
**Check List**
2-
- [ ] Tests has been run in packages where changes made if available
2+
- [ ] Tests have been run in packages where changes made if available
33
- [ ] Linter has been run for changed code
44
- [ ] Tests for the changes have been added if not covered yet
55
- [ ] Docs have been added / updated if required

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2838,7 +2838,7 @@ export class BaseQuery {
28382838
return this.timeGroupedColumn(granularity.granularityFromInterval(), dimension);
28392839
}
28402840

2841-
return this.dateBin(granularity.granularityInterval, dimension, granularity.originFormatted());
2841+
return this.dateBin(granularity.granularityInterval, dimension, granularity.originLocalFormatted());
28422842
}
28432843

28442844
/**

packages/cubejs-schema-compiler/src/adapter/Granularity.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export class Granularity {
1313

1414
public readonly granularityInterval: string;
1515

16+
public readonly queryTimezone: string;
17+
1618
public readonly granularityOffset: string | undefined;
1719

1820
public readonly origin: moment.Moment;
@@ -25,7 +27,8 @@ export class Granularity {
2527
) {
2628
this.granularity = timeDimension.granularity;
2729
this.predefinedGranularity = isPredefinedGranularity(this.granularity);
28-
this.origin = moment.tz('UTC').startOf('year'); // Defaults to current year start
30+
this.queryTimezone = query.timezone;
31+
this.origin = moment.tz(query.timezone).startOf('year'); // Defaults to current year start
2932

3033
if (this.predefinedGranularity) {
3134
this.granularityInterval = `1 ${this.granularity}`;
@@ -55,8 +58,11 @@ export class Granularity {
5558
return this.predefinedGranularity;
5659
}
5760

58-
public originFormatted(): string {
59-
return this.origin.format('YYYY-MM-DDTHH:mm:ss.SSS');
61+
/**
62+
* @returns origin date string in Query timezone
63+
*/
64+
public originLocalFormatted(): string {
65+
return this.origin.tz(this.queryTimezone).format('YYYY-MM-DDTHH:mm:ss.SSS');
6066
}
6167

6268
public minGranularity(): string {

packages/cubejs-schema-compiler/test/integration/clickhouse/custom-granularities.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ describe('Custom Granularities', () => {
4141
granularities:
4242
- name: half_year
4343
interval: 6 months
44+
origin: '2024-01-01' # to keep tests stable across time (year change, etc)
4445
- name: half_year_by_1st_april
4546
interval: 6 months
46-
offset: 3 months
47+
#offset: 3 months
48+
origin: '2024-04-01' # to keep tests stable across time (year change, etc)
4749
- name: two_weeks_by_friday
4850
interval: 2 weeks
4951
origin: '2024-08-23'

packages/cubejs-schema-compiler/test/integration/mssql/custom-granularities.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ describe('Custom Granularities', () => {
3737
granularities:
3838
- name: half_year
3939
interval: 6 months
40+
origin: '2024-01-01' # to keep tests stable across time (year change, etc)
4041
- name: half_year_by_1st_april
4142
interval: 6 months
42-
offset: 3 months
43+
#offset: 3 months
44+
origin: '2024-04-01' # to keep tests stable across time (year change, etc)
4345
- name: two_weeks_by_friday
4446
interval: 2 weeks
4547
origin: '2024-08-23'

packages/cubejs-schema-compiler/test/integration/mysql/custom-granularities.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ describe('Custom Granularities', () => {
3737
granularities:
3838
- name: half_year
3939
interval: 6 months
40+
origin: '2024-01-01' # to keep tests stable across time (year change, etc)
4041
- name: half_year_by_1st_april
4142
interval: 6 months
42-
offset: 3 months
43+
#offset: 3 months
44+
origin: '2024-04-01' # to keep tests stable across time (year change, etc)
4345
- name: two_weeks_by_friday
4446
interval: 2 weeks
4547
origin: '2024-08-23'

packages/cubejs-schema-compiler/test/integration/postgres/custom-granularities.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ describe('Custom Granularities', () => {
3939
granularities:
4040
- name: half_year
4141
interval: 6 months
42+
origin: '2024-01-01' # to keep tests stable across time (year change, etc)
4243
- name: half_year_by_1st_april
4344
interval: 6 months
44-
offset: 3 months
45+
#offset: 3 months
46+
origin: '2024-04-01' # to keep tests stable across time (year change, etc)
4547
- name: two_weeks_by_friday
4648
interval: 2 weeks
4749
origin: '2024-08-23'

packages/cubejs-testing-drivers/fixtures/_schemas.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@
9292
"granularities": [
9393
{
9494
"name": "half_year",
95-
"interval": "6 months"
95+
"interval": "6 months",
96+
"origin": "2024-01-01"
9697
},
9798
{
9899
"name": "half_year_by_1st_april",
99100
"interval": "6 months",
100-
"offset": "3 months"
101+
"origin": "2024-04-01"
101102
},
102103
{
103104
"name": "two_mo_by_feb",
@@ -118,12 +119,13 @@
118119
"granularities": [
119120
{
120121
"name": "half_year",
121-
"interval": "6 months"
122+
"interval": "6 months",
123+
"origin": "2024-01-01"
122124
},
123125
{
124126
"name": "half_year_by_1st_april",
125127
"interval": "6 months",
126-
"offset": "3 months"
128+
"origin": "2024-04-01"
127129
},
128130
{
129131
"name": "two_mo_by_feb",

0 commit comments

Comments
 (0)