Skip to content

Commit

Permalink
fix: handle python_date_format in ExploreMixin (apache#24062)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida authored May 15, 2023
1 parent de96372 commit 2938c5d
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions superset/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,12 @@ def _get_top_groups(

return or_(*groups)

def dttm_sql_literal(self, dttm: sa.DateTime, col_type: Optional[str]) -> str:
def dttm_sql_literal(
self,
col: "TableColumn",
dttm: sa.DateTime,
col_type: Optional[str],
) -> str:
"""Convert datetime object to a SQL expression string"""

sql = (
Expand All @@ -1281,6 +1286,22 @@ def dttm_sql_literal(self, dttm: sa.DateTime, col_type: Optional[str]) -> str:
if sql:
return sql

tf = col.python_date_format

# Fallback to the default format (if defined).
if not tf and self.db_extra:
tf = self.db_extra.get("python_date_format_by_column_name", {}).get(
col.column_name
)

if tf:
if tf in {"epoch_ms", "epoch_s"}:
seconds_since_epoch = int(dttm.timestamp())
if tf == "epoch_s":
return str(seconds_since_epoch)
return str(seconds_since_epoch * 1000)
return f"'{dttm.strftime(tf)}'"

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

def get_time_filter( # pylint: disable=too-many-arguments
Expand Down Expand Up @@ -1309,14 +1330,14 @@ def get_time_filter( # pylint: disable=too-many-arguments
l.append(
col
>= self.db_engine_spec.get_text_clause(
self.dttm_sql_literal(start_dttm, time_col.type)
self.dttm_sql_literal(time_col, start_dttm, time_col.type)
)
)
if end_dttm:
l.append(
col
< self.db_engine_spec.get_text_clause(
self.dttm_sql_literal(end_dttm, time_col.type)
self.dttm_sql_literal(time_col, end_dttm, time_col.type)
)
)
return and_(*l)
Expand Down

0 comments on commit 2938c5d

Please sign in to comment.