From 41c77668e2e8a3ba0159852f3681dbe04d2be75a Mon Sep 17 00:00:00 2001 From: prihoda Date: Fri, 8 Apr 2016 14:53:50 +0200 Subject: [PATCH 1/2] Fix week and month Time grain in MySQL With mysql datetime and timestamp columns, currently the Time grain "week" and "month" options don't remove the time part. This results in groupings like this: timestamp count 2015-04-05 07:00:00 1 2015-04-05 10:00:00 1 2015-04-05 11:00:00 2 2015-04-05 11:50:00 1 2015-04-05 12:00:00 5 2015-04-05 14:20:00 1 2015-04-05 14:30:00 1 and so on. This is solved by wrapping the DATE_SUB with DATE(). --- caravel/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/caravel/models.py b/caravel/models.py index e8973c489160..eace24d3057d 100644 --- a/caravel/models.py +++ b/caravel/models.py @@ -321,8 +321,8 @@ def grains(self): 'mysql': ( Grain('Time Column', '{col}'), Grain('day', 'DATE({col})'), - Grain('week', 'DATE_SUB({col}, INTERVAL DAYOFWEEK({col}) - 1 DAY)'), - Grain('month', 'DATE_SUB({col}, INTERVAL DAYOFMONTH({col}) - 1 DAY)'), + Grain('week', 'DATE(DATE_SUB({col}, INTERVAL DAYOFWEEK({col}) - 1 DAY))'), + Grain('month', 'DATE(DATE_SUB({col}, INTERVAL DAYOFMONTH({col}) - 1 DAY))'), ), } for db_type, grains in db_time_grains.items(): From 37e72f3833c9353ad5eead84d846ba3adccda614 Mon Sep 17 00:00:00 2001 From: prihoda Date: Fri, 8 Apr 2016 15:17:41 +0200 Subject: [PATCH 2/2] Keeping the 90 character line limit Splitting the line with MySQL Time Grain --- caravel/models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/caravel/models.py b/caravel/models.py index eace24d3057d..6ae1e31d50c5 100644 --- a/caravel/models.py +++ b/caravel/models.py @@ -321,8 +321,10 @@ def grains(self): 'mysql': ( Grain('Time Column', '{col}'), Grain('day', 'DATE({col})'), - Grain('week', 'DATE(DATE_SUB({col}, INTERVAL DAYOFWEEK({col}) - 1 DAY))'), - Grain('month', 'DATE(DATE_SUB({col}, INTERVAL DAYOFMONTH({col}) - 1 DAY))'), + Grain("week", "DATE(DATE_SUB({col}, " + "INTERVAL DAYOFWEEK({col}) - 1 DAY))"), + Grain("month", "DATE(DATE_SUB({col}, " + "INTERVAL DAYOFMONTH({col}) - 1 DAY))"), ), } for db_type, grains in db_time_grains.items():