villebro commented on a change in pull request #17048:
URL: https://github.com/apache/superset/pull/17048#discussion_r750150715



##########
File path: superset/db_engine_specs/elasticsearch.py
##########
@@ -59,9 +60,24 @@ def get_dbapi_exception_mapping(cls) -> 
Dict[Type[Exception], Type[Exception]]:
         }
 
     @classmethod
-    def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]:
+    def convert_dttm(
+        cls, target_type: str, dttm: datetime, db_extra: Optional[Dict[str, 
Any]] = None
+    ) -> Optional[str]:
+
+        db_extra = db_extra if db_extra else {}

Review comment:
       nit: `db_extra = db_extra or {}` should do the job, too

##########
File path: superset/db_engine_specs/elasticsearch.py
##########
@@ -59,9 +60,24 @@ def get_dbapi_exception_mapping(cls) -> 
Dict[Type[Exception], Type[Exception]]:
         }
 
     @classmethod
-    def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]:
+    def convert_dttm(
+        cls, target_type: str, dttm: datetime, db_extra: Optional[Dict[str, 
Any]] = None
+    ) -> Optional[str]:
+
+        db_extra = db_extra if db_extra else {}
         if target_type.upper() == utils.TemporalType.DATETIME:
+            es_version = db_extra.get("version")
+            # The elasticsearch CAST function does not take effect for the 
time zone
+            # setting. In elasticsearch7.8 and above, we can use the 
DATETIME_PARSE
+            # function to solve this problem.
+            if es_version and StrictVersion(es_version) >= 
StrictVersion("7.8"):
+                datetime_formatted = dttm.isoformat(sep=" ", 
timespec="seconds")
+                return (
+                    f"""DATETIME_PARSE('{datetime_formatted}', 'yyyy-MM-dd 
HH:mm:ss')"""
+                )

Review comment:
       I wonder if we should have a fallback/more clear error message if the 
version isn't parseable by `StrictVersion` (Since this is to be populated by 
the user, we can expect to bump into unparseable values here):
   ```python
   >>> from distutils.version import StrictVersion
   >>> StrictVersion("7.8.0.0")
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File 
"/Users/ville/.pyenv/versions/3.8-dev/lib/python3.8/distutils/version.py", line 
40, in __init__
       self.parse(vstring)
     File 
"/Users/ville/.pyenv/versions/3.8-dev/lib/python3.8/distutils/version.py", line 
137, in parse
       raise ValueError("invalid version number '%s'" % vstring)
   ValueError: invalid version number '7.8.0.0'
   ```
   Same for non-string values (I expect someone may try to enter it as a 
number, not string):
   ```python
   >>> StrictVersion(7.8)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File 
"/Users/ville/.pyenv/versions/3.8-dev/lib/python3.8/distutils/version.py", line 
40, in __init__
       self.parse(vstring)
     File 
"/Users/ville/.pyenv/versions/3.8-dev/lib/python3.8/distutils/version.py", line 
135, in parse
       match = self.version_re.match(vstring)
   TypeError: expected string or bytes-like object
   ```
   Just in case, perhaps we could do something as simple as
   ```python
   supports_dttm_parse = False
   try:
       if es_version:
           supports_dttm_parse = StrictVersion(es_version) >= 
StrictVersion("7.8")
       except ValueError:
           ...
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to