2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2020-06-20 16:05:49 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
2020-06-24 02:19:35 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2020-06-20 16:05:49 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Core/Field.h>
|
|
|
|
|
2020-06-24 02:19:35 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <Poco/String.h>
|
|
|
|
|
2020-06-20 16:05:49 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2020-06-20 16:05:49 +00:00
|
|
|
|
|
|
|
/** globalVariable('name') - takes constant string argument and returns the value of global variable with that name.
|
|
|
|
* It is intended for compatibility with MySQL.
|
|
|
|
*
|
|
|
|
* Currently it's a stub, no variables are implemented. Feel free to add more variables.
|
|
|
|
*/
|
|
|
|
class FunctionGlobalVariable : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "globalVariable";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr)
|
2020-06-20 16:05:49 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionGlobalVariable>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2020-06-20 16:05:49 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
if (!checkColumnConst<ColumnString>(arguments[0].column.get()))
|
2020-06-24 02:19:35 +00:00
|
|
|
throw Exception("Argument of function " + getName() + " must be constant string", ErrorCodes::BAD_ARGUMENTS);
|
2020-06-20 16:05:49 +00:00
|
|
|
|
|
|
|
String variable_name = assert_cast<const ColumnConst &>(*arguments[0].column).getValue<String>();
|
2020-06-24 02:19:35 +00:00
|
|
|
auto variable = global_variable_map.find(Poco::toLower(variable_name));
|
|
|
|
if (variable == global_variable_map.end())
|
2020-06-24 10:28:14 +00:00
|
|
|
return std::make_shared<DataTypeInt32>();
|
2020-06-24 02:19:35 +00:00
|
|
|
else
|
|
|
|
return variable->second.type;
|
2020-06-20 16:05:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2020-06-20 16:05:49 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
const ColumnWithTypeAndName & col = arguments[0];
|
2020-06-24 02:19:35 +00:00
|
|
|
String variable_name = assert_cast<const ColumnConst &>(*col.column).getValue<String>();
|
|
|
|
auto variable = global_variable_map.find(Poco::toLower(variable_name));
|
|
|
|
|
2020-06-24 10:28:14 +00:00
|
|
|
Field val = 0;
|
|
|
|
if (variable != global_variable_map.end())
|
2020-06-24 02:19:35 +00:00
|
|
|
val = variable->second.value;
|
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
return result_type->createColumnConst(input_rows_count, val);
|
2020-06-20 16:05:49 +00:00
|
|
|
}
|
2020-06-24 02:19:35 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct TypeAndValue
|
|
|
|
{
|
|
|
|
DataTypePtr type;
|
|
|
|
Field value;
|
|
|
|
};
|
2021-03-02 09:06:29 +00:00
|
|
|
std::unordered_map<String, TypeAndValue> global_variable_map
|
|
|
|
= {{"max_allowed_packet", {std::make_shared<DataTypeInt32>(), 67108864}},
|
|
|
|
{"version", {std::make_shared<DataTypeString>(), "5.7.30"}},
|
2021-03-02 10:53:06 +00:00
|
|
|
{"version_comment", {std::make_shared<DataTypeString>(), ""}},
|
2021-03-02 09:06:29 +00:00
|
|
|
{"transaction_isolation", {std::make_shared<DataTypeString>(), "READ-UNCOMMITTED"}}};
|
2020-06-20 16:05:49 +00:00
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2020-06-20 16:05:49 +00:00
|
|
|
|
|
|
|
void registerFunctionGlobalVariable(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionGlobalVariable>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|