2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2020-07-28 20:30:21 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <DataTypes/FieldToDataType.h>
|
|
|
|
#include <Interpreters/convertFieldToType.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Core/Field.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-07-28 20:30:21 +00:00
|
|
|
/// Get the value of a setting.
|
2021-06-01 12:20:52 +00:00
|
|
|
class FunctionGetSetting : public IFunction, WithContext
|
2020-07-28 20:30:21 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "getSetting";
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr context_) { return std::make_shared<FunctionGetSetting>(context_); }
|
|
|
|
explicit FunctionGetSetting(ContextPtr context_) : WithContext(context_) {}
|
2020-07-28 20:30:21 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
bool isDeterministic() const override { return false; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2020-07-28 20:30:21 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0}; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
2021-08-22 07:28:24 +00:00
|
|
|
{
|
|
|
|
auto value = getValue(arguments);
|
|
|
|
return applyVisitor(FieldToDataType{}, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
|
|
|
|
{
|
|
|
|
auto value = getValue(arguments);
|
|
|
|
return result_type->createColumnConst(input_rows_count, convertFieldToType(value, *result_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Field getValue(const ColumnsWithTypeAndName & arguments) const
|
2020-07-28 20:30:21 +00:00
|
|
|
{
|
|
|
|
if (!isString(arguments[0].type))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"The argument of function {} should be a constant string with the name of a setting",
|
|
|
|
String{name});
|
2020-07-28 20:30:21 +00:00
|
|
|
const auto * column = arguments[0].column.get();
|
|
|
|
if (!column || !checkAndGetColumnConstStringOrFixedString(column))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"The argument of function {} should be a constant string with the name of a setting",
|
|
|
|
String{name});
|
2020-07-28 20:30:21 +00:00
|
|
|
|
2022-07-17 15:22:12 +00:00
|
|
|
std::string_view setting_name{column->getDataAt(0).toView()};
|
2021-08-22 07:28:24 +00:00
|
|
|
return getContext()->getSettingsRef().get(setting_name);
|
2020-07-28 20:30:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2020-07-28 20:30:21 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(GetSetting)
|
2020-07-28 20:30:21 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionGetSetting>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|