ClickHouse/src/Functions/replicate.cpp

68 lines
2.4 KiB
C++
Raw Normal View History

2021-02-05 11:41:44 +00:00
#include <Functions/replicate.h>
2021-05-15 17:33:15 +00:00
#include <Functions/IFunctionOld.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <DataTypes/DataTypeArray.h>
#include <Columns/ColumnArray.h>
#include <Interpreters/Context.h>
namespace DB
{
namespace ErrorCodes
{
2020-02-25 18:02:41 +00:00
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int ILLEGAL_COLUMN;
extern const int TOO_FEW_ARGUMENTS_FOR_FUNCTION;
}
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)
{
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());
}
2021-02-05 11:41:44 +00:00
return std::make_shared<DataTypeArray>(arguments[0]);
}
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;
2021-02-05 11:41:44 +00:00
for (size_t i = 1; i < arguments.size(); ++i)
{
2021-02-05 11:41:44 +00:00
const ColumnArray * array_column = checkAndGetColumn<ColumnArray>(arguments[i].column.get());
ColumnPtr temp_column;
if (!array_column)
{
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());
}
2021-02-05 11:41:44 +00:00
if (!offsets || offsets->empty())
offsets = array_column->getOffsetsPtr();
}
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
}
void registerFunctionReplicate(FunctionFactory & factory)
{
factory.registerFunction<FunctionReplicate>();
}
}