Repository navigation
Keep Presto/Trino two-argument MAX(x, n)/MIN(x, n) as aggregates - #17
Merged
Merged
Conversation
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
This was referenced Nov 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In Presto and Trino,
max(x, n)/min(x, n)is an aggregate that returns an array of thenlargest / smallest values ofx. The multi-argumentMIN/MAX→LEAST/GREATESTnormalization rewrote it to a scalarGREATEST(x, n), which changes both the result and the output type. sqlglot keeps it unchanged.The normalization now skips two-argument
MIN/MAXwhen the source dialect is Presto, Trino or Athena. Three or more arguments, and other source dialects, still becomeLEAST/GREATEST(SELECT MAX(a, b, c)from generic →GREATEST(a, b, c)in Trino).Tests
tests/presto_max_min_top_n.rs(new): two-argumentMAX/MINround-trip unchanged in Trino, Presto and Athena (including underGROUP BY); many-argumentMAXfrom generic is stillGREATEST.cargo test --lib: 1343 passed.Generated by Claude Code