Coverage Report

Created: 2026-04-16 11:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/common/join_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
#include <algorithm>
21
#include <variant>
22
23
#include "exec/common/hash_table/hash_key_type.h"
24
#include "exec/common/hash_table/hash_map_context.h"
25
#include "exec/common/hash_table/join_hash_table.h"
26
27
namespace doris {
28
29
// Devirtualize compare_at for ASOF JOIN supported column types.
30
// ASOF JOIN only supports DateV2, DateTimeV2, and TimestampTZ.
31
// Dispatches to the concrete ColumnVector<T> once so that all compare_at
32
// calls inside `func` are direct (non-virtual) calls.
33
// `func` receives a single argument: a const pointer to the concrete column
34
// (or const IColumn* as fallback for unexpected types).
35
template <typename Func>
36
410
decltype(auto) asof_column_dispatch(const IColumn* col, Func&& func) {
37
410
    if (const auto* c_dv2 = check_and_get_column<ColumnDateV2>(col)) {
38
3
        return std::forward<Func>(func)(c_dv2);
39
407
    } else if (const auto* c_dtv2 = check_and_get_column<ColumnDateTimeV2>(col)) {
40
403
        return std::forward<Func>(func)(c_dtv2);
41
403
    } else if (const auto* c_tstz = check_and_get_column<ColumnTimeStampTz>(col)) {
42
4
        return std::forward<Func>(func)(c_tstz);
43
4
    } else {
44
0
        return std::forward<Func>(func)(col);
45
0
    }
46
410
}
47
using JoinOpVariants =
48
        std::variant<std::integral_constant<TJoinOp::type, TJoinOp::INNER_JOIN>,
49
                     std::integral_constant<TJoinOp::type, TJoinOp::LEFT_SEMI_JOIN>,
50
                     std::integral_constant<TJoinOp::type, TJoinOp::LEFT_ANTI_JOIN>,
51
                     std::integral_constant<TJoinOp::type, TJoinOp::LEFT_OUTER_JOIN>,
52
                     std::integral_constant<TJoinOp::type, TJoinOp::FULL_OUTER_JOIN>,
53
                     std::integral_constant<TJoinOp::type, TJoinOp::RIGHT_OUTER_JOIN>,
54
                     std::integral_constant<TJoinOp::type, TJoinOp::CROSS_JOIN>,
55
                     std::integral_constant<TJoinOp::type, TJoinOp::RIGHT_SEMI_JOIN>,
56
                     std::integral_constant<TJoinOp::type, TJoinOp::RIGHT_ANTI_JOIN>,
57
                     std::integral_constant<TJoinOp::type, TJoinOp::NULL_AWARE_LEFT_ANTI_JOIN>,
58
                     std::integral_constant<TJoinOp::type, TJoinOp::NULL_AWARE_LEFT_SEMI_JOIN>,
59
                     std::integral_constant<TJoinOp::type, TJoinOp::ASOF_LEFT_INNER_JOIN>,
60
                     std::integral_constant<TJoinOp::type, TJoinOp::ASOF_LEFT_OUTER_JOIN>>;
61
62
575k
inline bool is_asof_join(TJoinOp::type join_op) {
63
575k
    return join_op == TJoinOp::ASOF_LEFT_INNER_JOIN || join_op == TJoinOp::ASOF_LEFT_OUTER_JOIN;
64
575k
}
65
66
template <int JoinOpType>
67
inline constexpr bool is_asof_join_op_v =
68
        JoinOpType == TJoinOp::ASOF_LEFT_INNER_JOIN || JoinOpType == TJoinOp::ASOF_LEFT_OUTER_JOIN;
69
70
template <int JoinOpType>
71
inline constexpr bool is_asof_outer_join_op_v = JoinOpType == TJoinOp::ASOF_LEFT_OUTER_JOIN;
72
73
template <class T>
74
using PrimaryTypeHashTableContext = MethodOneNumber<T, JoinHashMap<T, HashCRC32<T>, false>>;
75
76
template <class T>
77
using DirectPrimaryTypeHashTableContext =
78
        MethodOneNumberDirect<T, JoinHashMap<T, HashCRC32<T>, true>>;
79
80
template <class Key>
81
using FixedKeyHashTableContext = MethodKeysFixed<JoinHashMap<Key, HashCRC32<Key>, false>>;
82
83
using SerializedHashTableContext =
84
        MethodSerialized<JoinHashMap<StringRef, DefaultHash<StringRef>, false>>;
85
using MethodOneString = MethodStringNoCache<JoinHashMap<StringRef, DefaultHash<StringRef>, false>>;
86
87
using HashTableVariants = std::variant<
88
        std::monostate, SerializedHashTableContext, PrimaryTypeHashTableContext<UInt8>,
89
        PrimaryTypeHashTableContext<UInt16>, PrimaryTypeHashTableContext<UInt32>,
90
        PrimaryTypeHashTableContext<UInt64>, PrimaryTypeHashTableContext<UInt128>,
91
        PrimaryTypeHashTableContext<UInt256>, DirectPrimaryTypeHashTableContext<UInt8>,
92
        DirectPrimaryTypeHashTableContext<UInt16>, DirectPrimaryTypeHashTableContext<UInt32>,
93
        DirectPrimaryTypeHashTableContext<UInt64>, DirectPrimaryTypeHashTableContext<UInt128>,
94
        FixedKeyHashTableContext<UInt64>, FixedKeyHashTableContext<UInt72>,
95
        FixedKeyHashTableContext<UInt96>, FixedKeyHashTableContext<UInt104>,
96
        FixedKeyHashTableContext<UInt128>, FixedKeyHashTableContext<UInt136>,
97
        FixedKeyHashTableContext<UInt256>, MethodOneString>;
98
99
struct JoinDataVariants {
100
    HashTableVariants method_variant;
101
102
124k
    void init(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
103
124k
        switch (type) {
104
14.8k
        case HashKeyType::serialized:
105
14.8k
            method_variant.emplace<SerializedHashTableContext>();
106
14.8k
            break;
107
2.41k
        case HashKeyType::int8_key:
108
2.41k
            method_variant.emplace<PrimaryTypeHashTableContext<UInt8>>();
109
2.41k
            break;
110
1.45k
        case HashKeyType::int16_key:
111
1.45k
            method_variant.emplace<PrimaryTypeHashTableContext<UInt16>>();
112
1.45k
            break;
113
23.4k
        case HashKeyType::int32_key:
114
23.4k
            method_variant.emplace<PrimaryTypeHashTableContext<UInt32>>();
115
23.4k
            break;
116
41.7k
        case HashKeyType::int64_key:
117
41.7k
            method_variant.emplace<PrimaryTypeHashTableContext<UInt64>>();
118
41.7k
            break;
119
779
        case HashKeyType::int128_key:
120
779
            method_variant.emplace<PrimaryTypeHashTableContext<UInt128>>();
121
779
            break;
122
40
        case HashKeyType::int256_key:
123
40
            method_variant.emplace<PrimaryTypeHashTableContext<UInt256>>();
124
40
            break;
125
2.57k
        case HashKeyType::string_key:
126
2.57k
            method_variant.emplace<MethodOneString>();
127
2.57k
            break;
128
8.04k
        case HashKeyType::fixed64:
129
8.04k
            method_variant.emplace<FixedKeyHashTableContext<UInt64>>(get_key_sizes(data_types));
130
8.04k
            break;
131
3.31k
        case HashKeyType::fixed72:
132
3.31k
            method_variant.emplace<FixedKeyHashTableContext<UInt72>>(get_key_sizes(data_types));
133
3.31k
            break;
134
8.11k
        case HashKeyType::fixed96:
135
8.11k
            method_variant.emplace<FixedKeyHashTableContext<UInt96>>(get_key_sizes(data_types));
136
8.11k
            break;
137
87
        case HashKeyType::fixed104:
138
87
            method_variant.emplace<FixedKeyHashTableContext<UInt104>>(get_key_sizes(data_types));
139
87
            break;
140
8.30k
        case HashKeyType::fixed128:
141
8.30k
            method_variant.emplace<FixedKeyHashTableContext<UInt128>>(get_key_sizes(data_types));
142
8.30k
            break;
143
960
        case HashKeyType::fixed136:
144
960
            method_variant.emplace<FixedKeyHashTableContext<UInt136>>(get_key_sizes(data_types));
145
960
            break;
146
8.00k
        case HashKeyType::fixed256:
147
8.00k
            method_variant.emplace<FixedKeyHashTableContext<UInt256>>(get_key_sizes(data_types));
148
8.00k
            break;
149
0
        default:
150
0
            throw Exception(ErrorCode::INTERNAL_ERROR,
151
0
                            "JoinDataVariants meet invalid key type, type={}", type);
152
124k
        }
153
124k
    }
154
};
155
156
template <typename Method>
157
void primary_to_direct_mapping(Method* context, const ColumnRawPtrs& key_columns,
158
55.2k
                               const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
159
55.2k
    using FieldType = typename Method::Base::Key;
160
55.2k
    FieldType max_key = std::numeric_limits<FieldType>::min();
161
55.2k
    FieldType min_key = std::numeric_limits<FieldType>::max();
162
163
55.2k
    size_t num_rows = key_columns[0]->size();
164
55.2k
    if (key_columns[0]->is_nullable()) {
165
0
        const FieldType* input_keys = (FieldType*)assert_cast<const ColumnNullable*>(key_columns[0])
166
0
                                              ->get_nested_column_ptr()
167
0
                                              ->get_raw_data()
168
0
                                              .data;
169
0
        const NullMap& null_map =
170
0
                assert_cast<const ColumnNullable*>(key_columns[0])->get_null_map_data();
171
        // skip first mocked row
172
0
        for (size_t i = 1; i < num_rows; i++) {
173
0
            if (null_map[i]) {
174
0
                continue;
175
0
            }
176
0
            max_key = std::max(max_key, input_keys[i]);
177
0
            min_key = std::min(min_key, input_keys[i]);
178
0
        }
179
55.2k
    } else {
180
55.2k
        const FieldType* input_keys = (FieldType*)key_columns[0]->get_raw_data().data;
181
        // skip first mocked row
182
25.1M
        for (size_t i = 1; i < num_rows; i++) {
183
25.0M
            max_key = std::max(max_key, input_keys[i]);
184
25.0M
            min_key = std::min(min_key, input_keys[i]);
185
25.0M
        }
186
55.2k
    }
187
188
55.2k
    constexpr auto MAX_MAPPING_RANGE = 1 << 23;
189
55.2k
    bool allow_direct_mapping = (max_key >= min_key && max_key - min_key < MAX_MAPPING_RANGE - 1);
190
55.2k
    if (allow_direct_mapping) {
191
32.5k
        for (const auto& variant_ptr : variant_ptrs) {
192
32.5k
            variant_ptr->method_variant.emplace<DirectPrimaryTypeHashTableContext<FieldType>>(
193
32.5k
                    max_key, min_key);
194
32.5k
        }
195
18.9k
    }
196
55.2k
}
_ZN5doris25primary_to_direct_mappingINS_15MethodOneNumberIhNS_13JoinHashTableIh9HashCRC32IhELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISC_EERKS9_ISt10shared_ptrINS_16JoinDataVariantsEESaISJ_EE
Line
Count
Source
158
2.15k
                               const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
159
2.15k
    using FieldType = typename Method::Base::Key;
160
2.15k
    FieldType max_key = std::numeric_limits<FieldType>::min();
161
2.15k
    FieldType min_key = std::numeric_limits<FieldType>::max();
162
163
2.15k
    size_t num_rows = key_columns[0]->size();
164
2.15k
    if (key_columns[0]->is_nullable()) {
165
0
        const FieldType* input_keys = (FieldType*)assert_cast<const ColumnNullable*>(key_columns[0])
166
0
                                              ->get_nested_column_ptr()
167
0
                                              ->get_raw_data()
168
0
                                              .data;
169
0
        const NullMap& null_map =
170
0
                assert_cast<const ColumnNullable*>(key_columns[0])->get_null_map_data();
171
        // skip first mocked row
172
0
        for (size_t i = 1; i < num_rows; i++) {
173
0
            if (null_map[i]) {
174
0
                continue;
175
0
            }
176
0
            max_key = std::max(max_key, input_keys[i]);
177
0
            min_key = std::min(min_key, input_keys[i]);
178
0
        }
179
2.15k
    } else {
180
2.15k
        const FieldType* input_keys = (FieldType*)key_columns[0]->get_raw_data().data;
181
        // skip first mocked row
182
6.00k
        for (size_t i = 1; i < num_rows; i++) {
183
3.84k
            max_key = std::max(max_key, input_keys[i]);
184
3.84k
            min_key = std::min(min_key, input_keys[i]);
185
3.84k
        }
186
2.15k
    }
187
188
2.15k
    constexpr auto MAX_MAPPING_RANGE = 1 << 23;
189
2.15k
    bool allow_direct_mapping = (max_key >= min_key && max_key - min_key < MAX_MAPPING_RANGE - 1);
190
2.15k
    if (allow_direct_mapping) {
191
1.62k
        for (const auto& variant_ptr : variant_ptrs) {
192
1.62k
            variant_ptr->method_variant.emplace<DirectPrimaryTypeHashTableContext<FieldType>>(
193
1.62k
                    max_key, min_key);
194
1.62k
        }
195
1.38k
    }
196
2.15k
}
_ZN5doris25primary_to_direct_mappingINS_15MethodOneNumberItNS_13JoinHashTableIt9HashCRC32ItELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISC_EERKS9_ISt10shared_ptrINS_16JoinDataVariantsEESaISJ_EE
Line
Count
Source
158
1.36k
                               const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
