mirror of
https://github.com/apache/superset.git
synced 2026-07-27 00:52:33 +00:00
fix(db-engine-specs): handle bare-year python_date_format columns in time-grain expressions
get_timestamp_expr() only knew how to convert epoch_s/epoch_ms raw values into a proper timestamp before applying a time-grain truncation function; any other python_date_format (e.g. "%Y") fell through and passed the raw value straight into the grain function. On SQLite that means DATETIME(year, 'start of year') interprets a bare integer year as a Julian day number rather than a calendar year, silently returning NULL for every row. This exact pattern ships in Superset's own video_game_sales example dataset (year: BIGINT, is_dttm=true, python_date_format: '%Y'), so any time-grain-truncated chart against it (Nightingale Rose included) returns no data. Add a year_to_dttm() hook (mirroring epoch_to_dttm/epoch_ms_to_dttm) that converts a bare year into a proper date before truncation, implemented for SQLite via printf()-based date construction with an explicit NULL guard (printf() otherwise treats a NULL argument as 0). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user