ClickHouse/src/Functions/getMacro.cpp

90 lines
2.6 KiB
C++
Raw Normal View History

2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.h>
2019-10-09 01:14:57 +00:00
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <DataTypes/DataTypeString.h>
#include <Columns/ColumnString.h>
#include <Interpreters/Context.h>
#include <Common/Macros.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
{
2019-10-09 01:14:57 +00:00
/** Get the value of macro from configuration file.
* For example, it may be used as a sophisticated replacement for the function 'hostName' if servers have complicated hostnames
* but you still need to distinguish them by some convenient names.
*/
class FunctionGetMacro : public IFunction
{
private:
MultiVersion<Macros>::Version macros;
public:
static constexpr auto name = "getMacro";
2021-06-01 12:20:52 +00:00
static FunctionPtr create(ContextPtr context)
2019-10-09 01:14:57 +00:00
{
return std::make_shared<FunctionGetMacro>(context->getMacros());
2019-10-09 01:14:57 +00:00
}
2020-03-18 03:27:32 +00:00
explicit FunctionGetMacro(MultiVersion<Macros>::Version macros_) : macros(std::move(macros_)) {}
2019-10-09 01:14:57 +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; }
2019-10-09 01:14:57 +00:00
bool isDeterministicInScopeOfQuery() const override
{
return false;
}
size_t getNumberOfArguments() const override
{
return 1;
}
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (!isString(arguments[0]))
throw Exception("The argument of function " + getName() + " must have String type", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return std::make_shared<DataTypeString>();
}
/** convertToFullColumn needed because in distributed query processing,
* each server returns its own value.
*/
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
2019-10-09 01:14:57 +00:00
{
2020-10-19 13:42:14 +00:00
const IColumn * arg_column = arguments[0].column.get();
2019-10-09 01:14:57 +00:00
const ColumnString * arg_string = checkAndGetColumnConstData<ColumnString>(arg_column);
if (!arg_string)
throw Exception("The argument of function " + getName() + " must be constant String", ErrorCodes::ILLEGAL_COLUMN);
2020-10-19 13:42:14 +00:00
return result_type->createColumnConst(
2019-10-09 01:14:57 +00:00
input_rows_count, macros->getValue(arg_string->getDataAt(0).toString()))->convertToFullColumnIfConst();
}
};
2020-09-07 18:00:37 +00:00
}
2019-10-09 01:14:57 +00:00
void registerFunctionGetMacro(FunctionFactory & factory)
{
factory.registerFunction<FunctionGetMacro>();
}
}