Coverage Report

Created: 2024-11-21 14:46

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