Snowflake ARRAY_SLICE can leave the newest event out of a recent-events report
A negative end index counts backward, but the endpoint is still excluded. Use the array length when the last element belongs in the result.
“Show the last three events, including the newest” sounds like a straightforward slice of an event array. In Snowflake, though, -1 as the end index means stop before the last element, not “go through the end.” If the array is ordered oldest to newest, the resulting report can look plausible while leaving out the very event the reader asked to see. Snowflake’s ARRAY_SLICE reference documents both rules: negative indexes count back from the end, and the to position is always excluded.
What the endpoint changes
Consider a dashboard backed by an array of deployment events, already ordered from oldest to newest. The newest event is a rollback. This illustrative query was not run against a live Snowflake account; the expected results follow from the documented indexing rules.
WITH example AS (
SELECT ['queued', 'built', 'deployed', 'rolled back'] AS events
)
SELECT
ARRAY_SLICE(events, -3, -1) AS stops_before_latest,
ARRAY_SLICE(events, -3, ARRAY_SIZE(events)) AS includes_latest
FROM example;
| Expression | Expected result |
|---|---|
ARRAY_SLICE(events, -3, -1) | ['built', 'deployed'] |
ARRAY_SLICE(events, -3, ARRAY_SIZE(events)) | ['built', 'deployed', 'rolled back'] |
In the first expression, -3 points to built and -1 points to rolled back. Snowflake includes the start but excludes the end, so that expression answers a different question: which events fall between those positions, stopping before the newest? In the second, the end index is four, one past the last valid index of this four-element array. Snowflake’s reference uses the same ARRAY_SIZE pattern in its example of slicing through the final item.
What to check in generated SQL
Make the input order part of the report contract. “Last” means last in the array, so the query above only means “latest” if the array is already in chronological order. Then test with a fixture where the final event changes the interpretation: a rollback after a successful deployment makes an omitted endpoint much easier to spot than three interchangeable success events. If the request actually says “before the latest event,” the exclusive -1 endpoint is useful. The two prompts should not produce the same slice.
sources
- Snowflake SQL reference: ARRAY_SLICEdocs.snowflake.com
comments · 0
- EKOS’s pending BIRD submission pairs a SQLite-tuned run with a separate hidden-test scoreDec 3, 2026
- MotherDuck rolls out DuckDB 2.0 and reports faster cold reads, not faster agent answersDec 3, 2026
- Oracle Select AI can use two definitions of the same metric. Source order won’t break the tieDec 3, 2026