2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
2019-02-14 14:56:11 +00:00
|
|
|
#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-12-29 01:13:17 +00:00
|
|
|
|
2019-02-14 14:56:11 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-03-14 23:10:51 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2019-03-14 23:10:51 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
2019-06-28 03:55:09 +00:00
|
|
|
/// arrayFlatten([[1, 2, 3], [4, 5]]) = [1, 2, 3, 4, 5] - flatten array.
|
|
|
|
class ArrayFlatten : public IFunction
|
2019-02-15 05:47:49 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-06-28 03:55:09 +00:00
|
|
|
static constexpr auto name = "arrayFlatten";
|
2019-02-14 14:56:11 +00:00
|
|
|
|
2019-06-28 03:55:09 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<ArrayFlatten>(); }
|
2019-02-14 14:56:11 +00:00
|
|
|
|
2019-02-15 05:47:49 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2019-03-01 20:07:58 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
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
|
|
|
|
{
|
2019-03-01 20:07:58 +00:00
|
|
|
/** We create an array column with array elements as the most deep elements of nested arrays,
|
|
|
|
* and construct offsets by selecting elements of most deep offsets by values of ancestor offsets.
|
|
|
|
*
|
2019-03-01 20:16:56 +00:00
|
|
|
Example 1:
|
2019-03-01 20:07:58 +00:00
|
|
|
|
|
|
|
Source column: Array(Array(UInt8)):
|
|
|
|
Row 1: [[1, 2, 3], [4, 5]], Row 2: [[6], [7, 8]]
|
|
|
|
data: [1, 2, 3], [4, 5], [6], [7, 8]
|
|
|
|
offsets: 2, 4
|
|
|
|
data.data: 1 2 3 4 5 6 7 8
|
|
|
|
data.offsets: 3 5 6 8
|
|
|
|
|
|
|
|
Result column: Array(UInt8):
|
|
|
|
Row 1: [1, 2, 3, 4, 5], Row 2: [6, 7, 8]
|
|
|
|
data: 1 2 3 4 5 6 7 8
|
|
|
|
offsets: 5 8
|
|
|
|
|
|
|
|
Result offsets are selected from the most deep (data.offsets) by previous deep (offsets) (and values are decremented by one):
|
|
|
|
3 5 6 8
|
|
|
|
^ ^
|
|
|
|
|
|
|
|
Example 2:
|
|
|
|
|
|
|
|
Source column: Array(Array(Array(UInt8))):
|
|
|
|
Row 1: [[], [[1], [], [2, 3]]], Row 2: [[[4]]]
|
|
|
|
|
|
|
|
most deep data: 1 2 3 4
|
|
|
|
|
|
|
|
offsets1: 2 3
|
|
|
|
offsets2: 0 3 4
|
|
|
|
- ^ ^ - select by prev offsets
|
|
|
|
offsets3: 1 1 3 4
|
|
|
|
- ^ ^ - select by prev offsets
|
|
|
|
|
|
|
|
result offsets: 3, 4
|
|
|
|
result: Row 1: [1, 2, 3], Row2: [4]
|
|
|
|
*/
|
|
|
|
|
|
|
|
const ColumnArray * src_col = checkAndGetColumn<ColumnArray>(block.getByPosition(arguments[0]).column.get());
|
|
|
|
|
|
|
|
if (!src_col)
|
2019-06-28 03:55:09 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName() + " in argument of function 'arrayFlatten'",
|
2019-03-01 20:07:58 +00:00
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
const IColumn::Offsets & src_offsets = src_col->getOffsets();
|
|
|
|
|
|
|
|
ColumnArray::ColumnOffsets::MutablePtr result_offsets_column;
|
|
|
|
const IColumn::Offsets * prev_offsets = &src_offsets;
|
|
|
|
const IColumn * prev_data = &src_col->getData();
|
|
|
|
|
|
|
|
while (const ColumnArray * next_col = checkAndGetColumn<ColumnArray>(prev_data))
|
2019-02-14 14:56:11 +00:00
|
|
|
{
|
2019-03-01 20:07:58 +00:00
|
|
|
if (!result_offsets_column)
|
|
|
|
result_offsets_column = ColumnArray::ColumnOffsets::create(input_rows_count);
|
|
|
|
|
|
|
|
IColumn::Offsets & result_offsets = result_offsets_column->getData();
|
|
|
|
|
|
|
|
const IColumn::Offsets * next_offsets = &next_col->getOffsets();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2019-03-01 20:17:30 +00:00
|
|
|
result_offsets[i] = (*next_offsets)[(*prev_offsets)[i] - 1]; /// -1 array subscript is Ok, see PaddedPODArray
|
2019-03-01 20:07:58 +00:00
|
|
|
|
|
|
|
prev_offsets = &result_offsets;
|
|
|
|
prev_data = &next_col->getData();
|
2019-02-14 14:56:11 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 20:07:58 +00:00
|
|
|
block.getByPosition(result).column = ColumnArray::create(
|
|
|
|
prev_data->getPtr(),
|
|
|
|
result_offsets_column ? std::move(result_offsets_column) : src_col->getOffsetsPtr());
|
2019-02-15 05:47:49 +00:00
|
|
|
}
|
2019-02-14 14:56:11 +00:00
|
|
|
|
2019-02-15 05:47:49 +00:00
|
|
|
private:
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-06-28 03:55:09 +00:00
|
|
|
void registerFunctionArrayFlatten(FunctionFactory & factory)
|
2019-02-15 05:47:49 +00:00
|
|
|
{
|
2019-06-28 03:55:09 +00:00
|
|
|
factory.registerFunction<ArrayFlatten>();
|
|
|
|
factory.registerAlias("flatten", "arrayFlatten", FunctionFactory::CaseInsensitive);
|
2019-02-15 05:47:49 +00:00
|
|
|
}
|
2019-02-14 14:56:11 +00:00
|
|
|
|
|
|
|
}
|