ClickHouse/src/Functions/getSetting.cpp

70 lines
2.3 KiB
C++
Raw Normal View History

2020-07-28 20:30:21 +00:00
#include <Functions/IFunctionImpl.h>
#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.
class FunctionGetSetting : public IFunction, WithContext
2020-07-28 20:30:21 +00:00
{
public:
static constexpr auto name = "getSetting";
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; }
size_t getNumberOfArguments() const override { return 1; }
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0}; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
if (!isString(arguments[0].type))
throw Exception{"The argument of function " + String{name} + " should be a constant string with the name of a setting",
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
const auto * column = arguments[0].column.get();
if (!column || !checkAndGetColumnConstStringOrFixedString(column))
throw Exception{"The argument of function " + String{name} + " should be a constant string with the name of a setting",
ErrorCodes::ILLEGAL_COLUMN};
std::string_view setting_name{column->getDataAt(0)};
value = getContext()->getSettingsRef().get(setting_name);
2020-07-28 20:30:21 +00:00
DataTypePtr type = applyVisitor(FieldToDataType{}, value);
value = convertFieldToType(value, *type);
return type;
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr & result_type, size_t input_rows_count) const override
2020-07-28 20:30:21 +00:00
{
2020-10-19 13:42:14 +00:00
return result_type->createColumnConst(input_rows_count, value);
2020-07-28 20:30:21 +00:00
}
private:
mutable Field value;
};
2020-09-07 18:00:37 +00:00
}
2020-07-28 20:30:21 +00:00
void registerFunctionGetSetting(FunctionFactory & factory)
{
factory.registerFunction<FunctionGetSetting>();
}
}