Dimitrije Zdravkovic created SPARK-62200:
--------------------------------------------
Summary: Add INTERPOLATE_FFILL, INTERPOLATE_BFILL and
INTERPOLATE_LINEAR window functions
Key: SPARK-62200
URL: https://issues.apache.org/jira/browse/SPARK-62200
Project: Spark
Issue Type: Umbrella
Components: SQL
Affects Versions: 4.4.0
Reporter: Dimitrije Zdravkovic
h2. Summary
Add three window functions, {{INTERPOLATE_FFILL}}, {{INTERPOLATE_BFILL}} and
{{INTERPOLATE_LINEAR}}, that fill the NULL values of a column in a time series
from the nearest non-NULL values in ORDER BY order. Non-NULL values are
returned unchanged, and no rows are added. They work on any input. After the
GAP FILL relation operator (SPARK-61276) they also fill the rows that GAP FILL
adds. Target use case: IoT, telemetry and generally time series data with
missing readings, where users today combine {{last_value}} and {{first_value}}
with IGNORE NULLS and custom window frames by hand.
h2. Syntax
{code:sql}
{ INTERPOLATE_FFILL | INTERPOLATE_BFILL | INTERPOLATE_LINEAR } ( expr )
OVER { window_name |
( [ PARTITION BY partition_expr [, ...] ]
ORDER BY order_expr [ ASC | DESC ]
[ NULLS { FIRST | LAST } ] ) }
{code}
h2. Arguments
* {{expr}}: the value to fill. INTERPOLATE_FFILL and INTERPOLATE_BFILL take
any type. INTERPOLATE_LINEAR takes numeric types only. The result has the type
of {{expr}}.
* {{PARTITION BY partition_expr, ...}}: optional. Each partition is filled on
its own. Without PARTITION BY, all rows form one partition.
* {{ORDER BY order_expr}}: required, exactly one expression of type TIMESTAMP,
TIMESTAMP_NTZ or DATE. Rows where {{order_expr}} is NULL keep their value and
are not used to fill other rows.
* {{window_name}}: a window from the WINDOW clause. It must have an ORDER BY
and no frame.
* A written window frame is not allowed, also one equal to the default. The
functions always read the whole partition.
h2. Behavior
* A row whose {{expr}} is not NULL keeps its value.
* INTERPOLATE_FFILL: otherwise the last non-NULL value of {{expr}} before the
row, in ORDER BY order, or NULL if there is none.
* INTERPOLATE_BFILL: otherwise the first non-NULL value of {{expr}} after the
row, or NULL if there is none.
* INTERPOLATE_LINEAR: otherwise the value at the row's {{order_expr}} on the
straight line between the last non-NULL value before the row and the first one
after it. NULL if either one is missing. For integer and DECIMAL values the
result is computed exactly and rounded once, half away from zero.
* With DESC, before and after swap. NULLS FIRST and NULLS LAST do not change
any result.
* If several rows share the ORDER BY value that is read, which of their values
is used is not defined.
* DISTINCT, FILTER, WITHIN GROUP, IGNORE NULLS and RESPECT NULLS are not
allowed. Streaming input is not supported.
h2. Examples
{code:sql}
/* Fill the NULL readings of each device */
SELECT device_id, ts, v,
INTERPOLATE_FFILL(v) OVER (PARTITION BY device_id ORDER BY ts) AS ffill,
INTERPOLATE_BFILL(v) OVER (PARTITION BY device_id ORDER BY ts) AS bfill,
INTERPOLATE_LINEAR(v) OVER (PARTITION BY device_id ORDER BY ts) AS linear
FROM sensor_readings
/* After GAP FILL: fill the added rows and the NULL input rows */
SELECT ts, battery_pct,
INTERPOLATE_LINEAR(battery_pct) OVER (PARTITION BY device_id ORDER BY
ts) AS filled_pct
FROM readings GAP FILL (PARTITION BY device_id ORDER BY ts BIN WIDTH INTERVAL
15 MINUTES)
/* One named window for several calls */
SELECT ts, v,
INTERPOLATE_FFILL(v) OVER w AS ffill,
INTERPOLATE_LINEAR(v) OVER w AS linear
FROM t
WINDOW w AS (ORDER BY ts)
{code}
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]