Skip to content

Commit 5a540db

Browse files
feat(schema-compiler): Add flag for using named timezones in MySQL Query class (#9111)
* Remove moment-timezone from vertica driver * Remove moment-timezone from druid driver * Remove moment-timezone from dremio driver * Remove moment-timezone from cubestore driver * update moment-timezone in api-gateway * update moment-timezone in backend-shared * Remove moment-timezone from query-orchestrator * update moment-timezone in schema-compiler * remove unused * remove moment.HTML5_FMT.DATETIME_LOCAL_MS in favor of just literal * linter fix * add mysqlUseNamedTimezones flag * fix dremio test * Tiny edits --------- Co-authored-by: Igor Lukanin <[email protected]>
1 parent e7fd576 commit 5a540db

File tree

19 files changed

+90
-36
lines changed

19 files changed

+90
-36
lines changed

docs/pages/product/configuration/data-sources/mysql.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ CUBEJS_DB_PASS=**********
3838
| `CUBEJS_DB_SSL` | If `true`, enables SSL encryption for database connections from Cube | `true`, `false` ||
3939
| `CUBEJS_CONCURRENCY` | The number of concurrent connections each queue has to the database. Default is `2` | A valid number ||
4040
| `CUBEJS_DB_MAX_POOL` | The maximum number of concurrent database connections to pool. Default is `8` | A valid number ||
41+
| `CUBEJS_DB_MYSQL_USE_NAMED_TIMEZONES` | The flag to use time zone names or numeric offsets for time zone conversion. Default is `false` | `true`, `false` ||
4142

4243
## Pre-Aggregation Feature Support
4344

docs/pages/reference/configuration/environment-variables.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,17 @@ The cluster name to use when connecting to [Materialize](/product/configuration/
663663
| --------------------------------------------------------- | ---------------------- | --------------------- |
664664
| A valid Materialize cluster name | N/A | N/A |
665665

666+
## `CUBEJS_DB_MYSQL_USE_NAMED_TIMEZONES`
667+
668+
This flag controls how time zones conversion is done in the generated SQL for MySQL:
669+
- If it is set to `true`, time zone names are used. In this case, your MySQL server needs
670+
to be [configured][mysql-server-tz-support] properly.
671+
- If it is set to `false`, numeric offsets are used instead.
672+
673+
| Possible Values | Default in Development | Default in Production |
674+
| --------------- | ---------------------- | --------------------- |
675+
| `true`, `false` | `false` | `false` |
676+
666677
## `CUBEJS_DB_SNOWFLAKE_ACCOUNT`
667678

668679
The Snowflake account identifier to use when connecting to the database.
@@ -1612,3 +1623,4 @@ The port for a Cube deployment to listen to API connections on.
16121623
[ref-sql-api]: /product/apis-integrations/sql-api
16131624
[ref-sql-api-streaming]: /product/apis-integrations/sql-api#streaming
16141625
[ref-row-limit]: /product/apis-integrations/queries#row-limit
1626+
[mysql-server-tz-support]: https://dev.mysql.com/doc/refman/8.4/en/time-zone-support.html

packages/cubejs-api-gateway/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"jsonwebtoken": "^9.0.2",
4444
"jwk-to-pem": "^2.0.4",
4545
"moment": "^2.24.0",
46-
"moment-timezone": "^0.5.27",
46+
"moment-timezone": "^0.5.46",
4747
"nexus": "^1.1.0",
4848
"node-fetch": "^2.6.1",
4949
"ramda": "^0.27.0",

packages/cubejs-backend-shared/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"fs-extra": "^9.1.0",
4545
"http-proxy-agent": "^4.0.1",
4646
"moment-range": "^4.0.1",
47-
"moment-timezone": "^0.5.33",
47+
"moment-timezone": "^0.5.46",
4848
"node-fetch": "^2.6.1",
4949
"shelljs": "^0.8.5",
5050
"throttle-debounce": "^3.0.1",

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,44 @@ const variables: Record<string, (...args: any) => any> = {
866866
return undefined;
867867
},
868868

869+
/** ****************************************************************
870+
* MySQL Driver *
871+
***************************************************************** */
872+
873+
/**
874+
* Use timezone names for date/time conversions.
875+
* Defaults to FALSE, meaning that numeric offsets for timezone will be used.
876+
* @see https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_convert-tz
877+
* @see https://dev.mysql.com/doc/refman/8.4/en/time-zone-support.html
878+
*/
879+
mysqlUseNamedTimezones: ({ dataSource }: { dataSource: string }) => {
880+
const val = process.env[
881+
keyByDataSource(
882+
'CUBEJS_DB_MYSQL_USE_NAMED_TIMEZONES',
883+
dataSource,
884+
)
885+
];
886+
887+
if (val) {
888+
if (val.toLocaleLowerCase() === 'true') {
889+
return true;
890+
} else if (val.toLowerCase() === 'false') {
891+
return false;
892+
} else {
893+
throw new TypeError(
894+
`The ${
895+
keyByDataSource(
896+
'CUBEJS_DB_MYSQL_USE_NAMED_TIMEZONES',
897+
dataSource,
898+
)
899+
} must be either 'true' or 'false'.`
900+
);
901+
}
902+
} else {
903+
return false;
904+
}
905+
},
906+
869907
/** ****************************************************************
870908
* Databricks Driver *
871909
***************************************************************** */

packages/cubejs-cubestore-driver/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"flatbuffers": "23.3.3",
3434
"fs-extra": "^9.1.0",
3535
"generic-pool": "^3.6.0",
36-
"moment-timezone": "^0.5.31",
3736
"node-fetch": "^2.6.1",
3837
"sqlstring": "^2.3.3",
3938
"tempy": "^1.0.1",

packages/cubejs-dremio-driver/driver/DremioQuery.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const moment = require('moment-timezone');
21
const { BaseFilter, BaseQuery } = require('@cubejs-backend/schema-compiler');
32

43
const GRANULARITY_TO_INTERVAL = {
@@ -36,17 +35,23 @@ class DremioQuery extends BaseQuery {
3635
return new DremioFilter(this, filter);
3736
}
3837

38+
/**
39+
* CONVERT_TIMEZONE([sourceTimezone string], destinationTimezone string,
40+
* timestamp date, timestamp, or string in ISO 8601 format) → timestamp
41+
* sourceTimezone (optional): The time zone of the timestamp. If you omit this parameter,
42+
* Dremio assumes that the source time zone is UTC.
43+
* @see https://docs.dremio.com/cloud/reference/sql/sql-functions/functions/CONVERT_TIMEZONE/
44+
*/
3945
convertTz(field) {
40-
const targetTZ = moment().tz(this.timezone).format('Z');
41-
return `CONVERT_TIMEZONE('${targetTZ}', ${field})`;
46+
return `CONVERT_TIMEZONE('${this.timezone}', ${field})`;
4247
}
4348

4449
timeStampCast(value) {
4550
return `TO_TIMESTAMP(${value}, 'YYYY-MM-DD"T"HH24:MI:SS.FFF')`;
4651
}
4752

4853
timestampFormat() {
49-
return moment.HTML5_FMT.DATETIME_LOCAL_MS;
54+
return 'YYYY-MM-DDTHH:mm:ss.SSS';
5055
}
5156

5257
dateTimeCast(value) {

packages/cubejs-dremio-driver/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"@cubejs-backend/schema-compiler": "1.1.16",
2727
"@cubejs-backend/shared": "1.1.12",
2828
"axios": "^0.21.1",
29-
"moment-timezone": "^0.5.31",
3029
"sqlstring": "^2.3.1"
3130
},
3231
"devDependencies": {

packages/cubejs-dremio-driver/test/DremioQuery.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ cube(\`sales\`, {
6363
const queryAndParams = query.buildSqlAndParams();
6464

6565
expect(queryAndParams[0]).toContain(
66-
'DATE_TRUNC(\'day\', CONVERT_TIMEZONE(\'-08:00\', "sales".sales_datetime))'
66+
'DATE_TRUNC(\'day\', CONVERT_TIMEZONE(\'America/Los_Angeles\', "sales".sales_datetime))'
6767
);
6868
}));
6969

packages/cubejs-druid-driver/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
"@cubejs-backend/base-driver": "1.1.16",
3232
"@cubejs-backend/schema-compiler": "1.1.16",
3333
"@cubejs-backend/shared": "1.1.12",
34-
"axios": "^0.21.1",
35-
"moment-timezone": "^0.5.31"
34+
"axios": "^0.21.1"
3635
},
3736
"devDependencies": {
3837
"@cubejs-backend/linter": "^1.0.0",

0 commit comments

Comments
 (0)