Coverage Report

Created: 2026-04-15 12:22

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
309
decltype(auto) asof_column_dispatch(const IColumn* col, Func&& func) {
37
309
    if (const auto* c_dv2 = check_and_get_column<ColumnDateV2>(col)) {
38
3
        return std::forward<Func>(func)(c_dv2);
39
306
    } else if (const auto* c_dtv2 = check_and_get_column<ColumnDateTimeV2>(col)) {
40
302
        return std::forward<Func>(func)(c_dtv2);
41
302
    } 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
309
}
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
928k
inline bool is_asof_join(TJoinOp::type join_op) {
63
928k
    return join_op == TJoinOp::ASOF_LEFT_INNER_JOIN || join_op == TJoinOp::ASOF_LEFT_OUTER_JOIN;
64
928k
}
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
174k
    void init(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
103
174k
        switch (type) {
104
28.7k
        case HashKeyType::serialized:
105
28.7k
            method_variant.emplace<SerializedHashTableContext>();
106
28.7k
            break;
107
2.33k
        case HashKeyType::int8_key:
108
2.33k
            method_variant.emplace<PrimaryTypeHashTableContext<UInt8>>();
109
2.33k
            break;
110
1.29k
        case HashKeyType::int16_key:
111
1.29k
            method_variant.emplace<PrimaryTypeHashTableContext<UInt16>>();
112
1.29k
            break;
113
23.3k
        case HashKeyType::int32_key:
114
23.3k
            method_variant.emplace<PrimaryTypeHashTableContext<UInt32>>();
115
23.3k
            break;
116
46.1k
        case HashKeyType::int64_key:
117
46.1k
            method_variant.emplace<PrimaryTypeHashTableContext<UInt64>>();
118
46.1k
            break;
119
937
        case HashKeyType::int128_key:
120
937
            method_variant.emplace<PrimaryTypeHashTableContext<UInt128>>();
121
937
            break;
122
31
        case HashKeyType::int256_key:
123
31
            method_variant.emplace<PrimaryTypeHashTableContext<UInt256>>();
124
31
            break;
125
2.54k
        case HashKeyType::string_key:
126
2.54k
            method_variant.emplace<MethodOneString>();
127
2.54k
            break;
128
12.0k
        case HashKeyType::fixed64:
129
12.0k
            method_variant.emplace<FixedKeyHashTableContext<UInt64>>(get_key_sizes(data_types));
130
12.0k
            break;
131
6.50k
        case HashKeyType::fixed72:
132
6.50k
            method_variant.emplace<FixedKeyHashTableContext<UInt72>>(get_key_sizes(data_types));
133
6.50k
            break;
134
16.0k
        case HashKeyType::fixed96:
135
16.0k
            method_variant.emplace<FixedKeyHashTableContext<UInt96>>(get_key_sizes(data_types));
136
16.0k
            break;
137
69
        case HashKeyType::fixed104:
138
69
            method_variant.emplace<FixedKeyHashTableContext<UInt104>>(get_key_sizes(data_types));
139
69
            break;
140
16.2k
        case HashKeyType::fixed128:
141
16.2k
            method_variant.emplace<FixedKeyHashTableContext<UInt128>>(get_key_sizes(data_types));
142
16.2k
            break;
143
1.92k
        case HashKeyType::fixed136:
144
1.92k
            method_variant.emplace<FixedKeyHashTableContext<UInt136>>(get_key_sizes(data_types));
145
1.92k
            break;
146
15.9k
        case HashKeyType::fixed256:
147
15.9k
            method_variant.emplace<FixedKeyHashTableContext<UInt256>>(get_key_sizes(data_types));
148
15.9k
            break;
149
0
        default:
150
0
            throw Exception(ErrorCode::INTERNAL_ERROR,
151
0
                            "JoinDataVariants meet invalid key type, type={}", type);
152
174k
        }
153
174k
    }