159
1.36k
    using FieldType = typename Method::Base::Key;
160
1.36k
    FieldType max_key = std::numeric_limits<FieldType>::min();
161
1.36k
    FieldType min_key = std::numeric_limits<FieldType>::max();
162
163
1.36k
    size_t num_rows = key_columns[0]->size();
164
1.36k
    if (key_columns[0]->is_nullable()) {
165
0
        const FieldType* input_keys = (FieldType*)assert_cast<const ColumnNullable*>(key_columns[0])
166
0
                                              ->get_nested_column_ptr()
167
0
                                              ->get_raw_data()
168
0
                                              .data;
169
0
        const NullMap& null_map =
170
0
                assert_cast<const ColumnNullable*>(key_columns[0])->get_null_map_data();
171
        // skip first mocked row
172
0
        for (size_t i = 1; i < num_rows; i++) {
173
0
            if (null_map[i]) {
174
0
                continue;
175
0
            }
176
0
            max_key = std::max(max_key, input_keys[i]);
177
0
            min_key = std::min(min_key, input_keys[i]);
178
0
        }
179
1.36k
    } else {
180
1.36k
        const FieldType* input_keys = (FieldType*)key_columns[0]->get_raw_data().data;
181
        // skip first mocked row
182
9.57k
        for (size_t i = 1; i < num_rows; i++) {
183
8.20k
            max_key = std::max(max_key, input_keys[i]);
184
8.20k
            min_key = std::min(min_key, input_keys[i]);
185
8.20k
        }
186
1.36k
    }
