You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by be...@apache.org on 2023/05/15 18:43:21 UTC

[superset] branch sc-70535 created (now 68628b3ed8)

This is an automated email from the ASF dual-hosted git repository.

beto pushed a change to branch sc-70535
in repository https://gitbox.apache.org/repos/asf/superset.git


      at 68628b3ed8 fix: handle python_date_format in ExploreMixin

This branch includes the following new commits:

     new 68628b3ed8 fix: handle python_date_format in ExploreMixin

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[superset] 01/01: fix: handle python_date_format in ExploreMixin

Posted by be...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

beto pushed a commit to branch sc-70535
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 68628b3ed8ba4eb2bce1577e5704f33b16bc257d
Author: Beto Dealmeida <ro...@dealmeida.net>
AuthorDate: Mon May 15 11:43:10 2023 -0700

    fix: handle python_date_format in ExploreMixin
---
 superset/models/helpers.py | 34 ++++++++++++++++++++++++++++------
 1 file changed, 28 insertions(+), 6 deletions(-)

diff --git a/superset/models/helpers.py b/superset/models/helpers.py
index b38089e381..959575d2d7 100644
--- a/superset/models/helpers.py
+++ b/superset/models/helpers.py
@@ -1269,7 +1269,9 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
 
         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 = (
@@ -1281,6 +1283,23 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
         if sql:
             return sql
 
+        tf = col.python_date_format
+
+        # Fallback to the default format (if defined).
+        if not tf:
+            try:
+                tf = self.db_extra["python_date_format_by_column_name"][col.column_name]
+            except (KeyError, TypeError):
+                pass
+
+        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
@@ -1292,7 +1311,7 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
         label: Optional[str] = "__time",
         template_processor: Optional[BaseTemplateProcessor] = None,
     ) -> ColumnElement:
-        col = (
+        col_name = (
             time_col.get_timestamp_expression(
                 time_grain=time_grain,
                 label=label,
@@ -1303,20 +1322,23 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
                 time_col, label=label, template_processor=template_processor
             )
         )
+        col = [
+            column for column in self.columns if column.column_name == str(col_name)
+        ][0]
 
         l = []
         if start_dttm:
             l.append(
-                col
+                col_name
                 >= self.db_engine_spec.get_text_clause(
-                    self.dttm_sql_literal(start_dttm, time_col.type)
+                    self.dttm_sql_literal(col, start_dttm, time_col.type)
                 )
             )
         if end_dttm:
             l.append(
-                col
+                col_name
                 < self.db_engine_spec.get_text_clause(
-                    self.dttm_sql_literal(end_dttm, time_col.type)
+                    self.dttm_sql_literal(col, end_dttm, time_col.type)
                 )
             )
         return and_(*l)