154
};
155
156
template <typename Method>
157
void primary_to_direct_mapping(Method* context, const ColumnRawPtrs& key_columns,
158
61.1k
                               const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
159
61.1k
    using FieldType = typename Method::Base::Key;
160
61.1k
    FieldType max_key = std::numeric_limits<FieldType>::min();
161
61.1k
    FieldType min_key = std::numeric_limits<FieldType>::max();
162
163
61.1k
    size_t num_rows = key_columns[0]->size();
164
61.1k
    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
61.1k
    } else {
180
61.1k
        const FieldType* input_keys = (FieldType*)key_columns[0]->get_raw_data().data;
181
        // skip first mocked row
182
24.9M
        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
61.1k
    }
187
188
61.1k
    constexpr auto MAX_MAPPING_RANGE = 1 << 23;
189
61.1k
    bool allow_direct_mapping = (max_key >= min_key && max_key - min_key < MAX_MAPPING_RANGE - 1);
190
61.1k
    if (allow_direct_mapping) {
191
33.9k
        for (const auto& variant_ptr : variant_ptrs) {
192
33.9k
            variant_ptr->method_variant.emplace<DirectPrimaryTypeHashTableContext<FieldType>>(
193
33.9k
                    max_key, min_key);
194
33.9k
        }
195
22.3k
    }
196
61.1k
}
_ZN5doris25primary_to_direct_mappingINS_15MethodOneNumberIhNS_13JoinHashTableIh9HashCRC32IhELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISC_EERKS9_ISt10shared_ptrINS_16JoinDataVariantsEESaISJ_EE
Line
Count
Source
158
1.63k
                               const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
159
1.63k
    using FieldType = typename Method::Base::Key;
160
1.63k
    FieldType max_key = std::numeric_limits<FieldType>::min();
161
1.63k
    FieldType min_key = std::numeric_limits<FieldType>::max();
162
163
1.63k
    size_t num_rows = key_columns[0]->size();
164
1.63k
    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.63k
    } else {
180
1.63k
        const FieldType* input_keys = (FieldType*)key_columns[0]->get_raw_data().data;
181
        // skip first mocked row
182
4.05k
        for (size_t i = 1; i < num_rows; i++) {
183
2.41k
            max_key = std::max(max_key, input_keys[i]);
184
2.41k
            min_key = std::min(min_key, input_keys[i]);
185
2.41k
        }
186
1.63k
    }
187
188
1.63k
    constexpr auto MAX_MAPPING_RANGE = 1 << 23;
189
1.63k
    bool allow_direct_mapping = (max_key >= min_key && max_key - min_key < MAX_MAPPING_RANGE - 1);
190
1.63k
    if (allow_direct_mapping) {
191
1.64k
        for (const auto& variant_ptr : variant_ptrs) {
192
1.64k
            variant_ptr->method_variant.emplace<DirectPrimaryTypeHashTableContext<FieldType>>(
193
1.64k
                    max_key, min_key);
194
1.64k
        }
195
983
    }
196
1.63k
}
_ZN5doris25primary_to_direct_mappingINS_15MethodOneNumberItNS_13JoinHashTableIt9HashCRC32ItELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISC_EERKS9_ISt10shared_ptrINS_16JoinDataVariantsEESaISJ_EE
Line
Count
Source
158
770
                               const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
159
770
    using FieldType = typename Method::Base::Key;
160
770
    FieldType max_key = std::numeric_limits<FieldType>::min();
161
770
    FieldType min_key = std::numeric_limits<FieldType>::max();
162
163
770
    size_t num_rows = key_columns[0]->size();
164
770
    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
770
    } else {
180
770
        const FieldType* input_keys = (FieldType*)key_columns[0]->get_raw_data().data;
181
        // skip first mocked row
182
4.06k
        for (size_t i = 1; i < num_rows; i++) {
183
3.29k
            max_key = std::max(max_key, input_keys[i]);
184
3.29k
            min_key = std::min(min_key, input_keys[i]);
185
3.29k
        }
186
770
    }
187
188
770
    constexpr auto MAX_MAPPING_RANGE = 1 << 23;
189
770
    bool allow_direct_mapping = (max_key >= min_key && max_key - min_key < MAX_MAPPING_RANGE - 1);
