2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
2019-08-04 23:24:16 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Columns/ColumnsCommon.h>
|
2022-08-17 20:13:23 +00:00
|
|
|
#include <Common/ErrorCodes.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2019-08-04 23:24:16 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2022-08-17 20:13:23 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2019-08-04 23:24:16 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2018-09-08 22:04:39 +00:00
|
|
|
extern const int FUNCTION_THROW_IF_VALUE_IS_NON_ZERO;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
/// Throw an exception if the argument is non zero.
|
2022-08-17 20:13:23 +00:00
|
|
|
class FunctionThrowIf : public IFunction, WithContext
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "throwIf";
|
2022-08-17 19:09:56 +00:00
|
|
|
|
2022-08-17 20:13:23 +00:00
|
|
|
static FunctionPtr create(ContextPtr context_)
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
2022-08-17 20:13:23 +00:00
|
|
|
return std::make_shared<FunctionThrowIf>(context_);
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 20:13:23 +00:00
|
|
|
explicit FunctionThrowIf(ContextPtr context_) : WithContext(context_) {}
|
2022-08-17 19:09:56 +00:00
|
|
|
String getName() const override { return name; }
|
2019-08-04 23:24:16 +00:00
|
|
|
bool isVariadic() const override { return true; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2022-08-17 19:09:56 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2019-08-04 23:24:16 +00:00
|
|
|
const size_t number_of_arguments = arguments.size();
|
|
|
|
|
2022-08-17 20:13:23 +00:00
|
|
|
const auto & settings = getContext()->getSettingsRef();
|
|
|
|
bool allow_custom_error_code_argument = settings.allow_custom_error_code_in_throwif;
|
|
|
|
|
|
|
|
if (number_of_arguments < 1 || number_of_arguments > (allow_custom_error_code_argument ? 3 : 2))
|
2021-12-20 13:00:43 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
2022-08-17 20:13:23 +00:00
|
|
|
"Number of arguments for function {} doesn't match: passed {}, should be {}",
|
|
|
|
getName(), toString(number_of_arguments), allow_custom_error_code_argument ? "1 or 2 or 3" : "1 or 2");
|
2019-08-04 23:24:16 +00:00
|
|
|
|
|
|
|
if (!isNativeNumber(arguments[0]))
|
2021-12-20 13:00:43 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
2022-08-17 20:13:23 +00:00
|
|
|
"First argument of function {} must be a number (passed: {})", getName(), arguments[0]->getName());
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2019-08-04 23:24:16 +00:00
|
|
|
if (number_of_arguments > 1 && !isString(arguments[1]))
|
2021-12-20 13:00:43 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
2022-08-17 20:13:23 +00:00
|
|
|
"Second argument of function {} must be a string (passed: {})", getName(), arguments[1]->getName());
|
|
|
|
|
|
|
|
if (allow_custom_error_code_argument)
|
|
|
|
if (number_of_arguments > 2 && !isNumber(arguments[2]))
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Third argument of function {} must be a number (passed: {})", getName(), arguments[2]->getName());
|
2019-08-04 23:24:16 +00:00
|
|
|
|
|
|
|
|
2018-09-08 22:04:39 +00:00
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
2021-12-20 11:49:05 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return false; }
|
2022-08-17 20:13:23 +00:00
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1, 2}; }
|
2021-12-20 13:00:43 +00:00
|
|
|
|
|
|
|
/** Prevent constant folding for FunctionThrowIf because for short circuit evaluation
|
|
|
|
* it is unsafe to evaluate this function during DAG analysis.
|
|
|
|
*/
|
2021-12-20 11:49:05 +00:00
|
|
|
bool isSuitableForConstantFolding() const override { return false; }
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2021-12-20 11:49:05 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
2021-12-20 11:49:05 +00:00
|
|
|
if (input_rows_count == 0)
|
|
|
|
return result_type->createColumn();
|
|
|
|
|
2019-08-04 23:24:16 +00:00
|
|
|
std::optional<String> custom_message;
|
2019-08-05 06:38:52 +00:00
|
|
|
if (arguments.size() == 2)
|
|
|
|
{
|
2021-12-20 13:00:43 +00:00
|
|
|
const auto * message_column = checkAndGetColumnConst<ColumnString>(arguments[1].column.get());
|
|
|
|
if (!message_column)
|
2022-08-17 19:09:56 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Second argument for function {} must be constant String", getName());
|
2021-12-20 13:00:43 +00:00
|
|
|
|
|
|
|
custom_message = message_column->getValue<String>();
|
2019-08-04 23:24:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 20:13:23 +00:00
|
|
|
std::optional<ErrorCodes::ErrorCode> custom_error_code;
|
|
|
|
if (arguments.size() == 3)
|
|
|
|
{
|
|
|
|
if (!isColumnConst(*(arguments[2].column)))
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Third argument for function {} must be constant number", getName());
|
|
|
|
|
|
|
|
custom_error_code = arguments[2].column->getUInt(0);
|
|
|
|
}
|
|
|
|
|
2021-12-20 11:49:05 +00:00
|
|
|
auto first_argument_column = arguments.front().column;
|
2021-12-20 13:00:43 +00:00
|
|
|
const auto * in = first_argument_column.get();
|
2020-10-19 15:27:41 +00:00
|
|
|
|
|
|
|
ColumnPtr res;
|
2022-08-17 20:13:23 +00:00
|
|
|
if (!((res = execute<UInt8>(in, custom_message, custom_error_code))
|
|
|
|
|| (res = execute<UInt16>(in, custom_message, custom_error_code))
|
|
|
|
|| (res = execute<UInt32>(in, custom_message, custom_error_code))
|
|
|
|
|| (res = execute<UInt64>(in, custom_message, custom_error_code))
|
|
|
|
|| (res = execute<Int8>(in, custom_message, custom_error_code))
|
|
|
|
|| (res = execute<Int16>(in, custom_message, custom_error_code))
|
|
|
|
|| (res = execute<Int32>(in, custom_message, custom_error_code))
|
|
|
|
|| (res = execute<Int64>(in, custom_message, custom_error_code))
|
|
|
|
|| (res = execute<Float32>(in, custom_message, custom_error_code))
|
|
|
|
|| (res = execute<Float64>(in, custom_message, custom_error_code))))
|
2021-12-20 13:00:43 +00:00
|
|
|
{
|
2022-08-17 19:09:56 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of first argument of function {}", in->getName(), getName());
|
2021-12-20 13:00:43 +00:00
|
|
|
}
|
2020-10-19 15:27:41 +00:00
|
|
|
|
|
|
|
return res;
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 19:09:56 +00:00
|
|
|
private:
|
2018-09-08 22:04:39 +00:00
|
|
|
template <typename T>
|
2022-08-17 20:13:23 +00:00
|
|
|
ColumnPtr execute(const IColumn * in_untyped, const std::optional<String> & message, const std::optional<ErrorCodes::ErrorCode> & error_code) const
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
2021-12-20 13:00:43 +00:00
|
|
|
const auto * in = checkAndGetColumn<ColumnVector<T>>(in_untyped);
|
|
|
|
|
|
|
|
if (!in)
|
|
|
|
in = checkAndGetColumnConstData<ColumnVector<T>>(in_untyped);
|
|
|
|
|
|
|
|
if (in)
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
const auto & in_data = in->getData();
|
2022-03-07 13:48:38 +00:00
|
|
|
if (!memoryIsZero(in_data.data(), 0, in_data.size() * sizeof(in_data[0])))
|
2021-12-20 13:55:15 +00:00
|
|
|
{
|
2022-08-17 20:13:23 +00:00
|
|
|
throw Exception(
|
|
|
|
error_code.value_or(ErrorCodes::FUNCTION_THROW_IF_VALUE_IS_NON_ZERO),
|
2022-08-17 19:09:56 +00:00
|
|
|
message.value_or("Value passed to '" + getName() + "' function is non-zero"));
|
2021-12-20 13:55:15 +00:00
|
|
|
}
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2022-03-22 11:30:25 +00:00
|
|
|
size_t result_size = in_untyped->size();
|
|
|
|
|
2018-09-08 22:04:39 +00:00
|
|
|
/// We return non constant to avoid constant folding.
|
2022-03-22 11:30:25 +00:00
|
|
|
return ColumnUInt8::create(result_size, 0);
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 15:27:41 +00:00
|
|
|
return nullptr;
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(ThrowIf)
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionThrowIf>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|