From df0f950c2a9c0461820c923a2493d6c2c8fe72e9 Mon Sep 17 00:00:00 2001 From: Thomas Desrosiers Date: Wed, 16 Feb 2022 20:42:11 -0800 Subject: [PATCH] Review: Pull out time_grain_expressoins into its own thing --- superset/db_engine_specs/databricks.py | 39 ++++++++++++++------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/superset/db_engine_specs/databricks.py b/superset/db_engine_specs/databricks.py index f519b77b685e7..f5f46bf491200 100644 --- a/superset/db_engine_specs/databricks.py +++ b/superset/db_engine_specs/databricks.py @@ -21,26 +21,32 @@ from superset.db_engine_specs.base import BaseEngineSpec from superset.db_engine_specs.hive import HiveEngineSpec +time_grain_expressions = { + None: "{col}", + "PT1S": "date_trunc('second', {col})", + "PT1M": "date_trunc('minute', {col})", + "PT1H": "date_trunc('hour', {col})", + "P1D": "date_trunc('day', {col})", + "P1W": "date_trunc('week', {col})", + "P1M": "date_trunc('month', {col})", + "P3M": "date_trunc('quarter', {col})", + "P1Y": "date_trunc('year', {col})", + "P1W/1970-01-03T00:00:00Z": ( + "date_trunc('week', {col} + interval '1 day') + interval '5 days'" + ), + "1969-12-28T00:00:00Z/P1W": ( + "date_trunc('week', {col} + interval '1 day') - interval '1 day'" + ), +} + class DatabricksHiveEngineSpec(HiveEngineSpec): engine = "databricks" engine_name = "Databricks Interactive Cluster" driver = "pyhive" _show_functions_column = "function" - - _time_grain_expressions = { - None: "{col}", - "PT1S": "date_trunc('second', {col})", - "PT1M": "date_trunc('minute', {col})", - "PT1H": "date_trunc('hour', {col})", - "P1D": "date_trunc('day', {col})", - "P1W": "date_trunc('week', {col})", - "P1M": "date_trunc('month', {col})", - "P3M": "date_trunc('quarter', {col})", - "P1Y": "date_trunc('year', {col})", - "P1W/1970-01-03T00:00:00Z": "date_trunc('week', {col} + interval '1 day') + interval '5 days'", - "1969-12-28T00:00:00Z/P1W": "date_trunc('week', {col} + interval '1 day') - interval '1 day'", - } + + _time_grain_expressions = time_grain_expressions class DatabricksODBCEngineSpec(BaseEngineSpec): @@ -48,10 +54,7 @@ class DatabricksODBCEngineSpec(BaseEngineSpec): engine_name = "Databricks SQL Endpoint" driver = "pyodbc" - # the syntax for the ODBC engine is identical to the Hive one, so - # we can reuse the expressions from `HiveEngineSpec` - # pylint: disable=protected-access - _time_grain_expressions = DatabricksHiveEngineSpec._time_grain_expressions + _time_grain_expressions = time_grain_expressions @classmethod def convert_dttm(