190
770
    if (allow_direct_mapping) {
191
1.09k
        for (const auto& variant_ptr : variant_ptrs) {
192
1.09k
            variant_ptr->method_variant.emplace<DirectPrimaryTypeHashTableContext<FieldType>>(
193
1.09k
                    max_key, min_key);
194
1.09k
        }
195
574
    }
196
770
}
_ZN5doris25primary_to_direct_mappingINS_15MethodOneNumberIjNS_13JoinHashTableIj9HashCRC32IjELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISC_EERKS9_ISt10shared_ptrINS_16JoinDataVariantsEESaISJ_EE
Line
Count
Source
158
16.1k
                               const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
159
16.1k
    using FieldType = typename Method::Base::Key;
160
16.1k
    FieldType max_key = std::numeric_limits<FieldType>::min();
161
16.1k
    FieldType min_key = std::numeric_limits<FieldType>::max();
162
163
16.1k
    size_t num_rows = key_columns[0]->size();
164
16.1k
    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
16.1k
    } else {
180
16.1k
        const FieldType* input_keys = (FieldType*)key_columns[0]->get_raw_data().data;
181
        // skip first mocked row
182
24.6M
        for (size_t i = 1; i < num_rows; i++) {
183
24.6M
            max_key = std::max(max_key, input_keys[i]);
184
24.6M
            min_key = std::min(min_key, input_keys[i]);
185
24.6M
        }
186
16.1k
    }
187
188
16.1k
    constexpr auto MAX_MAPPING_RANGE = 1 << 23;
189
16.1k
    bool allow_direct_mapping = (max_key >= min_key && max_key - min_key < MAX_MAPPING_RANGE - 1);
190
16.1k
    if (allow_direct_mapping) {
191
20.4k
        for (const auto& variant_ptr : variant_ptrs) {
192
20.4k
            variant_ptr->method_variant.emplace<DirectPrimaryTypeHashTableContext<FieldType>>(
193
20.4k
                    max_key, min_key);
194
20.4k
        }
195
13.5k
    }
196
16.1k
}
_ZN5doris25primary_to_direct_mappingINS_15MethodOneNumberImNS_13JoinHashTableIm9HashCRC32ImELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISC_EERKS9_ISt10shared_ptrINS_16JoinDataVariantsEESaISJ_EE
Line
Count
Source
158
41.9k
                               const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
159
41.9k
    using FieldType = typename Method::Base::Key;
160
41.9k
    FieldType max_key = std::numeric_limits<FieldType>::min();
161
41.9k
    FieldType min_key = std::numeric_limits<FieldType>::max();
162
163
41.9k
    size_t num_rows = key_columns[0]->size();
164
41.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
41.9k
    } else {
180
41.9k
        const FieldType* input_keys = (FieldType*)key_columns[0]->get_raw_data().data;
181
        // skip first mocked row
182
294k
        for (size_t i = 1; i < num_rows; i++) {
183
252k
            max_key = std::max(max_key, input_keys[i]);
184
252k
            min_key = std::min(min_key, input_keys[i]);
185
252k
        }
186
41.9k
    }
187
188
41.9k
    constexpr auto MAX_MAPPING_RANGE = 1 << 23;
189
41.9k
    bool allow_direct_mapping = (max_key >= min_key && max_key - min_key < MAX_MAPPING_RANGE - 1);
190
41.9k
    if (allow_direct_mapping) {
191
10.0k
        for (const auto& variant_ptr : variant_ptrs) {
192
10.0k
            variant_ptr->method_variant.emplace<DirectPrimaryTypeHashTableContext<FieldType>>(
193
10.0k
                    max_key, min_key);
194
10.0k
        }
195
6.64k
    }
196
41.9k
}
_ZN5doris25primary_to_direct_mappingINS_15MethodOneNumberIN4wide7integerILm128EjEENS_13JoinHashTableIS4_9HashCRC32IS4_ELb0EEEEEEEvPT_RKSt6vectorIPKNS_7IColumnESaISF_EERKSC_ISt10shared_ptrINS_16JoinDataVariantsEESaISM_EE
Line
Count
Source
158
719
                               const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
159
719
    using FieldType = typename Method::Base::Key;
160
719
    FieldType max_key = std::numeric_limits<FieldType>::min();
