forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashjoin_probe_operator.cpp
More file actions
695 lines (622 loc) · 33.2 KB
/
Copy pathhashjoin_probe_operator.cpp
File metadata and controls
695 lines (622 loc) · 33.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "exec/operator/hashjoin_probe_operator.h"
#include <gen_cpp/Opcodes_types.h>
#include <gen_cpp/PlanNodes_types.h>
#include <string>
#include "common/cast_set.h"
#include "common/logging.h"
#include "core/assert_cast.h"
#include "core/column/column_const.h"
#include "core/column/column_nullable.h"
#include "core/data_type/data_type_nullable.h"
#include "exec/operator/operator.h"
#include "runtime/descriptors.h"
namespace doris {
HashJoinProbeLocalState::HashJoinProbeLocalState(RuntimeState* state, OperatorXBase* parent)
: JoinProbeLocalState<HashJoinSharedState, HashJoinProbeLocalState>(state, parent),
_process_hashtable_ctx_variants(std::make_unique<HashTableCtxVariants>()) {}
Status HashJoinProbeLocalState::init(RuntimeState* state, LocalStateInfo& info) {
RETURN_IF_ERROR(JoinProbeLocalState::init(state, info));
SCOPED_TIMER(exec_time_counter());
SCOPED_TIMER(_init_timer);
_task_idx = info.task_idx;
auto& p = _parent->cast<HashJoinProbeOperatorX>();
_probe_expr_ctxs.resize(p._probe_expr_ctxs.size());
for (size_t i = 0; i < _probe_expr_ctxs.size(); i++) {
RETURN_IF_ERROR(p._probe_expr_ctxs[i]->clone(state, _probe_expr_ctxs[i]));
}
_other_join_conjuncts.resize(p._other_join_conjuncts.size());
for (size_t i = 0; i < _other_join_conjuncts.size(); i++) {
RETURN_IF_ERROR(p._other_join_conjuncts[i]->clone(state, _other_join_conjuncts[i]));
}
_mark_join_conjuncts.resize(p._mark_join_conjuncts.size());
for (size_t i = 0; i < _mark_join_conjuncts.size(); i++) {
RETURN_IF_ERROR(p._mark_join_conjuncts[i]->clone(state, _mark_join_conjuncts[i]));
}
_construct_mutable_join_block();
_probe_column_disguise_null.reserve(_probe_expr_ctxs.size());
_probe_arena_memory_usage = custom_profile()->AddHighWaterMarkCounter(
"MemoryUsageProbeKeyArena", TUnit::BYTES, "", 1);
// Probe phase
_probe_expr_call_timer = ADD_TIMER(custom_profile(), "ProbeExprCallTime");
_search_hashtable_timer = ADD_TIMER(custom_profile(), "ProbeWhenSearchHashTableTime");
_build_side_output_timer = ADD_TIMER(custom_profile(), "ProbeWhenBuildSideOutputTime");
_probe_side_output_timer = ADD_TIMER(custom_profile(), "ProbeWhenProbeSideOutputTime");
_non_equal_join_conjuncts_timer =
ADD_TIMER(custom_profile(), "NonEqualJoinConjunctEvaluationTime");
_init_probe_side_timer = ADD_TIMER(custom_profile(), "InitProbeSideTime");
// ASOF probe counters (only for ASOF join types)
if (is_asof_join(p._join_op) && state->enable_profile() && state->profile_level() >= 2) {
static constexpr auto SEARCH_HASH_TABLE_TIMER = "ProbeWhenSearchHashTableTime";
_asof_probe_expr_timer = ADD_CHILD_TIMER_WITH_LEVEL(custom_profile(), "AsofProbeExprTime",
SEARCH_HASH_TABLE_TIMER, 2);
_asof_probe_hash_chain_timer = ADD_CHILD_TIMER_WITH_LEVEL(
custom_profile(), "AsofProbeHashChainTime", SEARCH_HASH_TABLE_TIMER, 2);
_asof_probe_search_timer = ADD_CHILD_TIMER_WITH_LEVEL(
custom_profile(), "AsofProbeBinarySearchTime", SEARCH_HASH_TABLE_TIMER, 2);
}
return Status::OK();
}
Status HashJoinProbeLocalState::open(RuntimeState* state) {
SCOPED_TIMER(exec_time_counter());
SCOPED_TIMER(_open_timer);
RETURN_IF_ERROR(JoinProbeLocalState::open(state));
auto& p = _parent->cast<HashJoinProbeOperatorX>();
Status res;
std::visit(
[&](auto&& join_op_variants, auto have_other_join_conjunct) {
using JoinOpType = std::decay_t<decltype(join_op_variants)>;
if constexpr (JoinOpType::value == TJoinOp::CROSS_JOIN) {
res = Status::InternalError("hash join do not support cross join");
} else {
_process_hashtable_ctx_variants
->emplace<ProcessHashTableProbe<JoinOpType::value>>(
this, state->batch_size());
}
},
_shared_state->join_op_variants, make_bool_variant(p._have_other_join_conjunct));
return res;
}
void HashJoinProbeLocalState::prepare_for_next() {
_probe_index = 0;
_build_index = 0;
_ready_probe = false;
_last_probe_match = -1;
_last_probe_null_mark = -1;
_prepare_probe_block();
}
Status HashJoinProbeLocalState::close(RuntimeState* state) {
SCOPED_TIMER(exec_time_counter());
SCOPED_TIMER(_close_timer);
if (_closed) {
return Status::OK();
}
if (_process_hashtable_ctx_variants) {
std::visit(Overload {[&](std::monostate&) {},
[&](auto&& process_hashtable_ctx) {
if (process_hashtable_ctx._arena) {
process_hashtable_ctx._arena.reset();
}
}},
*_process_hashtable_ctx_variants);
}
_process_hashtable_ctx_variants = nullptr;
_null_map_column = nullptr;
_probe_block.clear();
return JoinProbeLocalState<HashJoinSharedState, HashJoinProbeLocalState>::close(state);
}
bool HashJoinProbeLocalState::_need_probe_null_map(Block& block,
const std::vector<int>& res_col_ids) {
for (size_t i = 0; i < _probe_expr_ctxs.size(); ++i) {
const auto* column = block.get_by_position(res_col_ids[i]).column.get();
if (column->is_nullable() &&
!_parent->cast<HashJoinProbeOperatorX>()._serialize_null_into_key[i]) {
return true;
}
}
return false;
}
void HashJoinProbeLocalState::_prepare_probe_block() {
// clear_column_data of _probe_block
if (!_probe_column_disguise_null.empty()) {
for (int i = 0; i < _probe_column_disguise_null.size(); ++i) {
auto column_to_erase = _probe_column_disguise_null[i];
_probe_block.erase(column_to_erase - i);
}
_probe_column_disguise_null.clear();
}
// remove add nullmap of probe columns
for (auto index : _probe_column_convert_to_null) {
auto& column_type = _probe_block.safe_get_by_position(index);
DCHECK(column_type.column->is_nullable() || is_column_const(*(column_type.column.get())));
DCHECK(column_type.type->is_nullable());
column_type.column = remove_nullable(column_type.column);
column_type.type = remove_nullable(column_type.type);
}
_key_columns_holder.clear();
_probe_block.clear_column_data(_parent->get_child()->row_desc().num_materialized_slots());
}
HashJoinProbeOperatorX::HashJoinProbeOperatorX(ObjectPool* pool, const TPlanNode& tnode,
int operator_id, const DescriptorTbl& descs)
: JoinProbeOperatorX<HashJoinProbeLocalState>(pool, tnode, operator_id, descs),
_join_distribution(tnode.hash_join_node.__isset.dist_type ? tnode.hash_join_node.dist_type
: TJoinDistributionType::NONE),
_is_broadcast_join(tnode.hash_join_node.__isset.is_broadcast_join &&
tnode.hash_join_node.is_broadcast_join),
_hash_output_slot_ids(tnode.hash_join_node.__isset.hash_output_slot_ids
? tnode.hash_join_node.hash_output_slot_ids
: std::vector<SlotId> {}),
_partition_exprs(tnode.__isset.distribute_expr_lists && !_is_broadcast_join
? tnode.distribute_expr_lists[0]
: std::vector<TExpr> {}) {}
Status HashJoinProbeOperatorX::pull(doris::RuntimeState* state, Block* output_block,
bool* eos) const {
auto& local_state = get_local_state(state);
if (local_state._shared_state->short_circuit_for_probe) {
// If we use a short-circuit strategy, should return empty block directly.
*eos = true;
return Status::OK();
}
//TODO: this short circuit maybe could refactor, no need to check at here.
if (local_state.empty_right_table_shortcut()) {
// when build table rows is 0 and not have other_join_conjunct and join type is one of LEFT_OUTER_JOIN/FULL_OUTER_JOIN/LEFT_ANTI_JOIN
// we could get the result is probe table + null-column(if need output)
// If we use a short-circuit strategy, should return block directly by add additional null data.
auto block_rows = local_state._probe_block.rows();
if (local_state._probe_eos && block_rows == 0) {
*eos = true;
return Status::OK();
}
//create build side null column, if need output
for (int i = 0;
(_join_op != TJoinOp::LEFT_ANTI_JOIN) && i < _right_output_slot_flags.size(); ++i) {
auto type = remove_nullable(_right_table_data_types[i]);
auto column = type->create_column();
// Here should NOT use `resize` instead of `insert_many_defaults` to create null column,
// because `insert_many_defaults` will create a column with default value.
// For example: for `Struct<a: int, b: string>`,
// If the column is created by `resize`,
// the nested columns' null-map will be random values(not only 1 and 0),
// if `struct_element` is called next, it will result in an invalid ColumnNullable data.
column->insert_many_defaults(block_rows);
auto null_map_column = ColumnUInt8::create(block_rows, 1);
auto nullable_column =
ColumnNullable::create(std::move(column), std::move(null_map_column));
local_state._probe_block.insert({std::move(nullable_column), make_nullable(type),
_right_table_column_names[i]});
}
/// No need to check the block size in `_filter_data_and_build_output` because here dose not
/// increase the output rows count(just same as `_probe_block`'s rows count).
RETURN_IF_ERROR(local_state.filter_data_and_build_output(state, output_block, eos,
&local_state._probe_block, false));
local_state._probe_block.clear_column_data(_child->row_desc().num_materialized_slots());
return Status::OK();
}
local_state._join_block.clear_column_data();
ScopedMutableBlock scoped_mutable_join_block(&local_state._join_block);
auto& mutable_join_block = scoped_mutable_join_block.mutable_block();
Block temp_block;
Status st;
if (local_state._probe_index < local_state._probe_block.rows()) {
DCHECK(local_state._has_set_need_null_map_for_probe);
std::visit(
[&](auto&& arg, auto&& process_hashtable_ctx) {
using HashTableProbeType = std::decay_t<decltype(process_hashtable_ctx)>;
if constexpr (!std::is_same_v<HashTableProbeType, std::monostate>) {
using HashTableCtxType = std::decay_t<decltype(arg)>;
if constexpr (!std::is_same_v<HashTableCtxType, std::monostate>) {
if (local_state._shared_state->left_semi_direct_return) {
process_hashtable_ctx.process_direct_return(
arg, mutable_join_block, &temp_block,
cast_set<uint32_t>(local_state._probe_block.rows()));
return;
}
st = process_hashtable_ctx.process(
arg,
local_state._null_map_column
? local_state._null_map_column->get_data().data()
: nullptr,
mutable_join_block, &temp_block,
cast_set<uint32_t>(local_state._probe_block.rows()),
_is_mark_join);
} else {
st = Status::InternalError("uninited hash table");
}
} else {
st = Status::InternalError("uninited hash table probe");
}
},
local_state._shared_state->hash_table_variant_vector.size() == 1
? local_state._shared_state->hash_table_variant_vector[0]->method_variant
: local_state._shared_state
->hash_table_variant_vector[local_state._task_idx]
->method_variant,
*local_state._process_hashtable_ctx_variants);
} else if (local_state._probe_eos) {
if (_is_right_semi_anti || (_is_outer_join && _join_op != TJoinOp::LEFT_OUTER_JOIN)) {
std::visit(
[&](auto&& arg, auto&& process_hashtable_ctx) {
using HashTableProbeType = std::decay_t<decltype(process_hashtable_ctx)>;
if constexpr (!std::is_same_v<HashTableProbeType, std::monostate>) {
using HashTableCtxType = std::decay_t<decltype(arg)>;
if constexpr (!std::is_same_v<HashTableCtxType, std::monostate>) {
st = process_hashtable_ctx.finish_probing(
arg, mutable_join_block, &temp_block, eos, _is_mark_join);
} else {
st = Status::InternalError("uninited hash table");
}
} else {
st = Status::InternalError("uninited hash table probe");
}
},
local_state._shared_state->hash_table_variant_vector.size() == 1
? local_state._shared_state->hash_table_variant_vector[0]
->method_variant
: local_state._shared_state
->hash_table_variant_vector[local_state._task_idx]
->method_variant,
*local_state._process_hashtable_ctx_variants);
} else {
*eos = true;
return Status::OK();
}
} else {
return Status::OK();
}
if (!st) {
return st;
}
local_state._estimate_memory_usage += temp_block.allocated_bytes();
// if left_semi_direct_return is true,
// here does not increase the output rows count(just same as `_probe_block`'s rows count).
RETURN_IF_ERROR(local_state.filter_data_and_build_output(
state, output_block, eos, &temp_block,
!local_state._shared_state->left_semi_direct_return));
// Here make _join_block release the columns' ptr
scoped_mutable_join_block.restore();
local_state._join_block.set_columns(local_state._join_block.clone_empty_columns());
return Status::OK();
}
std::string HashJoinProbeLocalState::debug_string(int indentation_level) const {
fmt::memory_buffer debug_string_buffer;
fmt::format_to(debug_string_buffer, "{}, short_circuit_for_probe: {}",
JoinProbeLocalState<HashJoinSharedState, HashJoinProbeLocalState>::debug_string(
indentation_level),
_shared_state ? std::to_string(_shared_state->short_circuit_for_probe) : "NULL");
return fmt::to_string(debug_string_buffer);
}
Status HashJoinProbeLocalState::_extract_join_column(Block& block,
const std::vector<int>& res_col_ids) {
if (empty_right_table_shortcut()) {
return Status::OK();
}
_probe_columns.resize(_probe_expr_ctxs.size());
if (!_has_set_need_null_map_for_probe) {
_has_set_need_null_map_for_probe = true;
_need_null_map_for_probe = _need_probe_null_map(block, res_col_ids);
}
if (_need_null_map_for_probe) {
if (!_null_map_column) {
_null_map_column = ColumnUInt8::create();
}
_null_map_column->get_data().assign(block.rows(), (uint8_t)0);
}
auto& shared_state = *_shared_state;
for (size_t i = 0; i < shared_state.build_exprs_size; ++i) {
const auto& column_ptr = block.get_by_position(res_col_ids[i]).column;
const auto* column = column_ptr.get();
const bool serialize_null_into_key =
_parent->cast<HashJoinProbeOperatorX>()._serialize_null_into_key[i];
// _do_evaluate() must have materialized Const(Nullable) probe keys. If this check fails,
// is_nullable() no longer implies a physical ColumnNullable for the logic below.
const auto* const_column = check_and_get_column<ColumnConst>(*column);
DORIS_CHECK(const_column == nullptr ||
!is_column_nullable(const_column->get_data_column()));
if (!column->is_nullable() && serialize_null_into_key) {
_key_columns_holder.emplace_back(
make_nullable(block.get_by_position(res_col_ids[i]).column));
_probe_columns[i] = _key_columns_holder.back().get();
} else if (const auto* nullable = check_and_get_column<ColumnNullable>(*column);
nullable && !serialize_null_into_key) {
// update nulllmap and split nested out of ColumnNullable when serialize_null_into_key is false and column is nullable
const auto& col_nested = nullable->get_nested_column();
const auto& col_nullmap = nullable->get_null_map_data();
DCHECK(_null_map_column);
VectorizedUtils::update_null_map(_null_map_column->get_data(), col_nullmap);
_probe_columns[i] = &col_nested;
} else {
_probe_columns[i] = column;
}
}
return Status::OK();
}
std::vector<uint16_t> HashJoinProbeLocalState::_convert_block_to_null(Block& block) {
std::vector<uint16_t> results;
for (int i = 0; i < block.columns(); ++i) {
if (auto& column_type = block.safe_get_by_position(i); !column_type.type->is_nullable()) {
DCHECK(!column_type.column->is_nullable());
column_type.column = make_nullable(column_type.column);
column_type.type = make_nullable(column_type.type);
results.emplace_back(i);
}
}
return results;
}
Status HashJoinProbeLocalState::filter_data_and_build_output(RuntimeState* state,
Block* output_block, bool* eos,
Block* temp_block,
bool check_rows_count) {
auto output_rows = temp_block->rows();
if (check_rows_count) {
DCHECK(output_rows <= state->batch_size());
}
{
SCOPED_TIMER(_join_filter_timer);
RETURN_IF_ERROR(filter_block(_conjuncts, temp_block));
}
RETURN_IF_ERROR(_build_output_block(temp_block, output_block));
reached_limit(output_block, eos);
return Status::OK();
}
bool HashJoinProbeOperatorX::need_more_input_data(RuntimeState* state) const {
auto& local_state = state->get_local_state(operator_id())->cast<HashJoinProbeLocalState>();
return (local_state._probe_block.rows() == 0 ||
local_state._probe_index == local_state._probe_block.rows()) &&
!local_state._probe_eos && !local_state._shared_state->short_circuit_for_probe;
}
Status HashJoinProbeOperatorX::_do_evaluate(Block& block, VExprContextSPtrs& exprs,
RuntimeProfile::Counter& expr_call_timer,
std::vector<int>& res_col_ids) const {
for (size_t i = 0; i < exprs.size(); ++i) {
int result_col_id = -1;
// execute build column
{
SCOPED_TIMER(&expr_call_timer);
RETURN_IF_ERROR(exprs[i]->execute(&block, &result_col_id));
}
// _extract_join_column() handles physical ColumnNullable only, so probe-key const
// columns, including Const(Nullable), must be materialized before probing.
// TODO: if const-key optimization is added, update _extract_join_column() together.
block.get_by_position(result_col_id).column =
block.get_by_position(result_col_id).column->convert_to_full_column_if_const();
res_col_ids[i] = result_col_id;
}
return Status::OK();
}
Status HashJoinProbeOperatorX::push(RuntimeState* state, Block* input_block, bool eos) const {
auto& local_state = get_local_state(state);
local_state.prepare_for_next();
local_state._probe_eos = eos;
const auto rows = input_block->rows();
size_t origin_size = input_block->allocated_bytes();
if (rows > 0) {
COUNTER_UPDATE(local_state._probe_rows_counter, rows);
std::vector<int> res_col_ids(local_state._probe_expr_ctxs.size());
RETURN_IF_ERROR(_do_evaluate(*input_block, local_state._probe_expr_ctxs,
*local_state._probe_expr_call_timer, res_col_ids));
if (_join_op == TJoinOp::RIGHT_OUTER_JOIN || _join_op == TJoinOp::FULL_OUTER_JOIN) {
local_state._probe_column_convert_to_null =
local_state._convert_block_to_null(*input_block);
}
RETURN_IF_ERROR(local_state._extract_join_column(*input_block, res_col_ids));
local_state._estimate_memory_usage += (input_block->allocated_bytes() - origin_size);
if (&local_state._probe_block != input_block) {
input_block->swap(local_state._probe_block);
COUNTER_SET(local_state._memory_used_counter,
(int64_t)local_state._probe_block.allocated_bytes());
}
}
return Status::OK();
}
Status HashJoinProbeOperatorX::init(const TPlanNode& tnode, RuntimeState* state) {
RETURN_IF_ERROR(JoinProbeOperatorX<HashJoinProbeLocalState>::init(tnode, state));
DCHECK(tnode.__isset.hash_join_node);
const std::vector<TEqJoinCondition>& eq_join_conjuncts = tnode.hash_join_node.eq_join_conjuncts;
for (const auto& eq_join_conjunct : eq_join_conjuncts) {
VExprContextSPtr ctx;
RETURN_IF_ERROR(VExpr::create_expr_tree(eq_join_conjunct.left, ctx));
_probe_expr_ctxs.push_back(ctx);
/// null safe equal means null = null is true, the operator in SQL should be: <=>.
const bool is_null_safe_equal =
eq_join_conjunct.__isset.opcode &&
(eq_join_conjunct.opcode == TExprOpcode::EQ_FOR_NULL) &&
// For a null safe equal join, FE may generate a plan that
// both sides of the conjuct are not nullable, we just treat it
// as a normal equal join conjunct.
(eq_join_conjunct.right.nodes[0].is_nullable ||
eq_join_conjunct.left.nodes[0].is_nullable);
if (eq_join_conjuncts.size() == 1) {
// single column key serialize method must use nullmap for represent null to instead serialize null into key
_serialize_null_into_key.emplace_back(false);
} else if (is_null_safe_equal) {
// use serialize null into key to represent multi column null value
_serialize_null_into_key.emplace_back(true);
} else {
// on normal conditions, because null!=null, it can be expressed directly with nullmap.
_serialize_null_into_key.emplace_back(false);
}
}
if (tnode.hash_join_node.__isset.other_join_conjuncts &&
!tnode.hash_join_node.other_join_conjuncts.empty()) {
RETURN_IF_ERROR(VExpr::create_expr_trees(tnode.hash_join_node.other_join_conjuncts,
_other_join_conjuncts));
DCHECK(!_build_unique);
DCHECK(_have_other_join_conjunct);
} else if (tnode.hash_join_node.__isset.vother_join_conjunct) {
_other_join_conjuncts.resize(1);
RETURN_IF_ERROR(VExpr::create_expr_tree(tnode.hash_join_node.vother_join_conjunct,
_other_join_conjuncts[0]));
// If LEFT SEMI JOIN/LEFT ANTI JOIN with not equal predicate,
// build table should not be deduplicated.
DCHECK(!_build_unique);
DCHECK(_have_other_join_conjunct);
}
if (tnode.hash_join_node.__isset.mark_join_conjuncts &&
!tnode.hash_join_node.mark_join_conjuncts.empty()) {
RETURN_IF_ERROR(VExpr::create_expr_trees(tnode.hash_join_node.mark_join_conjuncts,
_mark_join_conjuncts));
DCHECK(_is_mark_join);
/// We make mark join conjuncts as equal conjuncts for null aware join,
/// so `_mark_join_conjuncts` should be empty if this is null aware join.
DCHECK_EQ(_mark_join_conjuncts.empty(),
_join_op == TJoinOp::NULL_AWARE_LEFT_ANTI_JOIN ||
_join_op == TJoinOp::NULL_AWARE_LEFT_SEMI_JOIN);
}
// For ASOF JOIN, extract the probe-side expression from match_condition field.
// match_condition is bound on input tuples, so child(0) references probe child's slots.
if (is_asof_join(_join_op)) {
DORIS_CHECK(tnode.hash_join_node.__isset.match_condition);
VExprContextSPtr full_conjunct;
RETURN_IF_ERROR(
VExpr::create_expr_tree(tnode.hash_join_node.match_condition, full_conjunct));
DORIS_CHECK(full_conjunct);
DORIS_CHECK(full_conjunct->root());
DORIS_CHECK(full_conjunct->root()->get_num_children() == 2);
auto left_child_expr = full_conjunct->root()->get_child(0);
_asof_probe_expr = std::make_shared<VExprContext>(left_child_expr);
}
return Status::OK();
}
Status HashJoinProbeOperatorX::prepare(RuntimeState* state) {
RETURN_IF_ERROR(JoinProbeOperatorX<HashJoinProbeLocalState>::prepare(state));
// init left/right output slots flags, only column of slot_id in _hash_output_slot_ids need
// insert to output block of hash join.
// _left_output_slots_flags : column of left table need to output set flag = true
// _rgiht_output_slots_flags : column of right table need to output set flag = true
// if _hash_output_slot_ids is empty, means all column of left/right table need to output.
auto init_output_slots_flags = [&](auto& tuple_descs, auto& output_slot_flags,
bool init_finalize_flag = false) {
for (const auto& tuple_desc : tuple_descs) {
for (const auto& slot_desc : tuple_desc->slots()) {
output_slot_flags.emplace_back(
std::find(_hash_output_slot_ids.begin(), _hash_output_slot_ids.end(),
slot_desc->id()) != _hash_output_slot_ids.end());
if (init_finalize_flag && output_slot_flags.back() &&
slot_desc->type()->get_primitive_type() == PrimitiveType::TYPE_VARIANT) {
_need_finalize_variant_column = true;
}
}
}
};
init_output_slots_flags(_child->row_desc().tuple_descriptors(), _left_output_slot_flags, true);
init_output_slots_flags(_build_side_child->row_desc().tuple_descriptors(),
_right_output_slot_flags);
// _other_join_conjuncts are evaluated in the context of the rows produced by this node
for (auto& conjunct : _other_join_conjuncts) {
RETURN_IF_ERROR(conjunct->prepare(state, *_intermediate_row_desc));
conjunct->root()->collect_slot_column_ids(_should_not_lazy_materialized_column_ids);
}
for (auto& conjunct : _mark_join_conjuncts) {
RETURN_IF_ERROR(conjunct->prepare(state, *_intermediate_row_desc));
conjunct->root()->collect_slot_column_ids(_should_not_lazy_materialized_column_ids);
}
RETURN_IF_ERROR(VExpr::prepare(_probe_expr_ctxs, state, _child->row_desc()));
// Prepare ASOF probe-side expression against probe child's row_desc directly.
if (is_asof_join(_join_op)) {
DORIS_CHECK(_asof_probe_expr);
RETURN_IF_ERROR(_asof_probe_expr->prepare(state, _child->row_desc()));
RETURN_IF_ERROR(_asof_probe_expr->open(state));
}
DCHECK(_build_side_child != nullptr);
// right table data types
_right_table_data_types = VectorizedUtils::get_data_types(_build_side_child->row_desc());
_left_table_data_types = VectorizedUtils::get_data_types(_child->row_desc());
_right_table_column_names = VectorizedUtils::get_column_names(_build_side_child->row_desc());
std::vector<const SlotDescriptor*> slots_to_check;
for (const auto& tuple_descriptor : _intermediate_row_desc->tuple_descriptors()) {
for (const auto& slot : tuple_descriptor->slots()) {
slots_to_check.emplace_back(slot);
}
}
if (_is_mark_join) {
const auto* last_one = slots_to_check.back();
slots_to_check.pop_back();
auto data_type = last_one->get_data_type_ptr();
if (!data_type->is_nullable()) {
return Status::InternalError(
"The last column for mark join should be Nullable(UInt8), not {}",
data_type->get_name());
}
const auto& null_data_type = assert_cast<const DataTypeNullable&>(*data_type);
if (null_data_type.get_nested_type()->get_primitive_type() != PrimitiveType::TYPE_BOOLEAN) {
return Status::InternalError(
"The last column for mark join should be Nullable(UInt8), not {}",
data_type->get_name());
}
}
_right_col_idx = (_is_right_semi_anti && !_have_other_join_conjunct &&
(!_is_mark_join || _mark_join_conjuncts.empty()))
? 0
: _left_table_data_types.size();
size_t idx = 0;
for (const auto* slot : slots_to_check) {
auto data_type = slot->get_data_type_ptr();
const auto slot_on_left = idx < _right_col_idx;
if (slot_on_left) {
if (idx >= _left_table_data_types.size()) {
return Status::InternalError(
"Join node(id={}, OP={}) intermediate slot({}, #{})'s on left table "
"idx out bound of _left_table_data_types: {} vs {}",
_node_id, _join_op, slot->col_name(), slot->id(), idx,
_left_table_data_types.size());
}
} else if (idx - _right_col_idx >= _right_table_data_types.size()) {
return Status::InternalError(
"Join node(id={}, OP={}) intermediate slot({}, #{})'s on right table "
"idx out bound of _right_table_data_types: {} vs {}(idx = {}, _right_col_idx = "
"{})",
_node_id, _join_op, slot->col_name(), slot->id(), idx - _right_col_idx,
_right_table_data_types.size(), idx, _right_col_idx);
}
auto target_data_type = slot_on_left ? _left_table_data_types[idx]
: _right_table_data_types[idx - _right_col_idx];
++idx;
if (data_type->equals(*target_data_type)) {
continue;
}
/// For outer join(left/right/full/asof), the non-nullable columns may be converted to nullable.
const auto accept_nullable_not_match =
_join_op == TJoinOp::FULL_OUTER_JOIN ||
(slot_on_left ? (_join_op == TJoinOp::RIGHT_OUTER_JOIN)
: (_join_op == TJoinOp::LEFT_OUTER_JOIN ||
_join_op == TJoinOp::ASOF_LEFT_OUTER_JOIN));
if (accept_nullable_not_match) {
auto data_type_non_nullable = remove_nullable(data_type);
if (data_type_non_nullable->equals(*target_data_type)) {
continue;
}
} else if (data_type->equals(*target_data_type)) {
continue;
}
return Status::InternalError(
"Join node(id={}, OP={}) intermediate slot({}, #{})'s on {} table data type not "
"match: '{}' vs '{}'",
_node_id, _join_op, slot->col_name(), slot->id(), (slot_on_left ? "left" : "right"),
data_type->get_name(), target_data_type->get_name());
}
_build_side_child.reset();
RETURN_IF_ERROR(VExpr::open(_probe_expr_ctxs, state));
for (auto& conjunct : _other_join_conjuncts) {
RETURN_IF_ERROR(conjunct->open(state));
}
for (auto& conjunct : _mark_join_conjuncts) {
RETURN_IF_ERROR(conjunct->open(state));
}
return Status::OK();
}
} // namespace doris