Skip to content

[CALCITE-7862] ARG_MIN and ARG_MAX should support ARRAY, MAP and ROW keys - #5333

Merged
xiedeyantu merged 1 commit into
apache:mainfrom
LantaoJin:arg-min-array-key
Oct 19, 2026
Merged

xiedeyantu merged 1 commit into
apache:mainfrom
LantaoJin:arg-min-array-key

Conversation

@LantaoJin

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7862

Changes Proposed

ARG_MIN(value, key) and ARG_MAX(value, key), and their aliases MIN_BY and MAX_BY, pass validation when the key is an ARRAY, MAP or ROW, but the Enumerable convention cannot implement them. MIN, MAX and ORDER BY on the same values work.

select arg_min(x, v)
from (values (1, array[3, 1]), (2, array[1, 5]), (3, array[1, 4])) as t(x, v);

On main this fails with Unable to implement EnumerableAggregate(group=[{}], A=[ARG_MIN($0, $1)]), caused by NoSuchMethodException: SqlFunctions.ltNullable(java.util.List,java.util.List). With this change it returns 3.

Cause: ArgMinMaxImplementor compares a non-primitive key with SqlFunctions.ltNullable / gtNullable, which are only declared for T extends Comparable<T>. ARRAY, MAP and ROW values are List, Map and Object[] at run time, so code generation finds no method. CALCITE-4813 added overloads of lesser / greater for these types, which MIN / MAX use, but not of ltNullable / gtNullable.

Changes:

  • SqlFunctions gets List, Map and Object[] overloads of ltNullable and gtNullable, which delegate to the existing lt / gt overloads for those types, so keys compare the same way as in MIN, MAX and ORDER BY.
  • agg.iq covers ARG_MIN / ARG_MAX with ARRAY (including a NULL key, which is skipped), MAP and ROW keys, next to MIN / MAX on the same column. SqlOperatorTest.testArgMin covers an ARRAY key for ARG_MIN, ARG_MAX, MIN_BY and MAX_BY.

@sonarqubecloud

Copy link
Copy Markdown

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Oct 16, 2026
@xiedeyantu
xiedeyantu merged commit 7b1650e into apache:main Oct 19, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants