From 2425b8f6142e9c543351a2c08fc4fc1e90d0dc4d Mon Sep 17 00:00:00 2001 From: George Ke Date: Fri, 22 Jul 2016 12:10:05 -0700 Subject: [PATCH] [presto] Smaller granularity units; fallback support for H:m:s (#731) * [presto] Smaller granularity units; fallback support for H:m:s * break lines >90 * cast to DATETIME instead --- caravel/models.py | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/caravel/models.py b/caravel/models.py index ac0083f535..444a4da6b7 100644 --- a/caravel/models.py +++ b/caravel/models.py @@ -404,13 +404,26 @@ class Database(Model, AuditMixinNullable): db_time_grains = { 'presto': ( Grain('Time Column', _('Time Column'), '{col}'), - Grain('week', _('week'), "date_trunc('week', CAST({col} AS DATE))"), - Grain('month', _('month'), "date_trunc('month', CAST({col} AS DATE))"), - Grain('quarter', _('quarter'), "date_trunc('quarter', CAST({col} AS DATE))"), - Grain("week_ending_saturday", _('week_ending_saturday'), "date_add('day', 5, " - "date_trunc('week', date_add('day', 1, CAST({col} AS DATE))))"), - Grain("week_start_sunday", _('week_start_sunday'), "date_add('day', -1, " - "date_trunc('week', date_add('day', 1, CAST({col} AS DATE))))") + Grain('second', _('second'), + "date_trunc('second', CAST({col} AS TIMESTAMP))"), + Grain('minute', _('minute'), + "date_trunc('minute', CAST({col} AS TIMESTAMP))"), + Grain('hour', _('hour'), + "date_trunc('hour', CAST({col} AS TIMESTAMP))"), + Grain('day', _('day'), + "date_trunc('day', CAST({col} AS TIMESTAMP))"), + Grain('week', _('week'), + "date_trunc('week', CAST({col} AS TIMESTAMP))"), + Grain('month', _('month'), + "date_trunc('month', CAST({col} AS TIMESTAMP))"), + Grain('quarter', _('quarter'), + "date_trunc('quarter', CAST({col} AS TIMESTAMP))"), + Grain("week_ending_saturday", _('week_ending_saturday'), + "date_add('day', 5, date_trunc('week', date_add('day', 1, " + "CAST({col} AS TIMESTAMP))))"), + Grain("week_start_sunday", _('week_start_sunday'), + "date_add('day', -1, date_trunc('week', " + "date_add('day', 1, CAST({col} AS TIMESTAMP))))"), ), 'mysql': ( Grain('Time Column', _('Time Column'), '{col}'), @@ -430,8 +443,10 @@ class Database(Model, AuditMixinNullable): 'sqlite': ( Grain('Time Column', _('Time Column'), '{col}'), Grain('day', _('day'), 'DATE({col})'), - Grain("week", _('week'), "DATE({col}, -strftime('%w', {col}) || ' days')"), - Grain("month", _('month'), "DATE({col}, -strftime('%d', {col}) || ' days')"), + Grain("week", _('week'), + "DATE({col}, -strftime('%w', {col}) || ' days')"), + Grain("month", _('month'), + "DATE({col}, -strftime('%d', {col}) || ' days')"), ), 'postgresql': ( Grain("Time Column", _('Time Column'), "{col}"), @@ -944,11 +959,11 @@ class TableColumn(Model, AuditMixinNullable): def dttm_sql_literal(self, dttm): """Convert datetime object to string - If datebase_expression is empty, the internal dttm + If database_expression is empty, the internal dttm will be parsed as the string with the pattern that - user input (python_date_format) + the user inputted (python_date_format) If database_expression is not empty, the internal dttm - will be parsed as the sql sentence for datebase to convert + will be parsed as the sql sentence for the database to convert """ tf = self.python_date_format or '%Y-%m-%d %H:%M:%S.%f' if self.database_expression: