ClickHouse/dbms/Functions/array/arrayFill.cpp

131 lines
4.2 KiB
C++
Raw Normal View History

2019-10-18 08:45:39 +00:00
#include <DataTypes/DataTypesNumber.h>
#include <Columns/ColumnsNumber.h>
#include "FunctionArrayMapped.h"
#include <Functions/FunctionFactory.h>
2019-12-29 01:13:17 +00:00
2019-10-18 08:45:39 +00:00
namespace DB
{
2020-02-25 18:10:48 +00:00
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
}
2019-10-18 08:45:39 +00:00
2019-10-25 03:25:02 +00:00
template <bool reverse>
2019-10-18 08:45:39 +00:00
struct ArrayFillImpl
{
static bool needBoolean() { return true; }
static bool needExpression() { return true; }
static bool needOneArray() { return false; }
static DataTypePtr getReturnType(const DataTypePtr & /*expression_return*/, const DataTypePtr & array_element)
{
return std::make_shared<DataTypeArray>(array_element);
}
static ColumnPtr execute(const ColumnArray & array, ColumnPtr mapped)
{
const ColumnUInt8 * column_fill = typeid_cast<const ColumnUInt8 *>(&*mapped);
const IColumn & in_data = array.getData();
const IColumn::Offsets & in_offsets = array.getOffsets();
auto column_data = in_data.cloneEmpty();
IColumn & out_data = *column_data.get();
if (column_fill)
{
const IColumn::Filter & fill = column_fill->getData();
size_t array_begin = 0;
size_t array_end = 0;
size_t begin = 0;
size_t end = 0;
out_data.reserve(in_data.size());
2020-03-09 03:38:43 +00:00
for (auto in_offset : in_offsets)
2019-10-18 08:45:39 +00:00
{
2020-03-09 03:38:43 +00:00
array_end = in_offset - 1;
2019-10-18 08:45:39 +00:00
for (; end <= array_end; ++end)
{
2019-10-18 13:31:18 +00:00
if (end == array_end || fill[end + 1] != fill[begin])
{
2019-10-18 08:45:39 +00:00
if (fill[begin])
2019-10-21 03:19:11 +00:00
out_data.insertRangeFrom(in_data, begin, end + 1 - begin);
else
2019-10-18 08:45:39 +00:00
{
2019-10-25 03:25:02 +00:00
if constexpr (reverse)
2019-10-18 08:45:39 +00:00
{
if (end == array_end)
out_data.insertManyFrom(in_data, array_end, end + 1 - begin);
else
out_data.insertManyFrom(in_data, end + 1, end + 1 - begin);
}
else
{
if (begin == array_begin)
out_data.insertManyFrom(in_data, array_begin, end + 1 - begin);
else
out_data.insertManyFrom(in_data, begin - 1, end + 1 - begin);
}
}
begin = end + 1;
}
}
array_begin = array_end + 1;
}
}
else
{
auto column_fill_const = checkAndGetColumnConst<ColumnUInt8>(&*mapped);
if (!column_fill_const)
throw Exception("Unexpected type of cut column", ErrorCodes::ILLEGAL_COLUMN);
if (column_fill_const->getValue<UInt8>())
2019-10-21 03:19:11 +00:00
return ColumnArray::create(
array.getDataPtr(),
array.getOffsetsPtr()
);
2019-10-18 08:45:39 +00:00
2019-10-21 03:19:11 +00:00
size_t array_begin = 0;
size_t array_end = 0;
2019-10-18 08:45:39 +00:00
2019-10-21 03:19:11 +00:00
out_data.reserve(in_data.size());
2020-03-09 03:38:43 +00:00
for (auto in_offset : in_offsets)
2019-10-21 03:19:11 +00:00
{
2020-03-09 03:38:43 +00:00
array_end = in_offset - 1;
2019-10-18 08:45:39 +00:00
2019-10-25 03:25:02 +00:00
if constexpr (reverse)
2019-10-21 03:19:11 +00:00
out_data.insertManyFrom(in_data, array_end, array_end + 1 - array_begin);
else
out_data.insertManyFrom(in_data, array_begin, array_end + 1 - array_begin);
2019-10-18 08:45:39 +00:00
2019-10-21 03:19:11 +00:00
array_begin = array_end + 1;
2019-10-18 08:45:39 +00:00
}
}
return ColumnArray::create(
std::move(column_data),
array.getOffsetsPtr()
);
}
};
struct NameArrayFill { static constexpr auto name = "arrayFill"; };
struct NameArrayReverseFill { static constexpr auto name = "arrayReverseFill"; };
using FunctionArrayFill = FunctionArrayMapped<ArrayFillImpl<false>, NameArrayFill>;
using FunctionArrayReverseFill = FunctionArrayMapped<ArrayFillImpl<true>, NameArrayReverseFill>;
2019-10-25 03:25:02 +00:00
void registerFunctionsArrayFill(FunctionFactory & factory)
2019-10-18 08:45:39 +00:00
{
factory.registerFunction<FunctionArrayFill>();
factory.registerFunction<FunctionArrayReverseFill>();
}
}