forked from facebookincubator/velox
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add array_frequency Presto function (facebookincubator#3807)
Summary: Pull Request resolved: facebookincubator#3807 Implement `array_frequency` in velox with same signature [as presto](https://prestodb.io/docs/current/functions/array.html#array_frequency). Fixes facebookincubator#3752 Reviewed By: Yuhta Differential Revision: D42712223 fbshipit-source-id: cf29ac047b6988b5fd72d320887359064ab7573a
- Loading branch information
1 parent
534149a
commit cf55fb5
Showing
4 changed files
with
140 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
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,93 @@ | ||
/* | ||
* 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/functions/prestosql/tests/utils/FunctionBaseTest.h" | ||
|
||
using namespace facebook::velox; | ||
using namespace facebook::velox::test; | ||
|
||
namespace { | ||
class ArrayFrequencyTest : public functions::test::FunctionBaseTest { | ||
protected: | ||
// Evaluate an expression. | ||
void testExpr( | ||
const VectorPtr& expected, | ||
const std::string& expression, | ||
const std::vector<VectorPtr>& input) { | ||
auto result = evaluate<BaseVector>(expression, makeRowVector(input)); | ||
assertEqualVectors(expected, result); | ||
} | ||
}; | ||
} // namespace | ||
|
||
TEST_F(ArrayFrequencyTest, integerArray) { | ||
auto array = makeNullableArrayVector<int64_t>( | ||
{{2, 1, 1, -2}, | ||
{}, | ||
{1, 2, 1, 1, 1, 1}, | ||
{-1, std::nullopt, -1, -1}, | ||
{std::numeric_limits<int64_t>::max(), | ||
std::numeric_limits<int64_t>::max(), | ||
1, | ||
std::nullopt, | ||
0, | ||
1, | ||
std::nullopt, | ||
0}}); | ||
|
||
auto expectedKeys = makeFlatVector<int64_t>( | ||
{1, | ||
2, | ||
-2, | ||
// empty key | ||
1, | ||
2, | ||
-1, | ||
std::numeric_limits<int64_t>::max(), | ||
1, | ||
0}); | ||
auto expectedValues = makeFlatVector<int>( | ||
{2, | ||
1, | ||
1, | ||
// empty value | ||
5, | ||
1, | ||
3, | ||
2, | ||
2, | ||
2}); | ||
auto expected = makeMapVector({0, 3, 3, 5, 6}, expectedKeys, expectedValues); | ||
|
||
testExpr(expected, "array_frequency(C0)", {array}); | ||
} | ||
|
||
TEST_F(ArrayFrequencyTest, varcharArray) { | ||
using S = StringView; | ||
|
||
auto array = makeNullableArrayVector<StringView>({ | ||
{S("hello"), S("world"), S("!"), S("!"), S("!")}, | ||
{}, | ||
{S("hello"), S("world"), std::nullopt, S("!"), S("!")}, | ||
}); | ||
|
||
auto expectedKeys = makeFlatVector<StringView>( | ||
{S("hello"), S("world"), S("!"), S("hello"), S("world"), S("!")}); | ||
auto expectedValues = makeFlatVector<int>({1, 1, 3, 1, 1, 2}); | ||
auto expected = makeMapVector({0, 3, 3}, expectedKeys, expectedValues); | ||
|
||
testExpr(expected, "array_frequency(C0)", {array}); | ||
} |