blob: d77de089dfae5ec0f806c8de07a716add945ffb8 [file] [log] [blame] [view]
---
{
"title": "ARRAY_LAST",
"language": "en",
"description": "Finds the last element in the array that satisfies the lambda expression. Finds the last element that satisfies the condition and returns it."
}
---
## array_last
<version since="2.0.0">
</version>
## Description
Finds the last element in the array that satisfies the lambda expression. Finds the last element that satisfies the condition and returns it.
## Syntax
```sql
array_last(lambda, ARRAY<T> arr1, [ARRAY<T> arr2, ...])
```
### Parameters
- `lambda`lambda expression used to define search conditions
- `arr1, arr2, ...`ARRAY<T> type, arrays to search. Supports one or more array parameters.
**Supported types for T:**
- Numeric types: TINYINT, SMALLINT, INT, BIGINT, LARGEINT, FLOAT, DOUBLE, DECIMAL
- String types: CHAR, VARCHAR, STRING
- Date and time types: DATE, DATETIME, DATEV2, DATETIMEV2
- Boolean type: BOOLEAN
- IP types: IPV4, IPV6
- Complex types: ARRAY, MAP, STRUCT
### Return Value
Return type: T
Return value meaning:
- Returns the last element in the array that satisfies the lambda expression
- NULL: if no element satisfies the condition, or if the input array is NULL
Usage notes:
- The number of parameters in the lambda expression must match the number of array parameters
- If no element satisfies the condition, returns NULL
- Does not support NULL input parameters
- When there are multiple array parameters, all arrays must have the same length
- Lambda can use any scalar expression, but cannot use aggregate functions
- Lambda expressions can call other higher-order functions, but the return types must be compatible
- For null values in array elements: null elements will be passed to the lambda expression for processing, and lambda can check for null values
**Query Examples:**
Find the last element greater than or equal to 3 in a floating-point array:
```sql
SELECT array_last(x -> x >= 3, [1.1, 2.2, 3.3, 4.4, 5.5]);
+----------------------------------------------------+
| array_last(x -> x >= 3, [1.1, 2.2, 3.3, 4.4, 5.5]) |
+----------------------------------------------------+
| 5.5 |
+----------------------------------------------------+
```
Find the last element with length greater than 2 in a string array:
```sql
SELECT array_last(x -> length(x) > 2, ['a', 'bb', 'ccc', 'dddd', 'eeeee']);
+---------------------------------------------------------------------+
| array_last(x -> length(x) > 2, ['a', 'bb', 'ccc', 'dddd', 'eeeee']) |
+---------------------------------------------------------------------+
| eeeee |
+---------------------------------------------------------------------+
```
Empty array returns NULL:
```sql
SELECT array_last(x -> x > 0, []);
+-------------------------------------+
| array_last(x -> x > 0, []) |
+-------------------------------------+
| NULL |
+-------------------------------------+
```
NULL input parameter will error:
```sql
SELECT array_last(x -> x > 2, NULL);
ERROR 1105 (HY000): errCode = 2, detailMessage = lambda argument must be array but is NULL
SELECT array_last(NULL);
ERROR 1105 (HY000): errCode = 2, detailMessage = Can not build function: 'array_last', expression: array_last(NULL), The 1st arg of array_filter must be lambda but is NULL
```
Array containing null values, lambda can check for null:
```sql
SELECT array_last(x -> x is not null, [null, 1, null, 3, null, 5]);
+-------------------------------------------------------------+
| array_last(x -> x is not null, [null, 1, null, 3, null, 5]) |
+-------------------------------------------------------------+
| 5 |
+-------------------------------------------------------------+
```
Multi-array search, find the last element where the first array is greater than the second array:
```sql
SELECT array_last((x, y) -> x > y, [1, 2, 3, 4, 5], [1.1, 2.2, 3.3, 4.4, 5.5]);
+-------------------------------------------------------------------------+
| array_last((x, y) -> x > y, [1, 2, 3, 4, 5], [1.1, 2.2, 3.3, 4.4, 5.5]) |
+-------------------------------------------------------------------------+
| NULL |
+-------------------------------------------------------------------------+
```
Nested array search, find the last element where each sub-array length is greater than 2:
```sql
SELECT array_last(x -> size(x) > 2, [[1,2],[3,4,5],[6],[7,8,9,10]]);
+--------------------------------------------------------------+
| array_last(x -> size(x) > 2, [[1,2],[3,4,5],[6],[7,8,9,10]]) |
+--------------------------------------------------------------+
| [7, 8, 9, 10] |
+--------------------------------------------------------------+
```
Error when parameter count is wrong:
```sql
SELECT array_last();
ERROR 1105 (HY000): errCode = 2, detailMessage = Can not found function 'array_last' which has 0 arity. Candidate functions are: [array_last(Expression, Expression...)]
```
Error when the number of parameters in lambda expression doesn't match the number of array parameters:
```sql
SELECT array_last(x -> x > 0, [1,2,3], [4,5,6], [7,8,9]);
ERROR 1105 (HY000): errCode = 2, detailMessage = lambda x -> (x > 0) arguments' size is not equal parameters' size
```
Error when passing non-array type:
```sql
SELECT array_last(x -> x > 0, 'not_an_array');
ERROR 1105 (HY000): errCode = 2, detailMessage = Can not find the compatibility function signature: array_last(Expression, VARCHAR(12))
```
### Keywords
ARRAY, LAST, ARRAY_LAST