Skip to content

Commit 2914225

Browse files
authored
Add support for last_value window function (#780)
1 parent 139e24c commit 2914225

File tree

6 files changed

+803
-2
lines changed

6 files changed

+803
-2
lines changed

docs/docs/expressions/windowfunctions/arithmetic.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,23 @@ SELECT LAG(column1) OVER (PARTITION BY column2 ORDER BY column3) FROM ...
7979
SELECT LAG(column1, 2) OVER (PARTITION BY column2 ORDER BY column3) FROM ...
8080
-- Lag with offset 2 and default value set to 'hello'
8181
SELECT LAG(column1, 2, 'hello') OVER (PARTITION BY column2 ORDER BY column3) FROM ...
82-
```
82+
```
83+
84+
## Last Value
85+
86+
The `LAST_VALUE` window function returns the value of a specified column from the last row in the current window frame. It is useful for carrying forward the final value within a defined subset of rows.
87+
88+
By default, `LAST_VALUE` includes `NULL` values in its evaluation. However, when used with the `IGNORE NULLS` clause, the function will skip over `NULL` values and return the last non-null value in the frame (if any). If all values are null, the result is `NULL`.
89+
90+
This function **requires an ORDER BY clause** to establish row sequence and supports frame boundaries to control which rows are considered. You need to explicitly set the frame to `ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING` to get the expected last value from the full partition.
91+
92+
### SQL Usage
93+
94+
```sql
95+
-- Last value from start to current row
96+
SELECT LAST_VALUE(column1) OVER (PARTITION BY column2 ORDER BY column3) FROM ...
97+
-- Last value of the previous four rows and current row
98+
SELECT LAST_VALUE(column1) OVER (PARTITION BY column2 ORDER BY column3 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM ...
99+
-- Last value from start to current row ignoring nulls
100+
SELECT LAST_VALUE(column1) IGNORE NULLS OVER (PARTITION BY column2 ORDER BY column3) FROM ...
101+
```

src/FlowtideDotNet.Core/Compute/Columnar/Functions/WindowFunctions/BuiltInWindowFunctions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static void AddBuiltInWindowFunctions(FunctionsRegister functionsRegister
2424
functionsRegister.RegisterWindowFunction(FunctionsArithmetic.Uri, FunctionsArithmetic.Lead, new LeadWindowFunctionDefinition());
2525
functionsRegister.RegisterWindowFunction(FunctionsAggregateGeneric.Uri, FunctionsAggregateGeneric.SurrogateKeyInt64, new SurrogateKeyInt64WindowFunctionDefinition());
2626
functionsRegister.RegisterWindowFunction(FunctionsArithmetic.Uri, FunctionsArithmetic.Lag, new LagWindowFunctionDefinition());
27+
functionsRegister.RegisterWindowFunction(FunctionsArithmetic.Uri, FunctionsArithmetic.LastValue, new LastValueWindowFunctionDefinition());
2728
}
2829
}
2930
}

0 commit comments

Comments
 (0)