be/src/exprs/table_function/vexplode_bitmap.cpp
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 | | #include "exprs/table_function/vexplode_bitmap.h" |
19 | | |
20 | | #include <glog/logging.h> |
21 | | |
22 | | #include <memory> |
23 | | #include <ostream> |
24 | | |
25 | | #include "common/status.h" |
26 | | #include "core/block/block.h" |
27 | | #include "core/block/column_with_type_and_name.h" |
28 | | #include "core/column/column.h" |
29 | | #include "core/column/column_nullable.h" |
30 | | #include "core/string_ref.h" |
31 | | #include "core/value/bitmap_value.h" |
32 | | #include "exprs/table_function/table_function.h" |
33 | | #include "exprs/vexpr.h" |
34 | | #include "exprs/vexpr_context.h" |
35 | | |
36 | | namespace doris { |
37 | | |
38 | 182 | VExplodeBitmapTableFunction::VExplodeBitmapTableFunction() { |
39 | 182 | _fn_name = "vexplode_bitmap"; |
40 | 182 | } |
41 | | |
42 | 67 | Status VExplodeBitmapTableFunction::process_init(Block* block, RuntimeState* state) { |
43 | 67 | CHECK(_expr_context->root()->children().size() == 1) |
44 | 0 | << "VExplodeNumbersTableFunction must be have 1 children but have " |
45 | 0 | << _expr_context->root()->children().size(); |
46 | | |
47 | 67 | RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute_column( |
48 | 67 | _expr_context.get(), block, nullptr, block->rows(), _value_column)); |
49 | | |
50 | 67 | return Status::OK(); |
51 | 67 | } |
52 | | |
53 | 198 | void VExplodeBitmapTableFunction::reset() { |
54 | 198 | _eos = false; |
55 | 198 | _cur_offset = 0; |
56 | 198 | if (!current_empty()) { |
57 | 16 | _cur_iter = std::make_unique<BitmapValueIterator>(*_cur_bitmap); |
58 | 16 | } |
59 | 198 | } |
60 | | |
61 | 24 | void VExplodeBitmapTableFunction::forward(int step) { |
62 | 24 | if (!current_empty()) { |
63 | 48 | for (int i = 0; i < step; i++) { |
64 | 24 | ++(*_cur_iter); |
65 | 24 | } |
66 | 24 | } |
67 | 24 | TableFunction::forward(step); |
68 | 24 | } |
69 | | |
70 | 24 | void VExplodeBitmapTableFunction::get_same_many_values(MutableColumnPtr& column, int length) { |
71 | 24 | if (current_empty()) { |
72 | 0 | column->insert_many_defaults(length); |
73 | 24 | } else { |
74 | 24 | if (_is_nullable) { |
75 | 12 | assert_cast<ColumnInt64*>( |
76 | 12 | assert_cast<ColumnNullable*>(column.get())->get_nested_column_ptr().get()) |
77 | 12 | ->insert_many_vals(**_cur_iter, length); |
78 | 12 | assert_cast<ColumnUInt8*>( |
79 | 12 | assert_cast<ColumnNullable*>(column.get())->get_null_map_column_ptr().get()) |
80 | 12 | ->insert_many_defaults(length); |
81 | 12 | } else { |
82 | 12 | assert_cast<ColumnInt64*>(column.get())->insert_many_vals(**_cur_iter, length); |
83 | 12 | } |
84 | 24 | } |
85 | 24 | } |
86 | | |
87 | 182 | void VExplodeBitmapTableFunction::process_row(size_t row_idx) { |
88 | 182 | TableFunction::process_row(row_idx); |
89 | | //FIXME: use ColumnComplex instead |
90 | 182 | StringRef value = _value_column->get_data_at(row_idx); |
91 | | |
92 | 182 | if (value.data) { |
93 | 180 | _cur_bitmap = reinterpret_cast<const BitmapValue*>(value.data); |
94 | | |
95 | 180 | _cur_size = _cur_bitmap->cardinality(); |
96 | 180 | if (!current_empty()) { |
97 | 158 | _cur_iter = std::make_unique<BitmapValueIterator>(*_cur_bitmap); |
98 | 158 | } |
99 | 180 | } |
100 | 182 | } |
101 | | |
102 | 67 | void VExplodeBitmapTableFunction::process_close() { |
103 | 67 | _value_column = nullptr; |
104 | 67 | } |
105 | | |
106 | 180 | int VExplodeBitmapTableFunction::get_value(MutableColumnPtr& column, int max_step) { |
107 | 180 | max_step = std::min(max_step, (int)(_cur_size - _cur_offset)); |
108 | | // should dispose the empty status, forward one step |
109 | 180 | if (current_empty()) { |
110 | 2 | column->insert_default(); |
111 | 2 | max_step = 1; |
112 | 178 | } else { |
113 | 178 | ColumnInt64* target = nullptr; |
114 | 178 | if (_is_nullable) { |
115 | 70 | target = assert_cast<ColumnInt64*>( |
116 | 70 | assert_cast<ColumnNullable*>(column.get())->get_nested_column_ptr().get()); |
117 | 70 | assert_cast<ColumnUInt8*>( |
118 | 70 | assert_cast<ColumnNullable*>(column.get())->get_null_map_column_ptr().get()) |
119 | 70 | ->insert_many_defaults(max_step); |
120 | 108 | } else { |
121 | 108 | target = assert_cast<ColumnInt64*>(column.get()); |
122 | 108 | } |
123 | 178 | auto origin_size = target->size(); |
124 | 178 | target->resize(origin_size + max_step); |
125 | 178 | auto* target_data = target->get_data().data(); |
126 | 100k | for (int i = 0; i < max_step; ++i) { |
127 | 100k | target_data[i + origin_size] = **_cur_iter; |
128 | 100k | ++(*_cur_iter); |
129 | 100k | } |
130 | 178 | } |
131 | 180 | TableFunction::forward(max_step); |
132 | 180 | return max_step; |
133 | 180 | } |
134 | | } // namespace doris |