amorynan commented on code in PR #34501:
URL: https://github.com/apache/doris/pull/34501#discussion_r1593477018

##########
be/src/vec/functions/array/function_array_match_any.cpp:
##########
@@ -0,0 +1,292 @@
+// 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 <fmt/format.h>
+#include <glog/logging.h>
+#include <stddef.h>
+
+#include <memory>
+#include <ostream>
+#include <string>
+#include <utility>
+
+#include "common/status.h"
+#include "runtime/thread_context.h"
+#include "vec/columns/column.h"
+#include "vec/functions/array/function_array_index.h"
+//#include "vec/columns/column_array.h"
+//#include "vec/columns/column_const.h"
+//#include "vec/columns/column_nullable.h"
+//#include "vec/columns/column_vector.h"
+//#include "vec/columns/columns_number.h"
+//#include "vec/common/assert_cast.h"
+#include "vec/common/string_ref.h"
+#include "vec/core/block.h"
+#include "vec/core/column_numbers.h"
+//#include "vec/core/column_with_type_and_name.h"
+#include "vec/columns/column_map.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_array.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/data_types/data_type_number.h"
+#include "vec/functions/function.h"
+#include "vec/functions/function_helpers.h"
+#include "vec/functions/simple_function_factory.h"
+
+namespace doris {
+class FunctionContext;
+} // namespace doris
+
+namespace doris::vectorized {
+
+// array_match_any(columnArray, "a,b") -> true
+class FunctionArrayMatchAny : public IFunction {
+public:
+    static constexpr auto name = "array_match_any";
+
+    static FunctionPtr create() { return 
std::make_shared<FunctionArrayMatchAny>(); }
+
+    String get_name() const override { return name; }
+
+    bool is_variadic() const override { return true; }
+
+    size_t get_number_of_arguments() const override { return 3; }
+
+    // match functions only need to set 1/true
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        DCHECK(arguments.size() >= 2);
+        DCHECK(is_array(remove_nullable(arguments[0])))
+                << "First argument for function: " << name
+                << " should be DataTypeArray but it has type " << 
arguments[0]->get_name() << ".";
+        DCHECK(is_string(remove_nullable(arguments[1])))
+                << "Second argument for function: " << name << " should be 
String but it has type "
+                << arguments[1]->get_name() << ".";
+        const auto* array_type =
+                
check_and_get_data_type<DataTypeArray>(remove_nullable(arguments[0]).get());
+        DCHECK(is_string(remove_nullable(array_type->get_nested_type())))
+                << "Element type of array in function: " << name
+                << " should be String but it has type " << 
array_type->get_nested_type()->get_name()
+                << ".";
+        return std::make_shared<DataTypeUInt8>();
+    }
+
+    Status open(FunctionContext* context, FunctionContext::FunctionStateScope 
scope) override {
+        if (scope == FunctionContext::THREAD_LOCAL) {
+            return Status::OK();
+        }
+
+        DCHECK(context->get_num_args() >= 2);
+        DCHECK(context->get_arg_type(0)->is_array_type());
+        DCHECK(context->get_arg_type(1)->is_string_type());
+        std::shared_ptr<ParamValue> state = std::make_shared<ParamValue>();
+        Field field;
+        if (context->get_constant_col(1)) {
+            context->get_constant_col(1)->column_ptr->get(0, field);
+            state->value = field;
+            state->type = context->get_arg_type(1)->type;
+            context->set_function_state(scope, state);
+        }
+        return Status::OK();
+    }
+
+    Status eval_inverted_index(FunctionContext* context,
+                               const vectorized::NameAndTypePair& 
data_type_with_name,
+                               segment_v2::InvertedIndexIterator* iter, 
uint32_t num_rows,
+                               roaring::Roaring* bitmap) const override {
+        DBUG_EXECUTE_IF("match.invert_index_not_support_eval_match", {
+            return Status::Error<ErrorCode::INVERTED_INDEX_NOT_SUPPORTED>(
+                    "FunctionMatchAny not support eval_match");
+        })
+        std::shared_ptr<roaring::Roaring> roaring = 
std::make_shared<roaring::Roaring>();
+        auto* param_value = reinterpret_cast<ParamValue*>(
+                context->get_function_state(FunctionContext::FRAGMENT_LOCAL));
+        if (param_value == nullptr) {
+            return Status::Error<ErrorCode::INVERTED_INDEX_EVALUATE_SKIPPED>(
+                    "Inverted index evaluate skipped, param_value is nullptr");
+        }
+        std::unique_ptr<InvertedIndexQueryParamFactory> query_param = nullptr;
+        RETURN_IF_ERROR(InvertedIndexQueryParamFactory::create_query_value(
+                param_value->type, &param_value->value, query_param));
+        // here should use MATCH_ANY_QUERY
+        RETURN_IF_ERROR(iter->read_from_inverted_index(
+                data_type_with_name.first, query_param->get_value(),
+                segment_v2::InvertedIndexQueryType::MATCH_ANY_QUERY, num_rows, 
roaring));
+
+        segment_v2::InvertedIndexQueryCacheHandle null_bitmap_cache_handle;
+        RETURN_IF_ERROR(iter->read_null_bitmap(&null_bitmap_cache_handle));
+        std::shared_ptr<roaring::Roaring> null_bitmap = 
null_bitmap_cache_handle.get_bitmap();
+        if (null_bitmap) {
+            *bitmap -= *null_bitmap;
+        }
+        *bitmap = *roaring;
+        param_value->is_evaled_inverted_idx = true;
+        return Status::OK();
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) const override 
{
+        DBUG_EXECUTE_IF("match.invert_index_not_support_execute_match", {
+            return Status::Error<ErrorCode::INVERTED_INDEX_NOT_SUPPORTED>(
+                    "FunctionMatchAny not support execute_match");
+        })
+        bool is_eval_inverted_index = false;
+        auto* param_value = reinterpret_cast<ParamValue*>(
+                context->get_function_state(FunctionContext::FRAGMENT_LOCAL));
+        if (param_value != nullptr) {
+            is_eval_inverted_index = param_value->is_evaled_inverted_idx;
+        }
+
+        // the array column in block should prepared
+        auto left_column =
+                
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        const ColumnArray* array_column = nullptr;
+        const UInt8* array_null_map = nullptr;
+        if (left_column->is_nullable()) {
+            auto nullable_array = reinterpret_cast<const 
ColumnNullable*>(left_column.get());
+            array_column =
+                    reinterpret_cast<const 
ColumnArray*>(&nullable_array->get_nested_column());
+            array_null_map = 
nullable_array->get_null_map_column().get_data().data();
+        } else {
+            array_column = reinterpret_cast<const 
ColumnArray*>(left_column.get());
+        }
+        const auto& offsets = array_column->get_offsets();
+        // prepare dst column
+        auto dst = ColumnVector<UInt8>::create(offsets.size());
+        auto& dst_data = dst->get_data();
+        auto dst_null_column = ColumnUInt8::create(offsets.size());
+        auto& dst_null_data = dst_null_column->get_data();
+
+        if (is_eval_inverted_index) {
+            // here is no need to eval array column again,just put array 
column to res
+            for (size_t row = 0; row < offsets.size(); ++row) {
+                if (array_null_map && array_null_map[row]) {
+                    dst_null_data[row] = true;
+                    continue;
+                }
+                dst_null_data[row] = false;

Review Comment:
   done!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to