| 1 | #include <Columns/ColumnTuple.h> |
| 2 | #include <Columns/ColumnArray.h> |
| 3 | #include <DataTypes/DataTypeTuple.h> |
| 4 | #include <DataTypes/DataTypeArray.h> |
| 5 | #include <Functions/FunctionFactory.h> |
| 6 | #include <Functions/FunctionHelpers.h> |
| 7 | #include <IO/WriteHelpers.h> |
| 8 | |
| 9 | |
| 10 | namespace DB |
| 11 | { |
| 12 | |
| 13 | namespace ErrorCodes |
| 14 | { |
| 15 | extern const int SIZES_OF_ARRAYS_DOESNT_MATCH; |
| 16 | extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; |
| 17 | } |
| 18 | |
| 19 | /// arrayZip(['a', 'b', 'c'], ['d', 'e', 'f']) = [('a', 'd'), ('b', 'e'), ('c', 'f')] |
| 20 | class FunctionArrayZip : public IFunction |
| 21 | { |
| 22 | public: |
| 23 | static constexpr auto name = "arrayZip" ; |
| 24 | static FunctionPtr create(const Context &) { return std::make_shared<FunctionArrayZip>(); } |
| 25 | |
| 26 | String getName() const override |
| 27 | { |
| 28 | return name; |
| 29 | } |
| 30 | |
| 31 | bool isVariadic() const override { return true; } |
| 32 | size_t getNumberOfArguments() const override { return 0; } |
| 33 | bool useDefaultImplementationForConstants() const override { return true; } |
| 34 | |
| 35 | DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override |
| 36 | { |
| 37 | if (arguments.size() < 1) |
| 38 | throw Exception("Function " + getName() + " needs at least one argument; passed " + toString(arguments.size()) + "." |
| 39 | , ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); |
| 40 | |
| 41 | DataTypes arguments_types; |
| 42 | for (size_t index = 0; index < arguments.size(); ++index) |
| 43 | { |
| 44 | const DataTypeArray * array_type = checkAndGetDataType<DataTypeArray>(arguments[index].type.get()); |
| 45 | |
| 46 | if (!array_type) |
| 47 | throw Exception( |
| 48 | "Argument " + toString(index + 1) + " of function must be array. Found " + arguments[0].type->getName() + " instead." , |
| 49 | ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); |
| 50 | |
| 51 | arguments_types.emplace_back(array_type->getNestedType()); |
| 52 | } |
| 53 | |
| 54 | return std::make_shared<DataTypeArray>(std::make_shared<DataTypeTuple>(arguments_types)); |
| 55 | } |
| 56 | |
| 57 | void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override |
| 58 | { |
| 59 | auto first_argument = block.getByPosition(arguments[0]); |
| 60 | const auto & first_array_column = checkAndGetColumn<ColumnArray>(first_argument.column.get()); |
| 61 | |
| 62 | Columns res_tuple_columns(arguments.size()); |
| 63 | res_tuple_columns[0] = first_array_column->getDataPtr(); |
| 64 | |
| 65 | for (size_t index = 1; index < arguments.size(); ++index) |
| 66 | { |
| 67 | const auto & argument_type_and_column = block.getByPosition(arguments[index]); |
| 68 | const auto & argument_array_column = checkAndGetColumn<ColumnArray>(argument_type_and_column.column.get()); |
| 69 | |
| 70 | if (!first_array_column->hasEqualOffsets(*argument_array_column)) |
| 71 | throw Exception("The argument 1 and argument " + toString(index + 1) + " of function have different array sizes" , |
| 72 | ErrorCodes::SIZES_OF_ARRAYS_DOESNT_MATCH); |
| 73 | |
| 74 | res_tuple_columns[index] = argument_array_column->getDataPtr(); |
| 75 | } |
| 76 | |
| 77 | block.getByPosition(result).column = ColumnArray::create( |
| 78 | ColumnTuple::create(res_tuple_columns), first_array_column->getOffsetsPtr()); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | void registerFunctionArrayZip(FunctionFactory & factory) |
| 83 | { |
| 84 | factory.registerFunction<FunctionArrayZip>(); |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | |