2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.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>
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2018-09-08 22:31:20 +00:00
|
|
|
/** Creates an array, multiplying the column (the first argument) by the number of elements in the array (the second argument).
|
|
|
|
*/
|
|
|
|
class FunctionReplicate : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "replicate";
|
|
|
|
|
|
|
|
static FunctionPtr create(const Context &)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionReplicate>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
const DataTypeArray * array_type = checkAndGetDataType<DataTypeArray>(arguments[1].get());
|
|
|
|
if (!array_type)
|
|
|
|
throw Exception("Second argument for function " + getName() + " must be array.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
return std::make_shared<DataTypeArray>(arguments[0]);
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t) const override
|
2018-09-08 22:31:20 +00:00
|
|
|
{
|
2020-10-19 15:27:41 +00:00
|
|
|
ColumnPtr first_column = arguments[0].column;
|
|
|
|
const ColumnArray * array_column = checkAndGetColumn<ColumnArray>(arguments[1].column.get());
|
2018-09-08 22:31:20 +00:00
|
|
|
ColumnPtr temp_column;
|
|
|
|
if (!array_column)
|
|
|
|
{
|
2020-10-19 15:27:41 +00:00
|
|
|
const auto * const_array_column = checkAndGetColumnConst<ColumnArray>(arguments[1].column.get());
|
2018-09-08 22:31:20 +00:00
|
|
|
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());
|
|
|
|
}
|
2020-10-19 15:27:41 +00:00
|
|
|
return ColumnArray::create(first_column->replicate(array_column->getOffsets())->convertToFullColumnIfConst(), array_column->getOffsetsPtr());
|
2018-09-08 22:31:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2018-09-08 22:31:20 +00:00
|
|
|
|
|
|
|
void registerFunctionReplicate(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionReplicate>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|