Coverage Report

Created: 2026-04-15 15:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/index/index_writer.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 "common/exception.h"
19
#include "storage/field.h"
20
#include "storage/index/ann/ann_index_writer.h"
21
#include "storage/index/inverted/inverted_index_writer.h"
22
23
namespace doris::segment_v2 {
24
25
28.1M
bool IndexColumnWriter::check_support_inverted_index(const TabletColumn& column) {
26
    // bellow types are not supported in inverted index for extracted columns
27
28.1M
    static std::set<FieldType> invalid_types = {FieldType::OLAP_FIELD_TYPE_JSONB};
28
28.1M
    if (invalid_types.contains(column.type())) {
29
36.6k
        return false;
30
36.6k
    }
31
28.1M
    if (column.is_variant_type()) {
32
19.4k
        return false;
33
19.4k
    }
34
28.1M
    if (column.is_array_type()) {
35
        // only support one level array
36
118k
        const auto& subcolumn = column.get_sub_column(0);
37
118k
        return !subcolumn.is_array_type() && check_support_inverted_index(subcolumn);
38
118k
    }
39
28.0M
    return true;
40
28.1M
}
41
42
27.2M
bool IndexColumnWriter::check_support_ann_index(const TabletColumn& column) {
43
    // only array are supported in ann index
44
27.2M
    return column.is_array_type();
45
27.2M
}
46
47
// create index writer
48
Status IndexColumnWriter::create(const StorageField* field, std::unique_ptr<IndexColumnWriter>* res,
49
                                 IndexFileWriter* index_file_writer,
50
38.3k
                                 const TabletIndex* index_meta) {
51
38.3k
    const auto* typeinfo = field->type_info();
52
38.3k
    FieldType type = typeinfo->type();
53
38.3k
    std::string field_name;
54
38.3k
    auto storage_format = index_file_writer->get_storage_format();
55
38.3k
    if (storage_format == InvertedIndexStorageFormatPB::V1) {
56
586
        field_name = field->name();
57
37.8k
    } else {
58
37.8k
        if (field->is_extracted_column()) {
59
            // variant sub col
60
            // field_name format: parent_unique_id.sub_col_name
61
5.29k
            field_name = std::to_string(field->parent_unique_id()) + "." + field->name();
62
32.5k
        } else {
63
32.5k
            field_name = std::to_string(field->unique_id());
64
32.5k
        }
65
37.8k
    }
66
67
38.3k
    if (index_meta->is_inverted_index()) {
68
38.3k
        bool single_field = true;
69
38.3k
        if (type == FieldType::OLAP_FIELD_TYPE_ARRAY) {
70
2.11k
            const auto* array_typeinfo = dynamic_cast<const ArrayTypeInfo*>(typeinfo);
71
2.11k
            DBUG_EXECUTE_IF("InvertedIndexColumnWriter::create_array_typeinfo_is_nullptr",
72
2.11k
                            { array_typeinfo = nullptr; })
73
2.11k
            if (array_typeinfo != nullptr) {
74
2.11k
                typeinfo = array_typeinfo->item_type_info();
75
2.11k
                type = typeinfo->type();
76
2.11k
                single_field = false;
77
2.11k
            } else {
78
0
                return Status::NotSupported("unsupported array type for inverted index: " +
79
0
                                            std::to_string(int(type)));
80
0
            }
81
2.11k
        }
82
83
38.3k
        DBUG_EXECUTE_IF("InvertedIndexColumnWriter::create_unsupported_type_for_inverted_index",
84
38.3k
                        { type = FieldType::OLAP_FIELD_TYPE_JSONB; })
85
38.3k
        switch (type) {
86
0
#define M(TYPE)                                                                                 \
87
38.3k
    case TYPE:                                                                                  \
88
38.3k
        *res = std::make_unique<InvertedIndexColumnWriter<TYPE>>(field_name, index_file_writer, \
89
38.3k
                                                                 index_meta, single_field);     \
90
38.3k
        break;
91
319
            M(FieldType::OLAP_FIELD_TYPE_TINYINT)
92
290
            M(FieldType::OLAP_FIELD_TYPE_SMALLINT)
93
6.29k
            M(FieldType::OLAP_FIELD_TYPE_INT)
94
0
            M(FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT)
95
3.93k
            M(FieldType::OLAP_FIELD_TYPE_BIGINT)
96
448
            M(FieldType::OLAP_FIELD_TYPE_LARGEINT)
97
651
            M(FieldType::OLAP_FIELD_TYPE_CHAR)
98
6.05k
            M(FieldType::OLAP_FIELD_TYPE_VARCHAR)
99
8.01k
            M(FieldType::OLAP_FIELD_TYPE_STRING)
100
27
            M(FieldType::OLAP_FIELD_TYPE_DATE)
101
27
            M(FieldType::OLAP_FIELD_TYPE_DATETIME)
102
0
            M(FieldType::OLAP_FIELD_TYPE_DECIMAL)
103
4.29k
            M(FieldType::OLAP_FIELD_TYPE_DATEV2)
104
2.37k
            M(FieldType::OLAP_FIELD_TYPE_DATETIMEV2)
105
3
            M(FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ)
106
1.33k
            M(FieldType::OLAP_FIELD_TYPE_DECIMAL32)
107
590
            M(FieldType::OLAP_FIELD_TYPE_DECIMAL64)
108
440
            M(FieldType::OLAP_FIELD_TYPE_DECIMAL128I)
109
148
            M(FieldType::OLAP_FIELD_TYPE_DECIMAL256)
110
2.91k
            M(FieldType::OLAP_FIELD_TYPE_BOOL)
111
99
            M(FieldType::OLAP_FIELD_TYPE_IPV4)
112
114
            M(FieldType::OLAP_FIELD_TYPE_IPV6)
113
0
            M(FieldType::OLAP_FIELD_TYPE_FLOAT)
114
26
            M(FieldType::OLAP_FIELD_TYPE_DOUBLE)
115
0
#undef M
116
1
        default:
117
1
            return Status::NotSupported("unsupported type for inverted index: " +
118
1
                                        std::to_string(int(type)));
119
38.3k
        }
120
38.3k
        if (*res != nullptr) {
121
38.3k
            auto st = (*res)->init();
122
38.3k
            if (!st.ok()) {
123
0
                (*res)->close_on_error();
124
0
                return st;
125
0
            }
126
38.3k
        }
127
38.3k
    } else if (index_meta->is_ann_index()) {
128
2
        DCHECK(type == FieldType::OLAP_FIELD_TYPE_ARRAY);
129
2
        *res = std ::make_unique<AnnIndexColumnWriter>(index_file_writer, index_meta);
130
2
        if (*res != nullptr) {
131
2
            auto st = (*res)->init();
132
2
            if (!st.ok()) {
133
0
                (*res)->close_on_error();
134
0
                return st;
135
0
            }
136
2
        }
137
2
    }
138
139
38.3k
    return Status::OK();
140
38.3k
}
141
142
} // namespace doris::segment_v2