Coverage Report

Created: 2025-04-28 22:38

/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
#include "common/compile_check_begin.h"
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) {
52
0
        return Status::OK();
53
0
    }
54
0
    if (_check_evaluate(iterator)) {
55
0
        return Status::Error<ErrorCode::INVERTED_INDEX_INVALID_PARAMETERS>(
56
0
                "phrase queries require setting support_phrase = true");
57
0
    }
58
0
    auto type = name_with_type.second;
59
0
    const std::string& name = name_with_type.first;
60
0
    std::shared_ptr<roaring::Roaring> roaring = std::make_shared<roaring::Roaring>();
61
0
    auto inverted_index_query_type = _to_inverted_index_query_type(_match_type);
62
0
    auto primitive_type = type->get_primitive_type();
63
0
    if (is_string_type(primitive_type) ||
64
0
        (primitive_type == TYPE_ARRAY &&
65
0
         is_string_type(assert_cast<const vectorized::DataTypeArray*>(
66
0
                                vectorized::remove_nullable(type).get())
67
0
                                ->get_nested_type()
68
0
                                ->get_primitive_type()))) {
69
0
        StringRef match_value;
70
0
        auto length = _value.length();
71
0
        char* buffer = const_cast<char*>(_value.c_str());
72
0
        match_value.replace(buffer, int32_t(length)); //is it safe?
73
0
        RETURN_IF_ERROR(iterator->read_from_inverted_index(
74
0
                name, &match_value, inverted_index_query_type, num_rows, roaring));
75
0
    } else if (primitive_type == TYPE_ARRAY &&
76
0
               is_numeric_type(TabletColumn::get_field_type_by_type(
77
0
                       assert_cast<const vectorized::DataTypeArray*>(
78
0
                               vectorized::remove_nullable(type).get())
79
0
                               ->get_nested_type()
80
0
                               ->get_primitive_type()))) {
81
0
        std::vector<char> buf(assert_cast<const vectorized::DataTypeArray*>(
82
0
                                      vectorized::remove_nullable(type).get())
83
0
                                      ->get_nested_type()
84
0
                                      ->get_size_of_value_in_memory());
85
0
        const TypeInfo* type_info = get_scalar_type_info(TabletColumn::get_field_type_by_type(
86
0
                assert_cast<const vectorized::DataTypeArray*>(
87
0
                        vectorized::remove_nullable(type).get())
88
0
                        ->get_nested_type()
89
0
                        ->get_primitive_type()));
90
0
        RETURN_IF_ERROR(type_info->from_string(buf.data(), _value));
91
0
        RETURN_IF_ERROR(iterator->read_from_inverted_index(
92
0
                name, buf.data(), inverted_index_query_type, num_rows, roaring, true));
93
0
    }
94
95
    // mask out null_bitmap, since NULL cmp VALUE will produce NULL
96
    //  and be treated as false in WHERE
97
    // keep it after query, since query will try to read null_bitmap and put it to cache
98
0
    if (iterator->has_null()) {
99
0
        InvertedIndexQueryCacheHandle null_bitmap_cache_handle;
100
0
        RETURN_IF_ERROR(iterator->read_null_bitmap(&null_bitmap_cache_handle));
101
0
        std::shared_ptr<roaring::Roaring> null_bitmap = null_bitmap_cache_handle.get_bitmap();
102
0
        if (null_bitmap) {
103
0
            *bitmap -= *null_bitmap;
104
0
        }
105
0
    }
106
107
0
    *bitmap &= *roaring;
108
0
    return Status::OK();
109
0
}
110
111
0
InvertedIndexQueryType MatchPredicate::_to_inverted_index_query_type(MatchType match_type) const {
112
0
    auto ret = InvertedIndexQueryType::UNKNOWN_QUERY;
113
0
    switch (match_type) {
114
0
    case MatchType::MATCH_ANY:
115
0
        ret = InvertedIndexQueryType::MATCH_ANY_QUERY;
116
0
        break;
117
0
    case MatchType::MATCH_ALL:
118
0
        ret = InvertedIndexQueryType::MATCH_ALL_QUERY;
119
0
        break;
120
0
    case MatchType::MATCH_PHRASE:
121
0
        ret = InvertedIndexQueryType::MATCH_PHRASE_QUERY;
122
0
        break;
123
0
    case MatchType::MATCH_PHRASE_PREFIX:
124
0
        ret = InvertedIndexQueryType::MATCH_PHRASE_PREFIX_QUERY;
125
0
        break;
126
0
    case MatchType::MATCH_REGEXP:
127
0
        ret = InvertedIndexQueryType::MATCH_REGEXP_QUERY;
128
0
        break;
129
0
    case MatchType::MATCH_PHRASE_EDGE:
130
0
        ret = InvertedIndexQueryType::MATCH_PHRASE_EDGE_QUERY;
131
0
        break;
132
0
    default:
133
0
        DCHECK(false);
134
0
    }
135
0
    return ret;
136
0
}
137
138
0
bool MatchPredicate::_check_evaluate(InvertedIndexIterator* iterator) const {
139
0
    if (_match_type == MatchType::MATCH_PHRASE || _match_type == MatchType::MATCH_PHRASE_PREFIX ||
140
0
        _match_type == MatchType::MATCH_PHRASE_EDGE) {
141
0
        if (iterator->get_inverted_index_reader_type() == InvertedIndexReaderType::FULLTEXT &&
142
0
            get_parser_phrase_support_string_from_properties(iterator->get_index_properties()) ==
143
0
                    INVERTED_INDEX_PARSER_PHRASE_SUPPORT_NO) {
144
0
            return true;
145
0
        }
146
0
    }
147
0
    return false;
148
0
}
149
150
} // namespace doris
151
#include "common/compile_check_end.h"