Skip to content

Commit

Permalink
fix(mssql): week time grain should respect datefirst setting (#10811)
Browse files Browse the repository at this point in the history
* "P1W" grain should respect DATEFIRST setting in MS SQL Server

* Added "week_start_sunday" and "week_start_monday" grains support. Adjusted the "week" grain for better backward compatibility with MS SQL 2005/2008.

* Stylistic and linter-requested changes

* fix test

Co-authored-by: Valeriy Aleksashkin <v.aleksashkin@gmail.com>
Co-authored-by: Ville Brofeldt <ville.v.brofeldt@gmail.com>
  • Loading branch information
3 people committed Nov 16, 2021
1 parent 3ee9e11 commit 211b32a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
29 changes: 17 additions & 12 deletions superset/db_engine_specs/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,23 @@ class MssqlEngineSpec(BaseEngineSpec):

_time_grain_expressions = {
None: "{col}",
"PT1S": "DATEADD(second, DATEDIFF(second, '2000-01-01', {col}), '2000-01-01')",
"PT1M": "DATEADD(minute, DATEDIFF(minute, 0, {col}), 0)",
"PT5M": "DATEADD(minute, DATEDIFF(minute, 0, {col}) / 5 * 5, 0)",
"PT10M": "DATEADD(minute, DATEDIFF(minute, 0, {col}) / 10 * 10, 0)",
"PT15M": "DATEADD(minute, DATEDIFF(minute, 0, {col}) / 15 * 15, 0)",
"PT30M": "DATEADD(minute, DATEDIFF(minute, 0, {col}) / 30 * 30, 0)",
"PT1H": "DATEADD(hour, DATEDIFF(hour, 0, {col}), 0)",
"P1D": "DATEADD(day, DATEDIFF(day, 0, {col}), 0)",
"P1W": "DATEADD(week, DATEDIFF(week, 0, {col}), 0)",
"P1M": "DATEADD(month, DATEDIFF(month, 0, {col}), 0)",
"P3M": "DATEADD(quarter, DATEDIFF(quarter, 0, {col}), 0)",
"P1Y": "DATEADD(year, DATEDIFF(year, 0, {col}), 0)",
"PT1S": "DATEADD(SECOND, DATEDIFF(SECOND, '2000-01-01', {col}), '2000-01-01')",
"PT1M": "DATEADD(MINUTE, DATEDIFF(MINUTE, 0, {col}), 0)",
"PT5M": "DATEADD(MINUTE, DATEDIFF(MINUTE, 0, {col}) / 5 * 5, 0)",
"PT10M": "DATEADD(MINUTE, DATEDIFF(MINUTE, 0, {col}) / 10 * 10, 0)",
"PT15M": "DATEADD(MINUTE, DATEDIFF(MINUTE, 0, {col}) / 15 * 15, 0)",
"PT30M": "DATEADD(MINUTE, DATEDIFF(MINUTE, 0, {col}) / 30 * 30, 0)",
"PT1H": "DATEADD(HOUR, DATEDIFF(HOUR, 0, {col}), 0)",
"P1D": "DATEADD(DAY, DATEDIFF(DAY, 0, {col}), 0)",
"P1W": "DATEADD(DAY, 1 - DATEPART(WEEKDAY, {col}),"
" DATEADD(DAY, DATEDIFF(DAY, 0, {col}), 0))",
"P1M": "DATEADD(MONTH, DATEDIFF(MONTH, 0, {col}), 0)",
"P3M": "DATEADD(QUARTER, DATEDIFF(QUARTER, 0, {col}), 0)",
"P1Y": "DATEADD(YEAR, DATEDIFF(YEAR, 0, {col}), 0)",
"1969-12-28T00:00:00Z/P1W": "DATEADD(DAY, -1,"
" DATEADD(WEEK, DATEDIFF(WEEK, 0, {col}), 0))",
"1969-12-29T00:00:00Z/P1W": "DATEADD(WEEK,"
" DATEDIFF(WEEK, 0, DATEADD(DAY, -1, {col})), 0)",
}

custom_errors: Dict[Pattern[str], Tuple[str, SupersetErrorType, Dict[str, Any]]] = {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/db_engine_specs/mssql_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_time_exp_mixd_case_col_1y(self):
col = column("MixedCase")
expr = MssqlEngineSpec.get_timestamp_expr(col, None, "P1Y")
result = str(expr.compile(None, dialect=mssql.dialect()))
self.assertEqual(result, "DATEADD(year, DATEDIFF(year, 0, [MixedCase]), 0)")
self.assertEqual(result, "DATEADD(YEAR, DATEDIFF(YEAR, 0, [MixedCase]), 0)")

def test_convert_dttm(self):
dttm = self.get_dttm()
Expand Down

0 comments on commit 211b32a

Please sign in to comment.