|
| 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