Coverage Report

Created: 2024-11-18 12:21

/root/doris/be/src/runtime/runtime_predicate.h
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
#pragma once
19
20
#include <functional>
21
#include <memory>
22
#include <mutex>
23
#include <shared_mutex>
24
#include <string>
25
26
#include "common/status.h"
27
#include "exec/olap_common.h"
28
#include "olap/shared_predicate.h"
29
#include "olap/tablet_schema.h"
30
#include "runtime/define_primitive_type.h"
31
#include "runtime/primitive_type.h"
32
#include "util/binary_cast.hpp"
33
#include "vec/common/arena.h"
34
#include "vec/core/field.h"
35
#include "vec/core/types.h"
36
#include "vec/runtime/vdatetime_value.h"
37
38
namespace doris {
39
class ColumnPredicate;
40
41
namespace vectorized {
42
43
class RuntimePredicate {
44
public:
45
0
    RuntimePredicate() = default;
46
47
    Status init(PrimitiveType type, bool nulls_first, bool is_asc, const std::string& col_name);
48
49
0
    bool inited() const {
50
0
        std::shared_lock<std::shared_mutex> rlock(_rwlock);
51
0
        return _inited;
52
0
    }
53
54
0
    bool need_update() const {
55
0
        std::shared_lock<std::shared_mutex> rlock(_rwlock);
56
0
        return _inited && _tablet_schema;
57
0
    }
58
59
0
    Status set_tablet_schema(TabletSchemaSPtr tablet_schema) {
60
0
        std::unique_lock<std::shared_mutex> wlock(_rwlock);
61
        // when sort node and scan node are not in the same backend, predicate will not be initialized
62
0
        if (_tablet_schema || !_inited) {
63
0
            return Status::OK();
64
0
        }
65
0
        RETURN_IF_ERROR(tablet_schema->have_column(_col_name));
66
0
        _tablet_schema = tablet_schema;
67
0
        _predicate = SharedPredicate::create_shared(
68
0
                _tablet_schema->field_index(_tablet_schema->column(_col_name).unique_id()));
69
0
        return Status::OK();
70
0
    }
71
72
0
    std::shared_ptr<ColumnPredicate> get_predicate() {
73
0
        std::shared_lock<std::shared_mutex> rlock(_rwlock);
74
0
        return _predicate;
75
0
    }
76
77
    Status update(const Field& value);
78
79
private:
80
    mutable std::shared_mutex _rwlock;
81
    Field _orderby_extrem {Field::Types::Null};
82
    std::shared_ptr<ColumnPredicate> _predicate;
83
    TabletSchemaSPtr _tablet_schema = nullptr;
84
    Arena _predicate_arena;
85
    std::function<std::string(const Field&)> _get_value_fn;
86
    bool _nulls_first = true;
87
    bool _is_asc;
88
    std::function<ColumnPredicate*(const TabletColumn&, int, const std::string&, bool,
89
                                   vectorized::Arena*)>
90
            _pred_constructor;
91
    bool _inited = false;
92
    std::string _col_name;
93
94
    template <PrimitiveType type>
95
0
    static std::string get_normal_value(const Field& field) {
96
0
        using ValueType = typename PrimitiveTypeTraits<type>::CppType;
97
0
        return cast_to_string<type, ValueType>(field.get<ValueType>(), 0);
98
0
    }
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate16get_normal_valueILNS_13PrimitiveTypeE2EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate16get_normal_valueILNS_13PrimitiveTypeE3EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate16get_normal_valueILNS_13PrimitiveTypeE4EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate16get_normal_valueILNS_13PrimitiveTypeE5EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate16get_normal_valueILNS_13PrimitiveTypeE6EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate16get_normal_valueILNS_13PrimitiveTypeE7EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate16get_normal_valueILNS_13PrimitiveTypeE25EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate16get_normal_valueILNS_13PrimitiveTypeE26EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate16get_normal_valueILNS_13PrimitiveTypeE36EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate16get_normal_valueILNS_13PrimitiveTypeE37EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
99
100
0
    static std::string get_date_value(const Field& field) {
101
0
        using ValueType = typename PrimitiveTypeTraits<TYPE_DATE>::CppType;
102
0
        ValueType value;
103
0
        Int64 v = field.get<Int64>();
104
0
        auto* p = (VecDateTimeValue*)&v;
105
0
        value.from_olap_date(p->to_olap_date());
106
0
        value.cast_to_date();
107
0
        return cast_to_string<TYPE_DATE, ValueType>(value, 0);
108
0
    }
109
110
0
    static std::string get_datetime_value(const Field& field) {
111
0
        using ValueType = typename PrimitiveTypeTraits<TYPE_DATETIME>::CppType;
112
0
        ValueType value;
113
0
        Int64 v = field.get<Int64>();
114
0
        auto* p = (VecDateTimeValue*)&v;
115
0
        value.from_olap_datetime(p->to_olap_datetime());
116
0
        value.to_datetime();
117
0
        return cast_to_string<TYPE_DATETIME, ValueType>(value, 0);
118
0
    }
119
120
0
    static std::string get_decimalv2_value(const Field& field) {
121
        // can NOT use PrimitiveTypeTraits<TYPE_DECIMALV2>::CppType since
122
        //   it is DecimalV2Value and Decimal128V2 can not convert to it implicitly
123
0
        using ValueType = Decimal128V2::NativeType;
124
0
        auto v = field.get<DecimalField<Decimal128V2>>();
125
        // use TYPE_DECIMAL128I instead of TYPE_DECIMALV2 since v.get_scale()
126
        //   is always 9 for DECIMALV2
127
0
        return cast_to_string<TYPE_DECIMAL128I, ValueType>(v.get_value(), v.get_scale());
128
0
    }
129
130
    template <PrimitiveType type>
131
0
    static std::string get_decimal_value(const Field& field) {
132
0
        using ValueType = typename PrimitiveTypeTraits<type>::CppType;
133
0
        auto v = field.get<DecimalField<ValueType>>();
134
0
        return cast_to_string<type, ValueType>(v.get_value(), v.get_scale());
135
0
    }
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate17get_decimal_valueILNS_13PrimitiveTypeE28EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate17get_decimal_valueILNS_13PrimitiveTypeE29EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate17get_decimal_valueILNS_13PrimitiveTypeE30EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16RuntimePredicate17get_decimal_valueILNS_13PrimitiveTypeE35EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
136
};
137
138
} // namespace vectorized
139
} // namespace doris