Skip to content

Commit d777ca1

Browse files
amitkduttafacebook-github-bot
authored andcommitted
feat: Add map_key_exists function (facebookincubator#11735)
Summary: Pull Request resolved: facebookincubator#11735 Reviewed By: xiaoxmeng, kgpai Differential Revision: D65014614
1 parent 4cffa4b commit d777ca1

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#pragma once
17+
18+
#include "velox/expression/ComplexViewTypes.h"
19+
#include "velox/functions/Udf.h"
20+
21+
namespace facebook::velox::functions {
22+
23+
template <typename TExec>
24+
struct MapKeyExists {
25+
VELOX_DEFINE_FUNCTION_TYPES(TExec);
26+
27+
void call(
28+
bool& out,
29+
const arg_type<Map<Generic<T1>, Generic<T2>>>& inputMap,
30+
const arg_type<Generic<T1>>& key) {
31+
out = (inputMap.find(key) != inputMap.end());
32+
}
33+
};
34+
35+
} // namespace facebook::velox::functions

velox/functions/prestosql/MapRemoveNullValues.h

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
#pragma once
1717

18+
#include "velox/expression/ComplexViewTypes.h"
19+
#include "velox/functions/Udf.h"
20+
1821
namespace facebook::velox::functions {
1922

2023
template <typename TExec>

velox/functions/prestosql/registration/MapFunctionsRegistration.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "velox/functions/Registerer.h"
1919
#include "velox/functions/lib/MapConcat.h"
2020
#include "velox/functions/prestosql/Map.h"
21+
#include "velox/functions/prestosql/MapFunctions.h"
2122
#include "velox/functions/prestosql/MapNormalize.h"
2223
#include "velox/functions/prestosql/MapRemoveNullValues.h"
2324
#include "velox/functions/prestosql/MapSubset.h"
@@ -69,6 +70,14 @@ void registerMapRemoveNullValues(const std::string& prefix) {
6970
Map<Generic<T1>, Generic<T2>>>({prefix + "map_remove_null_values"});
7071
}
7172

73+
void registerMapKeyExists(const std::string& prefix) {
74+
registerFunction<
75+
MapKeyExists,
76+
bool,
77+
Map<Generic<T1>, Generic<T2>>,
78+
Generic<T1>>({prefix + "map_key_exists"});
79+
}
80+
7281
} // namespace
7382

7483
void registerMapFunctions(const std::string& prefix) {
@@ -116,6 +125,8 @@ void registerMapFunctions(const std::string& prefix) {
116125

117126
registerMapRemoveNullValues(prefix);
118127

128+
registerMapKeyExists(prefix);
129+
119130
registerFunction<
120131
MapNormalizeFunction,
121132
Map<Varchar, double>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "velox/common/base/tests/GTestUtils.h"
17+
#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h"
18+
19+
using namespace facebook::velox::test;
20+
21+
namespace facebook::velox::functions {
22+
namespace {
23+
24+
class MapKeyExistsTest : public test::FunctionBaseTest {};
25+
26+
TEST_F(MapKeyExistsTest, general) {
27+
// numeric key
28+
auto data = makeRowVector({makeMapVectorFromJson<int64_t, int32_t>(
29+
{"{1:4, 10:5, 3:6}", "{-2:5}", "{}"})});
30+
31+
auto result = evaluate("map_key_exists(c0, 1)", data);
32+
auto expected = makeFlatVector<bool>({true, false, false});
33+
34+
assertEqualVectors(expected, result);
35+
36+
// string key
37+
data = makeRowVector({makeMapVectorFromJson<std::string, std::string>({
38+
"{\"ad\":null, \"bc\":null, \"cd\":null}",
39+
})});
40+
41+
result = evaluate("map_key_exists(c0, 'bc')", data);
42+
expected = makeFlatVector<bool>(true);
43+
44+
assertEqualVectors(expected, result);
45+
}
46+
47+
TEST_F(MapKeyExistsTest, testFloats) {
48+
auto data = makeRowVector({makeMapVectorFromJson<double, int32_t>({
49+
"{1:10, NaN:20, 0:null, 4:40, 5:50, 6:60}",
50+
"{NaN:20, -0:20}",
51+
})});
52+
53+
auto result = evaluate("map_key_exists(c0, cast('NaN' as REAL))", data);
54+
auto expected = makeFlatVector<bool>({true, true});
55+
56+
result = evaluate("map_key_exists(c0, cast('0' as REAL))", data);
57+
expected = makeFlatVector<bool>({true, false});
58+
59+
result = evaluate("map_key_exists(c0, cast('-0' as REAL))", data);
60+
expected = makeFlatVector<bool>({true, true});
61+
62+
assertEqualVectors(expected, result);
63+
}
64+
65+
} // namespace
66+
} // namespace facebook::velox::functions

0 commit comments

Comments
 (0)