diff --git a/TODO.md b/TODO.md index 1e2ab02facd13..042531cdbef37 100644 --- a/TODO.md +++ b/TODO.md @@ -33,7 +33,6 @@ List of TODO items for Caravel * CREATE VIEW button from SQL editor * Test button for when editing SQL expression * Slider form element -* datasource in explore mode could be a dropdown * [druid] Allow for post aggregations (ratios!) * in/notin filters autocomplete (druid) diff --git a/caravel/models.py b/caravel/models.py index 099784e8eeb80..c3778fcce3db7 100644 --- a/caravel/models.py +++ b/caravel/models.py @@ -410,6 +410,24 @@ def grains(self): if self.sqlalchemy_uri.startswith(db_type): return grains + def dttm_converter(self, dttm): + """Returns a string that the database flavor understands as a date""" + default = "'{}'".format(dttm.strftime('%Y-%m-%d %H:%M:%S.%f')) + iso = dttm.isoformat() + d = { + 'mssql': "CONVERT(DATETIME, '{}', 126)".format(iso), #untested + 'mysql': default, + 'oracle': + """TO_TIMESTAMP('{}', 'YYYY-MM-DD"T"HH24:MI:SS.ff6')""".format( + dttm.isoformat()), + 'presto': default, + 'sqlite': default, + } + for k, v in d.items(): + if self.sqlalchemy_uri.startswith(k): + return v + return default + def grains_dict(self): return {grain.name: grain for grain in self.grains()} @@ -630,14 +648,16 @@ def query( # sqla tf = '%Y-%m-%d %H:%M:%S.%f' time_filter = [ - timestamp >= from_dttm.strftime(tf), - timestamp <= to_dttm.strftime(tf), + timestamp >= text(self.database.dttm_converter(from_dttm)), + timestamp <= text(self.database.dttm_converter(to_dttm)), ] inner_time_filter = copy(time_filter) if inner_from_dttm: - inner_time_filter[0] = timestamp >= inner_from_dttm.strftime(tf) + inner_time_filter[0] = timestamp >= text( + self.database.dttm_converter(inner_from_dttm)) if inner_to_dttm: - inner_time_filter[1] = timestamp <= inner_to_dttm.strftime(tf) + inner_time_filter[1] = timestamp <= text( + self.database.dttm_converter(inner_to_dttm)) else: inner_time_filter = [] diff --git a/caravel/views.py b/caravel/views.py index ec25b2d940629..543f781a316cc 100644 --- a/caravel/views.py +++ b/caravel/views.py @@ -112,7 +112,8 @@ class TableColumnInlineView(CompactCRUDMixin, CaravelModelView): # noqa can_delete = False edit_columns = [ 'column_name', 'verbose_name', 'description', 'groupby', 'filterable', - 'table', 'count_distinct', 'sum', 'min', 'max', 'expression', 'is_dttm'] + 'table', 'count_distinct', 'sum', 'min', 'max', 'expression', + 'is_dttm', ] add_columns = edit_columns list_columns = [ 'column_name', 'type', 'groupby', 'filterable', 'count_distinct',