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