be/src/exprs/table_function/vexplode_numbers.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_numbers.h" |
19 | | |
20 | | #include <glog/logging.h> |
21 | | |
22 | | #include <ostream> |
23 | | |
24 | | #include "common/status.h" |
25 | | #include "core/assert_cast.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_const.h" |
30 | | #include "core/column/column_nullable.h" |
31 | | #include "core/string_ref.h" |
32 | | #include "exprs/vexpr.h" |
33 | | #include "exprs/vexpr_context.h" |
34 | | #include "runtime/runtime_state.h" |
35 | | |
36 | | namespace doris { |
37 | | #include "common/compile_check_begin.h" |
38 | | |
39 | 201 | VExplodeNumbersTableFunction::VExplodeNumbersTableFunction() { |
40 | 201 | _fn_name = "vexplode_numbers"; |
41 | 201 | } |
42 | | |
43 | 109 | Status VExplodeNumbersTableFunction::process_init(Block* block, RuntimeState* state) { |
44 | 109 | CHECK(_expr_context->root()->children().size() == 1) |
45 | 0 | << "VExplodeNumbersTableFunction must be have 1 children but have " |
46 | 0 | << _expr_context->root()->children().size(); |
47 | | |
48 | 109 | int value_column_idx = -1; |
49 | 109 | RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute(_expr_context.get(), block, |
50 | 109 | &value_column_idx)); |
51 | 109 | _value_column = block->get_by_position(value_column_idx).column; |
52 | 109 | if (is_column_const(*_value_column)) { |
53 | 80 | _cur_size = 0; |
54 | | |
55 | | // the argument columns -> Int32 |
56 | 80 | const auto& column_nested = |
57 | 80 | assert_cast<const ColumnConst&>(*_value_column).get_data_column_ptr(); |
58 | 80 | if (column_nested->is_nullable()) { |
59 | 1 | if (!column_nested->is_null_at(0)) { |
60 | 0 | _cur_size = assert_cast<const ColumnInt32*>( |
61 | 0 | assert_cast<const ColumnNullable*>(column_nested.get()) |
62 | 0 | ->get_nested_column_ptr() |
63 | 0 | .get()) |
64 | 0 | ->get_element(0); |
65 | 0 | } |
66 | 79 | } else { |
67 | 79 | _cur_size = assert_cast<const ColumnInt32*>(column_nested.get())->get_element(0); |
68 | 79 | } |
69 | | |
70 | 80 | ((ColumnInt32*)_elements_column.get())->clear(); |
71 | | //_cur_size may be a negative number |
72 | 80 | _cur_size = std::max(static_cast<int64_t>(0L), _cur_size); |
73 | 80 | if (_cur_size && |
74 | 80 | _cur_size <= state->batch_size()) { // avoid elements_column too big or empty |
75 | 70 | _is_const = true; // use const optimize |
76 | 33.5k | for (int i = 0; i < _cur_size; i++) { |
77 | 33.4k | ((ColumnInt32*)_elements_column.get())->insert_value(i); |
78 | 33.4k | } |
79 | 70 | } |
80 | 80 | } |
81 | 109 | return Status::OK(); |
82 | 109 | } |
83 | | |
84 | 228 | void VExplodeNumbersTableFunction::process_row(size_t row_idx) { |
85 | 228 | TableFunction::process_row(row_idx); |
86 | 228 | if (_is_const) { |
87 | 140 | return; |
88 | 140 | } |
89 | | |
90 | 88 | StringRef value = _value_column->get_data_at(row_idx); |
91 | 88 | if (value.data != nullptr) { |
92 | 84 | _cur_size = std::max(0, *reinterpret_cast<const int*>(value.data)); |
93 | 84 | } |
94 | 88 | } |
95 | | |
96 | 108 | void VExplodeNumbersTableFunction::process_close() { |
97 | 108 | _value_column = nullptr; |
98 | 108 | } |
99 | | |
100 | 43 | void VExplodeNumbersTableFunction::get_same_many_values(MutableColumnPtr& column, int length) { |
101 | 43 | if (current_empty()) { |
102 | 0 | column->insert_many_defaults(length); |
103 | 43 | } else { |
104 | | // for explode numbers, the argument is int32. so cast is safe. |
105 | 43 | if (_is_nullable) { |
106 | 43 | assert_cast<ColumnInt32*>( |
107 | 43 | assert_cast<ColumnNullable*>(column.get())->get_nested_column_ptr().get()) |
108 | 43 | ->insert_many_vals(static_cast<int32_t>(_cur_offset), length); |
109 | 43 | assert_cast<ColumnUInt8*>( |
110 | 43 | assert_cast<ColumnNullable*>(column.get())->get_null_map_column_ptr().get()) |
111 | 43 | ->insert_many_defaults(length); |
112 | 43 | } else { |
113 | 0 | assert_cast<ColumnInt32*>(column.get()) |
114 | 0 | ->insert_many_vals(static_cast<int32_t>(_cur_offset), length); |
115 | 0 | } |
116 | 43 | } |
117 | 43 | } |
118 | | |
119 | | #include "common/compile_check_end.h" |
120 | | } // namespace doris |