forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_array_sortby.cpp
More file actions
177 lines (154 loc) · 7.69 KB
/
Copy pathfunction_array_sortby.cpp
File metadata and controls
177 lines (154 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <glog/logging.h>
#include <stddef.h>
#include <algorithm>
#include <memory>
#include <ostream>
#include <string>
#include <utility>
#include "common/status.h"
#include "core/assert_cast.h"
#include "core/block/block.h"
#include "core/block/column_numbers.h"
#include "core/block/column_with_type_and_name.h"
#include "core/column/column.h"
#include "core/column/column_array.h"
#include "core/column/column_nullable.h"
#include "core/column/column_vector.h"
#include "core/data_type/data_type.h"
#include "core/data_type/data_type_nullable.h"
#include "core/types.h"
#include "exprs/aggregate/aggregate_function.h"
#include "exprs/function/function.h"
#include "exprs/function/simple_function_factory.h"
namespace doris {
class FunctionContext;
} // namespace doris
namespace doris {
class FunctionArraySortBy : public IFunction {
public:
static constexpr auto name = "array_sortby";
static FunctionPtr create() { return std::make_shared<FunctionArraySortBy>(); }
String get_name() const override { return name; }
size_t get_number_of_arguments() const override { return 2; }
bool use_default_implementation_for_nulls() const override { return false; }
DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
DCHECK(arguments[0]->get_primitive_type() == TYPE_ARRAY)
<< "first argument for function: " << name << " should be DataTypeArray"
<< " and arguments[0] is " << arguments[0]->get_name();
return arguments[0];
}
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
ColumnPtr argument_columns[2] = {nullptr, nullptr};
ColumnPtr argument_nullmap[2] = {nullptr, nullptr};
for (int i = 0; i < 2; ++i) {
argument_columns[i] =
block.get_by_position(arguments[i]).column->convert_to_full_column_if_const();
if (auto* nullable = check_and_get_column<ColumnNullable>(*argument_columns[i])) {
argument_nullmap[i] = nullable->get_null_map_column_ptr();
argument_columns[i] = nullable->get_nested_column_ptr();
}
}
const auto& src_column_array = assert_cast<const ColumnArray&>(*argument_columns[0]);
const auto& key_column_array = assert_cast<const ColumnArray&>(*argument_columns[1]);
const auto& src_offsets = src_column_array.get_offsets();
const ColumnNullable& src_nested_nullable_column =
assert_cast<const ColumnNullable&>(src_column_array.get_data());
auto& src_nested_data_column = src_nested_nullable_column.get_nested_column();
const auto& key_offsets = key_column_array.get_offsets();
const ColumnNullable& key_nested_nullable_column =
assert_cast<const ColumnNullable&>(key_column_array.get_data());
auto result_data_column = src_nested_nullable_column.clone_empty();
ColumnPtr result_offset_column = src_column_array.get_offsets_ptr();
ColumnPtr result_nullmap = nullptr;
const ColumnUInt8::Container* src_null_map_data = nullptr;
if (argument_nullmap[0]) {
const auto& src_column_nullmap = assert_cast<const ColumnUInt8&>(*argument_nullmap[0]);
result_nullmap = argument_nullmap[0];
src_null_map_data = &(src_column_nullmap.get_data());
}
const ColumnUInt8::Container* key_null_map_data = nullptr;
if (argument_nullmap[1]) {
const auto& key_column_nullmap = assert_cast<const ColumnUInt8&>(*argument_nullmap[1]);
key_null_map_data = &(key_column_nullmap.get_data());
}
IColumn::Selector src_selector;
src_selector.reserve(src_nested_data_column.size());
IColumn::Permutation key_permutation(key_nested_nullable_column.size());
for (size_t i = 0; i < key_nested_nullable_column.size(); ++i) {
key_permutation[i] = i;
}
unsigned long null_step = 0;
for (int row = 0; row < input_rows_count; ++row) {
unsigned long cur_src_element_num = src_offsets[row] - src_offsets[row - 1];
unsigned long cur_key_element_num = key_offsets[row] - key_offsets[row - 1];
if (cur_src_element_num != cur_key_element_num) {
if (key_null_map_data != nullptr && (*key_null_map_data)[row]) {
// deal with this case if one of row like: ([1,2,3], NULL) --->([1,2,3])
for (unsigned long pos = src_offsets[row - 1]; pos < src_offsets[row]; ++pos) {
src_selector.push_back(pos);
}
null_step = null_step + cur_src_element_num;
continue;
} else if (src_null_map_data != nullptr && (*src_null_map_data)[row]) {
// deal with this case if one of row like: (NULL, [1,2,3]) --->(NULL)
null_step = null_step - cur_key_element_num;
continue;
} else {
return Status::InternalError(
"in array sortby function, the input column nested column data rows "
"are not equal, the first size is {}, but with second size is {} at "
"row {}.",
src_offsets[row] - src_offsets[row - 1],
key_offsets[row] - key_offsets[row - 1], row);
}
}
auto start = key_offsets[row - 1];
auto end = key_offsets[row];
std::sort(&key_permutation[start], &key_permutation[end],
Less(key_nested_nullable_column));
for (unsigned long pos = start; pos < end; ++pos) {
src_selector.push_back(key_permutation[pos] + null_step);
}
}
src_nested_nullable_column.append_data_by_selector(result_data_column, src_selector);
if (result_nullmap) {
block.replace_by_position(
result,
ColumnNullable::create(ColumnArray::create(std::move(result_data_column),
result_offset_column),
result_nullmap));
} else {
block.replace_by_position(result, ColumnArray::create(std::move(result_data_column),
result_offset_column));
}
return Status::OK();
}
struct Less {
const IColumn& column;
explicit Less(const IColumn& column_) : column(column_) {}
bool operator()(size_t lhs, size_t rhs) const {
return column.compare_at(lhs, rhs, column, -1) < 0;
}
};
};
void register_function_array_sortby(SimpleFunctionFactory& factory) {
factory.register_function<FunctionArraySortBy>();
}
} // namespace doris