2019-12-11 14:15:43 +00:00
|
|
|
#include <Columns/ColumnTuple.h>
|
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2023-02-27 08:31:40 +00:00
|
|
|
extern const int SIZES_OF_ARRAYS_DONT_MATCH;
|
2024-05-06 09:35:48 +00:00
|
|
|
extern const int TOO_FEW_ARGUMENTS_FOR_FUNCTION;
|
2020-01-17 19:16:02 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2019-12-11 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// arrayZip(['a', 'b', 'c'], ['d', 'e', 'f']) = [('a', 'd'), ('b', 'e'), ('c', 'f')]
|
|
|
|
class FunctionArrayZip : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "arrayZip";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionArrayZip>(); }
|
2019-12-11 14:15:43 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2019-12-11 14:15:43 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
2020-03-09 03:38:43 +00:00
|
|
|
if (arguments.empty())
|
2024-05-06 09:35:48 +00:00
|
|
|
throw Exception(ErrorCodes::TOO_FEW_ARGUMENTS_FOR_FUNCTION,
|
2023-01-23 21:13:58 +00:00
|
|
|
"Function {} needs at least one argument; passed {}." , getName(), arguments.size());
|
2019-12-11 14:15:43 +00:00
|
|
|
|
|
|
|
DataTypes arguments_types;
|
|
|
|
for (size_t index = 0; index < arguments.size(); ++index)
|
|
|
|
{
|
|
|
|
const DataTypeArray * array_type = checkAndGetDataType<DataTypeArray>(arguments[index].type.get());
|
|
|
|
|
|
|
|
if (!array_type)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Argument {} of function {} must be array. Found {} instead.",
|
|
|
|
toString(index + 1), getName(), arguments[0].type->getName());
|
2019-12-11 14:15:43 +00:00
|
|
|
|
|
|
|
arguments_types.emplace_back(array_type->getNestedType());
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeArray>(std::make_shared<DataTypeTuple>(arguments_types));
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2019-12-11 14:15:43 +00:00
|
|
|
{
|
2020-01-17 19:16:02 +00:00
|
|
|
size_t num_arguments = arguments.size();
|
2019-12-11 14:15:43 +00:00
|
|
|
|
2020-01-17 19:16:02 +00:00
|
|
|
ColumnPtr first_array_column;
|
|
|
|
Columns tuple_columns(num_arguments);
|
2019-12-12 19:12:18 +00:00
|
|
|
|
2020-01-17 19:16:02 +00:00
|
|
|
for (size_t i = 0; i < num_arguments; ++i)
|
2019-12-11 14:15:43 +00:00
|
|
|
{
|
2020-01-17 19:16:02 +00:00
|
|
|
/// Constant columns cannot be inside tuple. It's only possible to have constant tuple as a whole.
|
2020-10-19 21:21:10 +00:00
|
|
|
ColumnPtr holder = arguments[i].column->convertToFullColumnIfConst();
|
2020-01-17 19:16:02 +00:00
|
|
|
|
|
|
|
const ColumnArray * column_array = checkAndGetColumn<ColumnArray>(holder.get());
|
|
|
|
|
|
|
|
if (!column_array)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Argument {} of function {} must be array. Found column {} instead.",
|
|
|
|
i + 1, getName(), holder->getName());
|
2020-01-17 19:16:02 +00:00
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
first_array_column = holder;
|
|
|
|
}
|
|
|
|
else if (!column_array->hasEqualOffsets(static_cast<const ColumnArray &>(*first_array_column)))
|
|
|
|
{
|
2023-02-27 08:31:40 +00:00
|
|
|
throw Exception(ErrorCodes::SIZES_OF_ARRAYS_DONT_MATCH,
|
2023-01-23 21:13:58 +00:00
|
|
|
"The argument 1 and argument {} of function {} have different array sizes",
|
|
|
|
i + 1, getName());
|
2020-01-17 19:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tuple_columns[i] = column_array->getDataPtr();
|
2019-12-11 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 21:21:10 +00:00
|
|
|
return ColumnArray::create(
|
2020-01-17 19:16:02 +00:00
|
|
|
ColumnTuple::create(tuple_columns), static_cast<const ColumnArray &>(*first_array_column).getOffsetsPtr());
|
2019-12-11 14:15:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(ArrayZip)
|
2019-12-11 14:15:43 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionArrayZip>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|