187
188
1.36k
    constexpr auto MAX_MAPPING_RANGE = 1 << 23;
189
1.36k
    bool allow_direct_mapping = (max_key >= min_key && max_key - min_key < MAX_MAPPING_RANGE - 1);
190
1.36k
    if (allow_direct_mapping) {
191
1.21k
        for (const auto& variant_ptr : variant_ptrs) {
192
1.21k
            variant_ptr->method_variant.emplace<DirectPrimaryTypeHashTableContext<FieldType>>(
193
1.21k
                    max_key, min_key);
194
1.21k
        }
195
1.12k
    }
196
1.36k
}
_ZN5doris25primary_to_direct_mappingINS_15MethodOneNumberIjNS_13JoinHashTableIj9HashCRC32IjELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISC_EERKS9_ISt10shared_ptrINS_16JoinDataVariantsEESaISJ_EE
Line
Count
Source
158
13.3k
                               const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
159
13.3k
    using FieldType = typename Method::Base::Key;
160
13.3k
    FieldType max_key = std::numeric_limits<FieldType>::min();
161
13.3k
    FieldType min_key = std::numeric_limits<FieldType>::max();
162
163
13.3k
    size_t num_rows = key_columns[0]->size();
164
13.3k
    if (key_columns[0]->is_nullable()) {
165
0
        const FieldType* input_keys = (FieldType*)assert_cast<const ColumnNullable*>(key_columns[0])
166
0
                                              ->get_nested_column_ptr()
167
0
                                              ->get_raw_data()
168
0
                                              .data;
169
0
        const NullMap& null_map =
170
0
                assert_cast<const ColumnNullable*>(key_columns[0])->get_null_map_data();
171
        // skip first mocked row
172
0
        for (size_t i = 1; i < num_rows; i++) {
173
0
            if (null_map[i]) {
174
0
                continue;
175
0
            }
176
0
            max_key = std::max(max_key, input_keys[i]);
177
0
            min_key = std::min(min_key, input_keys[i]);
178
0
        }
179
13.3k
    } else {
180
13.3k
        const FieldType* input_keys = (FieldType*)key_columns[0]->get_raw_data().data;
181
        // skip first mocked row
182
24.8M
        for (size_t i = 1; i < num_rows; i++) {
183
24.8M
            max_key = std::max(max_key, input_keys[i]);
184
24.8M
            min_key = std::min(min_key, input_keys[i]);
185
24.8M
        }
186
13.3k
    }
