cube-js / cube-js/cube

Timezone calculations for MSSQL incorrect/not applied

Open
#5,133 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
20.8k
Forks
2.1k
Avg merge
1d 2h
Merged PRs (30d)
181

Description

**Describe the bug**
When trying to query data including a timezone and a timeDimension with mssql the timezone is not applied to the timeDimension.

**To Reproduce**
Query:
```json
{
"measures": [
"CrossingEvents.count"
],
"timeDimensions": [
{
"dimension": "CrossingEvents.timestamp",
"granularity": "hour",
"dateRange": [
"2022-08-18T00:00:00.000",
"2022-08-18T23:59:59.999"
]
}
],
"timezone": "Europe/Vienna"
}
```
Cube:
```javascript
cube(`CrossingEvents`, {
title: `Counting Line`,

sql: `SELECT CAST('2022-08-18T15:00:00.000Z' as DATETIME2) as timestamp,
1 as lineId
`,

measures: {
count: {
type: `count`,
title: `Count`,
description: `Count`
}
},

dimensions: {
lineId: {
title: `ID`,
description: `Counting Line ID`,
sql: `lineId`,
type: `string`
},
timestamp: {
title: `Timestamp`,
description: `Time of event generation`,
sql: `timestamp`,
type: `time`
},
}
});
```

**Expected behavior**

Timestamp for timeDimension fields should be converted according to timezone. For the example above I would expect data in response to be
```json
[
{
"CrossingEvents.timestamp.hour": "2022-08-18T17:00:00.000",
"CrossingEvents.timestamp": "2022-08-18T17:00:00.000",
"CrossingEvents.count": 1
}
]
```
but is
```json
[
{
"CrossingEvents.timestamp.hour": "2022-08-18T15:00:00.000",
"CrossingEvents.timestamp": "2022-08-18T15:00:00.000",
"CrossingEvents.count": 1
}
]
```
instead

**Version:**
Tested with 0.30.53, 0.29.48, 0.26.45

**Additional context**
Resulting Query for mssql is something like:
```sql
SELECT TOP 10000
dateadd(hour, DATEDIFF(hour, 0, TODATETIMEOFFSET("crossing_events".timestamp, '+02:00')), 0) "crossing_events__timestamp_hour", count(*) "crossing_events__count"
FROM
(SELECT timestamp,
lineId
FROM test.table_test (NOLOCK) WHERE lineId IS NOT NULL
) AS "crossing_events" WHERE ("crossing_events".timestamp >= CAST(@_1 AS DATETIME2) AND "crossing_events".timestamp <= CAST(@_2 AS DATETIME2)) AND ("crossing_events".lineId = @_3) GROUP BY dateadd(hour, DATEDIFF(hour, 0, TODATETIMEOFFSET("crossing_events".timestamp, '+02:00')), 0) ORDER BY 1 ASC
```
but the dateadd-part does not apply timezone correctly. E.g:
```sql
SELECT dateadd(hour, DATEDIFF(hour, 0, TODATETIMEOFFSET(CAST('2022-08-17T15:00:00.000Z' AS DATETIME2), '+02:00')), 0)
```
will return '2022-08-17T15:00:00.000' instead the likely intended '2022-08-17T17:00:00.000'

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.