rusackas commented on code in PR #42432:
URL: https://github.com/apache/superset/pull/42432#discussion_r3664588066


##########
superset/db_engine_specs/sqlite.py:
##########
@@ -126,6 +126,18 @@ class SqliteEngineSpec(BaseEngineSpec):
     def epoch_to_dttm(cls) -> str:
         return "datetime({col}, 'unixepoch')"
 
+    @classmethod
+    def year_to_dttm(cls) -> str:
+        # SQLite's date functions parse a 'YYYY-01-01' string just fine, but 
won't
+        # accept a bare integer/real year (it's read as a Julian day number 
instead).
+        # The CASE guard is needed because printf() treats a NULL argument as 
0,
+        # which would otherwise turn a missing year into '0000-01-01' rather 
than
+        # propagating the NULL.
+        return (
+            "CASE WHEN {col} IS NULL THEN NULL "
+            "ELSE printf('%04d-01-01', CAST({col} AS INTEGER)) END"
+        )

Review Comment:
   Good catch, fixed. Wrapped the SQLite expression in `datetime(...)` so it 
returns a full datetime even when no time grain is applied to wrap it further.



##########
tests/unit_tests/db_engine_specs/test_sqlite.py:
##########
@@ -131,3 +131,34 @@ def test_time_grain_expressions(dttm: str, grain: str, 
expected: str) -> None:
     with engine.connect() as connection:
         result = connection.execute(text(sql)).scalar()
     assert result == expected
+
+
[email protected](
+    "year,expected",
+    [
+        (2013, "2013-01-01 00:00:00"),
+        (2013.0, "2013-01-01 00:00:00"),
+        (None, None),
+    ],
+)
+def test_year_pdf_time_grain(year: Optional[float], expected: Optional[str]) 
-> None:

Review Comment:
   int's compatible with `float` under mypy's numeric tower, so this doesn't 
actually get flagged. Ran mypy on it locally to confirm, no error.



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