be/src/exprs/function/array/varray_match_function.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 <memory> |
19 | | #include <string> |
20 | | #include <utility> |
21 | | |
22 | | #include "common/status.h" |
23 | | #include "core/assert_cast.h" |
24 | | #include "core/block/block.h" |
25 | | #include "core/block/column_numbers.h" |
26 | | #include "core/block/column_with_type_and_name.h" |
27 | | #include "core/column/column.h" |
28 | | #include "core/column/column_array.h" |
29 | | #include "core/column/column_nullable.h" |
30 | | #include "core/column/column_vector.h" |
31 | | #include "core/data_type/data_type_number.h" // IWYU pragma: keep |
32 | | #include "exprs/aggregate/aggregate_function.h" |
33 | | #include "exprs/function/simple_function_factory.h" |
34 | | |
35 | | namespace doris { |
36 | | |
37 | | ///* bool array_match_all/any(array<boolean>) */// |
38 | | template <bool MATCH_ALL> |
39 | | class ArrayMatchFunction : public IFunction { |
40 | | public: |
41 | | static constexpr auto name = MATCH_ALL ? "array_match_all" : "array_match_any"; |
42 | 4 | static FunctionPtr create() { return std::make_shared<ArrayMatchFunction>(); }_ZN5doris18ArrayMatchFunctionILb1EE6createEv Line | Count | Source | 42 | 2 | static FunctionPtr create() { return std::make_shared<ArrayMatchFunction>(); } |
_ZN5doris18ArrayMatchFunctionILb0EE6createEv Line | Count | Source | 42 | 2 | static FunctionPtr create() { return std::make_shared<ArrayMatchFunction>(); } |
|
43 | | |
44 | 2 | std::string get_name() const override { return name; }_ZNK5doris18ArrayMatchFunctionILb1EE8get_nameB5cxx11Ev Line | Count | Source | 44 | 1 | std::string get_name() const override { return name; } |
_ZNK5doris18ArrayMatchFunctionILb0EE8get_nameB5cxx11Ev Line | Count | Source | 44 | 1 | std::string get_name() const override { return name; } |
|
45 | | |
46 | 2 | bool is_variadic() const override { return false; }_ZNK5doris18ArrayMatchFunctionILb1EE11is_variadicEv Line | Count | Source | 46 | 1 | bool is_variadic() const override { return false; } |
_ZNK5doris18ArrayMatchFunctionILb0EE11is_variadicEv Line | Count | Source | 46 | 1 | bool is_variadic() const override { return false; } |
|
47 | | |
48 | 0 | size_t get_number_of_arguments() const override { return 1; }Unexecuted instantiation: _ZNK5doris18ArrayMatchFunctionILb1EE23get_number_of_argumentsEv Unexecuted instantiation: _ZNK5doris18ArrayMatchFunctionILb0EE23get_number_of_argumentsEv |
49 | | |
50 | 0 | bool is_use_default_implementation_for_constants() const override { return false; }Unexecuted instantiation: _ZNK5doris18ArrayMatchFunctionILb1EE43is_use_default_implementation_for_constantsEv Unexecuted instantiation: _ZNK5doris18ArrayMatchFunctionILb0EE43is_use_default_implementation_for_constantsEv |
51 | | |
52 | 0 | bool use_default_implementation_for_nulls() const override { return false; }Unexecuted instantiation: _ZNK5doris18ArrayMatchFunctionILb1EE36use_default_implementation_for_nullsEv Unexecuted instantiation: _ZNK5doris18ArrayMatchFunctionILb0EE36use_default_implementation_for_nullsEv |
53 | | |
54 | 0 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
55 | 0 | return make_nullable(std::make_shared<DataTypeUInt8>()); |
56 | 0 | } Unexecuted instantiation: _ZNK5doris18ArrayMatchFunctionILb1EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS6_EE Unexecuted instantiation: _ZNK5doris18ArrayMatchFunctionILb0EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS6_EE |
57 | | |
58 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
59 | 0 | uint32_t result, size_t input_rows_count) const override { |
60 | | // here is executed by array_map filtered and arg[0] is bool result column |
61 | 0 | const auto& [src_column, src_const] = |
62 | 0 | unpack_if_const(block.get_by_position(arguments[0]).column); |
63 | 0 | const ColumnArray* array_column = nullptr; |
64 | 0 | const UInt8* array_null_map = nullptr; |
65 | 0 | if (src_column->is_nullable()) { |
66 | 0 | auto nullable_array = assert_cast<const ColumnNullable*>(src_column.get()); |
67 | 0 | array_column = assert_cast<const ColumnArray*>(&nullable_array->get_nested_column()); |
68 | 0 | array_null_map = nullable_array->get_null_map_column().get_data().data(); |
69 | 0 | } else { |
70 | 0 | array_column = assert_cast<const ColumnArray*>(src_column.get()); |
71 | 0 | } |
72 | |
|
73 | 0 | if (!array_column) { |
74 | 0 | return Status::RuntimeError("unsupported types for function {}({})", get_name(), |
75 | 0 | block.get_by_position(arguments[0]).type->get_name()); |
76 | 0 | } |
77 | | |
78 | 0 | const auto& offsets = array_column->get_offsets(); |
79 | 0 | ColumnPtr nested_column = nullptr; |
80 | 0 | const UInt8* nested_null_map = nullptr; |
81 | 0 | if (array_column->get_data().is_nullable()) { |
82 | 0 | const auto& nested_null_column = |
83 | 0 | assert_cast<const ColumnNullable&>(array_column->get_data()); |
84 | 0 | nested_null_map = nested_null_column.get_null_map_column().get_data().data(); |
85 | 0 | nested_column = nested_null_column.get_nested_column_ptr(); |
86 | 0 | } else { |
87 | 0 | nested_column = array_column->get_data_ptr(); |
88 | 0 | } |
89 | |
|
90 | 0 | if (!nested_column) { |
91 | 0 | return Status::RuntimeError("unsupported types for function {}({})", get_name(), |
92 | 0 | block.get_by_position(arguments[0]).type->get_name()); |
93 | 0 | } |
94 | | |
95 | 0 | const auto& nested_data = assert_cast<const ColumnUInt8&>(*nested_column).get_data(); |
96 | | |
97 | | // result is nullable bool column for every array column |
98 | 0 | auto result_data_column = ColumnUInt8::create(input_rows_count, 1); |
99 | 0 | auto result_null_column = ColumnUInt8::create(input_rows_count, 0); |
100 | | |
101 | | // iterate over all arrays with bool elements |
102 | 0 | for (int row = 0; row < input_rows_count; ++row) { |
103 | 0 | if (array_null_map && array_null_map[row]) { |
104 | | // current array is null, this is always null |
105 | 0 | result_null_column->get_data()[row] = 1; |
106 | 0 | result_data_column->get_data()[row] = 0; |
107 | 0 | } else { |
108 | | // we should calculate the bool result for current array |
109 | | // has_null in current array |
110 | 0 | bool has_null_elem = false; |
111 | | // res for current array |
112 | 0 | bool res_for_array = MATCH_ALL; |
113 | 0 | for (auto off = offsets[row - 1]; off < offsets[row]; ++off) { |
114 | 0 | if (nested_null_map && nested_null_map[off]) { |
115 | 0 | has_null_elem = true; |
116 | 0 | } else { |
117 | 0 | if (nested_data[off] != MATCH_ALL) { // not match |
118 | 0 | res_for_array = !MATCH_ALL; |
119 | 0 | break; |
120 | 0 | } // default is MATCH_ALL |
121 | 0 | } |
122 | 0 | } |
123 | 0 | result_null_column->get_data()[row] = has_null_elem && res_for_array == MATCH_ALL; |
124 | 0 | result_data_column->get_data()[row] = res_for_array; |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | | // insert the result column to block |
129 | 0 | DCHECK(block.get_by_position(result).type->is_nullable()); |
130 | 0 | ColumnPtr dst_column = ColumnNullable::create(std::move(result_data_column), |
131 | 0 | std::move(result_null_column)); |
132 | 0 | block.replace_by_position(result, std::move(dst_column)); |
133 | 0 | return Status::OK(); |
134 | 0 | } Unexecuted instantiation: _ZNK5doris18ArrayMatchFunctionILb1EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm Unexecuted instantiation: _ZNK5doris18ArrayMatchFunctionILb0EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm |
135 | | }; |
136 | | |
137 | 1 | void register_function_array_match(SimpleFunctionFactory& factory) { |
138 | 1 | factory.register_function<ArrayMatchFunction<true>>(); // MATCH_ALL = true means array_match_all |
139 | 1 | factory.register_function< |
140 | 1 | ArrayMatchFunction<false>>(); // MATCH_ALL = false means array_match_any |
141 | 1 | } |
142 | | } // namespace doris |