ClickHouse/dbms/src/Functions/flatten.cpp

83 lines
2.8 KiB
C++
Raw Normal View History

2019-02-14 14:56:11 +00:00
#include <Functions/IFunction.h>
#include <Functions/FunctionFactory.h>
2019-02-15 05:47:49 +00:00
#include <Functions/FunctionHelpers.h>
#include <DataTypes/DataTypeArray.h>
#include <Columns/ColumnArray.h>
2019-02-14 14:56:11 +00:00
namespace DB
{
2019-02-15 05:47:49 +00:00
/// flatten([[1, 2, 3], [4, 5]]) = [1, 2, 3, 4, 5] - flatten array.
class FunctionFlatten : public IFunction
{
public:
static constexpr auto name = "flatten";
2019-02-14 14:56:11 +00:00
2019-02-15 05:47:49 +00:00
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionFlatten>(context); }
2019-02-14 14:56:11 +00:00
2019-02-15 05:47:49 +00:00
size_t getNumberOfArguments() const override { return 1; }
2019-02-14 14:56:11 +00:00
2019-02-15 05:47:49 +00:00
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (!isArray(arguments[0]))
throw Exception("Illegal type " + arguments[0]->getName() +
" of argument of function " + getName() +
", expected Array", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
2019-02-14 14:56:11 +00:00
2019-02-15 05:47:49 +00:00
DataTypePtr nested_type = arguments[0];
while (isArray(nested_type))
nested_type = checkAndGetDataType<DataTypeArray>(nested_type.get())->getNestedType();
2019-02-14 14:56:11 +00:00
2019-02-15 05:47:49 +00:00
return std::make_shared<DataTypeArray>(nested_type);
}
2019-02-14 14:56:11 +00:00
2019-02-15 05:47:49 +00:00
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
{
const auto * arg_col = checkAndGetColumn<ColumnArray>(block.getByPosition(arguments[0]).column.get());
const IColumn & arg_data = arg_col->getData();
const IColumn::Offsets & arg_offsets = arg_col->getOffsets();
const DataTypePtr & result_type = block.getByPosition(result).type;
const DataTypePtr & result_nested_type = dynamic_cast<const DataTypeArray &>(*result_type).getNestedType();
auto result_col = ColumnArray::create(result_nested_type->createColumn());
IColumn & result_data = result_col->getData();
IColumn::Offsets & result_offsets = result_col->getOffsets();
2019-02-14 14:56:11 +00:00
2019-02-15 07:00:50 +00:00
// todo
// result_data.reserve();
// result_offsets.resize();
2019-02-15 05:47:49 +00:00
IColumn::Offset current_offset = 0;
for (size_t i = 0; i < input_rows_count; ++i)
2019-02-14 14:56:11 +00:00
{
2019-02-15 07:00:50 +00:00
const auto & flatten_data = flatten(arg_data, current_offset, arg_offsets[i]);
2019-02-15 05:47:49 +00:00
result_data.insertRangeFrom(flatten_data, 0, flatten_data.size());
current_offset += flatten_data.size();
result_offsets[i] = current_offset;
2019-02-14 14:56:11 +00:00
}
2019-02-15 05:47:49 +00:00
block.getByPosition(result).column = std::move(result_col);
}
2019-02-14 14:56:11 +00:00
2019-02-15 05:47:49 +00:00
private:
String getName() const override
{
return name;
}
2019-02-14 14:56:11 +00:00
2019-02-15 05:47:49 +00:00
bool addField(DataTypePtr type_res, const Field & f, Array & arr) const;
2019-02-14 14:56:11 +00:00
2019-02-15 07:00:50 +00:00
const ColumnArray & flatten(const IColumn & /*data*/, size_t /*from*/, size_t /*to*/) const
2019-02-14 14:56:11 +00:00
{
2019-02-15 05:47:49 +00:00
// todo
2019-02-14 14:56:11 +00:00
}
2019-02-15 05:47:49 +00:00
};
void registerFunctionFlatten(FunctionFactory & factory)
{
factory.registerFunction<FunctionFlatten>();
}
2019-02-14 14:56:11 +00:00
}