| 1 | #include "duckdb/common/operator/comparison_operators.hpp" |
| 2 | #include "duckdb/common/vector_operations/vector_operations.hpp" |
| 3 | #include "duckdb/execution/merge_join.hpp" |
| 4 | #include "duckdb/parser/expression/comparison_expression.hpp" |
| 5 | |
| 6 | using namespace duckdb; |
| 7 | using namespace std; |
| 8 | |
| 9 | template <class T> idx_t MergeJoinInner::Equality::Operation(ScalarMergeInfo &l, ScalarMergeInfo &r) { |
| 10 | throw NotImplementedException("Merge Join with Equality not implemented" ); |
| 11 | // if (l.pos >= l.count) { |
| 12 | // return 0; |
| 13 | // } |
| 14 | // assert(l.sel_vector && r.sel_vector); |
| 15 | // auto ldata = (T *)l.v.data; |
| 16 | // auto rdata = (T *)r.v.data; |
| 17 | // idx_t result_count = 0; |
| 18 | // while (true) { |
| 19 | // if (r.pos == r.count || duckdb::LessThan::Operation(ldata[l.sel_vector[l.pos]], rdata[r.sel_vector[r.pos]])) { |
| 20 | // // left side smaller: move left pointer forward |
| 21 | // l.pos++; |
| 22 | // if (l.pos >= l.count) { |
| 23 | // // left side exhausted |
| 24 | // break; |
| 25 | // } |
| 26 | // // we might need to go back on the right-side after going |
| 27 | // // forward on the left side because the new tuple might have |
| 28 | // // matches with the right side |
| 29 | // while (r.pos > 0 && duckdb::Equals::Operation(ldata[l.sel_vector[l.pos]], rdata[r.sel_vector[r.pos - 1]])) { |
| 30 | // r.pos--; |
| 31 | // } |
| 32 | // } else if (duckdb::GreaterThan::Operation(ldata[l.sel_vector[l.pos]], rdata[r.sel_vector[r.pos]])) { |
| 33 | // // right side smaller: move right pointer forward |
| 34 | // r.pos++; |
| 35 | // } else { |
| 36 | // // tuples match |
| 37 | // // output tuple |
| 38 | // l.result[result_count] = l.sel_vector[l.pos]; |
| 39 | // r.result[result_count] = r.sel_vector[r.pos]; |
| 40 | // result_count++; |
| 41 | // // move right side forward |
| 42 | // r.pos++; |
| 43 | // if (result_count == STANDARD_VECTOR_SIZE) { |
| 44 | // // out of space! |
| 45 | // break; |
| 46 | // } |
| 47 | // } |
| 48 | // } |
| 49 | // return result_count; |
| 50 | } |
| 51 | |
| 52 | template <class T> idx_t MergeJoinInner::LessThan::Operation(ScalarMergeInfo &l, ScalarMergeInfo &r) { |
| 53 | if (r.pos >= r.order.count) { |
| 54 | return 0; |
| 55 | } |
| 56 | auto ldata = (T *)l.order.vdata.data; |
| 57 | auto rdata = (T *)r.order.vdata.data; |
| 58 | auto &lorder = l.order.order; |
| 59 | auto &rorder = r.order.order; |
| 60 | idx_t result_count = 0; |
| 61 | while (true) { |
| 62 | if (l.pos < l.order.count) { |
| 63 | auto lidx = lorder.get_index(l.pos); |
| 64 | auto ridx = rorder.get_index(r.pos); |
| 65 | auto dlidx = l.order.vdata.sel->get_index(lidx); |
| 66 | auto dridx = r.order.vdata.sel->get_index(ridx); |
| 67 | if (duckdb::LessThan::Operation(ldata[dlidx], rdata[dridx])) { |
| 68 | // left side smaller: found match |
| 69 | l.result.set_index(result_count, lidx); |
| 70 | r.result.set_index(result_count, ridx); |
| 71 | result_count++; |
| 72 | // move left side forward |
| 73 | l.pos++; |
| 74 | if (result_count == STANDARD_VECTOR_SIZE) { |
| 75 | // out of space! |
| 76 | break; |
| 77 | } |
| 78 | continue; |
| 79 | } |
| 80 | } |
| 81 | // right side smaller or equal, or left side exhausted: move |
| 82 | // right pointer forward reset left side to start |
| 83 | l.pos = 0; |
| 84 | r.pos++; |
| 85 | if (r.pos == r.order.count) { |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | return result_count; |
| 90 | } |
| 91 | |
| 92 | template <class T> idx_t MergeJoinInner::LessThanEquals::Operation(ScalarMergeInfo &l, ScalarMergeInfo &r) { |
| 93 | if (r.pos >= r.order.count) { |
| 94 | return 0; |
| 95 | } |
| 96 | auto ldata = (T *)l.order.vdata.data; |
| 97 | auto rdata = (T *)r.order.vdata.data; |
| 98 | auto &lorder = l.order.order; |
| 99 | auto &rorder = r.order.order; |
| 100 | idx_t result_count = 0; |
| 101 | while (true) { |
| 102 | if (l.pos < l.order.count) { |
| 103 | auto lidx = lorder.get_index(l.pos); |
| 104 | auto ridx = rorder.get_index(r.pos); |
| 105 | auto dlidx = l.order.vdata.sel->get_index(lidx); |
| 106 | auto dridx = r.order.vdata.sel->get_index(ridx); |
| 107 | if (duckdb::LessThanEquals::Operation(ldata[dlidx], rdata[dridx])) { |
| 108 | // left side smaller: found match |
| 109 | l.result.set_index(result_count, lidx); |
| 110 | r.result.set_index(result_count, ridx); |
| 111 | result_count++; |
| 112 | // move left side forward |
| 113 | l.pos++; |
| 114 | if (result_count == STANDARD_VECTOR_SIZE) { |
| 115 | // out of space! |
| 116 | break; |
| 117 | } |
| 118 | continue; |
| 119 | } |
| 120 | } |
| 121 | // right side smaller or equal, or left side exhausted: move |
| 122 | // right pointer forward reset left side to start |
| 123 | l.pos = 0; |
| 124 | r.pos++; |
| 125 | if (r.pos == r.order.count) { |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | return result_count; |
| 130 | } |
| 131 | |
| 132 | INSTANTIATE_MERGEJOIN_TEMPLATES(MergeJoinInner, Equality, ScalarMergeInfo, ScalarMergeInfo); |
| 133 | INSTANTIATE_MERGEJOIN_TEMPLATES(MergeJoinInner, LessThan, ScalarMergeInfo, ScalarMergeInfo); |
| 134 | INSTANTIATE_MERGEJOIN_TEMPLATES(MergeJoinInner, LessThanEquals, ScalarMergeInfo, ScalarMergeInfo); |
| 135 | |