Coverage Report

Created: 2026-05-14 20:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/array/function_array_utils.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
18
#include "exprs/function/array/function_array_utils.h"
19
20
#include <stddef.h>
21
22
#include <algorithm>
23
#include <utility>
24
25
#include "core/column/column.h"
26
#include "core/column/column_nullable.h"
27
#include "core/column/column_variant.h"
28
#include "core/column/column_vector.h"
29
#include "core/data_type/data_type.h"
30
#include "core/data_type/data_type_nullable.h"
31
32
namespace doris {
33
34
48
bool extract_column_array_info(const IColumn& src, ColumnArrayExecutionData& data) {
35
48
    const IColumn* array_col = &src;
36
    // extract array nullable info
37
48
    if (src.is_nullable()) {
38
44
        const auto& null_col = reinterpret_cast<const ColumnNullable&>(src);
39
44
        data.array_nullmap_data = null_col.get_null_map_data().data();
40
44
        array_col = null_col.get_nested_column_ptr().get();
41
44
    }
42
43
    // check and get array column
44
48
    const auto array_col_guard = check_and_get_column<ColumnArray>(array_col);
45
48
    if (!array_col_guard) {
46
0
        return false;
47
0
    }
48
48
    data.array_col = array_col_guard.get();
49
50
    // extract array offsets and nested column
51
48
    data.offsets_ptr = &data.array_col->get_offsets();
52
48
    data.nested_col = data.array_col->get_data_ptr();
53
    // extract nested column is nullable
54
48
    if (data.nested_col->is_nullable()) {
55
42
        const auto& nested_null_col = reinterpret_cast<const ColumnNullable&>(*data.nested_col);
56
42
        data.nested_nullmap_data = nested_null_col.get_null_map_data().data();
57
42
        data.nested_col = nested_null_col.get_nested_column_ptr();
58
42
    }
59
48
    if (data.output_as_variant &&
60
48
        data.nested_type->get_primitive_type() != PrimitiveType::TYPE_VARIANT) {
61
        // set variant root column/type to from column/type
62
0
        auto variant = ColumnVariant::create(0, data.variant_enable_doc_mode);
63
0
        auto nullable_nested_type = make_nullable(data.nested_type);
64
0
        auto nullable_col = make_nullable(data.nested_col);
65
0
        variant->create_root(nullable_nested_type, std::move(*nullable_col).mutate());
66
0
        data.nested_col = variant->get_ptr();
67
0
    }
68
48
    return true;
69
48
}
70
71
0
ColumnArrayMutableData create_mutable_data(const IColumn* nested_col, bool is_nullable) {
72
0
    ColumnArrayMutableData dst;
73
0
    if (is_nullable) {
74
0
        dst.array_nested_col =
75
0
                ColumnNullable::create(nested_col->clone_empty(), ColumnUInt8::create());
76
0
        auto* nullable_col = reinterpret_cast<ColumnNullable*>(dst.array_nested_col.get());
77
0
        dst.nested_nullmap_data = &nullable_col->get_null_map_data();
78
0
        dst.nested_col = nullable_col->get_nested_column_ptr().get();
79
0
    } else {
80
0
        dst.array_nested_col = nested_col->clone_empty();
81
0
        dst.nested_col = dst.array_nested_col.get();
82
0
    }
83
0
    dst.offsets_col = ColumnArray::ColumnOffsets::create();
84
0
    dst.offsets_ptr =
85
0
            &reinterpret_cast<ColumnArray::ColumnOffsets*>(dst.offsets_col.get())->get_data();
86
0
    return dst;
87
0
}
88
89
0
MutableColumnPtr assemble_column_array(ColumnArrayMutableData& data) {
90
0
    return ColumnArray::create(std::move(data.array_nested_col), std::move(data.offsets_col));
91
0
}
92
93
void slice_array(ColumnArrayMutableData& dst, ColumnArrayExecutionData& src,
94
0
                 const IColumn& offset_column, const IColumn* length_column) {
95
0
    size_t cur = 0;
96
0
    for (size_t row = 0; row < src.offsets_ptr->size(); ++row) {
97
0
        size_t off = (*src.offsets_ptr)[row - 1];
98
0
        size_t len = (*src.offsets_ptr)[row] - off;
99
0
        Int64 start = offset_column.get_int(row);
100
0
        if (len == 0 || start == 0) {
101
0
            dst.offsets_ptr->push_back(cur);
102
0
            continue;
103
0
        }
104
0
        if (start > 0 && start <= len) {
105
0
            start += off - 1;
106
0
        } else if (start < 0 && -start <= len) {
107
0
            start += off + len;
108
0
        } else {
109
0
            dst.offsets_ptr->push_back(cur);
110
0
            continue;
111
0
        }
112
0
        Int64 end;
113
0
        if (length_column) {
114
0
            Int64 size = length_column->get_int(row);
115
0
            end = std::max((Int64)off, std::min((Int64)(off + len), start + size));
116
0
        } else {
117
0
            end = off + len;
118
0
        }
119
0
        for (size_t pos = start; pos < end; ++pos) {
120
0
            if (src.nested_nullmap_data && src.nested_nullmap_data[pos]) {
121
0
                dst.nested_col->insert_default();
122
0
                dst.nested_nullmap_data->push_back(1);
123
0
            } else {
124
0
                dst.nested_col->insert_from(*src.nested_col, pos);
125
0
                if (dst.nested_nullmap_data) {
126
0
                    dst.nested_nullmap_data->push_back(0);
127
0
                }
128
0
            }
129
0
        }
130
0
        if (start < end) {
131
0
            cur += end - start;
132
0
        }
133
0
        dst.offsets_ptr->push_back(cur);
134
0
    }
135
0
}
136
137
} // namespace doris