|
| 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/common/base/tests/GTestUtils.h" |
| 18 | +#include "velox/functions/sparksql/tests/SparkFunctionBaseTest.h" |
| 19 | + |
| 20 | +using namespace facebook::velox; |
| 21 | +using namespace facebook::velox::test; |
| 22 | + |
| 23 | +namespace facebook::velox::functions::sparksql::test { |
| 24 | +namespace { |
| 25 | + |
| 26 | +class ArrayUnionTest : public SparkFunctionBaseTest { |
| 27 | + protected: |
| 28 | + void testExpression( |
| 29 | + const std::string& expression, |
| 30 | + const std::vector<VectorPtr>& input, |
| 31 | + const VectorPtr& expected) { |
| 32 | + auto result = evaluate(expression, makeRowVector(input)); |
| 33 | + assertEqualVectors(expected, result); |
| 34 | + } |
| 35 | + |
| 36 | + template <typename T> |
| 37 | + void testFloatArray() { |
| 38 | + const auto array1 = makeArrayVector<T>( |
| 39 | + {{1.99, 2.78, 3.98, 4.01}, |
| 40 | + {3.89, 4.99, 5.13}, |
| 41 | + {7.13, 8.91, std::numeric_limits<T>::quiet_NaN()}, |
| 42 | + {10.02, 20.01, std::numeric_limits<T>::quiet_NaN()}}); |
| 43 | + const auto array2 = makeArrayVector<T>( |
| 44 | + {{2.78, 4.01, 5.99}, |
| 45 | + {3.89, 4.99, 5.13}, |
| 46 | + {7.13, 8.91, std::numeric_limits<T>::quiet_NaN()}, |
| 47 | + {40.99, 50.12}}); |
| 48 | + |
| 49 | + VectorPtr expected; |
| 50 | + expected = makeArrayVector<T>({ |
| 51 | + {1.99, 2.78, 3.98, 4.01, 5.99}, |
| 52 | + {3.89, 4.99, 5.13}, |
| 53 | + {7.13, 8.91, std::numeric_limits<T>::quiet_NaN()}, |
| 54 | + {10.02, 20.01, std::numeric_limits<T>::quiet_NaN(), 40.99, 50.12}, |
| 55 | + }); |
| 56 | + testExpression("array_union(c0, c1)", {array1, array2}, expected); |
| 57 | + |
| 58 | + expected = makeArrayVector<T>({ |
| 59 | + {2.78, 4.01, 5.99, 1.99, 3.98}, |
| 60 | + {3.89, 4.99, 5.13}, |
| 61 | + {7.13, 8.91, std::numeric_limits<T>::quiet_NaN()}, |
| 62 | + {40.99, 50.12, 10.02, 20.01, std::numeric_limits<T>::quiet_NaN()}, |
| 63 | + }); |
| 64 | + testExpression("array_union(c0, c1)", {array2, array1}, expected); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +// Union two integer arrays. |
| 69 | +TEST_F(ArrayUnionTest, intArray) { |
| 70 | + const auto array1 = makeArrayVector<int64_t>( |
| 71 | + {{1, 2, 3, 4}, {3, 4, 5}, {7, 8, 9}, {10, 20, 30}}); |
| 72 | + const auto array2 = |
| 73 | + makeArrayVector<int64_t>({{2, 4, 5}, {3, 4, 5}, {}, {40, 50}}); |
| 74 | + VectorPtr expected; |
| 75 | + |
| 76 | + expected = makeArrayVector<int64_t>({ |
| 77 | + {1, 2, 3, 4, 5}, |
| 78 | + {3, 4, 5}, |
| 79 | + {7, 8, 9}, |
| 80 | + {10, 20, 30, 40, 50}, |
| 81 | + }); |
| 82 | + testExpression("array_union(c0, c1)", {array1, array2}, expected); |
| 83 | + |
| 84 | + expected = makeArrayVector<int64_t>({ |
| 85 | + {2, 4, 5, 1, 3}, |
| 86 | + {3, 4, 5}, |
| 87 | + {7, 8, 9}, |
| 88 | + {40, 50, 10, 20, 30}, |
| 89 | + }); |
| 90 | + testExpression("array_union(c0, c1)", {array2, array1}, expected); |
| 91 | +} |
| 92 | + |
| 93 | +// Union two float or double arrays. |
| 94 | +TEST_F(ArrayUnionTest, floatArray) { |
| 95 | + testFloatArray<float>(); |
| 96 | + testFloatArray<double>(); |
| 97 | +} |
| 98 | + |
| 99 | +// Union two string arrays. |
| 100 | +TEST_F(ArrayUnionTest, stringArray) { |
| 101 | + const auto array1 = |
| 102 | + makeArrayVector<StringView>({{"foo", "bar"}, {"foo", "baz"}}); |
| 103 | + const auto array2 = |
| 104 | + makeArrayVector<StringView>({{"foo", "bar"}, {"bar", "baz"}}); |
| 105 | + VectorPtr expected; |
| 106 | + |
| 107 | + expected = makeArrayVector<StringView>({ |
| 108 | + {"foo", "bar"}, |
| 109 | + {"foo", "baz", "bar"}, |
| 110 | + }); |
| 111 | + testExpression("array_union(c0, c1)", {array1, array2}, expected); |
| 112 | +} |
| 113 | + |
| 114 | +// Union two integer arrays with null. |
| 115 | +TEST_F(ArrayUnionTest, nullArray) { |
| 116 | + const auto array1 = makeNullableArrayVector<int64_t>({ |
| 117 | + {{1, std::nullopt, 3, 4}}, |
| 118 | + {7, 8, 9}, |
| 119 | + {{10, std::nullopt, std::nullopt}}, |
| 120 | + }); |
| 121 | + const auto array2 = makeNullableArrayVector<int64_t>({ |
| 122 | + {{std::nullopt, std::nullopt, 3, 5}}, |
| 123 | + std::nullopt, |
| 124 | + {{1, 10}}, |
| 125 | + }); |
| 126 | + VectorPtr expected; |
| 127 | + |
| 128 | + expected = makeNullableArrayVector<int64_t>({ |
| 129 | + {{1, std::nullopt, 3, 4, 5}}, |
| 130 | + std::nullopt, |
| 131 | + {{10, std::nullopt, 1}}, |
| 132 | + }); |
| 133 | + testExpression("array_union(c0, c1)", {array1, array2}, expected); |
| 134 | + |
| 135 | + expected = makeNullableArrayVector<int64_t>({ |
| 136 | + {{std::nullopt, 3, 5, 1, 4}}, |
| 137 | + std::nullopt, |
| 138 | + {{1, 10, std::nullopt}}, |
| 139 | + }); |
| 140 | + testExpression("array_union(c0, c1)", {array2, array1}, expected); |
| 141 | +} |
| 142 | + |
| 143 | +// Union array vectors. |
| 144 | +TEST_F(ArrayUnionTest, complexTypes) { |
| 145 | + auto baseVector = makeArrayVector<int64_t>( |
| 146 | + {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}}); |
| 147 | + |
| 148 | + // Create arrays of array vector using above base vector. |
| 149 | + // [[1, 1], [2, 2]] |
| 150 | + // [[3, 3], [4, 4]] |
| 151 | + // [[5, 5], [6, 6]] |
| 152 | + auto arrayOfArrays1 = makeArrayVector({0, 2, 4}, baseVector); |
| 153 | + // [[1, 1], [2, 2], [3, 3]] |
| 154 | + // [[4, 4]] |
| 155 | + // [[5, 5], [6, 6]] |
| 156 | + auto arrayOfArrays2 = makeArrayVector({0, 3, 4}, baseVector); |
| 157 | + |
| 158 | + // [[1, 1], [2, 2], [3, 3]] |
| 159 | + // [[3, 3], [4, 4]] |
| 160 | + // [[5, 5], [6, 6]] |
| 161 | + auto expected = makeArrayVector( |
| 162 | + {0, 3, 5}, |
| 163 | + makeArrayVector<int64_t>( |
| 164 | + {{1, 1}, {2, 2}, {3, 3}, {3, 3}, {4, 4}, {5, 5}, {6, 6}})); |
| 165 | + |
| 166 | + testExpression( |
| 167 | + "array_union(c0, c1)", {arrayOfArrays1, arrayOfArrays2}, expected); |
| 168 | +} |
| 169 | + |
| 170 | +// Union double array vectors. |
| 171 | +TEST_F(ArrayUnionTest, complexDoubleType) { |
| 172 | + auto baseVector = makeArrayVector<double>( |
| 173 | + {{1.0, 1.0}, |
| 174 | + {2.0, 2.0}, |
| 175 | + {3.0, 3.0}, |
| 176 | + {4.0, 4.0}, |
| 177 | + {5.0, std::numeric_limits<double>::quiet_NaN()}, |
| 178 | + {6.0, 6.0}}); |
| 179 | + |
| 180 | + // Create arrays of array vector using above base vector. |
| 181 | + // [[1.0, 1.0], [2.0, 2.0]] |
| 182 | + // [[3.0, 3.0], [4.0, 4.0]] |
| 183 | + // [[5.0, NaN], [6.0, 6.0]] |
| 184 | + auto arrayOfArrays1 = makeArrayVector({0, 2, 4}, baseVector); |
| 185 | + // [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]] |
| 186 | + // [[4.0, 4.0]] |
| 187 | + // [[5.0, NaN], [6.0, 6.0]] |
| 188 | + auto arrayOfArrays2 = makeArrayVector({0, 3, 4}, baseVector); |
| 189 | + |
| 190 | + // [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]] |
| 191 | + // [[3.0, 3.0], [4.0, 4.0]] |
| 192 | + // [[5.0, NaN], [6.0, 6.0]] |
| 193 | + auto expected = makeArrayVector( |
| 194 | + {0, 3, 5}, |
| 195 | + makeArrayVector<double>( |
| 196 | + {{1.0, 1.0}, |
| 197 | + {2.0, 2.0}, |
| 198 | + {3.0, 3.0}, |
| 199 | + {3.0, 3.0}, |
| 200 | + {4.0, 4.0}, |
| 201 | + {5.0, std::numeric_limits<double>::quiet_NaN()}, |
| 202 | + {6.0, 6.0}})); |
| 203 | + |
| 204 | + testExpression( |
| 205 | + "array_union(c0, c1)", {arrayOfArrays1, arrayOfArrays2}, expected); |
| 206 | +} |
| 207 | +} // namespace |
| 208 | +} // namespace facebook::velox::functions::sparksql::test |
0 commit comments