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

Add Spark raise_error function #10110

Closed
4 changes: 4 additions & 0 deletions velox/docs/functions/spark/misc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Miscellaneous Functions
The function relies on partition IDs, which are provided by the framework
via the configuration 'spark.partition_id'.

.. spark:function:: raise_error(str) -> void
gaoyangxiaozhu marked this conversation as resolved.
Show resolved Hide resolved
gaoyangxiaozhu marked this conversation as resolved.
Show resolved Hide resolved

Throws an runtime exception with ``str``.
gaoyangxiaozhu marked this conversation as resolved.
Show resolved Hide resolved

.. spark:function:: spark_partition_id() -> integer

Returns the current partition id.
Expand Down
31 changes: 31 additions & 0 deletions velox/functions/sparksql/RaiseError.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 T>
struct RaiseErrorFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);

FOLLY_ALWAYS_INLINE void call(
gaoyangxiaozhu marked this conversation as resolved.
Show resolved Hide resolved
UnknownValue& result,
const arg_type<Varchar>& input) {
throw std::runtime_error(input);
gaoyangxiaozhu marked this conversation as resolved.
Show resolved Hide resolved
}
};
} // namespace facebook::velox::functions::sparksql
4 changes: 4 additions & 0 deletions velox/functions/sparksql/Register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "velox/functions/sparksql/LeastGreatest.h"
#include "velox/functions/sparksql/MightContain.h"
#include "velox/functions/sparksql/MonotonicallyIncreasingId.h"
#include "velox/functions/sparksql/RaiseError.h"
#include "velox/functions/sparksql/RegexFunctions.h"
#include "velox/functions/sparksql/RegisterArithmetic.h"
#include "velox/functions/sparksql/RegisterCompare.h"
Expand Down Expand Up @@ -460,6 +461,9 @@ void registerFunctions(const std::string& prefix) {
Array<Array<Generic<T1>>>>({prefix + "flatten"});

registerFunction<SoundexFunction, Varchar, Varchar>({prefix + "soundex"});

registerFunction<RaiseErrorFunction, UnknownValue, Varchar>(
{prefix + "raise_error"});
}

} // namespace sparksql
Expand Down
1 change: 1 addition & 0 deletions velox/functions/sparksql/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ add_executable(
MapTest.cpp
MightContainTest.cpp
MonotonicallyIncreasingIdTest.cpp
RaiseErrorTest.cpp
RandTest.cpp
RegexFunctionsTest.cpp
SizeTest.cpp
Expand Down
35 changes: 35 additions & 0 deletions velox/functions/sparksql/tests/RaiseErrorTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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/common/base/tests/GTestUtils.h"
#include "velox/functions/sparksql/tests/SparkFunctionBaseTest.h"

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

class RaiseErrorTest : public SparkFunctionBaseTest {
protected:
void raiseError(const std::optional<std::string>& errMsg) {
gaoyangxiaozhu marked this conversation as resolved.
Show resolved Hide resolved
evaluateOnce<UnknownValue>("raise_error(c0)", errMsg);
}
};

TEST_F(RaiseErrorTest, basic) {
VELOX_ASSERT_THROW(raiseError(""), "");
gaoyangxiaozhu marked this conversation as resolved.
Show resolved Hide resolved
VELOX_ASSERT_THROW(raiseError("0 > 1 is not true"), "0 > 1 is not true");
}
} // namespace
} // namespace facebook::velox::functions::sparksql::test
Loading