From 7e99d768af2e60eeb4cba80509196fcd9e38340d Mon Sep 17 00:00:00 2001 From: Ville Brofeldt <33317356+villebro@users.noreply.github.com> Date: Tue, 19 Jan 2021 14:32:33 +0200 Subject: [PATCH] feat(bigquery): implement custom minute time grains (#12581) * feat(bigquery): implement custom minute time grains * address review comment --- superset/db_engine_specs/base.py | 4 ++++ superset/db_engine_specs/bigquery.py | 12 ++++++++++++ tests/db_engine_specs/bigquery_tests.py | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/superset/db_engine_specs/base.py b/superset/db_engine_specs/base.py index c1240c5c7..2a29c9517 100644 --- a/superset/db_engine_specs/base.py +++ b/superset/db_engine_specs/base.py @@ -245,6 +245,10 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods date_trunc_function = cls._date_trunc_functions.get(type_) if date_trunc_function: time_expr = time_expr.replace("{func}", date_trunc_function) + if type_ and "{type}" in time_expr: + date_trunc_function = cls._date_trunc_functions.get(type_) + if date_trunc_function: + time_expr = time_expr.replace("{type}", type_) else: time_expr = "{col}" diff --git a/superset/db_engine_specs/bigquery.py b/superset/db_engine_specs/bigquery.py index 574a439cb..621525060 100644 --- a/superset/db_engine_specs/bigquery.py +++ b/superset/db_engine_specs/bigquery.py @@ -66,6 +66,18 @@ class BigQueryEngineSpec(BaseEngineSpec): None: "{col}", "PT1S": "{func}({col}, SECOND)", "PT1M": "{func}({col}, MINUTE)", + "PT5M": "CAST(TIMESTAMP_SECONDS(" + "5*60 * DIV(UNIX_SECONDS(CAST({col} AS TIMESTAMP)), 5*60)" + ") AS {type})", + "PT10M": "CAST(TIMESTAMP_SECONDS(" + "10*60 * DIV(UNIX_SECONDS(CAST({col} AS TIMESTAMP)), 10*60)" + ") AS {type})", + "PT15M": "CAST(TIMESTAMP_SECONDS(" + "15*60 * DIV(UNIX_SECONDS(CAST({col} AS TIMESTAMP)), 15*60)" + ") AS {type})", + "PT0.5H": "CAST(TIMESTAMP_SECONDS(" + "30*60 * DIV(UNIX_SECONDS(CAST({col} AS TIMESTAMP)), 30*60)" + ") AS {type})", "PT1H": "{func}({col}, HOUR)", "P1D": "{func}({col}, DAY)", "P1W": "{func}({col}, WEEK)", diff --git a/tests/db_engine_specs/bigquery_tests.py b/tests/db_engine_specs/bigquery_tests.py index 8e9ae7d0a..37e95e95e 100644 --- a/tests/db_engine_specs/bigquery_tests.py +++ b/tests/db_engine_specs/bigquery_tests.py @@ -74,6 +74,28 @@ class TestBigQueryDbEngineSpec(TestDbEngineSpec): ) self.assertEqual(str(actual), expected) + def test_custom_minute_timegrain_expressions(self): + """ + DB Eng Specs (bigquery): Test time grain expressions + """ + col = column("temporal") + test_cases = { + "DATE": "CAST(TIMESTAMP_SECONDS(" + "5*60 * DIV(UNIX_SECONDS(CAST(temporal AS TIMESTAMP)), 5*60)" + ") AS DATE)", + "DATETIME": "CAST(TIMESTAMP_SECONDS(" + "5*60 * DIV(UNIX_SECONDS(CAST(temporal AS TIMESTAMP)), 5*60)" + ") AS DATETIME)", + "TIMESTAMP": "CAST(TIMESTAMP_SECONDS(" + "5*60 * DIV(UNIX_SECONDS(CAST(temporal AS TIMESTAMP)), 5*60)" + ") AS TIMESTAMP)", + } + for type_, expected in test_cases.items(): + actual = BigQueryEngineSpec.get_timestamp_expr( + col=col, pdf=None, time_grain="PT5M", type_=type_ + ) + assert str(actual) == expected + def test_fetch_data(self): """ DB Eng Specs (bigquery): Test fetch data