Coverage Report

Created: 2026-10-22 01:09

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
432k
inline bool is_asof_join(TJoinOp::type join_op) {
52
432k
    return join_op == TJoinOp::ASOF_LEFT_INNER_JOIN || join_op == TJoinOp::ASOF_LEFT_OUTER_JOIN;
53
432k
}
54
55
template <TJoinOp::type 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 <TJoinOp::type 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
//   int64_t for TimestampNs.
66
// Rows are sorted by asof_value during build, then materialized into SoA arrays
67
// so probe-side binary search only touches the ASOF values hot path.
68
template <typename IntType>
69
struct AsofIndexGroup {
70
    using int_type = IntType;
71
72
    struct Entry {
73
        IntType asof_value;
74
        uint32_t row_index; // 1-based, 0 = invalid/padding
75
    };
76
77
    std::vector<Entry> entries;
78
    std::vector<IntType> asof_values;
79
    std::vector<uint32_t> row_indexes;
80
81
1.07k
    void add_row(IntType value, uint32_t row_idx) { entries.push_back({value, row_idx}); }
_ZN5doris14AsofIndexGroupIjE7add_rowEjj
Line
Count
Source
81
1.05k
    void add_row(IntType value, uint32_t row_idx) { entries.push_back({value, row_idx}); }
_ZN5doris14AsofIndexGroupImE7add_rowEmj
Line
Count
Source
81
12
    void add_row(IntType value, uint32_t row_idx) { entries.push_back({value, row_idx}); }
_ZN5doris14AsofIndexGroupIlE7add_rowElj
Line
Count
Source
81
5
    void add_row(IntType value, uint32_t row_idx) { entries.push_back({value, row_idx}); }
82
83
26
    void sort_and_finalize() {
84
26
        if (entries.empty()) {
85
4
            return;
86
4
        }
87
22
        if (entries.size() > 1) {
88
18
            pdqsort(entries.begin(), entries.end(),
89
2.07k
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
_ZZN5doris14AsofIndexGroupIjE17sort_and_finalizeEvENKUlRKNS1_5EntryES4_E_clES4_S4_
Line
Count
Source
89
2.06k
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
_ZZN5doris14AsofIndexGroupImE17sort_and_finalizeEvENKUlRKNS1_5EntryES4_E_clES4_S4_
Line
Count
Source
89
9
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
_ZZN5doris14AsofIndexGroupIlE17sort_and_finalizeEvENKUlRKNS1_5EntryES4_E_clES4_S4_
Line
Count
Source
89
3
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
90
18
        }
91
92
22
        asof_values.resize(entries.size());
93
22
        row_indexes.resize(entries.size());
94
1.09k
        for (size_t i = 0; i < entries.size(); ++i) {
95
1.07k
            asof_values[i] = entries[i].asof_value;
96
1.07k
            row_indexes[i] = entries[i].row_index;
97
1.07k
        }
98
99
22
        std::vector<Entry>().swap(entries);
100
22
    }
_ZN5doris14AsofIndexGroupIjE17sort_and_finalizeEv
Line
Count
Source
83
20
    void sort_and_finalize() {
84
20
        if (entries.empty()) {
85
4
            return;
86
4
        }
87
16
        if (entries.size() > 1) {
88
13
            pdqsort(entries.begin(), entries.end(),
89
13
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
90
13
        }
91
92
16
        asof_values.resize(entries.size());
93
16
        row_indexes.resize(entries.size());
94
1.07k
        for (size_t i = 0; i < entries.size(); ++i) {
95
1.05k
            asof_values[i] = entries[i].asof_value;
96
1.05k
            row_indexes[i] = entries[i].row_index;
97
1.05k
        }
98
99
16
        std::vector<Entry>().swap(entries);
100
16
    }
_ZN5doris14AsofIndexGroupImE17sort_and_finalizeEv
Line
Count
Source
83
4
    void sort_and_finalize() {
84
4
        if (entries.empty()) {
85
0
            return;
86
0
        }
87
4
        if (entries.size() > 1) {
88
3
            pdqsort(entries.begin(), entries.end(),
89
3
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
90
3
        }
91
92
4
        asof_values.resize(entries.size());
93
4
        row_indexes.resize(entries.size());
94
16
        for (size_t i = 0; i < entries.size(); ++i) {
95
12
            asof_values[i] = entries[i].asof_value;
96
12
            row_indexes[i] = entries[i].row_index;
97
12
        }
98
99
4
        std::vector<Entry>().swap(entries);
100
4
    }
_ZN5doris14AsofIndexGroupIlE17sort_and_finalizeEv
Line
Count
Source
83
2
    void sort_and_finalize() {
84
2
        if (entries.empty()) {
85
0
            return;
86
0
        }
87
2
        if (entries.size() > 1) {
88
2
            pdqsort(entries.begin(), entries.end(),
89
2
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
90
2
        }
91
92
2
        asof_values.resize(entries.size());
93
2
        row_indexes.resize(entries.size());
94
7
        for (size_t i = 0; i < entries.size(); ++i) {
95
5
            asof_values[i] = entries[i].asof_value;
96
5
            row_indexes[i] = entries[i].row_index;
97
5
        }
98
99
2
        std::vector<Entry>().swap(entries);
100
2
    }
101
102
1
    const IntType* values_data() const { return asof_values.data(); }
_ZNK5doris14AsofIndexGroupIjE11values_dataEv
Line
Count
Source
102
1
    const IntType* values_data() const { return asof_values.data(); }
Unexecuted instantiation: _ZNK5doris14AsofIndexGroupImE11values_dataEv
Unexecuted instantiation: _ZNK5doris14AsofIndexGroupIlE11values_dataEv
103
104
    // Branchless lower_bound: first i where asof_values[i] >= target
105
41
    ALWAYS_INLINE size_t lower_bound(IntType target) const {
106
41
        size_t lo = 0, n = asof_values.size();
107
187
        while (n > 1) {
108
146
            size_t half = n / 2;
109
146
            lo += half * (asof_values[lo + half] < target);
110
146
            n -= half;
111
146
        }
112
41
        if (lo < asof_values.size()) {
113
40
            lo += (asof_values[lo] < target);
114
40
        }
115
41
        return lo;
116
41
    }
_ZNK5doris14AsofIndexGroupIjE11lower_boundEj
Line
Count
Source
105
33
    ALWAYS_INLINE size_t lower_bound(IntType target) const {
106
33
        size_t lo = 0, n = asof_values.size();
107
163
        while (n > 1) {
108
130
            size_t half = n / 2;
109
130
            lo += half * (asof_values[lo + half] < target);
110
130
            n -= half;
111
130
        }
112
33
        if (lo < asof_values.size()) {
113
32
            lo += (asof_values[lo] < target);
114
32
        }
115
33
        return lo;
116
33
    }
_ZNK5doris14AsofIndexGroupImE11lower_boundEm
Line
Count
Source
105
6
    ALWAYS_INLINE size_t lower_bound(IntType target) const {
106
6
        size_t lo = 0, n = asof_values.size();
107
18
        while (n > 1) {
108
12
            size_t half = n / 2;
109
12
            lo += half * (asof_values[lo + half] < target);
110
12
            n -= half;
111
12
        }
112
6
        if (lo < asof_values.size()) {
113
6
            lo += (asof_values[lo] < target);
114
6
        }
115
6
        return lo;
116
6
    }
_ZNK5doris14AsofIndexGroupIlE11lower_boundEl
Line
Count
Source
105
2
    ALWAYS_INLINE size_t lower_bound(IntType target) const {
106
2
        size_t lo = 0, n = asof_values.size();
107
6
        while (n > 1) {
108
4
            size_t half = n / 2;
109
4
            lo += half * (asof_values[lo + half] < target);
110
4
            n -= half;
111
4
        }
112
2
        if (lo < asof_values.size()) {
113
2
            lo += (asof_values[lo] < target);
114
2
        }
115
2
        return lo;
116
2
    }
117
118
    // Branchless upper_bound: first i where asof_values[i] > target
119
46
    ALWAYS_INLINE size_t upper_bound(IntType target) const {
120
46
        size_t lo = 0, n = asof_values.size();
121
232
        while (n > 1) {
122
186
            size_t half = n / 2;
123
186
            lo += half * (asof_values[lo + half] <= target);
124
186
            n -= half;
125
186
        }
126
46
        if (lo < asof_values.size()) {
127
45
            lo += (asof_values[lo] <= target);
128
45
        }
129
46
        return lo;
130
46
    }
_ZNK5doris14AsofIndexGroupIjE11upper_boundEj
Line
Count
Source
119
38
    ALWAYS_INLINE size_t upper_bound(IntType target) const {
120
38
        size_t lo = 0, n = asof_values.size();
121
208
        while (n > 1) {
122
170
            size_t half = n / 2;
123
170
            lo += half * (asof_values[lo + half] <= target);
124
170
            n -= half;
125
170
        }
126
38
        if (lo < asof_values.size()) {
127
37
            lo += (asof_values[lo] <= target);
128
37
        }
129
38
        return lo;
130
38
    }
_ZNK5doris14AsofIndexGroupImE11upper_boundEm
Line
Count
Source
119
6
    ALWAYS_INLINE size_t upper_bound(IntType target) const {
120
6
        size_t lo = 0, n = asof_values.size();
121
18
        while (n > 1) {
122
12
            size_t half = n / 2;
123
12
            lo += half * (asof_values[lo + half] <= target);
124
12
            n -= half;
125
12
        }
126
6
        if (lo < asof_values.size()) {
127
6
            lo += (asof_values[lo] <= target);
128
6
        }
129
6
        return lo;
130
6
    }
_ZNK5doris14AsofIndexGroupIlE11upper_boundEl
Line
Count
Source
119
2
    ALWAYS_INLINE size_t upper_bound(IntType target) const {
120
2
        size_t lo = 0, n = asof_values.size();
121
6
        while (n > 1) {
122
4
            size_t half = n / 2;
123
4
            lo += half * (asof_values[lo + half] <= target);
124
4
            n -= half;
125
4
        }
126
2
        if (lo < asof_values.size()) {
127
2
            lo += (asof_values[lo] <= target);
128
2
        }
129
2
        return lo;
130
2
    }
131
132
    // Semantics by (is_greater, is_strict):
133
    //   (true,  false): probe >= build  ->  find largest  build value <= probe
134
    //   (true,  true):  probe >  build  ->  find largest  build value <  probe
135
    //   (false, false): probe <= build  ->  find smallest build value >= probe
136
    //   (false, true):  probe <  build  ->  find smallest build value >  probe
137
    // Returns the build row index of the best match, or 0 if no match.
138
    template <bool IsGreater, bool IsStrict>
139
73
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
140
73
        if (asof_values.empty()) {
141
4
            return 0;
142
4
        }
143
69
        if constexpr (IsGreater) {
144
36
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
145
36
            return pos > 0 ? row_indexes[pos - 1] : 0;
146
36
        } else {
147
33
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
148
33
            return pos < asof_values.size() ? row_indexes[pos] : 0;
149
33
        }
150
69
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb1ELb0EEEjj
Line
Count
Source
139
19
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
140
19
        if (asof_values.empty()) {
141
1
            return 0;
142
1
        }
143
18
        if constexpr (IsGreater) {
144
18
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
145
18
            return pos > 0 ? row_indexes[pos - 1] : 0;
146
        } else {
147
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
148
            return pos < asof_values.size() ? row_indexes[pos] : 0;
149
        }
150
18
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb1ELb1EEEjj
Line
Count
Source
139
15
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
140
15
        if (asof_values.empty()) {
141
1
            return 0;
142
1
        }
143
14
        if constexpr (IsGreater) {
144
14
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
145
14
            return pos > 0 ? row_indexes[pos - 1] : 0;
146
        } else {
147
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
148
            return pos < asof_values.size() ? row_indexes[pos] : 0;
149
        }
150
14
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb0ELb0EEEjj
Line
Count
Source
139
15
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
140
15
        if (asof_values.empty()) {
141
1
            return 0;
142
1
        }
143
        if constexpr (IsGreater) {
144
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
145
            return pos > 0 ? row_indexes[pos - 1] : 0;
146
14
        } else {
147
14
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
148
14
            return pos < asof_values.size() ? row_indexes[pos] : 0;
149
14
        }
150
14
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb0ELb1EEEjj
Line
Count
Source
139
16
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
140
16
        if (asof_values.empty()) {
141
1
            return 0;
142
1
        }
143
        if constexpr (IsGreater) {
144
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
145
            return pos > 0 ? row_indexes[pos - 1] : 0;
146
15
        } else {
147
15
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
148
15
            return pos < asof_values.size() ? row_indexes[pos] : 0;
149
15
        }
150
15
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb1ELb0EEEjm
Line
Count
Source
139
1
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
140
1
        if (asof_values.empty()) {
141
0
            return 0;
142
0
        }
143
1
        if constexpr (IsGreater) {
144
1
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
145
1
            return pos > 0 ? row_indexes[pos - 1] : 0;
146
        } else {
147
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
148
            return pos < asof_values.size() ? row_indexes[pos] : 0;
149
        }
150
1
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb1ELb1EEEjm
Line
Count
Source
139
1
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
140
1
        if (asof_values.empty()) {
141
0
            return 0;
142
0
        }
143
1
        if constexpr (IsGreater) {
144
1
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
145
1
            return pos > 0 ? row_indexes[pos - 1] : 0;
146
        } else {
147
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
148
            return pos < asof_values.size() ? row_indexes[pos] : 0;
149
        }
150
1
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb0ELb0EEEjm
Line
Count
Source
139
1
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
140
1
        if (asof_values.empty()) {
141
0
            return 0;
142
0
        }
143
        if constexpr (IsGreater) {
144
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
145
            return pos > 0 ? row_indexes[pos - 1] : 0;
146
1
        } else {
147
1
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
148
1
            return pos < asof_values.size() ? row_indexes[pos] : 0;
149
1
        }
150
1
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb0ELb1EEEjm
Line
Count
Source
139
1
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
140
1
        if (asof_values.empty()) {
141
0
            return 0;
142
0
        }
143
        if constexpr (IsGreater) {
144
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
145
            return pos > 0 ? row_indexes[pos - 1] : 0;
146
1
        } else {
147
1
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
148
1
            return pos < asof_values.size() ? row_indexes[pos] : 0;
149
1
        }
150
1
    }
_ZNK5doris14AsofIndexGroupIlE15find_best_matchILb1ELb0EEEjl
Line
Count
Source
139
1
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
140
1
        if (asof_values.empty()) {
141
0
            return 0;
142
0
        }
143
1
        if constexpr (IsGreater) {
144
1
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
145
1
            return pos > 0 ? row_indexes[pos - 1] : 0;
146
        } else {
147
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
148
            return pos < asof_values.size() ? row_indexes[pos] : 0;
149
        }
150
1
    }
_ZNK5doris14AsofIndexGroupIlE15find_best_matchILb1ELb1EEEjl
Line
Count
Source
139
1
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
140
1
        if (asof_values.empty()) {
141
0
            return 0;
142
0
        }
143
1
        if constexpr (IsGreater) {
144
1
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
145
1
            return pos > 0 ? row_indexes[pos - 1] : 0;
146
        } else {
147
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
148
            return pos < asof_values.size() ? row_indexes[pos] : 0;
149
        }
150
1
    }
_ZNK5doris14AsofIndexGroupIlE15find_best_matchILb0ELb0EEEjl
Line
Count
Source
139
1
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
140
1
        if (asof_values.empty()) {
141
0
            return 0;
142
0
        }
143
        if constexpr (IsGreater) {
144
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
145
            return pos > 0 ? row_indexes[pos - 1] : 0;
146
1
        } else {
147
1
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
148
1
            return pos < asof_values.size() ? row_indexes[pos] : 0;
149
1
        }
150
1
    }
_ZNK5doris14AsofIndexGroupIlE15find_best_matchILb0ELb1EEEjl
Line
Count
Source
139
1
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
140
1
        if (asof_values.empty()) {
141
0
            return 0;
142
0
        }
143
        if constexpr (IsGreater) {
144
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
145
            return pos > 0 ? row_indexes[pos - 1] : 0;
146
1
        } else {
147
1
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
148
1
            return pos < asof_values.size() ? row_indexes[pos] : 0;
149
1
        }
150
1
    }
151
};
152
153
// Type-erased container for all ASOF index groups.
154
// DateV2 -> uint32_t, DateTimeV2/TimestampTZ -> uint64_t, TimestampNs -> int64_t.
155
using AsofIndexVariant =
156
        std::variant<std::monostate, std::vector<AsofIndexGroup<uint32_t>>,
157
                     std::vector<AsofIndexGroup<uint64_t>>, std::vector<AsofIndexGroup<int64_t>>>;
158
159
} // namespace doris