Coverage Report

Created: 2026-03-16 21:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/array/function_array_zip.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/array/arrayZip.cpp
19
// and modified by Doris
20
21
#include <fmt/format.h>
22
#include <glog/logging.h>
23
#include <stddef.h>
24
25
#include <algorithm>
26
#include <memory>
27
#include <ostream>
28
#include <string>
29
#include <utility>
30
31
#include "common/status.h"
32
#include "core/block/block.h"
33
#include "core/block/column_numbers.h"
34
#include "core/block/column_with_type_and_name.h"
35
#include "core/column/column.h"
36
#include "core/column/column_array.h"
37
#include "core/column/column_nullable.h"
38
#include "core/column/column_struct.h"
39
#include "core/column/column_vector.h"
40
#include "core/data_type/data_type.h"
41
#include "core/data_type/data_type_array.h"
42
#include "core/data_type/data_type_nullable.h"
43
#include "core/data_type/data_type_struct.h"
44
#include "core/types.h"
45
#include "exprs/aggregate/aggregate_function.h"
46
#include "exprs/function/function.h"
47
#include "exprs/function/function_helpers.h"
48
#include "exprs/function/simple_function_factory.h"
49
50
namespace doris {
51
class FunctionContext;
52
} // namespace doris
53
54
namespace doris {
55
56
// Combines multiple arrays into a single array
57
// array_zip(['d', 'o', 'r', 'i', 's'], [1, 2, 3, 4, 5]) -> [('d', 1), ('o', 2), ('r', 3), ('i', 4), ('s', 5)]
58
class FunctionArrayZip : public IFunction {
59
public:
60
    static constexpr auto name = "array_zip";
61
2
    static FunctionPtr create() { return std::make_shared<FunctionArrayZip>(); }
62
63
    /// Get function name.
64
0
    String get_name() const override { return name; }
65
66
1
    bool is_variadic() const override { return true; }
67
68
0
    size_t get_number_of_arguments() const override { return 0; }
69
70
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
71
0
        DCHECK(arguments.size() > 0)
72
0
                << "function: " << get_name() << ", arguments should not be empty";
73
74
0
        DataTypes res_data_types;
75
0
        size_t num_elements = arguments.size();
76
0
        for (size_t i = 0; i < num_elements; ++i) {
77
0
            DCHECK(arguments[i]->get_primitive_type() == TYPE_ARRAY)
78
0
                    << i << "-th element is not array type";
79
80
0
            const auto* array_type = check_and_get_data_type<DataTypeArray>(arguments[i].get());
81
0
            DCHECK(array_type) << "function: " << get_name() << " " << i + 1
82
0
                               << "-th argument is not array";
83
84
0
            res_data_types.emplace_back(
85
0
                    make_nullable(remove_nullable((array_type->get_nested_type()))));
86
0
        }
87
88
0
        auto res = std::make_shared<DataTypeArray>(
89
0
                make_nullable(std::make_shared<DataTypeStruct>(res_data_types)));
90
0
        return res;
91
0
    }
92
93
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
94
0
                        uint32_t result, size_t input_rows_count) const override {
95
0
        size_t num_element = arguments.size();
96
97
        // all the columns must have the same size as the first column
98
0
        ColumnPtr first_array_column;
99
0
        Columns tuple_columns(num_element);
100
101
0
        for (size_t i = 0; i < num_element; ++i) {
102
0
            auto col = block.get_by_position(arguments[i]).column;
103
0
            col = col->convert_to_full_column_if_const();
104
105
0
            const auto* column_array = check_and_get_column<ColumnArray>(col.get());
106
0
            if (!column_array) {
107
0
                return Status::RuntimeError(fmt::format(
108
0
                        "execute failed, function {}'s {}-th argument should be array bet get {}",
109
0
                        get_name(), i + 1, block.get_by_position(arguments[i]).type->get_name()));
110
0
            }
111
112
0
            if (i == 0) {
113
0
                first_array_column = col;
114
0
            } else if (!column_array->has_equal_offsets(
115
0
                               static_cast<const ColumnArray&>(*first_array_column))) {
116
0
                return Status::RuntimeError(
117
0
                        fmt::format("execute failed, function {}'s {}-th argument should have same "
118
0
                                    "offsets with first argument",
119
0
                                    get_name(), i + 1));
120
0
            }
121
122
0
            tuple_columns[i] = column_array->get_data_ptr();
123
0
        }
124
125
0
        auto tuples = ColumnStruct::create(tuple_columns);
126
0
        auto nullable_tuples =
127
0
                ColumnNullable::create(std::move(tuples), ColumnUInt8::create(tuples->size(), 0));
128
0
        auto res_column = ColumnArray::create(
129
0
                std::move(nullable_tuples),
130
0
                static_cast<const ColumnArray&>(*first_array_column).get_offsets_ptr());
131
0
        block.replace_by_position(result, std::move(res_column));
132
0
        return Status::OK();
133
0
    }
134
};
135
136
1
void register_function_array_zip(SimpleFunctionFactory& factory) {
137
1
    factory.register_function<FunctionArrayZip>();
138
1
}
139
140
} // namespace doris