2018-09-08 22:04:39 +00:00
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2018-09-27 15:55:22 +00:00
|
|
|
#include <DataTypes/DataTypeLowCardinality.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
2018-09-27 15:55:22 +00:00
|
|
|
#include <Columns/ColumnLowCardinality.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class FunctionToLowCardinality: public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "toLowCardinality";
|
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionToLowCardinality>(); }
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2018-09-27 15:55:22 +00:00
|
|
|
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2018-09-27 15:55:22 +00:00
|
|
|
if (arguments[0]->lowCardinality())
|
2018-09-08 22:04:39 +00:00
|
|
|
return arguments[0];
|
|
|
|
|
2018-09-27 15:55:22 +00:00
|
|
|
return std::make_shared<DataTypeLowCardinality>(arguments[0]);
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override
|
|
|
|
{
|
|
|
|
auto arg_num = arguments[0];
|
|
|
|
const auto & arg = block.getByPosition(arg_num);
|
|
|
|
auto & res = block.getByPosition(result);
|
|
|
|
|
2018-09-27 15:55:22 +00:00
|
|
|
if (arg.type->lowCardinality())
|
2018-09-08 22:04:39 +00:00
|
|
|
res.column = arg.column;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto column = res.type->createColumn();
|
2018-09-27 15:55:22 +00:00
|
|
|
typeid_cast<ColumnLowCardinality &>(*column).insertRangeFromFullColumn(*arg.column, 0, arg.column->size());
|
2018-09-08 22:04:39 +00:00
|
|
|
res.column = std::move(column);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void registerFunctionToLowCardinality(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionToLowCardinality>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|