The situation
Cold-storage sensors report a temperature every 15 minutes. Sometimes a sensor fails to report and the reading arrives as NULL. A monitoring chart needs a value at every point, so each missing reading should show the last temperature the device did report.
A first attempt used the previous row's value. It works for a single missing reading and leaves holes wherever two or more are missing in a row.
Input tables
readings
One row per device every 15 minutes. A device never has two rows with the same timestamp.
| Column | Type | Description |
|---|---|---|
device_id |
BIGINT | The sensor. |
reading_ts |
TIMESTAMP_NTZ | When the reading was due. |
temperature |
DOUBLE | Degrees Celsius. NULL if the sensor did not report. |
Requirement
Return every row of readings with one more column, filled_temperature.
- If
temperatureis present,filled_temperatureis the same value. - If it is NULL,
filled_temperatureis the device's most recent earlier temperature that is not NULL, however many readings back that is. - Values are never taken from another device.
- Values are never taken from a later reading. If a device has no earlier reading
at all,
filled_temperatureis NULL.
Expected output
One row per row of readings.
| Column | Type | Description |
|---|---|---|
device_id |
BIGINT | |
reading_ts |
TIMESTAMP_NTZ | |
temperature |
DOUBLE | The original value, NULL included. |
filled_temperature |
DOUBLE | The reading, or the last known one before it. |
Solve it in the browser
This editor runs DuckDB inside your browser. Nothing is installed and nothing is sent
to a server. DuckDB's SQL is close to Spark SQL but not identical. If you use IGNORE NULLS, DuckDB wants it inside the brackets: LAST_VALUE(temperature IGNORE NULLS) OVER (...). Spark wants it after them: LAST_VALUE(temperature) IGNORE NULLS OVER (...).
The Colab notebook runs real Spark.
Your result appears here. Your draft is kept in this browser.
Explanation
Show explanationGives the answer away. Try the problem first.
The approach
"The last known value" means: look back through the device's readings, in time
order, until you find one that is not NULL. How far back that is differs per row,
so a fixed look-back such as LAG cannot do it.
There are two standard ways.
1. Let the window function skip the NULLs.
LAST_VALUE(temperature) IGNORE NULLS OVER (
PARTITION BY device_id
ORDER BY reading_ts
) AS filled_temperature
The window's default frame runs from the device's first row to the current row.
LAST_VALUE ... IGNORE NULLS returns the last reading in that frame that is not
NULL: the current one if it is there, otherwise the nearest earlier one. This is the
PySpark reference solution too: F.last("temperature", ignorenulls=True).
2. Build groups with a running count. This is the SQL reference solution, and it
works in every engine, including ones with no IGNORE NULLS.
COUNT(temperature) OVER (PARTITION BY device_id ORDER BY reading_ts) AS reading_group
COUNT(temperature) counts only real readings. As a running count it goes up by one
at every real reading and stays level through the gap behind it. So a reading and
all the missing rows that follow it share one number, and every such group contains
exactly one real temperature. MAX(temperature) over the group picks it up. (MIN
would do as well: there is only one value.)
Both stop at the device boundary because of PARTITION BY device_id, and neither
looks forward, so missing readings at the very start of a device stay NULL.
Of the 320 rows, 78 have no temperature. 33 of those get a value. 45 stay NULL: all 40 readings of device 7, which never reported, and the leading gaps of devices 1, 3 and 4.
Common wrong answers
COALESCE(temperature, LAG(temperature) OVER (...)). Looks back exactly one
row. If that row is missing too, the result is NULL. 8 rows that should have a value
stay empty: every missing reading after the first one in a run. This is the first
attempt from the story.
LAST_VALUE(temperature) without IGNORE NULLS. The last row of the frame is
the current row, so this returns the current row's own value, NULL included.
Nothing is filled: all 33 rows are still empty.
No PARTITION BY device_id. Readings of all devices are put in one time line,
and a gap is filled with whatever any sensor reported last. 77 rows are wrong,
including all 40 of the sensor that never reported: it now shows the temperatures
of its neighbours.
A running MAX(temperature). MAX over "everything so far" is the highest
temperature so far, not the latest. It only looks right while the temperature is
rising. 228 rows wrong.
Filling the start from the next reading. Back-filling the leading gaps gives a value to 5 rows that must stay NULL. The requirement is explicit: a value is never taken from a later reading. On a chart, a back-filled value shows a temperature at a time when nobody knew it.
Filling with the device's average. An average is not a reading, and it uses later readings as well. 36 rows wrong.
Overwriting temperature. The output keeps the original column, NULLs included,
next to the filled one. Once the fill has replaced the original you can no longer
tell a measured value from a carried one.
IGNORE NULLS is spelled differently per engine
| Engine | Syntax |
|---|---|
| Spark SQL | LAST_VALUE(temperature) IGNORE NULLS OVER (...) or LAST_VALUE(temperature, TRUE) OVER (...) |
| DuckDB (the browser editor) | LAST_VALUE(temperature IGNORE NULLS) OVER (...) |
Each engine rejects the other's spelling with a syntax error. The running-count version has no such problem.
How long may a value be carried?
Here: forever. In a real pipeline, ask. A cold-storage sensor that has been silent for six hours is not "still at 4.1 degrees"; it is unknown, and probably an alert. Forward fill usually comes with a limit ("at most one hour"), and that needs the timestamp of the last real reading as well as its value.
Edge cases in the data
| Case | Where | What it catches |
|---|---|---|
| Five missing readings in a row | device 2 | LAG. |
| Missing readings at the start | device 1 (three), devices 3 and 4 (one each) | Back-filling; a missing PARTITION BY. |
| A sensor that never reported | device 7 | A missing PARTITION BY: every row must stay NULL. |
| Nothing missing | device 8 | filled_temperature equals temperature on every row. |
| Temperatures that go down as well as up | all devices | A running MAX. |