2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
2019-10-19 20:36:35 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get scalar value of sub queries from query context via IAST::Hash.
|
|
|
|
*/
|
|
|
|
class FunctionGetScalar : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "__getScalar";
|
|
|
|
static FunctionPtr create(const Context & context)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionGetScalar>(context);
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:27:32 +00:00
|
|
|
explicit FunctionGetScalar(const Context & context_) : context(context_) {}
|
2019-10-19 20:36:35 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
2019-11-03 18:44:56 +00:00
|
|
|
if (arguments.size() != 1 || !isString(arguments[0].type) || !arguments[0].column || !isColumnConst(*arguments[0].column))
|
2019-10-19 20:36:35 +00:00
|
|
|
throw Exception("Function " + getName() + " accepts one const string argument", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2020-06-28 20:20:49 +00:00
|
|
|
auto scalar_name = assert_cast<const ColumnConst &>(*arguments[0].column).getValue<String>();
|
2019-10-19 20:36:35 +00:00
|
|
|
scalar = context.getScalar(scalar_name).getByPosition(0);
|
|
|
|
return scalar.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override
|
|
|
|
{
|
|
|
|
block.getByPosition(result).column = ColumnConst::create(scalar.column, input_rows_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable ColumnWithTypeAndName scalar;
|
|
|
|
const Context & context;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void registerFunctionGetScalar(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionGetScalar>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|