ClickHouse/src/Functions/array/arrayResize.cpp

144 lines
5.5 KiB
C++
Raw Normal View History

#include <Functions/IFunctionImpl.h>
2018-09-09 20:57:54 +00:00
#include <Functions/FunctionFactory.h>
#include <Functions/GatherUtils/GatherUtils.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/getLeastSupertype.h>
#include <Columns/ColumnArray.h>
2018-09-09 21:15:40 +00:00
#include <Columns/ColumnConst.h>
2018-09-09 20:57:54 +00:00
#include <Interpreters/castColumn.h>
#include <IO/WriteHelpers.h>
#include <Common/typeid_cast.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
class FunctionArrayResize : public IFunction
{
public:
static constexpr auto name = "arrayResize";
2020-04-14 21:05:45 +00:00
static FunctionPtr create(const Context &) { return std::make_shared<FunctionArrayResize>(); }
2018-09-09 20:57:54 +00:00
String getName() const override { return name; }
bool isVariadic() const override { return true; }
size_t getNumberOfArguments() const override { return 0; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
const size_t number_of_arguments = arguments.size();
if (number_of_arguments < 2 || number_of_arguments > 3)
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
+ toString(number_of_arguments) + ", should be 2 or 3",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
if (arguments[0]->onlyNull())
return arguments[0];
2020-04-22 08:31:10 +00:00
const auto * array_type = typeid_cast<const DataTypeArray *>(arguments[0].get());
2018-09-09 20:57:54 +00:00
if (!array_type)
throw Exception("First argument for function " + getName() + " must be an array but it has type "
+ arguments[0]->getName() + ".", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
if (WhichDataType(array_type->getNestedType()).isNothing())
throw Exception("Function " + getName() + " cannot resize " + array_type->getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
if (!isInteger(removeNullable(arguments[1])) && !arguments[1]->onlyNull())
throw Exception(
"Argument " + toString(1) + " for function " + getName() + " must be integer but it has type "
+ arguments[1]->getName() + ".", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
if (number_of_arguments == 2)
return arguments[0];
else /* if (number_of_arguments == 3) */
return std::make_shared<DataTypeArray>(getLeastSupertype({array_type->getNestedType(), arguments[2]}));
}
2020-10-14 14:04:50 +00:00
void executeImpl(ColumnsWithTypeAndName & columns, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) const override
2018-09-09 20:57:54 +00:00
{
2020-10-14 14:04:50 +00:00
const auto & return_type = columns[result].type;
2018-09-09 20:57:54 +00:00
if (return_type->onlyNull())
{
2020-10-14 14:04:50 +00:00
columns[result].column = return_type->createColumnConstWithDefaultValue(input_rows_count);
2018-09-09 20:57:54 +00:00
return;
}
auto result_column = return_type->createColumn();
2020-10-14 14:04:50 +00:00
auto array_column = columns[arguments[0]].column;
auto size_column = columns[arguments[1]].column;
2018-09-09 20:57:54 +00:00
2020-10-14 14:04:50 +00:00
if (!columns[arguments[0]].type->equals(*return_type))
array_column = castColumn(columns[arguments[0]], return_type);
2018-09-09 20:57:54 +00:00
const DataTypePtr & return_nested_type = typeid_cast<const DataTypeArray &>(*return_type).getNestedType();
size_t size = array_column->size();
ColumnPtr appended_column;
if (arguments.size() == 3)
{
2020-10-14 14:04:50 +00:00
appended_column = columns[arguments[2]].column;
if (!columns[arguments[2]].type->equals(*return_nested_type))
appended_column = castColumn(columns[arguments[2]], return_nested_type);
2018-09-09 20:57:54 +00:00
}
else
appended_column = return_nested_type->createColumnConstWithDefaultValue(size);
std::unique_ptr<GatherUtils::IArraySource> array_source;
std::unique_ptr<GatherUtils::IValueSource> value_source;
bool is_const = false;
2020-04-22 08:31:10 +00:00
if (const auto * const_array_column = typeid_cast<const ColumnConst *>(array_column.get()))
2018-09-09 20:57:54 +00:00
{
is_const = true;
array_column = const_array_column->getDataColumnPtr();
}
2020-04-22 08:31:10 +00:00
if (const auto * argument_column_array = typeid_cast<const ColumnArray *>(array_column.get()))
2018-09-09 20:57:54 +00:00
array_source = GatherUtils::createArraySource(*argument_column_array, is_const, size);
else
throw Exception{"First arguments for function " + getName() + " must be array.", ErrorCodes::LOGICAL_ERROR};
bool is_appended_const = false;
2020-04-22 08:31:10 +00:00
if (const auto * const_appended_column = typeid_cast<const ColumnConst *>(appended_column.get()))
2018-09-09 20:57:54 +00:00
{
is_appended_const = true;
appended_column = const_appended_column->getDataColumnPtr();
}
value_source = GatherUtils::createValueSource(*appended_column, is_appended_const, size);
auto sink = GatherUtils::createArraySink(typeid_cast<ColumnArray &>(*result_column), size);
if (isColumnConst(*size_column))
2018-09-09 20:57:54 +00:00
GatherUtils::resizeConstantSize(*array_source, *value_source, *sink, size_column->getInt(0));
else
GatherUtils::resizeDynamicSize(*array_source, *value_source, *sink, *size_column);
2020-10-14 14:04:50 +00:00
columns[result].column = std::move(result_column);
2018-09-09 20:57:54 +00:00
}
bool useDefaultImplementationForConstants() const override { return true; }
bool useDefaultImplementationForNulls() const override { return false; }
};
void registerFunctionArrayResize(FunctionFactory & factory)
{
factory.registerFunction<FunctionArrayResize>();
}
}