forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvarray_match_function.cpp
More file actions
140 lines (121 loc) · 6.19 KB
/
Copy pathvarray_match_function.cpp
File metadata and controls
140 lines (121 loc) · 6.19 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
// 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 <memory>
#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_number.h" // IWYU pragma: keep
#include "exprs/aggregate/aggregate_function.h"
#include "exprs/function/simple_function_factory.h"
namespace doris {
///* bool array_match_all/any(array<boolean>) *///
template <bool MATCH_ALL>
class ArrayMatchFunction : public IFunction {
public:
static constexpr auto name = MATCH_ALL ? "array_match_all" : "array_match_any";
static FunctionPtr create() { return std::make_shared<ArrayMatchFunction>(); }
std::string get_name() const override { return name; }
bool is_variadic() const override { return false; }
size_t get_number_of_arguments() const override { return 1; }
bool is_use_default_implementation_for_constants() const override { return false; }
bool use_default_implementation_for_nulls() const override { return false; }
DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
return make_nullable(std::make_shared<DataTypeUInt8>());
}
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
// here is executed by array_map filtered and arg[0] is bool result column
const auto& [src_column, src_const] =
unpack_if_const(block.get_by_position(arguments[0]).column);
const ColumnArray* array_column = nullptr;
const UInt8* array_null_map = nullptr;
if (const auto* nullable_array = check_and_get_column<ColumnNullable>(src_column.get())) {
array_column = assert_cast<const ColumnArray*>(&nullable_array->get_nested_column());
array_null_map = nullable_array->get_null_map_column().get_data().data();
} else {
array_column = assert_cast<const ColumnArray*>(src_column.get());
}
if (!array_column) {
return Status::RuntimeError("unsupported types for function {}({})", get_name(),
block.get_by_position(arguments[0]).type->get_name());
}
const auto& offsets = array_column->get_offsets();
ColumnPtr nested_column = nullptr;
const UInt8* nested_null_map = nullptr;
if (const auto* nested_null_column =
check_and_get_column<ColumnNullable>(&array_column->get_data())) {
nested_null_map = nested_null_column->get_null_map_column().get_data().data();
nested_column = nested_null_column->get_nested_column_ptr();
} else {
nested_column = array_column->get_data_ptr();
}
if (!nested_column) {
return Status::RuntimeError("unsupported types for function {}({})", get_name(),
block.get_by_position(arguments[0]).type->get_name());
}
const auto& nested_data = assert_cast<const ColumnUInt8&>(*nested_column).get_data();
// result is nullable bool column for every array column
auto result_data_column = ColumnUInt8::create(input_rows_count, 1);
auto result_null_column = ColumnUInt8::create(input_rows_count, 0);
// iterate over all arrays with bool elements
for (int row = 0; row < input_rows_count; ++row) {
if (array_null_map && array_null_map[row]) {
// current array is null, this is always null
result_null_column->get_data()[row] = 1;
result_data_column->get_data()[row] = 0;
} else {
// we should calculate the bool result for current array
// has_null in current array
bool has_null_elem = false;
// res for current array
bool res_for_array = MATCH_ALL;
for (auto off = offsets[row - 1]; off < offsets[row]; ++off) {
if (nested_null_map && nested_null_map[off]) {
has_null_elem = true;
} else {
if (nested_data[off] != MATCH_ALL) { // not match
res_for_array = !MATCH_ALL;
break;
} // default is MATCH_ALL
}
}
result_null_column->get_data()[row] = has_null_elem && res_for_array == MATCH_ALL;
result_data_column->get_data()[row] = res_for_array;
}
}
// insert the result column to block
DCHECK(block.get_by_position(result).type->is_nullable());
ColumnPtr dst_column = ColumnNullable::create(std::move(result_data_column),
std::move(result_null_column));
block.replace_by_position(result, std::move(dst_column));
return Status::OK();
}
};
void register_function_array_match(SimpleFunctionFactory& factory) {
factory.register_function<ArrayMatchFunction<true>>(); // MATCH_ALL = true means array_match_all
factory.register_function<
ArrayMatchFunction<false>>(); // MATCH_ALL = false means array_match_any
}
} // namespace doris