From b6edb96c3867a808c8ca51269f23ab5e0bd34100 Mon Sep 17 00:00:00 2001 From: Luca Albertalli Date: Fri, 23 Sep 2016 17:33:56 -0700 Subject: [PATCH 1/2] Made time grain configurable --- caravel/config.py | 19 +++++++++++++++++++ caravel/models.py | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/caravel/config.py b/caravel/config.py index 267a7777333a9..3c6a60b9fc574 100644 --- a/caravel/config.py +++ b/caravel/config.py @@ -235,6 +235,25 @@ class CeleryConfig(object): # Timeout duration for SQL Lab synchronous queries SQLLAB_TIMEOUT = 30 +# --------------------------------------------------- +# Configure Caravel to use a custom Time Grain for Databases +# --------------------------------------------------- + +# DB_CUSTOM_GRAIN = ( +# ('vertica', ( +# ("Time Column", 'Time Column', "{col}"), +# ("second", 'second', "DATE_TRUNC('second', {col})"), +# ("minute", 'minute', "DATE_TRUNC('minute', {col})"), +# ("hour", 'hour', "DATE_TRUNC('hour', {col})"), +# ("day", 'day', "DATE_TRUNC('day', {col})"), +# ("week", 'week', "DATE({col}) - DAYOFWEEK(DATE({col})) + 1"), +# ("month", 'month', "DATE_TRUNC('month', {col})"), +# ("year", 'year', "DATE_TRUNC('year', {col})"), +# )), +# ) + +DB_CUSTOM_GRAIN = None + try: from caravel_config import * # noqa except ImportError: diff --git a/caravel/models.py b/caravel/models.py index 5f7414ed617e3..9980fa07ed7a8 100644 --- a/caravel/models.py +++ b/caravel/models.py @@ -594,6 +594,13 @@ def grains(self): } db_time_grains['redshift'] = db_time_grains['postgresql'] db_time_grains['vertica'] = db_time_grains['postgresql'] + custom_grain = config.get('DB_CUSTOM_GRAIN') + if custom_grain: + for db, grains in custom_grain: + db_time_grains[db] = tuple( + Grain(name, label, function) + for name, label, function in grains) + for db_type, grains in db_time_grains.items(): if self.sqlalchemy_uri.startswith(db_type): return grains From e4a6e8a60e6681d4277c71e8463ef98586424a98 Mon Sep 17 00:00:00 2001 From: Luca Albertalli Date: Fri, 23 Sep 2016 17:47:32 -0700 Subject: [PATCH 2/2] Make the Database Time Grain configurable. This will make easier to support additional database supported by SQLAlchemy as well as adding additional time grains (e.g: the problem raised in issue https://github.com/airbnb/caravel/issues/971 --- caravel/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/caravel/config.py b/caravel/config.py index 3c6a60b9fc574..e8a5af123f2ab 100644 --- a/caravel/config.py +++ b/caravel/config.py @@ -240,7 +240,7 @@ class CeleryConfig(object): # --------------------------------------------------- # DB_CUSTOM_GRAIN = ( -# ('vertica', ( +# ('postgresql', ( # ("Time Column", 'Time Column', "{col}"), # ("second", 'second', "DATE_TRUNC('second', {col})"), # ("minute", 'minute', "DATE_TRUNC('minute', {col})"),