Coverage Report

Created: 2026-03-12 15:09

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
#include "common/compile_check_begin.h"
25
26
21.2M
bool IndexColumnWriter::check_support_inverted_index(const TabletColumn& column) {
27
    // bellow types are not supported in inverted index for extracted columns
28
21.2M
    static std::set<FieldType> invalid_types = {FieldType::OLAP_FIELD_TYPE_JSONB};
29
21.2M
    if (invalid_types.contains(column.type())) {
30
33.0k
        return false;
31
33.0k
    }
32
21.2M
    if (column.is_variant_type()) {
33
20.2k
        return false;
34
20.2k
    }
35
21.2M
    if (column.is_array_type()) {
36
        // only support one level array
37
118k
        const auto& subcolumn = column.get_sub_column(0);
38
118k
        return !subcolumn.is_array_type() && check_support_inverted_index(subcolumn);
39
118k
    }
40
21.1M
    return true;
41
21.2M
}
42
43
20.3M
bool IndexColumnWriter::check_support_ann_index(const TabletColumn& column) {
44
    // only array are supported in ann index
45
20.3M
    return column.is_array_type();
46
20.3M
}
47
48
// create index writer
49
Status IndexColumnWriter::create(const StorageField* field, std::unique_ptr<IndexColumnWriter>* res,
50
                                 IndexFileWriter* index_file_writer,
51
38.6k
                                 const TabletIndex* index_meta) {
52
38.6k
    const auto* typeinfo = field->type_info();
53
38.6k
    FieldType type = typeinfo->type();
54
38.6k
    std::string field_name;
55
38.6k
    auto storage_format = index_file_writer->get_storage_format();
56
38.6k
    if (storage_format == InvertedIndexStorageFormatPB::V1) {
57
571
        field_name = field->name();
58
38.0k
    } else {
59
38.0k
        if (field->is_extracted_column()) {
60
            // variant sub col
61
            // field_name format: parent_unique_id.sub_col_name
62
5.19k
            field_name = std::to_string(field->parent_unique_id()) + "." + field->name();
63
32.8k
        } else {
64
32.8k
            field_name = std::to_string(field->unique_id());
65
32.8k
        }
66
38.0k
    }
67
68
38.6k
    if (index_meta->is_inverted_index()) {
69
38.6k
        bool single_field = true;
70
38.6k
        if (type == FieldType::OLAP_FIELD_TYPE_ARRAY) {
71
2.44k
            const auto* array_typeinfo = dynamic_cast<const ArrayTypeInfo*>(typeinfo);
72
2.44k
            DBUG_EXECUTE_IF("InvertedIndexColumnWriter::create_array_typeinfo_is_nullptr",
73
2.44k
                            { array_typeinfo = nullptr; })
74
2.44k
            if (array_typeinfo != nullptr) {
75
2.44k
                typeinfo = array_typeinfo->item_type_info();
76
2.44k
                type = typeinfo->type();
77
2.44k
                single_field = false;
78
2.44k
            } else {
79
0
                return Status::NotSupported("unsupported array type for inverted index: " +
80
0
                                            std::to_string(int(type)));
81
0
            }
82
2.44k
        }
83
84
38.6k
        DBUG_EXECUTE_IF("InvertedIndexColumnWriter::create_unsupported_type_for_inverted_index",
85
38.6k
                        { type = FieldType::OLAP_FIELD_TYPE_JSONB; })
86
38.6k
        switch (type) {
87
0
#define M(TYPE)                                                                                 \
88
38.6k
    case TYPE:                                                                                  \
89
38.6k
        *res = std::make_unique<InvertedIndexColumnWriter<TYPE>>(field_name, index_file_writer, \
90
38.6k
                                                                 index_meta, single_field);     \
91
38.6k
        break;
92
344
            M(FieldType::OLAP_FIELD_TYPE_TINYINT)
93
315
            M(FieldType::OLAP_FIELD_TYPE_SMALLINT)
94
6.29k
            M(FieldType::OLAP_FIELD_TYPE_INT)
95
0
            M(FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT)
96
3.76k
            M(FieldType::OLAP_FIELD_TYPE_BIGINT)
97
487
            M(FieldType::OLAP_FIELD_TYPE_LARGEINT)
98
664
            M(FieldType::OLAP_FIELD_TYPE_CHAR)
99
6.07k
            M(FieldType::OLAP_FIELD_TYPE_VARCHAR)
100
8.07k
            M(FieldType::OLAP_FIELD_TYPE_STRING)
101
27
            M(FieldType::OLAP_FIELD_TYPE_DATE)
102
27
            M(FieldType::OLAP_FIELD_TYPE_DATETIME)
103
0
            M(FieldType::OLAP_FIELD_TYPE_DECIMAL)
104
4.31k
            M(FieldType::OLAP_FIELD_TYPE_DATEV2)
105
2.41k
            M(FieldType::OLAP_FIELD_TYPE_DATETIMEV2)
106
3
            M(FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ)
107
1.37k
            M(FieldType::OLAP_FIELD_TYPE_DECIMAL32)
108
652
            M(FieldType::OLAP_FIELD_TYPE_DECIMAL64)
109
461
            M(FieldType::OLAP_FIELD_TYPE_DECIMAL128I)
110
148
            M(FieldType::OLAP_FIELD_TYPE_DECIMAL256)
111
2.94k
            M(FieldType::OLAP_FIELD_TYPE_BOOL)
112
101
            M(FieldType::OLAP_FIELD_TYPE_IPV4)
113
114
            M(FieldType::OLAP_FIELD_TYPE_IPV6)
114
0
            M(FieldType::OLAP_FIELD_TYPE_FLOAT)
115
26
            M(FieldType::OLAP_FIELD_TYPE_DOUBLE)
116
0
#undef M
117
1
        default:
118
1
            return Status::NotSupported("unsupported type for inverted index: " +
119
1
                                        std::to_string(int(type)));
120
38.6k
        }
121
38.6k
        if (*res != nullptr) {
122
38.6k
            auto st = (*res)->init();
123
38.6k
            if (!st.ok()) {
124
0
                (*res)->close_on_error();
125
0
                return st;
126
0
            }
127
38.6k
        }
128
38.6k
    } else if (index_meta->is_ann_index()) {
129
2
        DCHECK(type == FieldType::OLAP_FIELD_TYPE_ARRAY);
130
2
        *res = std ::make_unique<AnnIndexColumnWriter>(index_file_writer, index_meta);
131
2
        if (*res != nullptr) {
132
2
            auto st = (*res)->init();
133
2
            if (!st.ok()) {
134
0
                (*res)->close_on_error();
135
0
                return st;
136
0
            }
137
2
        }
138
2
    }
139
140
38.6k
    return Status::OK();
141
38.6k
}
142
143
#include "common/compile_check_end.h"
144
} // namespace doris::segment_v2