Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support of MAP_TOP_N_KEYS to Presto #18625

Merged
merged 1 commit into from
Nov 8, 2022

Conversation

jainavi17
Copy link
Contributor

@jainavi17 jainavi17 commented Nov 3, 2022

Add an UDF for getting top N keys of a map, which returns an array of the top N keys of map. Optionally a lambda comparator can also be passed to perform a custom comparison of the values. Returns all the value if the value N is greater than or equal to size of the map. N must be >= 0. For N = 0, the function returns empty array.

map_top_n_keys(map(K, V), N) -> array(K)

  • For N > 0, returns the top N keys of a map. If N happens to larger than size of the map, it returns all the keys of the map in descending order
  • For N = 0, return empty array
  • For N < 0, function throws an error.

map_top_n_keys(map(K, V), N, function(K, K, int)) -> array(K)
Return the top N keys of the map sorted using the lambda comparator.

Test plan - (Please fill in how you tested your changes)
Added unit tests.
Build successfully using the following terminal command

  • ./mvnw clean install -Dtest=TestMapTopNKeysFunction -fn -pl presto-main
  • ./mvnw clean install -Dtest=TestMapTopNKeysComparatorFunction -fn -pl presto-main

Fill in the release notes towards the bottom of the PR description.
See Release Notes Guidelines for details.

== RELEASE NOTES ==

General Changes
* Add support for MAP_TOP_N_KEYS
* Returns an array of the top N keys of map. Optionally a lambda comparator can also be passed to perform a custom comparison of the keys. Returns all the keys if the value N is greater than or equal to size of the map. For N < 0, the function returns keys in the reverse order. For N = 0, the function returns empty array.

.. function:: map_top_n_keys(map(K, V), N) -> array(K)
   Returns the top N keys of ``map``. If N must be >= 0. For N = 0, it returns empty ``array``.

.. function:: map_top_n_keys(map(K, V), N, function(K, K, int)) -> array(K)
   Returns the top N keys of ``map`` sorted using lambda comparator. N must be >= 0. For N = 0, it returns empty ``array``.

`SELECT MAP_TOP_N_KEYS(MAP(ARRAY[4, 5, 6], ARRAY[1, 2, 3]), 2)); -- 6, 5`
`SELECT MAP_TOP_N_KEYS(MAP(ARRAY['a', 'b', 'c', 'd'], ARRAY[1, 2, 3, NULL, NULL]), -3)); -- 'a', 'b', 'c'`
`SELECT MAP_TOP_N_KEYS(MAP(ARRAY[4, 5, 6], ARRAY[1, 2, 3]), 4, (x, y) -> IF(x < y, 1, IF(x = y, 0, -1))); -- 4, 5, 6`

@jainavi17 jainavi17 requested a review from a team as a code owner November 3, 2022 23:20
@jainavi17 jainavi17 requested a review from presto-oss November 3, 2022 23:20
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Nov 3, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: jainavi17 / name: Avinash Jain (ae9c23c5f094d38214b7b5dbecceef40151b3040)

@jainavi17
Copy link
Contributor Author

Can we merge this?

@kaikalur kaikalur merged commit 05fcfbf into prestodb:master Nov 8, 2022
@jainavi17 jainavi17 deleted the map_top_n_keys branch November 8, 2022 15:33
@wanglinsong wanglinsong mentioned this pull request Jan 12, 2023
30 tasks
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.

3 participants