Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Spark array_append function #12043

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions velox/docs/functions/spark/array.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ Array Functions

SELECT array(1, 2, 3); -- [1,2,3]

.. spark:function:: array_append(array(E), value) -> array(E)

Add the element at the end of the array passed as first argument.
Type of element should be similar to type of the elements of the array.
Null element is also appended into the array. But if the array passed, is NULL output is NULL ::

SELECT array_append(array(1, 2, 3), 2); -- [1, 2, 3, 2]
SELECT array_append(array(1, 2, 3), NULL); -- [1, 2, 3, NULL]

.. spark:function:: array_contains(array(E), value) -> boolean

Returns true if the array contains the value. ::
Expand Down
40 changes: 40 additions & 0 deletions velox/functions/sparksql/ArrayAppend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*/
#pragma once

#include "velox/functions/Macros.h"

namespace facebook::velox::functions::sparksql {

template <typename TExec>
struct ArrayAppendFunction {
VELOX_DEFINE_FUNCTION_TYPES(TExec);

FOLLY_ALWAYS_INLINE bool callNullable(
out_type<Array<Generic<T1>>>& out,
const arg_type<Array<Generic<T1>>>* array,
const arg_type<Generic<T1>>* element) {
leoluan2009 marked this conversation as resolved.
Show resolved Hide resolved
if (array == nullptr) {
return false;
}
out.reserve(array->size() + 1);
out.add_items(*array);
out.push_back(*element);
return true;
}
};

} // namespace facebook::velox::functions::sparksql
6 changes: 6 additions & 0 deletions velox/functions/sparksql/registration/RegisterArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "velox/functions/lib/Repeat.h"
#include "velox/functions/lib/Slice.h"
#include "velox/functions/prestosql/ArrayFunctions.h"
#include "velox/functions/sparksql/ArrayAppend.h"
#include "velox/functions/sparksql/ArrayFlattenFunction.h"
#include "velox/functions/sparksql/ArrayInsert.h"
#include "velox/functions/sparksql/ArrayMinMaxFunction.h"
Expand Down Expand Up @@ -141,6 +142,11 @@ void registerArrayFunctions(const std::string& prefix) {
makeArrayShuffleWithCustomSeed,
getMetadataForArrayShuffle());
registerIntegerSliceFunction(prefix);
registerFunction<
ArrayAppendFunction,
Array<Generic<T1>>,
Array<Generic<T1>>,
Generic<T1>>({prefix + "array_append"});
}

} // namespace sparksql
Expand Down
68 changes: 68 additions & 0 deletions velox/functions/sparksql/tests/ArrayAppendTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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/sparksql/tests/SparkFunctionBaseTest.h"

using namespace facebook::velox::test;

namespace facebook::velox::functions::sparksql::test {
namespace {
class ArrayAppendTest : public SparkFunctionBaseTest {
protected:
void testExpression(
const std::string& expression,
const std::vector<VectorPtr>& input,
const VectorPtr& expected) {
auto result = evaluate(expression, makeRowVector(input));
assertEqualVectors(expected, result);
}
};

TEST_F(ArrayAppendTest, intArrays) {
const auto arrayVector = makeArrayVector<int64_t>(
{{1, 2, 3, 4}, {3, 4, 5}, {7, 8, 9}, {10, 20, 30}});
const auto elementVector = makeFlatVector<int64_t>({11, 22, 33, 44});
VectorPtr expected;

expected = makeArrayVector<int64_t>({
{1, 2, 3, 4, 11},
{3, 4, 5, 22},
{7, 8, 9, 33},
{10, 20, 30, 44},
});
testExpression(
"array_append(c0, c1)", {arrayVector, elementVector}, expected);
}

TEST_F(ArrayAppendTest, nullArrays) {
const auto arrayVector = makeNullableArrayVector<int64_t>(
{{1, 2, 3, std::nullopt}, {3, 4, 5}, {7, 8, 9}, {10, 20, std::nullopt}});
const auto elementVector =
makeNullableFlatVector<int64_t>({11, std::nullopt, 33, std::nullopt});
VectorPtr expected;

expected = makeNullableArrayVector<int64_t>({
{1, 2, 3, std::nullopt, 11},
{3, 4, 5, std::nullopt},
{7, 8, 9, 33},
{10, 20, std::nullopt, std::nullopt},
});
testExpression(
"array_append(c0, c1)", {arrayVector, elementVector}, expected);
}

} // namespace
} // namespace facebook::velox::functions::sparksql::test
1 change: 1 addition & 0 deletions velox/functions/sparksql/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
add_executable(
velox_functions_spark_test
ArithmeticTest.cpp
ArrayAppendTest.cpp
ArrayFlattenTest.cpp
ArrayGetTest.cpp
ArrayInsertTest.cpp
Expand Down
Loading