161
719
    FieldType min_key = std::numeric_limits<FieldType>::max();
162
163
719
    size_t num_rows = key_columns[0]->size();
164
719
    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
719
    } else {
180
719
        const FieldType* input_keys = (FieldType*)key_columns[0]->get_raw_data().data;
181
        // skip first mocked row
182
2.18k
        for (size_t i = 1; i < num_rows; i++) {
183
1.46k
            max_key = std::max(max_key, input_keys[i]);
184
1.46k
            min_key = std::min(min_key, input_keys[i]);
185
1.46k
        }
186
719
    }
187
188
719
    constexpr auto MAX_MAPPING_RANGE = 1 << 23;
189
719
    bool allow_direct_mapping = (max_key >= min_key && max_key - min_key < MAX_MAPPING_RANGE - 1);
190
719
    if (allow_direct_mapping) {
191
801
        for (const auto& variant_ptr : variant_ptrs) {
192
801
            variant_ptr->method_variant.emplace<DirectPrimaryTypeHashTableContext<FieldType>>(
193
801
                    max_key, min_key);
194
801
        }
195
639
    }
196
719
}
197
198
template <typename Method>
199
void try_convert_to_direct_mapping(
200
        Method* method, const ColumnRawPtrs& key_columns,
201
97.9k
        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
28.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
16
        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
11.0k
        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
6.49k
        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
16.0k
        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
52
        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
16.2k
        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
1.92k
        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
15.9k
        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.61k
        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
1.63k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
206
1.63k
    primary_to_direct_mapping(context, key_columns, variant_ptrs);
207
1.63k
}
208
209
inline void try_convert_to_direct_mapping(
210
        PrimaryTypeHashTableContext<UInt16>* context, const ColumnRawPtrs& key_columns,
211
770
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
212
770
    primary_to_direct_mapping(context, key_columns, variant_ptrs);
213
770
}
214
215
inline void try_convert_to_direct_mapping(
216
        PrimaryTypeHashTableContext<UInt32>* context, const ColumnRawPtrs& key_columns,
217
16.1k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
218
16.1k
    primary_to_direct_mapping(context, key_columns, variant_ptrs);
219
16.1k
}
220
221
inline void try_convert_to_direct_mapping(
222
        PrimaryTypeHashTableContext<UInt64>* context, const ColumnRawPtrs& key_columns,
223
41.9k
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
224
41.9k
    primary_to_direct_mapping(context, key_columns, variant_ptrs);
225
41.9k
}
226
227
inline void try_convert_to_direct_mapping(
228
        PrimaryTypeHashTableContext<UInt128>* context, const ColumnRawPtrs& key_columns,
229
719
        const std::vector<std::shared_ptr<JoinDataVariants>>& variant_ptrs) {
230
719
    primary_to_direct_mapping(context, key_columns, variant_ptrs);
231
719
}
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
5.98k
    void add_row(IntType value, uint32_t row_idx) { entries.push_back({value, row_idx}); }
_ZN5doris14AsofIndexGroupIjE7add_rowEjj
Line
Count
Source
251
2.11k
    void add_row(IntType value, uint32_t row_idx) { entries.push_back({value, row_idx}); }
_ZN5doris14AsofIndexGroupImE7add_rowEmj
Line
Count
Source
251
3.86k
    void add_row(IntType value, uint32_t row_idx) { entries.push_back({value, row_idx}); }
