Coverage Report

Created: 2025-07-27 03:09

/root/doris/be/src/olap/match_predicate.cpp
Line
Count
Source (jump to first uncovered line)
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 "olap/match_predicate.h"
19
20
#include <roaring/roaring.hh>
21
22
#include "exec/olap_utils.h"
23
#include "olap/field.h"
24
#include "olap/inverted_index_parser.h"
25
#include "olap/olap_common.h"
26
#include "olap/rowset/segment_v2/inverted_index_cache.h"
27
#include "olap/rowset/segment_v2/inverted_index_reader.h"
28
#include "olap/schema.h"
29
#include "olap/tablet_schema.h"
30
#include "olap/types.h"
31
#include "olap/utils.h"
32
#include "runtime/define_primitive_type.h"
33
#include "runtime/types.h"
34
#include "vec/common/assert_cast.h"
35
#include "vec/common/string_ref.h"
36
#include "vec/data_types/data_type.h"
37
#include "vec/data_types/data_type_array.h"
38
39
namespace doris {
40
41
MatchPredicate::MatchPredicate(uint32_t column_id, const std::string& value, MatchType match_type)
42
0
        : ColumnPredicate(column_id), _value(value), _match_type(match_type) {}
43
44
0
PredicateType MatchPredicate::type() const {
45
0
    return PredicateType::MATCH;
46
0
}
47
48
Status MatchPredicate::evaluate(const vectorized::IndexFieldNameAndTypePair& name_with_type,
49
                                InvertedIndexIterator* iterator, uint32_t num_rows,
50
0
                                roaring::Roaring* bitmap) const {
51
0
    if (iterator == nullptr) {
  Branch (51:9): [True: 0, False: 0]
52
0
        return Status::Error<ErrorCode::INVERTED_INDEX_EVALUATE_SKIPPED>(
53
0
                "Inverted index evaluate skipped, no inverted index reader can not support "
54
0
                "match predicate");
55
0
    }
56
0
    if (_check_evaluate(iterator)) {
  Branch (56:9): [True: 0, False: 0]
57
0
        return Status::Error<ErrorCode::INVERTED_INDEX_INVALID_PARAMETERS>(
58
0
                "phrase queries require setting support_phrase = true");
59
0
    }
60
0
    auto type = name_with_type.second;
61
0
    std::shared_ptr<roaring::Roaring> roaring = std::make_shared<roaring::Roaring>();
62
0
    auto inverted_index_query_type = _to_inverted_index_query_type(_match_type);
63
0
    TypeDescriptor column_desc = type->get_type_as_type_descriptor();
64
0
    if (is_string_type(column_desc.type) ||
  Branch (64:9): [True: 0, False: 0]
65
0
        (column_desc.type == TYPE_ARRAY && is_string_type(column_desc.children[0].type))) {
  Branch (65:10): [True: 0, False: 0]
  Branch (65:44): [True: 0, False: 0]
66
0
        StringRef match_value;
67
0
        int32_t length = _value.length();
68
0
        char* buffer = const_cast<char*>(_value.c_str());
69
0
        match_value.replace(buffer, length); //is it safe?
70
0
        RETURN_IF_ERROR(iterator->read_from_inverted_index(
71
0
                name_with_type, &match_value, inverted_index_query_type, num_rows, roaring));
72
0
    } else if (column_desc.type == TYPE_ARRAY &&
  Branch (72:16): [True: 0, False: 0]
  Branch (72:16): [True: 0, False: 0]
73
0
               is_numeric_type(
  Branch (73:16): [True: 0, False: 0]
74
0
                       TabletColumn::get_field_type_by_type(column_desc.children[0].type))) {
75
0
        std::vector<char> buf(column_desc.children[0].len);
76
0
        const TypeInfo* type_info = get_scalar_type_info(
77
0
                TabletColumn::get_field_type_by_type(column_desc.children[0].type));
78
0
        RETURN_IF_ERROR(type_info->from_string(buf.data(), _value));
79
0
        RETURN_IF_ERROR(iterator->read_from_inverted_index(
80
0
                name_with_type, buf.data(), inverted_index_query_type, num_rows, roaring, true));
81
0
    }
82
83
    // mask out null_bitmap, since NULL cmp VALUE will produce NULL
84
    //  and be treated as false in WHERE
85
    // keep it after query, since query will try to read null_bitmap and put it to cache
86
0
    if (iterator->has_null()) {
  Branch (86:9): [True: 0, False: 0]
87
0
        InvertedIndexQueryCacheHandle null_bitmap_cache_handle;
88
0
        RETURN_IF_ERROR(iterator->read_null_bitmap(&null_bitmap_cache_handle));
89
0
        std::shared_ptr<roaring::Roaring> null_bitmap = null_bitmap_cache_handle.get_bitmap();
90
0
        if (null_bitmap) {
  Branch (90:13): [True: 0, False: 0]
91
0
            *bitmap -= *null_bitmap;
92
0
        }
93
0
    }
94
95
0
    *bitmap &= *roaring;
96
0
    return Status::OK();
97
0
}
98
99
0
InvertedIndexQueryType MatchPredicate::_to_inverted_index_query_type(MatchType match_type) const {
100
0
    auto ret = InvertedIndexQueryType::UNKNOWN_QUERY;
101
0
    switch (match_type) {
102
0
    case MatchType::MATCH_ANY:
  Branch (102:5): [True: 0, False: 0]
103
0
        ret = InvertedIndexQueryType::MATCH_ANY_QUERY;
104
0
        break;
105
0
    case MatchType::MATCH_ALL:
  Branch (105:5): [True: 0, False: 0]
106
0
        ret = InvertedIndexQueryType::MATCH_ALL_QUERY;
107
0
        break;
108
0
    case MatchType::MATCH_PHRASE:
  Branch (108:5): [True: 0, False: 0]
109
0
        ret = InvertedIndexQueryType::MATCH_PHRASE_QUERY;
110
0
        break;
111
0
    case MatchType::MATCH_PHRASE_PREFIX:
  Branch (111:5): [True: 0, False: 0]
112
0
        ret = InvertedIndexQueryType::MATCH_PHRASE_PREFIX_QUERY;
113
0
        break;
114
0
    case MatchType::MATCH_REGEXP:
  Branch (114:5): [True: 0, False: 0]
115
0
        ret = InvertedIndexQueryType::MATCH_REGEXP_QUERY;
116
0
        break;
117
0
    case MatchType::MATCH_PHRASE_EDGE:
  Branch (117:5): [True: 0, False: 0]
118
0
        ret = InvertedIndexQueryType::MATCH_PHRASE_EDGE_QUERY;
119
0
        break;
120
0
    default:
  Branch (120:5): [True: 0, False: 0]
121
0
        DCHECK(false);
122
0
    }
123
0
    return ret;
124
0
}
125
126
0
bool MatchPredicate::_check_evaluate(InvertedIndexIterator* iterator) const {
127
0
    if (_match_type == MatchType::MATCH_PHRASE || _match_type == MatchType::MATCH_PHRASE_PREFIX ||
  Branch (127:9): [True: 0, False: 0]
  Branch (127:51): [True: 0, False: 0]
128
0
        _match_type == MatchType::MATCH_PHRASE_EDGE) {
  Branch (128:9): [True: 0, False: 0]
129
0
        auto reader = iterator->get_reader(InvertedIndexReaderType::FULLTEXT);
130
0
        if (reader &&
  Branch (130:13): [True: 0, False: 0]
  Branch (130:13): [True: 0, False: 0]
131
0
            get_parser_phrase_support_string_from_properties(reader->get_index_properties()) ==
  Branch (131:13): [True: 0, False: 0]
132
0
                    INVERTED_INDEX_PARSER_PHRASE_SUPPORT_NO) {
133
0
            return true;
134
0
        }
135
0
    }
136
0
    return false;
137
0
}
138
139
} // namespace doris