be/src/exprs/function/array/function_array_reverse.h
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 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/array/arrayReverse.cpp |
19 | | // and modified by Doris |
20 | | #pragma once |
21 | | |
22 | | #include "core/column/column_array.h" |
23 | | #include "core/column/column_const.h" |
24 | | #include "core/data_type/data_type_array.h" |
25 | | #include "core/data_type/data_type_number.h" |
26 | | #include "exprs/function/array/function_array_utils.h" |
27 | | |
28 | | namespace doris { |
29 | | |
30 | | struct ArrayReverseImpl { |
31 | | static Status _execute(Block& block, const ColumnNumbers& arguments, uint32_t result, |
32 | 0 | size_t input_rows_count) { |
33 | 0 | ColumnPtr src_column = |
34 | 0 | block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); |
35 | 0 | ColumnArrayExecutionData src; |
36 | 0 | if (!extract_column_array_info(*src_column, src)) { |
37 | 0 | return Status::RuntimeError( |
38 | 0 | fmt::format("execute failed, unsupported types for function {}({})", "reverse", |
39 | 0 | block.get_by_position(arguments[0]).type->get_name())); |
40 | 0 | } |
41 | | |
42 | 0 | bool is_nullable = src.nested_nullmap_data ? true : false; |
43 | 0 | ColumnArrayMutableData dst = create_mutable_data(src.nested_col.get(), is_nullable); |
44 | 0 | dst.offsets_ptr->reserve(input_rows_count); |
45 | |
|
46 | 0 | auto res_val = _execute_internal(*src.nested_col, *src.offsets_ptr, *dst.nested_col, |
47 | 0 | *dst.offsets_ptr, src.nested_nullmap_data, |
48 | 0 | dst.nested_nullmap_data); |
49 | 0 | if (!res_val) { |
50 | 0 | return Status::RuntimeError( |
51 | 0 | fmt::format("execute failed or unsupported types for function {}({})", |
52 | 0 | "reverse", block.get_by_position(arguments[0]).type->get_name())); |
53 | 0 | } |
54 | | |
55 | 0 | ColumnPtr res_column = assemble_column_array(dst); |
56 | 0 | block.replace_by_position(result, std::move(res_column)); |
57 | 0 | return Status::OK(); |
58 | 0 | } |
59 | | |
60 | | static bool _execute_internal(const IColumn& src_column, |
61 | | const ColumnArray::Offsets64& src_offsets, IColumn& dest_column, |
62 | | ColumnArray::Offsets64& dest_offsets, const UInt8* src_null_map, |
63 | 0 | ColumnUInt8::Container* dest_null_map) { |
64 | 0 | size_t prev_src_offset = 0; |
65 | |
|
66 | 0 | for (auto curr_src_offset : src_offsets) { |
67 | 0 | size_t array_size = curr_src_offset - prev_src_offset; |
68 | 0 | for (size_t j = 0; j < array_size; ++j) { |
69 | 0 | size_t j_reverse = curr_src_offset - j - 1; |
70 | 0 | if (src_null_map && src_null_map[j_reverse]) { |
71 | 0 | DCHECK(dest_null_map != nullptr); |
72 | | // Note: here we need to insert default value |
73 | 0 | dest_column.insert_default(); |
74 | 0 | (*dest_null_map).push_back(true); |
75 | 0 | continue; |
76 | 0 | } |
77 | | |
78 | 0 | dest_column.insert_from(src_column, j_reverse); |
79 | 0 | if (dest_null_map) { |
80 | 0 | (*dest_null_map).push_back(false); |
81 | 0 | } |
82 | 0 | } |
83 | |
|
84 | 0 | dest_offsets.push_back(curr_src_offset); |
85 | 0 | prev_src_offset = curr_src_offset; |
86 | 0 | } |
87 | |
|
88 | 0 | return true; |
89 | 0 | } |
90 | | }; |
91 | | |
92 | | } // namespace doris |