2021-02-05 11:41:44 +00:00
|
|
|
#include <Functions/replicate.h>
|
2021-05-15 17:33:15 +00:00
|
|
|
#include <Functions/IFunctionOld.h>
|
2018-09-08 22:31:20 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <Columns/ColumnArray.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2018-09-08 22:31:20 +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;
|
2021-02-04 20:36:50 +00:00
|
|
|
extern const int TOO_FEW_ARGUMENTS_FOR_FUNCTION;
|
2019-03-14 23:10:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 11:41:44 +00:00
|
|
|
DataTypePtr FunctionReplicate::getReturnTypeImpl(const DataTypes & arguments) const
|
2020-09-07 18:00:37 +00:00
|
|
|
{
|
2021-02-05 11:41:44 +00:00
|
|
|
if (arguments.size() < 2)
|
|
|
|
throw Exception(ErrorCodes::TOO_FEW_ARGUMENTS_FOR_FUNCTION,
|
2021-02-08 09:04:19 +00:00
|
|
|
"Function {} expect at least two arguments, got {}", getName(), arguments.size());
|
2020-09-07 18:00:37 +00:00
|
|
|
|
2021-02-05 11:41:44 +00:00
|
|
|
for (size_t i = 1; i < arguments.size(); ++i)
|
2018-09-08 22:31:20 +00:00
|
|
|
{
|
2021-02-05 11:41:44 +00:00
|
|
|
const DataTypeArray * array_type = checkAndGetDataType<DataTypeArray>(arguments[i].get());
|
|
|
|
if (!array_type)
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Argument {} for function {} must be array.",
|
|
|
|
i + 1, getName());
|
2018-09-08 22:31:20 +00:00
|
|
|
}
|
2021-02-05 11:41:44 +00:00
|
|
|
return std::make_shared<DataTypeArray>(arguments[0]);
|
|
|
|
}
|
2018-09-08 22:31:20 +00:00
|
|
|
|
2021-02-05 11:41:44 +00:00
|
|
|
ColumnPtr FunctionReplicate::executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t) const
|
|
|
|
{
|
|
|
|
ColumnPtr first_column = arguments[0].column;
|
|
|
|
ColumnPtr offsets;
|
2018-09-08 22:31:20 +00:00
|
|
|
|
2021-02-05 11:41:44 +00:00
|
|
|
for (size_t i = 1; i < arguments.size(); ++i)
|
2018-09-08 22:31:20 +00:00
|
|
|
{
|
2021-02-05 11:41:44 +00:00
|
|
|
const ColumnArray * array_column = checkAndGetColumn<ColumnArray>(arguments[i].column.get());
|
|
|
|
ColumnPtr temp_column;
|
|
|
|
if (!array_column)
|
2018-09-08 22:31:20 +00:00
|
|
|
{
|
2021-02-05 11:41:44 +00:00
|
|
|
const auto * const_array_column = checkAndGetColumnConst<ColumnArray>(arguments[i].column.get());
|
|
|
|
if (!const_array_column)
|
|
|
|
throw Exception("Unexpected column for replicate", ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
temp_column = const_array_column->convertToFullColumn();
|
|
|
|
array_column = checkAndGetColumn<ColumnArray>(temp_column.get());
|
2018-09-08 22:31:20 +00:00
|
|
|
}
|
2021-02-04 20:36:50 +00:00
|
|
|
|
2021-02-05 11:41:44 +00:00
|
|
|
if (!offsets || offsets->empty())
|
|
|
|
offsets = array_column->getOffsetsPtr();
|
2018-09-08 22:31:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 11:41:44 +00:00
|
|
|
const auto & offsets_data = assert_cast<const ColumnArray::ColumnOffsets &>(*offsets).getData();
|
|
|
|
return ColumnArray::create(first_column->replicate(offsets_data)->convertToFullColumnIfConst(), offsets);
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2018-09-08 22:31:20 +00:00
|
|
|
|
|
|
|
void registerFunctionReplicate(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionReplicate>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|