be/src/exprs/table_function/vexplode_map.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_map.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 "exprs/vexpr.h" |
29 | | #include "exprs/vexpr_context.h" |
30 | | |
31 | | namespace doris { |
32 | | #include "common/compile_check_begin.h" |
33 | | |
34 | 133 | VExplodeMapTableFunction::VExplodeMapTableFunction() { |
35 | 133 | _fn_name = "vexplode_map"; |
36 | 133 | } |
37 | | |
38 | 43 | bool extract_column_map_info(const IColumn& src, ColumnMapExecutionData& data) { |
39 | 43 | const IColumn* map_col = &src; |
40 | | // extract array nullable info |
41 | 43 | if (src.is_nullable()) { |
42 | 25 | const auto& null_col = reinterpret_cast<const ColumnNullable&>(src); |
43 | | // map column's nullmap |
44 | 25 | data.map_nullmap_data = null_col.get_null_map_data().data(); |
45 | 25 | map_col = null_col.get_nested_column_ptr().get(); |
46 | 25 | } |
47 | | |
48 | 43 | if (data.map_col = check_and_get_column<ColumnMap>(map_col); !data.map_col) { |
49 | 0 | return false; |
50 | 0 | } |
51 | | |
52 | 43 | data.offsets_ptr = &data.map_col->get_offsets(); |
53 | 43 | return true; |
54 | 43 | } |
55 | | |
56 | 43 | Status VExplodeMapTableFunction::process_init(Block* block, RuntimeState* state) { |
57 | 43 | CHECK(_expr_context->root()->children().size() == 1) |
58 | 0 | << "VExplodeMapTableFunction only support 1 child but has " |
59 | 0 | << _expr_context->root()->children().size(); |
60 | | |
61 | 43 | int value_column_idx = -1; |
62 | 43 | RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute(_expr_context.get(), block, |
63 | 43 | &value_column_idx)); |
64 | | |
65 | 43 | _collection_column = |
66 | 43 | block->get_by_position(value_column_idx).column->convert_to_full_column_if_const(); |
67 | | |
68 | 43 | if (!extract_column_map_info(*_collection_column, _map_detail)) { |
69 | 0 | return Status::NotSupported("column type {} not supported now, only support array or map", |
70 | 0 | block->get_by_position(value_column_idx).column->get_name()); |
71 | 0 | } |
72 | | |
73 | 43 | return Status::OK(); |
74 | 43 | } |
75 | | |
76 | 173 | void VExplodeMapTableFunction::process_row(size_t row_idx) { |
77 | 173 | DCHECK(row_idx < _collection_column->size()); |
78 | 173 | TableFunction::process_row(row_idx); |
79 | | |
80 | 173 | if (!_map_detail.map_nullmap_data || !_map_detail.map_nullmap_data[row_idx]) { |
81 | 161 | _collection_offset = (*_map_detail.offsets_ptr)[row_idx - 1]; |
82 | 161 | _cur_size = (*_map_detail.offsets_ptr)[row_idx] - _collection_offset; |
83 | 161 | } |
84 | 173 | } |
85 | | |
86 | 43 | void VExplodeMapTableFunction::process_close() { |
87 | 43 | _collection_column = nullptr; |
88 | 43 | _map_detail.reset(); |
89 | 43 | _collection_offset = 0; |
90 | 43 | } |
91 | | |
92 | 0 | void VExplodeMapTableFunction::get_same_many_values(MutableColumnPtr& column, int length) { |
93 | | // now we only support map column explode to struct column |
94 | 0 | size_t pos = _collection_offset + _cur_offset; |
95 | | // if current is empty map row, also append a default value |
96 | 0 | if (current_empty()) { |
97 | 0 | column->insert_many_defaults(length); |
98 | 0 | return; |
99 | 0 | } |
100 | 0 | ColumnStruct* ret = nullptr; |
101 | | // this _is_nullable is whole output column's nullable |
102 | 0 | if (_is_nullable) { |
103 | | // make map kv value into struct |
104 | 0 | ret = assert_cast<ColumnStruct*>( |
105 | 0 | assert_cast<ColumnNullable*>(column.get())->get_nested_column_ptr().get()); |
106 | 0 | assert_cast<ColumnUInt8*>( |
107 | 0 | assert_cast<ColumnNullable*>(column.get())->get_null_map_column_ptr().get()) |
108 | 0 | ->insert_many_defaults(length); |
109 | 0 | } else if (is_column<ColumnStruct>(column.get())) { |
110 | 0 | ret = assert_cast<ColumnStruct*>(column.get()); |
111 | 0 | } else { |
112 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, |
113 | 0 | "only support map column explode to struct column"); |
114 | 0 | } |
115 | 0 | if (!ret || ret->tuple_size() != 2) { |
116 | 0 | throw Exception( |
117 | 0 | ErrorCode::INTERNAL_ERROR, |
118 | 0 | "only support map column explode to two column, but given: ", ret->tuple_size()); |
119 | 0 | } |
120 | 0 | ret->get_column(0).insert_many_from(_map_detail.map_col->get_keys(), pos, length); |
121 | 0 | ret->get_column(1).insert_many_from(_map_detail.map_col->get_values(), pos, length); |
122 | 0 | } |
123 | | |
124 | 161 | int VExplodeMapTableFunction::get_value(MutableColumnPtr& column, int max_step) { |
125 | 161 | max_step = std::min(max_step, (int)(_cur_size - _cur_offset)); |
126 | 161 | size_t pos = _collection_offset + _cur_offset; |
127 | 161 | if (current_empty()) { |
128 | 5 | column->insert_default(); |
129 | 5 | max_step = 1; |
130 | 156 | } else { |
131 | 156 | ColumnStruct* struct_column = nullptr; |
132 | 156 | if (_is_nullable) { |
133 | 156 | auto* nullable_column = assert_cast<ColumnNullable*>(column.get()); |
134 | 156 | struct_column = |
135 | 156 | assert_cast<ColumnStruct*>(nullable_column->get_nested_column_ptr().get()); |
136 | 156 | auto* nullmap_column = |
137 | 156 | assert_cast<ColumnUInt8*>(nullable_column->get_null_map_column_ptr().get()); |
138 | | // here nullmap_column insert max_step many defaults as if MAP[row_idx] is NULL |
139 | | // will be not update value, _cur_size = 0, means current_empty; |
140 | | // so here could insert directly |
141 | 156 | nullmap_column->insert_many_defaults(max_step); |
142 | 156 | } else { |
143 | 0 | struct_column = assert_cast<ColumnStruct*>(column.get()); |
144 | 0 | } |
145 | 156 | if (!struct_column || struct_column->tuple_size() != 2) { |
146 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, |
147 | 0 | "only support map column explode to two column, but given: ", |
148 | 0 | struct_column->tuple_size()); |
149 | 0 | } |
150 | 156 | struct_column->get_column(0).insert_range_from(_map_detail.map_col->get_keys(), pos, |
151 | 156 | max_step); |
152 | 156 | struct_column->get_column(1).insert_range_from(_map_detail.map_col->get_values(), pos, |
153 | 156 | max_step); |
154 | 156 | } |
155 | 161 | forward(max_step); |
156 | 161 | return max_step; |
157 | 161 | } |
158 | | |
159 | | #include "common/compile_check_end.h" |
160 | | } // namespace doris |