ClickHouse/src/Functions/getScalar.cpp

71 lines
2.0 KiB
C++
Raw Normal View History

2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.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;
}
2020-09-07 18:00:37 +00:00
namespace
{
2019-10-19 20:36:35 +00:00
/** Get scalar value of sub queries from query context via IAST::Hash.
*/
class FunctionGetScalar : public IFunction, WithConstContext
2019-10-19 20:36:35 +00:00
{
public:
static constexpr auto name = "__getScalar";
static FunctionPtr create(ContextConstPtr context_)
2019-10-19 20:36:35 +00:00
{
return std::make_shared<FunctionGetScalar>(context_);
2019-10-19 20:36:35 +00:00
}
explicit FunctionGetScalar(ContextConstPtr context_) : WithConstContext(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
{
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);
auto scalar_name = assert_cast<const ColumnConst &>(*arguments[0].column).getValue<String>();
ContextConstPtr query_context = getContext()->hasQueryContext() ? getContext()->getQueryContext() : getContext();
scalar = query_context->getScalar(scalar_name).getByPosition(0);
2019-10-19 20:36:35 +00:00
return scalar.type;
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
2019-10-19 20:36:35 +00:00
{
2020-10-19 13:42:14 +00:00
return ColumnConst::create(scalar.column, input_rows_count);
2019-10-19 20:36:35 +00:00
}
private:
mutable ColumnWithTypeAndName scalar;
};
2020-09-07 18:00:37 +00:00
}
2019-10-19 20:36:35 +00:00
void registerFunctionGetScalar(FunctionFactory & factory)
{
factory.registerFunction<FunctionGetScalar>();
}
}