#12105·ibis

bug(trino): negative array index compiles to element_at(arr, idx + 1), which is index 0 for [-1]

Author: Ramin314Created Sep 5, 2026Updated Sep 8, 2026

What happened?

On the Trino (and Athena) backend a negative array index is compiled by adding 1 to it, so arr[-1] becomes element_at(arr, 0) and Trino raises INVALID_FUNCTION_ARGUMENT: SQL array indices start at 1 on every row. DuckDB compiles the same expression to list_extract(arr, -1), which is correct.

python
import ibis
t = ibis.table({"s": "string"}, name="t")
print(ibis.to_sql(t.select(last=t.s.split(":")[-1]), dialect="trino"))
sql
SELECT ELEMENT_AT(SPLIT("t0"."s", ':'), -1 + 1) AS "last" FROM "t" AS "t0"

Trino's element_at already accepts negative indices (counting from the end), so a negative literal can be passed through unchanged; only non-negative indices need the + 1.

TrinoCompiler.visit_ArrayIndex is return self.f.element_at(arg, index + 1). The backend test test_array_index is parametrised over range(3) only, so no negative index is covered.

What version of ibis are you using?

12.0.0

What backend(s) are you using, if any?

Trino, Athena (Athena engine v3, observed on a real table)

Relevant log output

pyathena.error.OperationalError: INVALID_FUNCTION_ARGUMENT: SQL array indices start at 1