Skip to content

Conversation

@gertjanal
Copy link
Contributor

@gertjanal gertjanal commented Nov 12, 2025

Description

Backstory: reason why element_at exists:
SELECT ARRAY['foo', 'bar'][10] throws array out of bounds exception
SELECT element_at(ARRAY['foo', 'bar'], 10) returns NULL, which is convenient

In many queries I use, I need the first element. However, as functions are nested, the arguments are not always very readable:
element_at(element_at(filter(persons, person -> person.age > 18), 1).addresses, 1).street

This PR introduces element_first and element_last:
element_first(element_first(filter(persons, person -> person.age > 18)).addresses).street

The element_last function can be used to return the last element.

Additional context and related issues

In Java, a List has getFirst and getLast as well: https://docs.oracle.com/en/java/javase/25/docs/api//java.base/java/util/List.html#getFirst()

Naming considerations

I considered first() and array_first(), but I think element_first() is better because it is much closer to the other function that people will use, the element_at() (also better when the client auto-suggests function names when typing element_)

Release notes

## General
* Add `array_first` and `array_last` functions

@gertjanal gertjanal force-pushed the element_first_element_last branch from 2cc7cfc to 7e0539a Compare November 13, 2025 20:11
Copy link
Member

@raunaqmorarka raunaqmorarka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm
@martint PTAL

@martint
Copy link
Member

martint commented Nov 14, 2025

Seems reasonable to add these. Let's rename the functions to array_first and array_last, though.

@ebyhr ebyhr changed the title Added array functions element_first and element_last Add array_first and array_last functions Nov 14, 2025
@gertjanal gertjanal force-pushed the element_first_element_last branch 2 times, most recently from 126092b to 894253a Compare November 17, 2025 20:20
Copy link
Member

@martint martint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of minor comments. Otherwise, it looks good. Once you apply the changes, I'll merge it. Thanks!

@gertjanal gertjanal force-pushed the element_first_element_last branch from 894253a to 80b826b Compare November 19, 2025 21:50
@gertjanal gertjanal requested a review from martint November 19, 2025 21:50
@martint
Copy link
Member

martint commented Nov 19, 2025

There's a build failure:

Error:  src/main/java/io/trino/metadata/SystemFunctionBundle.java:[103,1] (imports) ImportOrder: Wrong order for 'io.trino.operator.scalar.ArrayExceptFunction' import.

@gertjanal gertjanal force-pushed the element_first_element_last branch 2 times, most recently from 6c0b224 to 1be93d5 Compare November 21, 2025 06:22
@gertjanal gertjanal force-pushed the element_first_element_last branch from a86e1eb to 0db0599 Compare November 21, 2025 19:31
@ebyhr ebyhr merged commit 925298b into trinodb:master Nov 22, 2025
195 of 198 checks passed
@github-actions github-actions bot added this to the 479 milestone Nov 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants