be/src/exprs/function/array/function_array_utils.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/function/array/function_array_utils.h" |
19 | | |
20 | | #include <stddef.h> |
21 | | |
22 | | #include <algorithm> |
23 | | #include <utility> |
24 | | |
25 | | #include "core/column/column.h" |
26 | | #include "core/column/column_nullable.h" |
27 | | #include "core/column/column_variant.h" |
28 | | #include "core/column/column_vector.h" |
29 | | #include "core/data_type/data_type.h" |
30 | | #include "core/data_type/data_type_nullable.h" |
31 | | |
32 | | namespace doris { |
33 | | |
34 | 38 | bool extract_column_array_info(const IColumn& src, ColumnArrayExecutionData& data) { |
35 | 38 | const IColumn* array_col = &src; |
36 | | // extract array nullable info |
37 | 38 | if (src.is_nullable()) { |
38 | 38 | const auto& null_col = reinterpret_cast<const ColumnNullable&>(src); |
39 | 38 | data.array_nullmap_data = null_col.get_null_map_data().data(); |
40 | 38 | array_col = null_col.get_nested_column_ptr().get(); |
41 | 38 | } |
42 | | |
43 | | // check and get array column |
44 | 38 | data.array_col = check_and_get_column<ColumnArray>(array_col); |
45 | 38 | if (!data.array_col) { |
46 | 0 | return false; |
47 | 0 | } |
48 | | |
49 | | // extract array offsets and nested column |
50 | 38 | data.offsets_ptr = &data.array_col->get_offsets(); |
51 | 38 | data.nested_col = data.array_col->get_data_ptr(); |
52 | | // extract nested column is nullable |
53 | 38 | if (data.nested_col->is_nullable()) { |
54 | 38 | const auto& nested_null_col = reinterpret_cast<const ColumnNullable&>(*data.nested_col); |
55 | 38 | data.nested_nullmap_data = nested_null_col.get_null_map_data().data(); |
56 | 38 | data.nested_col = nested_null_col.get_nested_column_ptr(); |
57 | 38 | } |
58 | 38 | if (data.output_as_variant && |
59 | 38 | data.nested_type->get_primitive_type() != PrimitiveType::TYPE_VARIANT) { |
60 | | // set variant root column/type to from column/type |
61 | 0 | auto variant = ColumnVariant::create(true /*always nullable*/); |
62 | 0 | auto nullable_nested_type = make_nullable(data.nested_type); |
63 | 0 | auto nullable_col = make_nullable(data.nested_col); |
64 | 0 | variant->create_root(nullable_nested_type, std::move(*nullable_col).mutate()); |
65 | 0 | data.nested_col = variant->get_ptr(); |
66 | 0 | } |
67 | 38 | return true; |
68 | 38 | } |
69 | | |
70 | 0 | ColumnArrayMutableData create_mutable_data(const IColumn* nested_col, bool is_nullable) { |
71 | 0 | ColumnArrayMutableData dst; |
72 | 0 | if (is_nullable) { |
73 | 0 | dst.array_nested_col = |
74 | 0 | ColumnNullable::create(nested_col->clone_empty(), ColumnUInt8::create()); |
75 | 0 | auto* nullable_col = reinterpret_cast<ColumnNullable*>(dst.array_nested_col.get()); |
76 | 0 | dst.nested_nullmap_data = &nullable_col->get_null_map_data(); |
77 | 0 | dst.nested_col = nullable_col->get_nested_column_ptr().get(); |
78 | 0 | } else { |
79 | 0 | dst.array_nested_col = nested_col->clone_empty(); |
80 | 0 | dst.nested_col = dst.array_nested_col.get(); |
81 | 0 | } |
82 | 0 | dst.offsets_col = ColumnArray::ColumnOffsets::create(); |
83 | 0 | dst.offsets_ptr = |
84 | 0 | &reinterpret_cast<ColumnArray::ColumnOffsets*>(dst.offsets_col.get())->get_data(); |
85 | 0 | return dst; |
86 | 0 | } |
87 | | |
88 | 0 | MutableColumnPtr assemble_column_array(ColumnArrayMutableData& data) { |
89 | 0 | return ColumnArray::create(std::move(data.array_nested_col), std::move(data.offsets_col)); |
90 | 0 | } |
91 | | |
92 | | void slice_array(ColumnArrayMutableData& dst, ColumnArrayExecutionData& src, |
93 | 0 | const IColumn& offset_column, const IColumn* length_column) { |
94 | 0 | size_t cur = 0; |
95 | 0 | for (size_t row = 0; row < src.offsets_ptr->size(); ++row) { |
96 | 0 | size_t off = (*src.offsets_ptr)[row - 1]; |
97 | 0 | size_t len = (*src.offsets_ptr)[row] - off; |
98 | 0 | Int64 start = offset_column.get_int(row); |
99 | 0 | if (len == 0 || start == 0) { |
100 | 0 | dst.offsets_ptr->push_back(cur); |
101 | 0 | continue; |
102 | 0 | } |
103 | 0 | if (start > 0 && start <= len) { |
104 | 0 | start += off - 1; |
105 | 0 | } else if (start < 0 && -start <= len) { |
106 | 0 | start += off + len; |
107 | 0 | } else { |
108 | 0 | dst.offsets_ptr->push_back(cur); |
109 | 0 | continue; |
110 | 0 | } |
111 | 0 | Int64 end; |
112 | 0 | if (length_column) { |
113 | 0 | Int64 size = length_column->get_int(row); |
114 | 0 | end = std::max((Int64)off, std::min((Int64)(off + len), start + size)); |
115 | 0 | } else { |
116 | 0 | end = off + len; |
117 | 0 | } |
118 | 0 | for (size_t pos = start; pos < end; ++pos) { |
119 | 0 | if (src.nested_nullmap_data && src.nested_nullmap_data[pos]) { |
120 | 0 | dst.nested_col->insert_default(); |
121 | 0 | dst.nested_nullmap_data->push_back(1); |
122 | 0 | } else { |
123 | 0 | dst.nested_col->insert_from(*src.nested_col, pos); |
124 | 0 | if (dst.nested_nullmap_data) { |
125 | 0 | dst.nested_nullmap_data->push_back(0); |
126 | 0 | } |
127 | 0 | } |
128 | 0 | } |
129 | 0 | if (start < end) { |
130 | 0 | cur += end - start; |
131 | 0 | } |
132 | 0 | dst.offsets_ptr->push_back(cur); |
133 | 0 | } |
134 | 0 | } |
135 | | |
136 | | } // namespace doris |