187
188
13.3k
    constexpr auto MAX_MAPPING_RANGE = 1 << 23;
189
13.3k
    bool allow_direct_mapping = (max_key >= min_key && max_key - min_key < MAX_MAPPING_RANGE - 1);
190
13.3k
    if (allow_direct_mapping) {
191
20.1k
        for (const auto& variant_ptr : variant_ptrs) {
192
20.1k
            variant_ptr->method_variant.emplace<DirectPrimaryTypeHashTableContext<FieldType>>(
193
20.1k
                    max_key, min_key);
194
20.1k
        }
195
10.4k
    }
196
13.3k
}
_ZN5doris25primary_to_direct_mappingINS_15MethodOneNumberImNS_13JoinHashTableIm9HashCRC32ImELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISC_EERKS9_ISt10shared_ptrINS_16JoinDataVariantsEESaISJ_EE
Line
Count
Source
158
37.9k
                               const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
159
37.9k
    using FieldType = typename Method::Base::Key;
160
37.9k
    FieldType max_key = std::numeric_limits<FieldType>::min();
161
37.9k
    FieldType min_key = std::numeric_limits<FieldType>::max();
162
163
37.9k
    size_t num_rows = key_columns[0]->size();
164
37.9k
    if (key_columns[0]->is_nullable()) {
165
0
        const FieldType* input_keys = (FieldType*)assert_cast<const ColumnNullable*>(key_columns[0])
166
0
                                              ->get_nested_column_ptr()
167
0
                                              ->get_raw_data()
168
0
                                              .data;
169
0
        const NullMap& null_map =
170
0
                assert_cast<const ColumnNullable*>(key_columns[0])->get_null_map_data();
171
        // skip first mocked row
172
0
        for (size_t i = 1; i < num_rows; i++) {
173
0
            if (null_map[i]) {
174
0
                continue;
175
0
            }
176
0
            max_key = std::max(max_key, input_keys[i]);
177
0
            min_key = std::min(min_key, input_keys[i]);
178
0
        }
179
37.9k
    } else {
180
37.9k
        const FieldType* input_keys = (FieldType*)key_columns[0]->get_raw_data().data;
181
        // skip first mocked row
182
284k
        for (size_t i = 1; i < num_rows; i++) {
183
246k
            max_key = std::max(max_key, input_keys[i]);
184
246k
            min_key = std::min(min_key, input_keys[i]);
185
246k
        }
186
37.9k
    }
187
188
37.9k
    constexpr auto MAX_MAPPING_RANGE = 1 << 23;
189
37.9k
    bool allow_direct_mapping = (max_key >= min_key && max_key - min_key < MAX_MAPPING_RANGE - 1);
190
37.9k
    if (allow_direct_mapping) {
191
8.78k
        for (const auto& variant_ptr : variant_ptrs) {
192
8.78k
            variant_ptr->method_variant.emplace<DirectPrimaryTypeHashTableContext<FieldType>>(
193
8.78k
                    max_key, min_key);
194
8.78k
        }
195
5.55k
    }
