Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hughhhh committed Aug 2, 2022
1 parent d332e71 commit ca9b20e
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions superset/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,36 @@ def _get_top_groups(

return or_(*groups)

def dttm_sql_literal(self, dttm: sa.DateTime, col_type: Optional[str]) -> str:
"""Convert datetime object to a SQL expression string"""
sql = (
self.db_engine_spec.convert_dttm(col_type, dttm, db_extra=None)
if col_type
else None
)

if sql:
return sql

return f'{dttm.strftime("%Y-%m-%d %H:%M:%S.%f")}'

def get_time_filter(
self,
time_col: Dict[str, Any],
start_dttm: sa.DateTime,
end_dttm: sa.DateTime,
) -> ColumnElement:
label = "__time"
col = time_col.get("column_name")
sqla_col = literal_column(col)
my_col = self.make_sqla_column_compatible(sqla_col, label)
l = []
if start_dttm:
l.append(my_col >= self.dttm_sql_literal(start_dttm, time_col.get("type")))
if end_dttm:
l.append(my_col < self.dttm_sql_literal(end_dttm, time_col.get("type")))
return and_(*l)

def values_for_column(self, column_name: str, limit: int = 10000) -> List[Any]:
"""Runs query against sqla to retrieve some
sample values for the given column.
Expand Down Expand Up @@ -1490,6 +1520,33 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
select_exprs.insert(0, timestamp)
groupby_all_columns[timestamp.name] = timestamp

# Use main dttm column to support index with secondary dttm columns.
if (
db_engine_spec.time_secondary_columns
and self.main_dttm_col in self.dttm_cols
and self.main_dttm_col != dttm_col.column_name
):
if isinstance(self.main_dttm_col, dict):
time_filters.append(
self.get_time_filter(
self.main_dttm_col,
from_dttm,
to_dttm,
)
)
else:
time_filters.append(
columns_by_name[self.main_dttm_col].get_time_filter(
from_dttm,
to_dttm,
)
)

if isinstance(dttm_col, dict):
time_filters.append(dttm_col.get_time_filter(from_dttm, to_dttm))
else:
time_filters.append(self.get_time_filter(dttm_col, from_dttm, to_dttm))

# Always remove duplicates by column name, as sometimes `metrics_exprs`
# can have the same name as a groupby column (e.g. when users use
# raw columns as custom SQL adhoc metric).
Expand Down

0 comments on commit ca9b20e

Please sign in to comment.