Add function "getMacro" #7239

This commit is contained in:
Alexey Milovidov 2019-10-09 04:14:57 +03:00
parent e6b9f9c70b
commit 9f8d562543
4 changed files with 95 additions and 0 deletions

View File

@ -74,6 +74,13 @@ String Macros::expand(const String & s, size_t level, const String & database_na
return expand(res, level + 1, database_name, table_name);
}
String Macros::getValue(const String & key) const
{
if (auto it = macros.find(key); it != macros.end())
return it->second;
throw Exception("No macro " + key + " in config", ErrorCodes::SYNTAX_ERROR);
}
String Macros::expand(const String & s, const String & database_name, const String & table_name) const
{
return expand(s, 0, database_name, table_name);

View File

@ -43,6 +43,8 @@ public:
using MacroMap = std::map<String, String>;
const MacroMap getMacroMap() const { return macros; }
String getValue(const String & key) const;
private:
MacroMap macros;
};

View File

@ -0,0 +1,84 @@
#include <Functions/IFunction.h>
#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;
}
/** 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";
static FunctionPtr create(const Context & context)
{
return std::make_shared<FunctionGetMacro>(context.getMacros());
}
FunctionGetMacro(MultiVersion<Macros>::Version macros_) : macros(std::move(macros_)) {}
String getName() const override
{
return name;
}
bool isDeterministic() const override { return false; }
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.
*/
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
{
const IColumn * arg_column = block.getByPosition(arguments[0]).column.get();
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);
block.getByPosition(result).column = block.getByPosition(result).type->createColumnConst(
input_rows_count, macros->getValue(arg_string->getDataAt(0).toString()))->convertToFullColumnIfConst();
}
};
void registerFunctionGetMacro(FunctionFactory & factory)
{
factory.registerFunction<FunctionGetMacro>();
}
}

View File

@ -50,6 +50,7 @@ void registerFunctionFilesystem(FunctionFactory &);
void registerFunctionEvalMLMethod(FunctionFactory &);
void registerFunctionBasename(FunctionFactory &);
void registerFunctionTransform(FunctionFactory &);
void registerFunctionGetMacro(FunctionFactory &);
#if USE_ICU
void registerFunctionConvertCharset(FunctionFactory &);
@ -102,6 +103,7 @@ void registerFunctionsMiscellaneous(FunctionFactory & factory)
registerFunctionEvalMLMethod(factory);
registerFunctionBasename(factory);
registerFunctionTransform(factory);
registerFunctionGetMacro(factory);
#if USE_ICU
registerFunctionConvertCharset(factory);