EnxDev commented on code in PR #44720:
URL: https://github.com/apache/superset/pull/44720#discussion_r4137502702


##########
superset/db_engine_specs/crate.py:
##########
@@ -88,5 +88,36 @@ def convert_dttm(
 
     @classmethod
     def alter_new_orm_column(cls, orm_col: TableColumn) -> None:
-        if orm_col.type == "TIMESTAMP":
+        if orm_col.type in {
+            "TIMESTAMP",
+            "TIMESTAMP WITHOUT TIME ZONE",
+            "TIMESTAMP WITH TIME ZONE",
+        }:
             orm_col.python_date_format = "epoch_ms"
+
+    @classmethod
+    def fetch_data(cls, cursor: Any, limit: int | None = None) -> 
list[tuple[Any, ...]]:
+        """Decode typed timestamp results without depending on dataset 
metadata."""
+        data = super().fetch_data(cursor, limit)
+        # CrateDB's DBAPI description omits type codes. The HTTP result retains
+        # them: 11 = TIMESTAMP WITH TIME ZONE, 15 = WITHOUT TIME ZONE.
+        timestamp_indexes = [
+            index
+            for index, type_code in enumerate(
+                getattr(cursor, "_result", {}).get("col_types", [])
+            )
+            if type_code in (11, 15)
+        ]
+        if not timestamp_indexes:
+            return data
+        rows = []
+        for row in data:
+            values = list(row)
+            for index in timestamp_indexes:
+                value = values[index]
+                if isinstance(value, (int, float)) and not isinstance(value, 
bool):
+                    # UTC-naive matches Superset's datetime normalization and
+                    # avoids interpreting epoch milliseconds as nanoseconds.
+                    values[index] = datetime(1970, 1, 1) + 
timedelta(milliseconds=value)

Review Comment:
   Python datetimes stop at years 1 to 9999, but CrateDB timestamps go well 
past that in both directions. A single row like `'10000-01-01'::timestamp` 
raises `OverflowError` here, so the whole SQL Lab query or chart fails where it 
used to just return the int.
   
   Could a follow-up catch `OverflowError` and leave the raw value? A test row 
with an out-of-range value alongside the `-1` case would pin it down.
   
   ```python
   try:
       values[index] = datetime(1970, 1, 1) + timedelta(milliseconds=value)
   except OverflowError:
       pass
   ```



-- 
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