252
253
691
    void sort_and_finalize() {
254
691
        if (entries.empty()) {
255
8
            return;
256
8
        }
257
683
        if (entries.size() > 1) {
258
565
            pdqsort(entries.begin(), entries.end(),
259
19.3k
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
_ZZN5doris14AsofIndexGroupIjE17sort_and_finalizeEvENKUlRKNS1_5EntryES4_E_clES4_S4_
Line
Count
Source
259
4.12k
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
_ZZN5doris14AsofIndexGroupImE17sort_and_finalizeEvENKUlRKNS1_5EntryES4_E_clES4_S4_
Line
Count
Source
259
15.2k
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
260
565
        }
261
262
683
        asof_values.resize(entries.size());
263
683
        row_indexes.resize(entries.size());
264
6.68k
        for (size_t i = 0; i < entries.size(); ++i) {
265
6.00k
            asof_values[i] = entries[i].asof_value;
266
6.00k
            row_indexes[i] = entries[i].row_index;
267
6.00k
        }
268
269
683
        std::vector<Entry>().swap(entries);
270
683
    }
_ZN5doris14AsofIndexGroupIjE17sort_and_finalizeEv
Line
Count
Source
253
44
    void sort_and_finalize() {
254
44
        if (entries.empty()) {
255
8
            return;
256
8
        }
257
36
        if (entries.size() > 1) {
258
27
            pdqsort(entries.begin(), entries.end(),
259
27
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
260
27
        }
261
262
36
        asof_values.resize(entries.size());
263
36
        row_indexes.resize(entries.size());
264
2.15k
        for (size_t i = 0; i < entries.size(); ++i) {
265
2.11k
            asof_values[i] = entries[i].asof_value;
266
2.11k
            row_indexes[i] = entries[i].row_index;
267
2.11k
        }
268
269
36
        std::vector<Entry>().swap(entries);
270
36
    }
_ZN5doris14AsofIndexGroupImE17sort_and_finalizeEv
Line
Count
Source
253
647
    void sort_and_finalize() {
254
647
        if (entries.empty()) {
255
0
            return;
256
0
        }
257
647
        if (entries.size() > 1) {
258
538
            pdqsort(entries.begin(), entries.end(),
259
538
                    [](const Entry& a, const Entry& b) { return a.asof_value < b.asof_value; });
260
538
        }
261
262
647
        asof_values.resize(entries.size());
263
647
        row_indexes.resize(entries.size());
264
4.53k
        for (size_t i = 0; i < entries.size(); ++i) {
265
3.88k
            asof_values[i] = entries[i].asof_value;
266
3.88k
            row_indexes[i] = entries[i].row_index;
267
3.88k
        }
268
269
647
        std::vector<Entry>().swap(entries);
270
647
    }
271
272
818
    const IntType* values_data() const { return asof_values.data(); }
_ZNK5doris14AsofIndexGroupIjE11values_dataEv
Line
Count
Source
272
2
    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
493
    ALWAYS_INLINE size_t lower_bound(IntType target) const {
276
493
        size_t lo = 0, n = asof_values.size();
277
2.48k
        while (n > 1) {
278
1.99k
            size_t half = n / 2;
279
1.99k
            lo += half * (asof_values[lo + half] < target);
280
1.99k
            n -= half;
281
1.99k
        }
282
493
        if (lo < asof_values.size()) {
283
491
            lo += (asof_values[lo] < target);
284
491
        }
285
493
        return lo;
286
493
    }
_ZNK5doris14AsofIndexGroupIjE11lower_boundEj
Line
Count
Source
275
66
    ALWAYS_INLINE size_t lower_bound(IntType target) const {
276
66
        size_t lo = 0, n = asof_values.size();
277
326
        while (n > 1) {
278
260
            size_t half = n / 2;
279
260
            lo += half * (asof_values[lo + half] < target);
280
260
            n -= half;
281
260
        }
282
66
        if (lo < asof_values.size()) {
283
64
            lo += (asof_values[lo] < target);
284
64
        }
285
66
        return lo;
286
66
    }
_ZNK5doris14AsofIndexGroupImE11lower_boundEm
Line
Count
Source
275
427
    ALWAYS_INLINE size_t lower_bound(IntType target) const {
276
427
        size_t lo = 0, n = asof_values.size();
277
2.15k
        while (n > 1) {
278
1.73k
            size_t half = n / 2;
279
1.73k
            lo += half * (asof_values[lo + half] < target);
280
1.73k
            n -= half;
281
1.73k
        }
282
427
        if (lo < asof_values.size()) {
283
427
            lo += (asof_values[lo] < target);
284
427
        }
285
427
        return lo;
286
427
    }
287
288
    // Branchless upper_bound: first i where asof_values[i] > target
289
990
    ALWAYS_INLINE size_t upper_bound(IntType target) const {
290
990
        size_t lo = 0, n = asof_values.size();
291
4.29k
        while (n > 1) {
292
3.30k
            size_t half = n / 2;
293
3.30k
            lo += half * (asof_values[lo + half] <= target);
294
3.30k
            n -= half;
295
3.30k
        }
296
990
        if (lo < asof_values.size()) {
297
988
            lo += (asof_values[lo] <= target);
298
988
        }
299
990
        return lo;
300
990
    }
_ZNK5doris14AsofIndexGroupIjE11upper_boundEj
Line
Count
Source
289
79
    ALWAYS_INLINE size_t upper_bound(IntType target) const {
290
79
        size_t lo = 0, n = asof_values.size();
291
423
        while (n > 1) {
292
344
            size_t half = n / 2;
293
344
            lo += half * (asof_values[lo + half] <= target);
294
344
            n -= half;
295
344
        }
296
79
        if (lo < asof_values.size()) {
297
77
            lo += (asof_values[lo] <= target);
298
77
        }
299
79
        return lo;
300
79
    }
_ZNK5doris14AsofIndexGroupImE11upper_boundEm
Line
Count
Source
289
911
    ALWAYS_INLINE size_t upper_bound(IntType target) const {
290
911
        size_t lo = 0, n = asof_values.size();
291
3.86k
        while (n > 1) {
292
2.95k
            size_t half = n / 2;
293
2.95k
            lo += half * (asof_values[lo + half] <= target);
294
2.95k
            n -= half;
295
2.95k
        }
296
911
        if (lo < asof_values.size()) {
297
911
            lo += (asof_values[lo] <= target);
298
911
        }
299
911
        return lo;
300
911
    }
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.45k
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
1.45k
        if (asof_values.empty()) {
311
8
            return 0;
312
8
        }
313
1.44k
        if constexpr (IsGreater) {
314
977
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
977
            return pos > 0 ? row_indexes[pos - 1] : 0;
316
977
        } else {
317
470
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
470
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
470
        }
320
1.44k
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb1ELb1EEEjj
Line
Count
Source
309
30
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
30
        if (asof_values.empty()) {
311
2
            return 0;
312
2
        }
313
28
        if constexpr (IsGreater) {
314
28
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
28
            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
28
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb1ELb0EEEjj
Line
Count
Source
309
41
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
41
        if (asof_values.empty()) {
311
2
            return 0;
312
2
        }
313
39
        if constexpr (IsGreater) {
314
39
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
39
            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
39
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb0ELb1EEEjj
Line
Count
Source
309
32
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
32
        if (asof_values.empty()) {
311
2
            return 0;
312
2
        }
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
30
        } else {
317
30
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
30
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
30
        }
320
30
    }
_ZNK5doris14AsofIndexGroupIjE15find_best_matchILb0ELb0EEEjj
Line
Count
Source
309
30
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
30
        if (asof_values.empty()) {
311
2
            return 0;
312
2
        }
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
28
        } else {
317
28
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
28
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
28
        }
320
28
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb1ELb1EEEjm
Line
Count
Source
309
206
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
206
        if (asof_values.empty()) {
311
0
            return 0;
312
0
        }
313
206
        if constexpr (IsGreater) {
314
206
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
206
            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
206
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb1ELb0EEEjm
Line
Count
Source
309
704
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
704
        if (asof_values.empty()) {
311
0
            return 0;
312
0
        }
313
704
        if constexpr (IsGreater) {
314
704
            size_t pos = IsStrict ? lower_bound(probe_value) : upper_bound(probe_value);
315
704
            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
704
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb0ELb1EEEjm
Line
Count
Source
309
199
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
199
        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
199
        } else {
317
199
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
199
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
199
        }
320
199
    }
_ZNK5doris14AsofIndexGroupImE15find_best_matchILb0ELb0EEEjm
Line
Count
Source
309
213
    ALWAYS_INLINE uint32_t find_best_match(IntType probe_value) const {
310
213
        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
213
        } else {
317
213
            size_t pos = IsStrict ? upper_bound(probe_value) : lower_bound(probe_value);
318
213
            return pos < asof_values.size() ? row_indexes[pos] : 0;
319
213
        }
320
213
    }
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