#include #include #include #include #include #include #include namespace DB { namespace ErrorCodes { extern const int ILLEGAL_TYPE_OF_ARGUMENT; extern const int ILLEGAL_COLUMN; } namespace { /// Get the value of a setting. class FunctionGetSetting : public IFunction, WithContext { public: static constexpr auto name = "getSetting"; static FunctionPtr create(ContextPtr context_) { return std::make_shared(context_); } explicit FunctionGetSetting(ContextPtr context_) : WithContext(context_) {} String getName() const override { return name; } bool isDeterministic() const override { return false; } bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; } size_t getNumberOfArguments() const override { return 1; } ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0}; } DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override { 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 { 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)}; return getContext()->getSettingsRef().get(setting_name); } }; } void registerFunctionGetSetting(FunctionFactory & factory) { factory.registerFunction(); } }