Coverage Report

Created: 2026-08-17 23:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/common/join_op_utils.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
// Lightweight join-op level types split out of join_utils.h so that
21
// exec/pipeline/dependency.h (which holds JoinOpVariants / AsofIndexVariant
22
// members by value) does not have to see the hash-table machinery.
23
// Everything here depends only on thrift enums and std containers.
24
25
#include <gen_cpp/PlanNodes_types.h>
26
#include <pdqsort.h>
27
28
#include <cstdint>
29
#include <variant>
30
#include <vector>
31
32
#include "common/compiler_util.h"
33
34
namespace doris {
35
36
using JoinOpVariants =
37
        std::variant<std::integral_constant<TJoinOp::type, TJoinOp::INNER_JOIN>,
38
                     std::integral_constant<TJoinOp::type, TJoinOp::LEFT_SEMI_JOIN>,
39
                     std::integral_constant<TJoinOp::type, TJoinOp::LEFT_ANTI_JOIN>,
40
                     std::integral_constant<TJoinOp::type, TJoinOp::LEFT_OUTER_JOIN>,
41
                     std::integral_constant<TJoinOp::type, TJoinOp::FULL_OUTER_JOIN>,
42
                     std::integral_constant<TJoinOp::type, TJoinOp::RIGHT_OUTER_JOIN>,
43
                     std::integral_constant<TJoinOp::type, TJoinOp::CROSS_JOIN>,
44
                     std::integral_constant<TJoinOp::type, TJoinOp::RIGHT_SEMI_JOIN>,
45
                     std::integral_constant<TJoinOp::type, TJoinOp::RIGHT_ANTI_JOIN>,
46
                     std::integral_constant<TJoinOp::type, TJoinOp::NULL_AWARE_LEFT_ANTI_JOIN>,
47
                     std::integral_constant<TJoinOp::type, TJoinOp::NULL_AWARE_LEFT_SEMI_JOIN>,
48
                     std::integral_constant<TJoinOp::type, TJoinOp::ASOF_LEFT_INNER_JOIN>,
49
                     std::integral_constant<TJoinOp::type, TJoinOp::ASOF_LEFT_OUTER_JOIN>>;
50
51
336k
inline bool is_asof_join(TJoinOp::type join_op) {
52
336k
    return join_op == TJoinOp::ASOF_LEFT_INNER_JOIN || join_op == TJoinOp::ASOF_LEFT_OUTER_JOIN;
53
336k
}
54
55
template <int JoinOpType>
56
inline constexpr bool is_asof_join_op_v =
57
        JoinOpType == TJoinOp::ASOF_LEFT_INNER_JOIN || JoinOpType == TJoinOp::ASOF_LEFT_OUTER_JOIN;
58
59
template <int JoinOpType>
60
inline constexpr bool is_asof_outer_join_op_v = JoinOpType == TJoinOp::ASOF_LEFT_OUTER_JOIN;
61
62
// ASOF JOIN index with inline values for cache-friendly branchless binary search.
63
// IntType is the integer representation of the ASOF column value:
64
//   uint32_t for DateV2, uint64_t for DateTimeV2 and TimestampTZ.
65
// Rows are sorted by asof_value during build, then materialized into SoA arrays
66
// so probe-side binary search only touches the ASOF values hot path.
67
template <typename IntType>
68
struct AsofIndexGroup {
69
    using int_type = IntType;
70
71
    struct Entry {
72
        IntType asof_value;
73
        uint32_t row_index; // 1-based, 0 = invalid/padding
74
    };
75
76
    std::vector<Entry> entries;
77
    std::vector<IntType> asof_values;
78
    std::vector<uint32_t> row_indexes;
79
80
1.06k
    void add_row(IntType value, uint32_t row_idx) { entries.push_back({value, row_idx}); }
_ZN5doris14AsofIndexGroupIjE7add_rowEjj
Line
Count
Source
80
1.05k
    void add_row(IntType value, uint32_t row_idx) { entries.push_back({value, row_idx}); }
_ZN5doris14AsofIndexGroupImE7add_rowEmj
Line
Count
Source
80
12
    void add_row(IntType value, uint32_t row_idx) { entries.push_back({value, row_idx}); }
81
82
23
    void sort_and_finalize() {
83
23
        if (entries.empty()) {
84
4
            return;
85
4
        }
86
19
        if (entries.size() > 1) {
87
15
            pdqsort(entries.begin(), entries.end(),
88
2.06k
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
_ZZN5doris14AsofIndexGroupIjE17sort_and_finalizeEvENKUlRKNS1_5EntryES4_E_clES4_S4_
Line
Count
Source
88
2.05k
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
_ZZN5doris14AsofIndexGroupImE17sort_and_finalizeEvENKUlRKNS1_5EntryES4_E_clES4_S4_
Line
Count
Source
88
9
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
89
15
        }
90
91
19
        asof_values.resize(entries.size());
92
19
        row_indexes.resize(entries.size());
93
1.08k
        for (size_t i = 0; i < entries.size(); ++i) {
94
1.06k
            asof_values[i] = entries[i].asof_value;
95
1.06k
            row_indexes[i] = entries[i].row_index;
96
1.06k
        }
97
98
19
        std::vector<Entry>().swap(entries);
99
19
    }
_ZN5doris14AsofIndexGroupIjE17sort_and_finalizeEv
Line
Count
Source
82
19
    void sort_and_finalize() {
83
19
        if (entries.empty()) {
84
4
            return;
85
4
        }
86
15
        if (entries.size() > 1) {
87
12
            pdqsort(entries.begin(), entries.end(),
88
12
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
89
12
        }
90
91
15
        asof_values.resize(entries.size());
92
15
        row_indexes.resize(entries.size());
93
1.06k
        for (size_t i = 0; i < entries.size(); ++i) {
94
1.05k
            asof_values[i] = entries[i].asof_value;
95
1.05k
            row_indexes[i] = entries[i].row_index;
96
1.05k
        }
97
98
15
        std::vector<Entry>().swap(entries);
99
15
    }
_ZN5doris14AsofIndexGroupImE17sort_and_finalizeEv
Line
Count
Source
82
4
    void sort_and_finalize() {
83
4
        if (entries.empty()) {
84
0
            return;
85
0
        }
86
4
        if (entries.size() > 1) {
87
3
            pdqsort(entries.begin(), entries.end(),
88
3
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
89
3
        }
90
91
4
        asof_values.resize(entries.size());
92
4
        row_indexes.resize(entries.size());
93
16
        for (size_t i = 0; i < entries.size(); ++i) {
94
12
            asof_values[i] = entries[i].asof_value;
95
12
            row_indexes[i] = entries[i].row_index;
96
12
        }
97
98
4
        std::vector<Entry>().swap(entries);
99
4
    }
100
101
1
    const IntType* values_data() const { return asof_values.data(); }
_ZNK5doris14AsofIndexGroupIjE11values_dataEv
Line
Count
Source
101
1
    const IntType* values_data() const { return asof_values.data(); }
Unexecuted instantiation: _ZNK5doris14AsofIndexGroupImE11values_dataEv
102
103
    // Branchless lower_bound: first i where asof_values[i] >= target
104
39
    ALWAYS_INLINE size_t lower_bound(IntType target) const {
105
39
        size_t lo = 0, n = asof_values.size();
106
181
        while (n > 1) {
107
142
            size_t half = n / 2;
108
142
            lo += half * (asof_values[lo + half] < target);
109
142
            n -= half;
110
142
        }
111
39
        if (lo < asof_values.size()) {
112
38
            lo += (asof_values[lo] < target);
113
38
        }
114
39
        return lo;
115
39
    }
_ZNK5doris14AsofIndexGroupIjE11lower_boundEj
Line
Count
Source
104
33
    ALWAYS_INLINE size_t lower_bound(IntType target) const {
105
33
        size_t lo = 0, n = asof_values.size();
106
163
        while (n > 1) {
107
130
            size_t half = n / 2;
108
130
            lo += half * (asof_values[lo + half] < target);
109
130
            n -= half;
110
130
        }
111
33
        if (lo < asof_values.size()) {
112
32
            lo += (asof_values[lo] < target);
113
32
        }
114
33
        return lo;
115
33
    }
_ZNK5doris14AsofIndexGroupImE11lower_boundEm
Line
Count
Source
104
6
    ALWAYS_INLINE size_t lower_bound(IntType target) const {
105
6
        size_t lo = 0, n = asof_values.size();
106
18
        while (n > 1) {
107
12
            size_t half = n / 2;
108
12
            lo += half * (asof_values[lo + half] < target);
109
12
            n -= half;
110
12
        }
111
6
        if (lo < asof_values.size()) {
112
6
            lo += (asof_values[lo] < target);
113
6
        }
114
6
        return lo;
115
6
    }
116
117
    // Branchless upper_bound: first i where asof_values[i] > target
118
44
    ALWAYS_INLINE size_t upper_bound(IntType target) const {
119
44
        size_t lo = 0, n = asof_values.size();
120
226
        while (n > 1) {
121
182
            size_t half = n / 2;
122
182
            lo += half * (asof_values[lo + half] <= target);
123
182
            n -= half;
124
182
        }
125
44
        if (lo < asof_values.size()) {
126
43
            lo += (asof_values[lo] <= target);
127
43
        }
128
44
        return lo;
129
44
    }
_ZNK5doris14AsofIndexGroupIjE11upper_boundEj
Line
Count
Source
118
38
    ALWAYS_INLINE size_t upper_bound(IntType target) const {
119
38
        size_t lo = 0, n = asof_values.size();
120
208
        while (n > 1) {
121
170
            size_t half = n / 2;
122
170
            lo += half * (asof_values[lo + half] <= target);
123
170
            n -= half;
124
170
        }
125
38
        if (lo < asof_values.size()) {
126
37
            lo += (asof_values[lo] <= target);
127
37
        }
128
38
        return lo;
129
38
    }
_ZNK5doris14AsofIndexGroupImE11upper_boundEm
Line
Count
Source
118
6
    ALWAYS_INLINE size_t upper_bound(IntType target) const {
119
6
        size_t lo = 0, n = asof_values.size();
120
18
        while (n > 1) {
121
12
            size_t half = n / 2;
122
12
            lo += half * (asof_values[lo + half] <= target);
123
12
            n -= half;
124
12
        }
125
6
        if (lo < asof_values.size()) {
126
6
            lo += (asof_values[lo] <= target);
127
6
        }
128
6
        return lo;
129
6
    }
130
131
    // Semantics by (is_greater, is_strict):
132
    //   (true,  false): probe >= build  ->  find largest  build value <= probe
133
    //   (true,  true):  probe >  build  ->  find largest  build value <  probe
134
    //   (false, false): probe <= build  ->  find smallest build value >= probe
135
    //   (false, true):  probe <  build  ->  find smallest build value >  probe
136
    // Returns the build row index of the best match, or 0 if no match.
137
    template <bool IsGreater, bool IsStrict>
138
69
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
139
69
        if (asof_values.empty()) {
140
4
            return 0;
141
4
        }
142
65
        if constexpr (IsGreater) {
143
34
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
144
34
            return pos > 0 ? row_indexes[pos - 1] : 0;
145
34
        } else {
146
31
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
147
31
            return pos < asof_values.size() ? row_indexes[pos] : 0;
148
31
        }
149
65
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb1ELb0EEEjj
Line
Count
Source
138
19
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
139
19
        if (asof_values.empty()) {
140
1
            return 0;
141
1
        }
142
18
        if constexpr (IsGreater) {
143
18
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
144
18
            return pos > 0 ? row_indexes[pos - 1] : 0;
145
        } else {
146
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
147
            return pos < asof_values.size() ? row_indexes[pos] : 0;
148
        }
149
18
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb1ELb1EEEjj
Line
Count
Source
138
15
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
139
15
        if (asof_values.empty()) {
140
1
            return 0;
141
1
        }
142
14
        if constexpr (IsGreater) {
143
14
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
144
14
            return pos > 0 ? row_indexes[pos - 1] : 0;
145
        } else {
146
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
147
            return pos < asof_values.size() ? row_indexes[pos] : 0;
148
        }
149
14
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb0ELb0EEEjj
Line
Count
Source
138
15
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
139
15
        if (asof_values.empty()) {
140
1
            return 0;
141
1
        }
142
        if constexpr (IsGreater) {
143
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
144
            return pos > 0 ? row_indexes[pos - 1] : 0;
145
14
        } else {
146
14
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
147
14
            return pos < asof_values.size() ? row_indexes[pos] : 0;
148
14
        }
149
14
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb0ELb1EEEjj
Line
Count
Source
138
16
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
139
16
        if (asof_values.empty()) {
140
1
            return 0;
141
1
        }
142
        if constexpr (IsGreater) {
143
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
144
            return pos > 0 ? row_indexes[pos - 1] : 0;
145
15
        } else {
146
15
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
147
15
            return pos < asof_values.size() ? row_indexes[pos] : 0;
148
15
        }
149
15
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb1ELb0EEEjm
Line
Count
Source
138
1
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
139
1
        if (asof_values.empty()) {
140
0
            return 0;
141
0
        }
142
1
        if constexpr (IsGreater) {
143
1
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
144
1
            return pos > 0 ? row_indexes[pos - 1] : 0;
145
        } else {
146
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
147
            return pos < asof_values.size() ? row_indexes[pos] : 0;
148
        }
149
1
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb1ELb1EEEjm
Line
Count
Source
138
1
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
139
1
        if (asof_values.empty()) {
140
0
            return 0;
141
0
        }
142
1
        if constexpr (IsGreater) {
143
1
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
144
1
            return pos > 0 ? row_indexes[pos - 1] : 0;
145
        } else {
146
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
147
            return pos < asof_values.size() ? row_indexes[pos] : 0;
148
        }
149
1
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb0ELb0EEEjm
Line
Count
Source
138
1
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
139
1
        if (asof_values.empty()) {
140
0
            return 0;
141
0
        }
142
        if constexpr (IsGreater) {
143
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
144
            return pos > 0 ? row_indexes[pos - 1] : 0;
145
1
        } else {
146
1
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
147
1
            return pos < asof_values.size() ? row_indexes[pos] : 0;
148
1
        }
149
1
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb0ELb1EEEjm
Line
Count
Source
138
1
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
139
1
        if (asof_values.empty()) {
140
0
            return 0;
141
0
        }
142
        if constexpr (IsGreater) {
143
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
144
            return pos > 0 ? row_indexes[pos - 1] : 0;
145
1
        } else {
146
1
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
147
1
            return pos < asof_values.size() ? row_indexes[pos] : 0;
148
1
        }
149
1
    }
150
};
151
152
// Type-erased container for all ASOF index groups.
153
// DateV2 -> uint32_t, DateTimeV2/TimestampTZ -> uint64_t.
154
using AsofIndexVariant = std::variant<std::monostate, std::vector<AsofIndexGroup<uint32_t>>,
155
                                      std::vector<AsofIndexGroup<uint64_t>>>;
156
157
} // namespace doris