Skip to content

Commit

Permalink
fix: Update the Description of map_top_n (facebookincubator#12216)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebookincubator#12216

Update the description of map_top_n

map function **map_top_n** returns a new map containing only the top N elements, sorted in descending order by value. In cases where values are tied, the function sorts the tied elements in lexicographical descending order by key.

Since both 'a' and 'c' have null values, 'c' is included in the top 2 results along with 'b',

Reviewed By: zacw7

Differential Revision: D68913230

fbshipit-source-id: 7d0483d28e2f7eada5e1dcb7e0bd000857f831ac
  • Loading branch information
duxiao1212 authored and ArnavBalyan committed Jan 31, 2025
1 parent 253cec9 commit 63c64a2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion velox/docs/functions/presto/map.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Map Functions
``n`` must be a non-negative BIGINT value.::

SELECT map_top_n(map(ARRAY['a', 'b', 'c'], ARRAY[2, 3, 1]), 2) --- {'b' -> 3, 'a' -> 2}
SELECT map_top_n(map(ARRAY['a', 'b', 'c'], ARRAY[NULL, 3, NULL]), 2) --- {'b' -> 3, 'a' -> NULL}
SELECT map_top_n(map(ARRAY['a', 'b', 'c'], ARRAY[NULL, 3, NULL]), 2) --- {'b' -> 3, 'c' -> NULL}

.. function:: map_top_n_keys(map(K,V), n) -> array(K)

Expand Down
10 changes: 10 additions & 0 deletions velox/functions/prestosql/tests/MapTopNTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ TEST_F(MapTopNTest, equalValues) {
auto expectedResults = makeMapVector({0, 3}, expectedKeys, expectedValues);

assertEqualVectors(expectedResults, result);

auto dataWithStrKey =
makeRowVector({makeMapVectorFromJson<std::string, int64_t>(
{R"({"a":2, "b":3, "c":1})", R"({"a":null, "b":3, "c":null})"})});

auto resultWithStrKey = evaluate("map_top_n(c0, 2)", dataWithStrKey);

auto expectedWithStrKey = makeMapVectorFromJson<std::string, int64_t>(
{R"({"b":3, "a":2})", R"({"b":3, "c":null})"});
assertEqualVectors(expectedWithStrKey, resultWithStrKey);
}

TEST_F(MapTopNTest, timestampWithTimeZone) {
Expand Down

0 comments on commit 63c64a2

Please sign in to comment.