LAST_VALUE returns the last non-null value in a data stream within an OVER window. Use it to track the most recent valid value for each partition as new records arrive.
Limits
Supported only in Realtime Compute for Apache Flink that uses Ververica Runtime (VVR) 3.0.0 or later.
Syntax
T LAST_VALUE(T value)
T LAST_VALUE(T value, BIGINT order)Input parameters
| Parameter | Data type | Description |
|---|---|---|
value | Any data type | The value to evaluate. Null values are skipped. |
order | BIGINT | A ranking key that determines which record is considered last. The non-null record with the largest order value is returned. |
All input parameters must be of the same data type.
Return value
Returns the same data type as value.
Usage notes
Using the order parameter
When you provide an order value, LAST_VALUE selects the non-null record with the largest order value instead of relying on processing order.
Example
Test data (table T1)
| a (BIGINT) | b (INT) | c (VARCHAR) |
|---|---|---|
| 1 | 1 | Hello |
| 2 | 2 | Hello |
| 3 | 3 | Hello |
| 4 | 4 | Hello |
| 5 | 5 | Hello |
| 6 | 6 | Hello |
| 7 | 7 | NULL |
| 8 | 7 | Hello World |
| 9 | 8 | Hello World |
| 10 | 20 | Hello World |
Test statement
SELECT c, LAST_VALUE(b)
OVER (PARTITION BY c ORDER BY PROCTIME() RANGE UNBOUNDED PRECEDING) AS var1
FROM T1;Result
| c (VARCHAR) | var1 (INT) |
|---|---|
| Hello | 1 |
| Hello | 2 |
| Hello | 3 |
| Hello | 4 |
| Hello | 5 |
| Hello | 6 |
| NULL | 7 |
| Hello World | 7 |
| Hello World | 8 |
| Hello World | 20 |
The NULL row in c returns var1 = 7 because that partition contains only one row (b = 7). For the Hello World partition, each row reflects the last non-null b value up to and including that row (7, then 8, then 20), because the window frame is RANGE UNBOUNDED PRECEDING.