Coverage Report

Created: 2025-04-26 21:10

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