2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.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>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2019-08-04 23:24:16 +00:00
|
|
|
#include <IO/WriteHelpers.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Throw an exception if the argument is non zero.
|
|
|
|
class FunctionThrowIf : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "throwIf";
|
|
|
|
static FunctionPtr create(const Context &)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionThrowIf>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2019-08-04 23:24:16 +00:00
|
|
|
bool isVariadic() const override { return true; }
|
2018-09-08 22:04:39 +00:00
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
2019-08-05 06:38:52 +00:00
|
|
|
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();
|
|
|
|
|
|
|
|
if (number_of_arguments < 1 || number_of_arguments > 2)
|
|
|
|
throw Exception{"Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
+ toString(number_of_arguments) + ", should be 1 or 2",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
|
|
|
|
|
|
|
if (!isNativeNumber(arguments[0]))
|
2018-09-08 22:04:39 +00:00
|
|
|
throw Exception{"Argument for function " + getName() + " must be number", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
|
2019-08-04 23:24:16 +00:00
|
|
|
if (number_of_arguments > 1 && !isString(arguments[1]))
|
|
|
|
throw Exception{"Illegal type " + arguments[1]->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
|
|
|
|
|
2018-09-08 22:04:39 +00:00
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2019-08-05 06:38:52 +00:00
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override
|
|
|
|
{
|
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)
|
|
|
|
{
|
2019-08-04 23:24:16 +00:00
|
|
|
auto * msg_column = checkAndGetColumnConst<ColumnString>(block.getByPosition(arguments[1]).column.get());
|
|
|
|
if (!msg_column)
|
|
|
|
throw Exception{"Second argument for function " + getName() + " must be constant String", ErrorCodes::ILLEGAL_COLUMN};
|
|
|
|
custom_message = msg_column->getValue<String>();
|
|
|
|
}
|
|
|
|
|
2018-09-08 22:04:39 +00:00
|
|
|
const auto in = block.getByPosition(arguments.front()).column.get();
|
|
|
|
|
2019-08-04 23:24:16 +00:00
|
|
|
if ( !execute<UInt8>(block, in, result, custom_message)
|
|
|
|
&& !execute<UInt16>(block, in, result, custom_message)
|
|
|
|
&& !execute<UInt32>(block, in, result, custom_message)
|
|
|
|
&& !execute<UInt64>(block, in, result, custom_message)
|
|
|
|
&& !execute<Int8>(block, in, result, custom_message)
|
|
|
|
&& !execute<Int16>(block, in, result, custom_message)
|
|
|
|
&& !execute<Int32>(block, in, result, custom_message)
|
|
|
|
&& !execute<Int64>(block, in, result, custom_message)
|
|
|
|
&& !execute<Float32>(block, in, result, custom_message)
|
|
|
|
&& !execute<Float64>(block, in, result, custom_message))
|
2018-09-08 22:04:39 +00:00
|
|
|
throw Exception{"Illegal column " + in->getName() + " of first argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-08-04 23:24:16 +00:00
|
|
|
bool execute(Block & block, const IColumn * in_untyped, const size_t result, const std::optional<String> & message)
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
if (const auto in = checkAndGetColumn<ColumnVector<T>>(in_untyped))
|
|
|
|
{
|
|
|
|
const auto & in_data = in->getData();
|
|
|
|
if (!memoryIsZero(in_data.data(), in_data.size() * sizeof(in_data[0])))
|
2019-08-04 23:24:16 +00:00
|
|
|
throw Exception{message.value_or("Value passed to '" + getName() + "' function is non zero"),
|
|
|
|
ErrorCodes::FUNCTION_THROW_IF_VALUE_IS_NON_ZERO};
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
/// We return non constant to avoid constant folding.
|
|
|
|
block.getByPosition(result).column = ColumnUInt8::create(in_data.size(), 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void registerFunctionThrowIf(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionThrowIf>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|