196
37.9k
}
_ZN5doris25primary_to_direct_mappingINS_15MethodOneNumberIN4wide7integerILm128EjEENS_13JoinHashTableIS4_9HashCRC32IS4_ELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISF_EERKSC_ISt10shared_ptrINS_16JoinDataVariantsEESaISM_EE
Line
Count
Source
158
498
                               const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
159
498
    using FieldType = typename Method::Base::Key;
160
498
    FieldType max_key = std::numeric_limits<FieldType>::min();
161
498
    FieldType min_key = std::numeric_limits<FieldType>::max();
162
163
498
    size_t num_rows = key_columns[0]->size();
164
498
    if (key_columns[0]->is_nullable()) {
165
0
        const FieldType* input_keys = (FieldType*)assert_cast<const ColumnNullable*>(key_columns[0])
166
0
                                              ->get_nested_column_ptr()
167
0
                                              ->get_raw_data()
168
0
                                              .data;
169
0
        const NullMap& null_map =
170
0
                assert_cast<const ColumnNullable*>(key_columns[0])->get_null_map_data();
171
        // skip first mocked row
172
0
        for (size_t i = 1; i < num_rows; i++) {
173
0
            if (null_map[i]) {
174
0
                continue;
175
0
            }
176
0
            max_key = std::max(max_key, input_keys[i]);
177
0
            min_key = std::min(min_key, input_keys[i]);
178
0
        }
179
498
    } else {
180
498
        const FieldType* input_keys = (FieldType*)key_columns[0]->get_raw_data().data;
181
        // skip first mocked row
182
1.64k
        for (size_t i = 1; i < num_rows; i++) {
183
1.14k
            max_key = std::max(max_key, input_keys[i]);
184
1.14k
            min_key = std::min(min_key, input_keys[i]);
185
1.14k
        }
186
498
    }
187
188
498
    constexpr auto MAX_MAPPING_RANGE = 1 << 23;
189
498
    bool allow_direct_mapping = (max_key >= min_key && max_key - min_key < MAX_MAPPING_RANGE - 1);
190
498
    if (allow_direct_mapping) {
191
686
        for (const auto& variant_ptr : variant_ptrs) {
192
686
            variant_ptr->method_variant.emplace<DirectPrimaryTypeHashTableContext<FieldType>>(
193
686
                    max_key, min_key);
194
686
        }
195
439
    }
