|
| 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 | + |
| 17 | +#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h" |
| 18 | + |
| 19 | +namespace facebook::velox { |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +class RegexpSplitTest : public functions::test::FunctionBaseTest {}; |
| 24 | + |
| 25 | +TEST_F(RegexpSplitTest, split) { |
| 26 | + auto input = makeRowVector({ |
| 27 | + makeFlatVector<std::string>({ |
| 28 | + "1a 2b 14m", |
| 29 | + "1a 2b 14", |
| 30 | + "", |
| 31 | + "a123b", |
| 32 | + }), |
| 33 | + }); |
| 34 | + auto result = evaluate("regexp_split(c0, '\\s*[a-z]+\\s*')", input); |
| 35 | + |
| 36 | + auto expected = makeArrayVector<std::string>({ |
| 37 | + {"1", "2", "14", ""}, |
| 38 | + {"1", "2", "14"}, |
| 39 | + {""}, |
| 40 | + {"", "123", ""}, |
| 41 | + }); |
| 42 | + test::assertEqualVectors(expected, result); |
| 43 | + |
| 44 | + result = evaluate("regexp_split(c0, '\\s*\\d+\\s*')", input); |
| 45 | + expected = makeArrayVector<std::string>({ |
| 46 | + {"", "a", "b", "m"}, |
| 47 | + {"", "a", "b", ""}, |
| 48 | + {""}, |
| 49 | + {"a", "b"}, |
| 50 | + }); |
| 51 | + test::assertEqualVectors(expected, result); |
| 52 | + |
| 53 | + // Test for empty matches |
| 54 | + result = evaluate("regexp_split(c0, '')", input); |
| 55 | + expected = makeArrayVector<std::string>({ |
| 56 | + {"", "1", "a", " ", "2", "b", " ", "1", "4", "m", ""}, |
| 57 | + {"", "1", "a", " ", "2", "b", " ", "1", "4", ""}, |
| 58 | + {"", ""}, |
| 59 | + {"", "a", "1", "2", "3", "b", ""}, |
| 60 | + }); |
| 61 | + test::assertEqualVectors(expected, result); |
| 62 | + |
| 63 | + // Test for another case of empty matches |
| 64 | + result = evaluate("regexp_split(c0, '\\s*[a-z]*\\s*')", input); |
| 65 | + expected = makeArrayVector<std::string>({ |
| 66 | + {"", "1", "", "2", "", "1", "4", "", ""}, |
| 67 | + {"", "1", "", "2", "", "1", "4", ""}, |
| 68 | + {"", ""}, |
| 69 | + {"", "", "1", "2", "3", "", ""}, |
| 70 | + }); |
| 71 | + test::assertEqualVectors(expected, result); |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace |
| 75 | +} // namespace facebook::velox |
0 commit comments