1 | #include "duckdb/execution/merge_join.hpp" |
2 | |
3 | #include "duckdb/parser/expression/comparison_expression.hpp" |
4 | |
5 | using namespace duckdb; |
6 | using namespace std; |
7 | |
8 | template <class MJ, class L_ARG, class R_ARG> static idx_t merge_join(L_ARG &l, R_ARG &r) { |
9 | switch (l.type) { |
10 | case TypeId::BOOL: |
11 | case TypeId::INT8: |
12 | return MJ::template Operation<int8_t>(l, r); |
13 | case TypeId::INT16: |
14 | return MJ::template Operation<int16_t>(l, r); |
15 | case TypeId::INT32: |
16 | return MJ::template Operation<int32_t>(l, r); |
17 | case TypeId::INT64: |
18 | return MJ::template Operation<int64_t>(l, r); |
19 | case TypeId::FLOAT: |
20 | return MJ::template Operation<float>(l, r); |
21 | case TypeId::DOUBLE: |
22 | return MJ::template Operation<double>(l, r); |
23 | case TypeId::VARCHAR: |
24 | return MJ::template Operation<string_t>(l, r); |
25 | default: |
26 | throw NotImplementedException("Type not implemented for merge join!" ); |
27 | } |
28 | } |
29 | |
30 | template <class T, class L_ARG, class R_ARG> |
31 | static idx_t perform_merge_join(L_ARG &l, R_ARG &r, ExpressionType comparison_type) { |
32 | switch (comparison_type) { |
33 | case ExpressionType::COMPARE_EQUAL: |
34 | return merge_join<typename T::Equality, L_ARG, R_ARG>(l, r); |
35 | case ExpressionType::COMPARE_LESSTHAN: |
36 | return merge_join<typename T::LessThan, L_ARG, R_ARG>(l, r); |
37 | case ExpressionType::COMPARE_LESSTHANOREQUALTO: |
38 | return merge_join<typename T::LessThanEquals, L_ARG, R_ARG>(l, r); |
39 | case ExpressionType::COMPARE_GREATERTHAN: |
40 | return merge_join<typename T::GreaterThan, L_ARG, R_ARG>(l, r); |
41 | default: |
42 | // "Unimplemented comparison type for merge join!" |
43 | assert(comparison_type == ExpressionType::COMPARE_GREATERTHANOREQUALTO); |
44 | return merge_join<typename T::GreaterThanEquals, L_ARG, R_ARG>(l, r); |
45 | } |
46 | } |
47 | |
48 | idx_t MergeJoinInner::Perform(MergeInfo &l, MergeInfo &r, ExpressionType comparison_type) { |
49 | assert(l.info_type == MergeInfoType::SCALAR_MERGE_INFO && r.info_type == MergeInfoType::SCALAR_MERGE_INFO); |
50 | auto &left = (ScalarMergeInfo &)l; |
51 | auto &right = (ScalarMergeInfo &)r; |
52 | assert(left.type == right.type); |
53 | if (left.order.count == 0 || right.order.count == 0) { |
54 | return 0; |
55 | } |
56 | return perform_merge_join<MergeJoinInner, ScalarMergeInfo, ScalarMergeInfo>(left, right, comparison_type); |
57 | } |
58 | |
59 | idx_t MergeJoinMark::Perform(MergeInfo &l, MergeInfo &r, ExpressionType comparison_type) { |
60 | assert(l.info_type == MergeInfoType::SCALAR_MERGE_INFO && r.info_type == MergeInfoType::CHUNK_MERGE_INFO); |
61 | auto &left = (ScalarMergeInfo &)l; |
62 | auto &right = (ChunkMergeInfo &)r; |
63 | assert(left.type == right.type); |
64 | if (left.order.count == 0 || right.data_chunks.count == 0) { |
65 | return 0; |
66 | } |
67 | return perform_merge_join<MergeJoinMark, ScalarMergeInfo, ChunkMergeInfo>(left, right, comparison_type); |
68 | } |
69 | |