diff --git a/superset/db_engine_specs/base.py b/superset/db_engine_specs/base.py index eee5635160a..1d1c89d00b6 100644 --- a/superset/db_engine_specs/base.py +++ b/superset/db_engine_specs/base.py @@ -1110,6 +1110,14 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods time_expr = time_expr.replace("{col}", cls.epoch_to_dttm()) elif pdf == "epoch_ms": time_expr = time_expr.replace("{col}", cls.epoch_ms_to_dttm()) + elif pdf == "%Y": + # a bare four-digit year (e.g. the `year` column on the `video_game_sales` + # example dataset) has no native date type to lean on; without this the + # column value is passed straight into the grain function below, which + # every engine interprets as something other than a calendar year (SQLite + # reads a bare integer as a Julian day number, for instance), silently + # producing NULL for every row. + time_expr = time_expr.replace("{col}", cls.year_to_dttm()) return TimestampExpression(time_expr, col, type_=col.type) @@ -1326,6 +1334,17 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods """ return cls.epoch_to_dttm().replace("{col}", "({col}/1000)") + @classmethod + def year_to_dttm(cls) -> str: + """ + SQL expression that converts a bare four-digit year value to the January 1st + datetime of that year, for use in a query. The reference column should be + denoted as `{col}` in the return expression, e.g. "MAKE_DATE({col}, 1, 1)" + + :return: SQL Expression + """ + raise NotImplementedError() + @classmethod def get_datatype(cls, type_code: Any) -> str | None: """ diff --git a/superset/db_engine_specs/sqlite.py b/superset/db_engine_specs/sqlite.py index c704a120fee..0d196fc6bb4 100644 --- a/superset/db_engine_specs/sqlite.py +++ b/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" + ) + @classmethod def convert_dttm( cls, target_type: str, dttm: datetime, db_extra: dict[str, Any] | None = None diff --git a/tests/unit_tests/db_engine_specs/test_sqlite.py b/tests/unit_tests/db_engine_specs/test_sqlite.py index 79c4f8fc5ca..6e47087befa 100644 --- a/tests/unit_tests/db_engine_specs/test_sqlite.py +++ b/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 + + +@pytest.mark.parametrize( + "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: + """A bare four-digit year (e.g. the `year` column on the `video_game_sales` + example dataset) has no native date type; without `year_to_dttm` the raw + value is passed straight into the grain function, which SQLite reads as a + Julian day number rather than a calendar year, silently producing NULL.""" + from sqlalchemy import column + + from superset.db_engine_specs.sqlite import SqliteEngineSpec + + engine = create_engine("sqlite://", future=True) + with engine.begin() as connection: + connection.execute(text("CREATE TABLE t (year REAL)")) + connection.execute(text("INSERT INTO t VALUES (:year)"), {"year": year}) + + expression = SqliteEngineSpec.get_timestamp_expr( + col=column("year"), pdf="%Y", time_grain=TimeGrain.YEAR + ) + sql = f"SELECT {expression} FROM t" # noqa: S608 + with engine.connect() as connection: + result = connection.execute(text(sql)).scalar() + assert result == expected