Skip to content

Keep Presto/Trino two-argument MAX(x, n)/MIN(x, n) as aggregates - #17

Merged
ravenac95 merged 1 commit into
mainfrom
trino-max-min-top-n
Nov 9, 2026
Merged

ravenac95 merged 1 commit into
mainfrom
trino-max-min-top-n

Conversation

@ravenac95

Copy link
Copy Markdown
Member

In Presto and Trino, max(x, n) / min(x, n) is an aggregate that returns an array of the n largest / smallest values of x. The multi-argument MIN/MAX → LEAST/GREATEST normalization rewrote it to a scalar GREATEST(x, n), which changes both the result and the output type. sqlglot keeps it unchanged.

The normalization now skips two-argument MIN/MAX when the source dialect is Presto, Trino or Athena. Three or more arguments, and other source dialects, still become LEAST/GREATEST (SELECT MAX(a, b, c) from generic → GREATEST(a, b, c) in Trino).

Tests

  • tests/presto_max_min_top_n.rs (new): two-argument MAX/MIN round-trip unchanged in Trino, Presto and Athena (including under GROUP BY); many-argument MAX from generic is still GREATEST.
  • cargo test --lib: 1343 passed.

Generated by Claude Code

In Presto and Trino, max(x, n)/min(x, n) aggregate the n largest/smallest
values of x into an array. Rewriting them to the scalar GREATEST/LEAST changed
the query's meaning and its output type; sqlglot keeps them unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017CxHoCFbTWJA9bcZWC9ydu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants