2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-09-09 20:57:54 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/GatherUtils/GatherUtils.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/getLeastSupertype.h>
|
|
|
|
#include <Interpreters/castColumn.h>
|
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/range.h>
|
2018-09-09 20:57:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// arrayConcat(arr1, ...) - concatenate arrays.
|
|
|
|
class FunctionArrayConcat : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "arrayConcat";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionArrayConcat>(); }
|
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; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2018-09-09 20:57:54 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.empty())
|
|
|
|
throw Exception{"Function " + getName() + " requires at least one argument.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
for (auto i : collections::range(0, arguments.size()))
|
2018-09-09 20:57:54 +00:00
|
|
|
{
|
2020-04-22 07:03:43 +00:00
|
|
|
const auto * array_type = typeid_cast<const DataTypeArray *>(arguments[i].get());
|
2018-09-09 20:57:54 +00:00
|
|
|
if (!array_type)
|
|
|
|
throw Exception("Argument " + std::to_string(i) + " for function " + getName() + " must be an array but it has type "
|
|
|
|
+ arguments[i]->getName() + ".", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return getLeastSupertype(arguments);
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2018-09-09 20:57:54 +00:00
|
|
|
{
|
2020-10-19 21:21:10 +00:00
|
|
|
if (result_type->onlyNull())
|
|
|
|
return result_type->createColumnConstWithDefaultValue(input_rows_count);
|
2018-09-09 20:57:54 +00:00
|
|
|
|
|
|
|
size_t rows = input_rows_count;
|
|
|
|
size_t num_args = arguments.size();
|
|
|
|
|
|
|
|
Columns preprocessed_columns(num_args);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num_args; ++i)
|
|
|
|
{
|
2020-10-19 21:21:10 +00:00
|
|
|
const ColumnWithTypeAndName & arg = arguments[i];
|
2018-09-09 20:57:54 +00:00
|
|
|
ColumnPtr preprocessed_column = arg.column;
|
|
|
|
|
2020-10-19 21:21:10 +00:00
|
|
|
if (!arg.type->equals(*result_type))
|
|
|
|
preprocessed_column = castColumn(arg, result_type);
|
2018-09-09 20:57:54 +00:00
|
|
|
|
|
|
|
preprocessed_columns[i] = std::move(preprocessed_column);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<GatherUtils::IArraySource>> sources;
|
|
|
|
|
|
|
|
for (auto & argument_column : preprocessed_columns)
|
|
|
|
{
|
|
|
|
bool is_const = false;
|
|
|
|
|
2020-04-22 07:03:43 +00:00
|
|
|
if (const auto * argument_column_const = typeid_cast<const ColumnConst *>(argument_column.get()))
|
2018-09-09 20:57:54 +00:00
|
|
|
{
|
|
|
|
is_const = true;
|
|
|
|
argument_column = argument_column_const->getDataColumnPtr();
|
|
|
|
}
|
|
|
|
|
2020-04-22 07:03:43 +00:00
|
|
|
if (const auto * argument_column_array = typeid_cast<const ColumnArray *>(argument_column.get()))
|
2018-09-09 20:57:54 +00:00
|
|
|
sources.emplace_back(GatherUtils::createArraySource(*argument_column_array, is_const, rows));
|
|
|
|
else
|
|
|
|
throw Exception{"Arguments for function " + getName() + " must be arrays.", ErrorCodes::LOGICAL_ERROR};
|
|
|
|
}
|
|
|
|
|
2020-09-16 13:07:32 +00:00
|
|
|
auto sink = GatherUtils::concat(sources);
|
2018-09-09 20:57:54 +00:00
|
|
|
|
2020-10-19 21:21:10 +00:00
|
|
|
return sink;
|
2018-09-09 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void registerFunctionArrayConcat(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionArrayConcat>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|