be/src/exprs/table_function/vexplode.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.h" |
19 | | |
20 | | #include <glog/logging.h> |
21 | | |
22 | | #include <ostream> |
23 | | |
24 | | #include "common/status.h" |
25 | | #include "core/block/block.h" |
26 | | #include "core/block/column_with_type_and_name.h" |
27 | | #include "core/column/column.h" |
28 | | #include "core/column/column_array.h" |
29 | | #include "core/column/column_nothing.h" |
30 | | #include "core/column/column_variant.h" |
31 | | #include "core/data_type/data_type.h" |
32 | | #include "core/data_type/data_type_array.h" |
33 | | #include "core/data_type/data_type_nothing.h" |
34 | | #include "exprs/function/function_helpers.h" |
35 | | #include "exprs/vexpr.h" |
36 | | #include "exprs/vexpr_context.h" |
37 | | |
38 | | namespace doris { |
39 | | #include "common/compile_check_begin.h" |
40 | | |
41 | 2 | VExplodeTableFunction::VExplodeTableFunction() { |
42 | 2 | _fn_name = "vexplode"; |
43 | 2 | } |
44 | | |
45 | 0 | Status VExplodeTableFunction::_process_init_variant(Block* block, int value_column_idx) { |
46 | | // explode variant array |
47 | 0 | auto column_without_nullable = remove_nullable(block->get_by_position(value_column_idx).column); |
48 | 0 | auto column = column_without_nullable->convert_to_full_column_if_const(); |
49 | 0 | auto& variant_column = assert_cast<ColumnVariant&>(*(column->assume_mutable())); |
50 | 0 | variant_column.finalize(); |
51 | 0 | _detail.output_as_variant = true; |
52 | 0 | if (!variant_column.is_null_root()) { |
53 | 0 | _array_column = variant_column.get_root(); |
54 | | // We need to wrap the output nested column within a variant column. |
55 | | // Otherwise the type is missmatched |
56 | 0 | const auto* array_type = check_and_get_data_type<DataTypeArray>( |
57 | 0 | remove_nullable(variant_column.get_root_type()).get()); |
58 | 0 | if (array_type == nullptr) { |
59 | 0 | return Status::NotSupported("explode not support none array type {}", |
60 | 0 | variant_column.get_root_type()->get_name()); |
61 | 0 | } |
62 | 0 | _detail.nested_type = array_type->get_nested_type(); |
63 | 0 | } else { |
64 | | // null root, use nothing type |
65 | 0 | _array_column = ColumnNullable::create(ColumnArray::create(ColumnNothing::create(0)), |
66 | 0 | ColumnUInt8::create(0)); |
67 | 0 | _array_column->assume_mutable()->insert_many_defaults(variant_column.size()); |
68 | 0 | _detail.nested_type = std::make_shared<DataTypeNothing>(); |
69 | 0 | } |
70 | 0 | return Status::OK(); |
71 | 0 | } |
72 | | |
73 | 5 | Status VExplodeTableFunction::process_init(Block* block, RuntimeState* state) { |
74 | 5 | CHECK(_expr_context->root()->children().size() == 1) |
75 | 0 | << "VExplodeTableFunction only support 1 child but has " |
76 | 0 | << _expr_context->root()->children().size(); |
77 | | |
78 | 5 | int value_column_idx = -1; |
79 | 5 | RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute(_expr_context.get(), block, |
80 | 5 | &value_column_idx)); |
81 | 5 | if (block->get_by_position(value_column_idx).type->get_primitive_type() == TYPE_VARIANT) { |
82 | 0 | RETURN_IF_ERROR(_process_init_variant(block, value_column_idx)); |
83 | 5 | } else { |
84 | 5 | _array_column = |
85 | 5 | block->get_by_position(value_column_idx).column->convert_to_full_column_if_const(); |
86 | 5 | } |
87 | 5 | if (!extract_column_array_info(*_array_column, _detail)) { |
88 | 0 | return Status::NotSupported("column type {} not supported now", |
89 | 0 | block->get_by_position(value_column_idx).column->get_name()); |
90 | 0 | } |
91 | | |
92 | 5 | return Status::OK(); |
93 | 5 | } |
94 | | |
95 | 15 | void VExplodeTableFunction::process_row(size_t row_idx) { |
96 | 15 | DCHECK(row_idx < _array_column->size()); |
97 | 15 | TableFunction::process_row(row_idx); |
98 | | |
99 | 15 | if (!_detail.array_nullmap_data || !_detail.array_nullmap_data[row_idx]) { |
100 | 10 | _array_offset = (*_detail.offsets_ptr)[row_idx - 1]; |
101 | 10 | _cur_size = (*_detail.offsets_ptr)[row_idx] - _array_offset; |
102 | 10 | } |
103 | 15 | } |
104 | | |
105 | 0 | void VExplodeTableFunction::process_close() { |
106 | 0 | _array_column = nullptr; |
107 | 0 | _detail.reset(); |
108 | 0 | _array_offset = 0; |
109 | 0 | } |
110 | | |
111 | 22 | void VExplodeTableFunction::get_same_many_values(MutableColumnPtr& column, int length) { |
112 | 22 | size_t pos = _array_offset + _cur_offset; |
113 | 22 | if (current_empty() || (_detail.nested_nullmap_data && _detail.nested_nullmap_data[pos])) { |
114 | 8 | column->insert_many_defaults(length); |
115 | 14 | } else { |
116 | 14 | if (_is_nullable) { |
117 | 14 | assert_cast<ColumnNullable*>(column.get()) |
118 | 14 | ->get_nested_column_ptr() |
119 | 14 | ->insert_many_from(*_detail.nested_col, pos, length); |
120 | 14 | assert_cast<ColumnUInt8*>( |
121 | 14 | assert_cast<ColumnNullable*>(column.get())->get_null_map_column_ptr().get()) |
122 | 14 | ->insert_many_defaults(length); |
123 | 14 | } else { |
124 | 0 | column->insert_many_from(*_detail.nested_col, pos, length); |
125 | 0 | } |
126 | 14 | } |
127 | 22 | } |
128 | | |
129 | 0 | int VExplodeTableFunction::get_value(MutableColumnPtr& column, int max_step) { |
130 | 0 | max_step = std::min(max_step, (int)(_cur_size - _cur_offset)); |
131 | 0 | size_t pos = _array_offset + _cur_offset; |
132 | 0 | if (current_empty()) { |
133 | 0 | column->insert_default(); |
134 | 0 | max_step = 1; |
135 | 0 | } else { |
136 | 0 | if (_is_nullable) { |
137 | 0 | auto* nullable_column = assert_cast<ColumnNullable*>(column.get()); |
138 | 0 | auto nested_column = nullable_column->get_nested_column_ptr(); |
139 | 0 | auto* nullmap_column = |
140 | 0 | assert_cast<ColumnUInt8*>(nullable_column->get_null_map_column_ptr().get()); |
141 | 0 | nested_column->insert_range_from(*_detail.nested_col, pos, max_step); |
142 | 0 | size_t old_size = nullmap_column->size(); |
143 | 0 | nullmap_column->resize(old_size + max_step); |
144 | 0 | memcpy(nullmap_column->get_data().data() + old_size, |
145 | 0 | _detail.nested_nullmap_data + pos * sizeof(UInt8), max_step * sizeof(UInt8)); |
146 | 0 | } else { |
147 | 0 | column->insert_range_from(*_detail.nested_col, pos, max_step); |
148 | 0 | } |
149 | 0 | } |
150 | 0 | forward(max_step); |
151 | 0 | return max_step; |
152 | 0 | } |
153 | | |
154 | | #include "common/compile_check_end.h" |
155 | | } // namespace doris |