196
498
}
197
198
template <typename Method>
199
void try_convert_to_direct_mapping(
200
        Method* method, const ColumnRawPtrs& key_columns,
201
52.0k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {}
Unexecuted instantiation: _ZN5doris29try_convert_to_direct_mappingISt9monostateEEvPT_RKSt6vectorIPKNS_7IColumnESaIS7_EERKS4_ISt10shared_ptrINS_16JoinDataVariantsEESaISE_EE
_ZN5doris29try_convert_to_direct_mappingINS_16MethodSerializedINS_13JoinHashTableINS_9StringRefE11DefaultHashIS3_vELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISD_EERKSA_ISt10shared_ptrINS_16JoinDataVariantsEESaISK_EE
Line
Count
Source
201
14.6k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {}
_ZN5doris29try_convert_to_direct_mappingINS_15MethodOneNumberIN4wide7integerILm256EjEENS_13JoinHashTableIS4_9HashCRC32IS4_ELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISF_EERKSC_ISt10shared_ptrINS_16JoinDataVariantsEESaISM_EE
Line
Count
Source
201
25
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {}
Unexecuted instantiation: _ZN5doris29try_convert_to_direct_mappingINS_21MethodOneNumberDirectIhNS_13JoinHashTableIh9HashCRC32IhELb1EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISC_EERKS9_ISt10shared_ptrINS_16JoinDataVariantsEESaISJ_EE
Unexecuted instantiation: _ZN5doris29try_convert_to_direct_mappingINS_21MethodOneNumberDirectItNS_13JoinHashTableIt9HashCRC32ItELb1EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISC_EERKS9_ISt10shared_ptrINS_16JoinDataVariantsEESaISJ_EE
Unexecuted instantiation: _ZN5doris29try_convert_to_direct_mappingINS_21MethodOneNumberDirectIjNS_13JoinHashTableIj9HashCRC32IjELb1EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISC_EERKS9_ISt10shared_ptrINS_16JoinDataVariantsEESaISJ_EE
Unexecuted instantiation: _ZN5doris29try_convert_to_direct_mappingINS_21MethodOneNumberDirectImNS_13JoinHashTableIm9HashCRC32ImELb1EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISC_EERKS9_ISt10shared_ptrINS_16JoinDataVariantsEESaISJ_EE
Unexecuted instantiation: _ZN5doris29try_convert_to_direct_mappingINS_21MethodOneNumberDirectIN4wide7integerILm128EjEENS_13JoinHashTableIS4_9HashCRC32IS4_ELb1EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISF_EERKSC_ISt10shared_ptrINS_16JoinDataVariantsEESaISM_EE
_ZN5doris29try_convert_to_direct_mappingINS_15MethodKeysFixedINS_13JoinHashTableIm9HashCRC32ImELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISC_EERKS9_ISt10shared_ptrINS_16JoinDataVariantsEESaISJ_EE
Line
Count
Source
201
7.11k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {}
_ZN5doris29try_convert_to_direct_mappingINS_15MethodKeysFixedINS_13JoinHashTableINS_6UInt72E9HashCRC32IS3_ELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISD_EERKSA_ISt10shared_ptrINS_16JoinDataVariantsEESaISK_EE
Line
Count
Source
201
3.31k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {}
_ZN5doris29try_convert_to_direct_mappingINS_15MethodKeysFixedINS_13JoinHashTableINS_6UInt96E9HashCRC32IS3_ELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISD_EERKSA_ISt10shared_ptrINS_16JoinDataVariantsEESaISK_EE
Line
Count
Source
201
8.10k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {}
_ZN5doris29try_convert_to_direct_mappingINS_15MethodKeysFixedINS_13JoinHashTableINS_7UInt104E9HashCRC32IS3_ELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISD_EERKSA_ISt10shared_ptrINS_16JoinDataVariantsEESaISK_EE
Line
Count
Source
201
72
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {}
_ZN5doris29try_convert_to_direct_mappingINS_15MethodKeysFixedINS_13JoinHashTableIN4wide7integerILm128EjEE9HashCRC32IS5_ELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISF_EERKSC_ISt10shared_ptrINS_16JoinDataVariantsEESaISM_EE
Line
Count
Source
201
8.25k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {}
_ZN5doris29try_convert_to_direct_mappingINS_15MethodKeysFixedINS_13JoinHashTableINS_7UInt136E9HashCRC32IS3_ELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISD_EERKSA_ISt10shared_ptrINS_16JoinDataVariantsEESaISK_EE
Line
Count
Source
201
960
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {}
_ZN5doris29try_convert_to_direct_mappingINS_15MethodKeysFixedINS_13JoinHashTableIN4wide7integerILm256EjEE9HashCRC32IS5_ELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISF_EERKSC_ISt10shared_ptrINS_16JoinDataVariantsEESaISM_EE
Line
Count
Source
201
7.97k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {}
_ZN5doris29try_convert_to_direct_mappingINS_19MethodStringNoCacheINS_13JoinHashTableINS_9StringRefE11DefaultHashIS3_vELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISD_EERKSA_ISt10shared_ptrINS_16JoinDataVariantsEESaISK_EE
Line
Count
Source
201
1.58k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {}
202
203
inline void try_convert_to_direct_mapping(
204
        PrimaryTypeHashTableContext<UInt8>* context, const ColumnRawPtrs& key_columns,
205
2.15k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
206
2.15k
    primary_to_direct_mapping(context, key_columns, variant_ptrs);
207
2.15k
}
208
209
inline void try_convert_to_direct_mapping(
210
        PrimaryTypeHashTableContext<UInt16>* context, const ColumnRawPtrs& key_columns,
211
1.36k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
212
1.36k
    primary_to_direct_mapping(context, key_columns, variant_ptrs);
213
1.36k
}
214
215
inline void try_convert_to_direct_mapping(
216
        PrimaryTypeHashTableContext<UInt32>* context, const ColumnRawPtrs& key_columns,
217
13.3k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
218
13.3k
    primary_to_direct_mapping(context, key_columns, variant_ptrs);
219
13.3k
}
220
221
inline void try_convert_to_direct_mapping(
222
        PrimaryTypeHashTableContext<UInt64>* context, const ColumnRawPtrs& key_columns,
223
37.9k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
224
37.9k
    primary_to_direct_mapping(context, key_columns, variant_ptrs);
225
37.9k
}
226
227
inline void try_convert_to_direct_mapping(
228
        PrimaryTypeHashTableContext<UInt128>* context, const ColumnRawPtrs& key_columns,
229
498
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
230
498
    primary_to_direct_mapping(context, key_columns, variant_ptrs);
231
498
}
232
233
// ASOF JOIN index with inline values for cache-friendly branchless binary search.
234
// IntType is the integer representation of the ASOF column value:
235
//   uint32_t for DateV2, uint64_t for DateTimeV2 and TimestampTZ.
236
// Rows are sorted by asof_value during build, then materialized into SoA arrays
237
// so probe-side binary search only touches the ASOF values hot path.
238
template <typename IntType>
239
struct AsofIndexGroup {
240
    using int_type = IntType;
241
242
    struct Entry {
243
        IntType asof_value;
244
        uint32_t row_index; // 1-based, 0 = invalid/padding
245
    };
246
247
    std::vector<Entry> entries;
248
    std::vector<IntType> asof_values;
249
    std::vector<uint32_t> row_indexes;
250
251
3.89k
    void add_row(IntType value, uint32_t row_idx) { entries.push_back({value, row_idx}); }
_ZN5doris14AsofIndexGroupIjE7add_rowEjj
Line
Count
Source
251
1.06k
    void add_row(IntType value, uint32_t row_idx) { entries.push_back({value, row_idx}); }
_ZN5doris14AsofIndexGroupImE7add_rowEmj
Line
Count
Source
251
2.83k
    void add_row(IntType value, uint32_t row_idx) { entries.push_back({value, row_idx}); }
252
253
920
    void sort_and_finalize() {
254
920
        if (entries.empty()) {
255
4
            return;
256
4
        }
257
916
        if (entries.size() > 1) {
258
791
            pdqsort(entries.begin(), entries.end(),
259
8.42k
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
_ZZN5doris14AsofIndexGroupIjE17sort_and_finalizeEvENKUlRKNS1_5EntryES4_E_clES4_S4_
Line
Count
Source
259
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
259
6.36k
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
260
791
        }
261
262
916
        asof_values.resize(entries.size());
263
916
        row_indexes.resize(entries.size());
264
4.81k
        for (size_t i = 0; i < entries.size(); ++i) {
265
3.90k
            asof_values[i] = entries[i].asof_value;
266
3.90k
            row_indexes[i] = entries[i].row_index;
267
3.90k
        }
268
269
916
        std::vector<Entry>().swap(entries);
270
916
    }
_ZN5doris14AsofIndexGroupIjE17sort_and_finalizeEv
Line
Count
Source
253
25
    void sort_and_finalize() {
254
25
        if (entries.empty()) {
255
4
            return;
256
4
        }
257
21
        if (entries.size() > 1) {
258
15
            pdqsort(entries.begin(), entries.end(),
259
15
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
260
15
        }
261
262
21
        asof_values.resize(entries.size());
263
21
        row_indexes.resize(entries.size());
264
1.08k
        for (size_t i = 0; i < entries.size(); ++i) {
265
1.06k
            asof_values[i] = entries[i].asof_value;
266
1.06k
            row_indexes[i] = entries[i].row_index;
267
1.06k
        }
268
269
21
        std::vector<Entry>().swap(entries);
270
21
    }
_ZN5doris14AsofIndexGroupImE17sort_and_finalizeEv
Line
Count
Source
253
895
    void sort_and_finalize() {
254
895
        if (entries.empty()) {
255
0
            return;
256
0
        }
257
895
        if (entries.size() > 1) {
258
776
            pdqsort(entries.begin(), entries.end(),
259
776
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
260
776
        }
261
262
895
        asof_values.resize(entries.size());
263
895
        row_indexes.resize(entries.size());
264
3.73k
        for (size_t i = 0; i < entries.size(); ++i) {
265
2.83k
            asof_values[i] = entries[i].asof_value;
266
2.83k
            row_indexes[i] = entries[i].row_index;
267
2.83k
        }
268
269
895
        std::vector<Entry>().swap(entries);
270
895
    }
271
272
817
    const IntType* values_data() const { return asof_values.data(); }
_ZNK5doris14AsofIndexGroupIjE11values_dataEv
Line
Count
Source
272
1
    const IntType* values_data() const { return asof_values.data(); }
_ZNK5doris14AsofIndexGroupImE11values_dataEv
Line
Count
Source
272
816
    const IntType* values_data() const { return asof_values.data(); }
273
274
    // Branchless lower_bound: first i where asof_values[i] >= target
275
454
    ALWAYS_INLINE size_t lower_bound(IntType target) const {
276
454
        size_t lo = 0, n = asof_values.size();
277
2.30k
        while (n > 1) {
278
1.85k
            size_t half = n / 2;
279
1.85k
            lo += half * (asof_values[lo + half] < target);
280
1.85k
            n -= half;
281
1.85k
        }
282
454
        if (lo < asof_values.size()) {
283
453
            lo += (asof_values[lo] < target);
284
453
        }
285
454
        return lo;
286
454
    }
_ZNK5doris14AsofIndexGroupIjE11lower_boundEj
Line
Count
Source
275
33
    ALWAYS_INLINE size_t lower_bound(IntType target) const {
276
33
        size_t lo = 0, n = asof_values.size();
277
163
        while (n > 1) {
278
130
            size_t half = n / 2;
279
130
            lo += half * (asof_values[lo + half] < target);
280
130
            n -= half;
281
130
        }
282
33
        if (lo < asof_values.size()) {
283
32
            lo += (asof_values[lo] < target);
284
32
        }
285
33
        return lo;
286
33
    }
_ZNK5doris14AsofIndexGroupImE11lower_boundEm
Line
Count
Source
275
421
    ALWAYS_INLINE size_t lower_bound(IntType target) const {
276
421
        size_t lo = 0, n = asof_values.size();
277
2.14k
        while (n > 1) {
278
1.72k
            size_t half = n / 2;
279
1.72k
            lo += half * (asof_values[lo + half] < target);
280
1.72k
            n -= half;
281
1.72k
        }
282
421
        if (lo < asof_values.size()) {
283
421
            lo += (asof_values[lo] < target);
284
421
        }
285
421
        return lo;
286
421
    }
287
288
    // Branchless upper_bound: first i where asof_values[i] > target
289
946
    ALWAYS_INLINE size_t upper_bound(IntType target) const {
290
946
        size_t lo = 0, n = asof_values.size();
291
4.06k
        while (n > 1) {
292
3.12k
            size_t half = n / 2;
293
3.12k
            lo += half * (asof_values[lo + half] <= target);
294
3.12k
            n -= half;
295
3.12k
        }
296
946
        if (lo < asof_values.size()) {
297
945
            lo += (asof_values[lo] <= target);
298
945
        }
299
946
        return lo;
300
946
    }
_ZNK5doris14AsofIndexGroupIjE11upper_boundEj
Line
Count
Source
289
41
    ALWAYS_INLINE size_t upper_bound(IntType target) const {
290
41
        size_t lo = 0, n = asof_values.size();
291
215
        while (n > 1) {
292
174
            size_t half = n / 2;
293
174
            lo += half * (asof_values[lo + half] <= target);
294
174
            n -= half;
295
174
        }
296
41
        if (lo < asof_values.size()) {
297
40
            lo += (asof_values[lo] <= target);
298
40
        }
299
41
        return lo;
300
41
    }
_ZNK5doris14AsofIndexGroupImE11upper_boundEm
Line
Count
Source
289
905
    ALWAYS_INLINE size_t upper_bound(IntType target) const {
290
905
        size_t lo = 0, n = asof_values.size();
291
3.85k
        while (n > 1) {
292
2.94k
            size_t half = n / 2;
293
2.94k
            lo += half * (asof_values[lo + half] <= target);
294
2.94k
            n -= half;
295
2.94k
        }
296
905
        if (lo < asof_values.size()) {
297
905
            lo += (asof_values[lo] <= target);
298
905
        }
299
905
        return lo;
300
905
    }
301
302
    // Semantics by (is_greater, is_strict):
303
    //   (true,  false): probe >= build  ->  find largest  build value <= probe
304
    //   (true,  true):  probe >  build  ->  find largest  build value <  probe
305
    //   (false, false): probe <= build  ->  find smallest build value >= probe
306
    //   (false, true):  probe <  build  ->  find smallest build value >  probe
307
    // Returns the build row index of the best match, or 0 if no match.
308
    template <bool IsGreater, bool IsStrict>
309
1.38k
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
1.38k
        if (asof_values.empty()) {
311
4
            return 0;
312
4
        }
313
1.38k
        if constexpr (IsGreater) {
314
943
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
943
            return pos > 0 ? row_indexes[pos - 1] : 0;
316
943
        } else {
317
439
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
439
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
439
        }
320
1.38k
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb1ELb1EEEjj
Line
Count
Source
309
15
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
15
        if (asof_values.empty()) {
311
1
            return 0;
312
1
        }
313
14
        if constexpr (IsGreater) {
314
14
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
14
            return pos > 0 ? row_indexes[pos - 1] : 0;
316
        } else {
317
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
        }
320
14
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb1ELb0EEEjj
Line
Count
Source
309
22
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
22
        if (asof_values.empty()) {
311
1
            return 0;
312
1
        }
313
21
        if constexpr (IsGreater) {
314
21
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
21
            return pos > 0 ? row_indexes[pos - 1] : 0;
316
        } else {
317
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
        }
320
21
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb0ELb1EEEjj
Line
Count
Source
309
16
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
16
        if (asof_values.empty()) {
311
1
            return 0;
312
1
        }
313
        if constexpr (IsGreater) {
314
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
            return pos > 0 ? row_indexes[pos - 1] : 0;
316
15
        } else {
317
15
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
15
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
15
        }
320
15
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb0ELb0EEEjj
Line
Count
Source
309
15
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
15
        if (asof_values.empty()) {
311
1
            return 0;
312
1
        }
313
        if constexpr (IsGreater) {
314
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
            return pos > 0 ? row_indexes[pos - 1] : 0;
316
14
        } else {
317
14
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
14
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
14
        }
320
14
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb1ELb1EEEjm
Line
Count
Source
309
205
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
205
        if (asof_values.empty()) {
311
0
            return 0;
312
0
        }
313
205
        if constexpr (IsGreater) {
314
205
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
205
            return pos > 0 ? row_indexes[pos - 1] : 0;
316
        } else {
317
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
        }
320
205
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb1ELb0EEEjm
Line
Count
Source
309
703
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
703
        if (asof_values.empty()) {
311
0
            return 0;
312
0
        }
313
703
        if constexpr (IsGreater) {
314
703
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
703
            return pos > 0 ? row_indexes[pos - 1] : 0;
316
        } else {
317
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
        }
320
703
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb0ELb1EEEjm
Line
Count
Source
309
198
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
198
        if (asof_values.empty()) {
311
0
            return 0;
312
0
        }
313
        if constexpr (IsGreater) {
314
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
            return pos > 0 ? row_indexes[pos - 1] : 0;
316
198
        } else {
317
198
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
198
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
198
        }
320
198
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb0ELb0EEEjm
Line
Count
Source
309
212
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
212
        if (asof_values.empty()) {
311
0
            return 0;
312
0
        }
313
        if constexpr (IsGreater) {
314
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
            return pos > 0 ? row_indexes[pos - 1] : 0;
316
212
        } else {
317
212
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
212
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
212
        }
320
212
    }
321
};
322
323
// Type-erased container for all ASOF index groups.
324
// DateV2 -> uint32_t, DateTimeV2/TimestampTZ -> uint64_t.
325
using AsofIndexVariant = std::variant<std::monostate, std::vector<AsofIndexGroup<uint32_t>>,
326
                                      std::vector<AsofIndexGroup<uint64_t>>>;
327
328
} // namespace doris