ClickHouse
164 строки · 6.6 Кб
1#include <Functions/IFunction.h>
2#include <Functions/FunctionFactory.h>
3#include <Functions/FunctionHelpers.h>
4#include <Columns/ColumnString.h>
5#include <Columns/ColumnsNumber.h>
6#include <Columns/ColumnsCommon.h>
7#include <Common/ErrorCodes.h>
8#include <DataTypes/DataTypesNumber.h>
9#include <IO/WriteHelpers.h>
10#include <Interpreters/Context.h>
11
12namespace DB
13{
14namespace ErrorCodes
15{
16extern const int ILLEGAL_COLUMN;
17extern const int ILLEGAL_TYPE_OF_ARGUMENT;
18extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
19extern const int FUNCTION_THROW_IF_VALUE_IS_NON_ZERO;
20}
21
22namespace
23{
24
25/// Throw an exception if the argument is non zero.
26class FunctionThrowIf : public IFunction
27{
28public:
29static constexpr auto name = "throwIf";
30
31static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionThrowIf>(context); }
32
33explicit FunctionThrowIf(ContextPtr context_) : allow_custom_error_code_argument(context_->getSettingsRef().allow_custom_error_code_in_throwif) {}
34String getName() const override { return name; }
35bool isVariadic() const override { return true; }
36bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
37size_t getNumberOfArguments() const override { return 0; }
38
39DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
40{
41const size_t number_of_arguments = arguments.size();
42
43if (number_of_arguments < 1 || number_of_arguments > (allow_custom_error_code_argument ? 3 : 2))
44throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
45"Number of arguments for function {} doesn't match: passed {}, should be {}",
46getName(), number_of_arguments, allow_custom_error_code_argument ? "1 or 2 or 3" : "1 or 2");
47
48if (!isNativeNumber(arguments[0]))
49throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
50"First argument of function {} must be a number (passed: {})", getName(), arguments[0]->getName());
51
52if (number_of_arguments > 1 && !isString(arguments[1]))
53throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
54"Second argument of function {} must be a string (passed: {})", getName(), arguments[1]->getName());
55
56if (allow_custom_error_code_argument && number_of_arguments > 2)
57{
58WhichDataType which(arguments[2]);
59if (!(which.isInt8() || which.isInt16() || which.isInt32()))
60throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
61"Third argument of function {} must be Int8, Int16 or Int32 (passed: {})", getName(), arguments[2]->getName());
62}
63
64
65return std::make_shared<DataTypeUInt8>();
66}
67
68bool useDefaultImplementationForConstants() const override { return false; }
69ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1, 2}; }
70
71/** Prevent constant folding for FunctionThrowIf because for short circuit evaluation
72* it is unsafe to evaluate this function during DAG analysis.
73*/
74bool isSuitableForConstantFolding() const override { return false; }
75
76ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
77{
78if (input_rows_count == 0)
79return result_type->createColumn();
80
81std::optional<String> custom_message;
82if (arguments.size() == 2)
83{
84const auto * message_column = checkAndGetColumnConst<ColumnString>(arguments[1].column.get());
85if (!message_column)
86throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Second argument for function {} must be constant String", getName());
87
88custom_message = message_column->getValue<String>();
89}
90
91std::optional<ErrorCodes::ErrorCode> custom_error_code;
92if (allow_custom_error_code_argument && arguments.size() == 3)
93{
94if (!isColumnConst(*(arguments[2].column)))
95throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Third argument for function {} must be constant number", getName());
96
97custom_error_code = arguments[2].column->getInt(0);
98}
99
100auto first_argument_column = arguments.front().column;
101const auto * in = first_argument_column.get();
102
103ColumnPtr res;
104if (!((res = execute<UInt8>(in, custom_message, custom_error_code))
105|| (res = execute<UInt16>(in, custom_message, custom_error_code))
106|| (res = execute<UInt32>(in, custom_message, custom_error_code))
107|| (res = execute<UInt64>(in, custom_message, custom_error_code))
108|| (res = execute<Int8>(in, custom_message, custom_error_code))
109|| (res = execute<Int16>(in, custom_message, custom_error_code))
110|| (res = execute<Int32>(in, custom_message, custom_error_code))
111|| (res = execute<Int64>(in, custom_message, custom_error_code))
112|| (res = execute<Float32>(in, custom_message, custom_error_code))
113|| (res = execute<Float64>(in, custom_message, custom_error_code))))
114{
115throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of first argument of function {}", in->getName(), getName());
116}
117
118return res;
119}
120
121private:
122template <typename T>
123ColumnPtr execute(const IColumn * in_untyped, const std::optional<String> & message, const std::optional<ErrorCodes::ErrorCode> & error_code) const
124{
125const auto * in = checkAndGetColumn<ColumnVector<T>>(in_untyped);
126
127if (!in)
128in = checkAndGetColumnConstData<ColumnVector<T>>(in_untyped);
129
130if (in)
131{
132const auto & in_data = in->getData();
133if (!memoryIsZero(in_data.data(), 0, in_data.size() * sizeof(in_data[0])))
134{
135if (message.has_value())
136throw Exception::createRuntime(
137error_code.value_or(ErrorCodes::FUNCTION_THROW_IF_VALUE_IS_NON_ZERO),
138*message);
139else
140throw Exception(
141error_code.value_or(ErrorCodes::FUNCTION_THROW_IF_VALUE_IS_NON_ZERO),
142"Value passed to '{}' function is non-zero", getName());
143}
144
145size_t result_size = in_untyped->size();
146
147/// We return non constant to avoid constant folding.
148return ColumnUInt8::create(result_size, 0);
149}
150
151return nullptr;
152}
153
154bool allow_custom_error_code_argument;
155};
156
157}
158
159REGISTER_FUNCTION(ThrowIf)
160{
161factory.registerFunction<FunctionThrowIf>();
162}
163
164}
165