Coverage Report

Created: 2026-10-14 14:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/function_search.h
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
#pragma once
19
20
// CLucene's conversion warnings must not depend on the unity include order.
21
#ifdef __clang__
22
#pragma clang diagnostic push
23
#pragma clang diagnostic ignored "-Wconversion"
24
#endif
25
#include <CLucene.h>
26
#ifdef __clang__
27
#pragma clang diagnostic pop
28
#endif
29
#include <gen_cpp/Exprs_types.h>
30
31
#include <map>
32
#include <memory>
33
#include <string>
34
#include <unordered_map>
35
#include <vector>
36
37
#include "core/block/block.h"
38
#include "core/data_type/data_type.h"
39
#include "core/data_type/data_type_number.h"
40
#include "core/types.h"
41
#include "exprs/function/function.h"
42
#include "exprs/function/variant_inverted_index_search.h"
43
#include "storage/index/index_query_context.h"
44
#include "storage/index/inverted/inverted_index_cache.h"
45
#include "storage/index/inverted/query_v2/boolean_query/operator_boolean_query.h"
46
47
CL_NS_USE(index)
48
namespace doris {
49
class TabletColumn;
50
51
using namespace doris::segment_v2;
52
53
class IndexExecContext;
54
55
class FunctionSearch : public IFunction {
56
public:
57
    static constexpr auto name = "search";
58
59
10
    static FunctionPtr create() { return std::make_shared<FunctionSearch>(); }
60
61
2
    String get_name() const override { return name; }
62
63
3
    bool is_variadic() const override { return true; }
64
65
1
    size_t get_number_of_arguments() const override { return 0; }
66
67
    // We manage nulls explicitly for index pushdown only.
68
5
    bool use_default_implementation_for_nulls() const override { return false; }
69
6
    bool is_use_default_implementation_for_constants() const override { return false; }
70
71
4
    bool use_default_implementation_for_constants() const override { return false; }
72
73
2
    DataTypePtr get_return_type_impl(const DataTypes& /*arguments*/) const override {
74
2
        return std::make_shared<DataTypeUInt8>();
75
2
    }
76
77
    Status execute_impl(FunctionContext* /*context*/, Block& /*block*/,
78
                        const ColumnNumbers& /*arguments*/, uint32_t /*result*/,
79
                        size_t /*input_rows_count*/) const override;
80
81
1
    bool can_push_down_to_index() const override { return true; }
82
83
    Status evaluate_inverted_index(
84
            const ColumnsWithTypeAndName& arguments,
85
            const std::vector<IndexFieldNameAndTypePair>& data_type_with_names,
86
            std::vector<IndexIterator*> iterators, uint32_t num_rows,
87
            const InvertedIndexAnalyzerCtx* /*analyzer_ctx*/,
88
            InvertedIndexResultBitmap& bitmap_result) const override;
89
90
    Status evaluate_inverted_index_with_search_param(
91
            const TSearchParam& search_param,
92
            const std::unordered_map<std::string, IndexFieldNameAndTypePair>& data_type_with_names,
93
            std::unordered_map<std::string, IndexIterator*> iterators, uint32_t num_rows,
94
            InvertedIndexResultBitmap& bitmap_result, bool enable_cache = true) const;
95
96
    Status evaluate_inverted_index_with_search_param(
97
            const TSearchParam& search_param,
98
            const std::unordered_map<std::string, IndexFieldNameAndTypePair>& data_type_with_names,
99
            std::unordered_map<std::string, IndexIterator*> iterators, uint32_t num_rows,
100
            InvertedIndexResultBitmap& bitmap_result, bool enable_cache,
101
            const IndexExecContext* index_exec_ctx, const TabletColumn* nested_column,
102
            const std::shared_ptr<IndexQueryContext>& index_query_context = nullptr) const;
103
104
    // The body of the overload above; it may throw, so only that overload calls it.
105
    Status evaluate_inverted_index_with_search_param_unguarded(
106
            const TSearchParam& search_param,
107
            const std::unordered_map<std::string, IndexFieldNameAndTypePair>& data_type_with_names,
108
            std::unordered_map<std::string, IndexIterator*> iterators, uint32_t num_rows,
109
            InvertedIndexResultBitmap& bitmap_result, bool enable_cache,
110
            const IndexExecContext* index_exec_ctx, const TabletColumn* nested_column,
111
            const std::shared_ptr<IndexQueryContext>& index_query_context) const;
112
113
    // Public methods for testing
114
    enum class ClauseTypeCategory {
115
        NON_TOKENIZED, // TERM, PREFIX, WILDCARD, REGEXP, RANGE, LIST - no tokenization, use EQUAL_QUERY
116
        TOKENIZED,     // PHRASE, MATCH, ANY, ALL - need tokenization, use MATCH_ANY_QUERY
117
        COMPOUND       // AND, OR, NOT - boolean operations
118
    };
119
120
    ClauseTypeCategory get_clause_type_category(const std::string& clause_type) const;
121
122
    // Analyze query type for a specific field in the search clause
123
    InvertedIndexQueryType analyze_field_query_type(const std::string& field_name,
124
                                                    const TSearchClause& clause) const;
125
126
    // Map clause_type string to InvertedIndexQueryType
127
    InvertedIndexQueryType clause_type_to_query_type(const std::string& clause_type) const;
128
129
    Status build_query_recursive(const TSearchClause& clause,
130
                                 const std::shared_ptr<IndexQueryContext>& context,
131
                                 FieldReaderResolver& resolver,
132
                                 inverted_index::query_v2::QueryPtr* out, std::string* binding_key,
133
                                 const std::string& default_operator, int32_t minimum_should_match,
134
                                 uint32_t num_rows = 0) const;
135
136
    Status build_leaf_query(const TSearchClause& clause,
137
                            const std::shared_ptr<IndexQueryContext>& context,
138
                            FieldReaderResolver& resolver, inverted_index::query_v2::QueryPtr* out,
139
                            std::string* binding_key, const std::string& default_operator,
140
                            int32_t minimum_should_match, uint32_t num_rows = 0) const;
141
};
142
143
} // namespace doris