forked from facebookincubator/velox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add map_top_n_keys Presto function (facebookincubator#10271)
Summary: Pull Request resolved: facebookincubator#10271 This function returns the respective items within the input vector with the top N keys in descending order. map_top_n_keys is a Presto function defined here https://www.internalfb.com/code/fbsource/[2d472d9e8215dd5f1a792f38b3d8c2dbba320698]/fbcode/github/presto-trunk/presto-main/src/main/java/com/facebook/presto/operator/scalar/sql/MapSqlFunctions.java?lines=60 Differential Revision: D58621202
- Loading branch information
1 parent
31800f5
commit 221968a
Showing
6 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#pragma once | ||
|
||
#include "velox/expression/ComplexViewTypes.h" | ||
#include "velox/functions/Udf.h" | ||
|
||
namespace facebook::velox::functions { | ||
|
||
template <typename TExec, typename Compare> | ||
struct MapTopNImpl { | ||
VELOX_DEFINE_FUNCTION_TYPES(TExec); | ||
|
||
void call( | ||
out_type<Array<Orderable<T1>>>& out, | ||
const arg_type<Map<Orderable<T1>, Orderable<T2>>>& inputMap, | ||
int64_t n) { | ||
VELOX_USER_CHECK_GE(n, 0, "n must be greater than or equal to 0") | ||
|
||
if (n == 0) { | ||
return; | ||
} | ||
using It = typename arg_type<Map<Orderable<T1>, Orderable<T2>>>::Iterator; | ||
Compare comparator; | ||
std::priority_queue<It, std::vector<It>, Compare> topEntries(comparator); | ||
|
||
for (auto it = inputMap.begin(); it != inputMap.end(); ++it) { | ||
if (topEntries.size() < n) { | ||
topEntries.push(it); | ||
} else if (comparator(it, topEntries.top())) { | ||
topEntries.pop(); | ||
topEntries.push(it); | ||
} | ||
} | ||
std::vector<It> result; | ||
result.reserve(topEntries.size()); | ||
while (!topEntries.empty()) { | ||
result.push_back(topEntries.top()); | ||
topEntries.pop(); | ||
} | ||
// Reverse the order of the result to be in descending order. | ||
for (size_t i = result.size() - 1; i >= 0; i--) { | ||
out.push_back(result[i]->first); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace facebook::velox::functions |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#pragma once | ||
|
||
#include "velox/expression/ComplexViewTypes.h" | ||
#include "velox/functions/Udf.h" | ||
#include "velox/functions/prestosql/MapTopNImpl.h" | ||
|
||
namespace facebook::velox::functions { | ||
|
||
template <typename TExec> | ||
struct CompareKeys { | ||
VELOX_DEFINE_FUNCTION_TYPES(TExec); | ||
|
||
using It = typename arg_type<Map<Orderable<T1>, Orderable<T2>>>::Iterator; | ||
|
||
bool operator()(const It& l, const It& r) const { | ||
static const CompareFlags flags{ | ||
false /*nullsFirst*/, | ||
true /*ascending*/, | ||
false /*equalsOnly*/, | ||
CompareFlags::NullHandlingMode::kNullAsIndeterminate}; | ||
return l->first.compare(r->first, flags) > 0; | ||
} | ||
}; | ||
|
||
// Returns an array with the top N keys in descending order. | ||
template <typename TExec> | ||
struct MapTopNKeysFunction : MapTopNImpl<TExec, CompareKeys<TExec>> {}; | ||
|
||
} // namespace facebook::velox::functions |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "velox/common/base/tests/GTestUtils.h" | ||
#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h" | ||
|
||
using namespace facebook::velox::test; | ||
|
||
namespace facebook::velox::functions { | ||
namespace { | ||
|
||
class MapTopNKeysTest : public test::FunctionBaseTest {}; | ||
|
||
TEST_F(MapTopNKeysTest, emptyMap) { | ||
RowVectorPtr input = makeRowVector({ | ||
makeMapVectorFromJson<int32_t, int64_t>({ | ||
"{}", | ||
}), | ||
}); | ||
|
||
assertEqualVectors( | ||
evaluate("map_top_n_keys(c0, 3)", input), | ||
makeArrayVectorFromJson<int32_t>({ | ||
"[]", | ||
})); | ||
} | ||
|
||
TEST_F(MapTopNKeysTest, multipleMaps) { | ||
RowVectorPtr input = makeRowVector({ | ||
makeMapVectorFromJson<int32_t, int64_t>({ | ||
"{3:1, 2:1, 5:1, 4:1, 1:1}", | ||
"{3:1, 2:1, 1:1}", | ||
"{2:1, 1:1}", | ||
}), | ||
}); | ||
|
||
assertEqualVectors( | ||
evaluate("map_top_n_keys(c0, 3)", input), | ||
makeArrayVectorFromJson<int32_t>({ | ||
"[5, 4, 3]", | ||
"[3, 2, 1]", | ||
"[2, 1]", | ||
})); | ||
} | ||
|
||
TEST_F(MapTopNKeysTest, nIsZero) { | ||
RowVectorPtr input = makeRowVector({ | ||
makeMapVectorFromJson<int32_t, int64_t>({ | ||
"{2:1, 1:1}", | ||
}), | ||
}); | ||
|
||
assertEqualVectors( | ||
evaluate("map_top_n_keys(c0, 0)", input), | ||
makeArrayVectorFromJson<int32_t>({"[]"})); | ||
} | ||
|
||
TEST_F(MapTopNKeysTest, nIsNegative) { | ||
RowVectorPtr input = makeRowVector({ | ||
makeMapVectorFromJson<int32_t, int64_t>({ | ||
"{2:1, 1:1}", | ||
}), | ||
}); | ||
|
||
VELOX_ASSERT_THROW( | ||
evaluate("map_top_n_keys(c0, -1)", input), | ||
"n must be greater than or equal to 0"); | ||
} | ||
|
||
} // namespace | ||
} // namespace facebook::velox::functions |