cambyzju commented on code in PR #17020:
URL: https://github.com/apache/doris/pull/17020#discussion_r1115218788


##########
be/src/vec/functions/array/function_array_apply.cpp:
##########
@@ -0,0 +1,220 @@
+// 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 <vec/columns/column_array.h>
+#include <vec/columns/column_nullable.h>
+#include <vec/columns/columns_number.h>
+#include <vec/data_types/data_type_array.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::vectorized {
+
+// array_apply([1, 2, 3, 10], ">=", 5) -> [10]
+// This function is temporary, use it to meet the requirement before 
implementing the lambda function.
+class FunctionArrayApply : public IFunction {
+public:
+    static constexpr auto name = "array_apply";
+
+    static FunctionPtr create() { return 
std::make_shared<FunctionArrayApply>(); }
+
+    String get_name() const override { return name; }
+
+    size_t get_number_of_arguments() const override { return 3; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        DCHECK(is_array(arguments[0]))
+                << "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,
+                        size_t result, size_t input_rows_count) override {
+        ColumnPtr src_column =
+                
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        const auto& src_column_array = 
check_and_get_column<ColumnArray>(*src_column);
+        if (!src_column_array) {
+            return Status::RuntimeError(
+                    fmt::format("unsupported types for function {}({})", 
get_name(),
+                                
block.get_by_position(arguments[0]).type->get_name()));
+        }
+        const auto& src_offsets = src_column_array->get_offsets();
+        const auto* src_nested_column = &src_column_array->get_data();
+        DCHECK(src_nested_column != nullptr);
+
+        DataTypePtr src_column_type = block.get_by_position(arguments[0]).type;
+        auto nested_type = assert_cast<const 
DataTypeArray&>(*src_column_type).get_nested_type();
+        const std::string& condition =
+                
block.get_by_position(arguments[1]).column->get_data_at(0).to_string();
+        if (!is_column_const(*block.get_by_position(arguments[2]).column)) {
+            return Status::RuntimeError(
+                    "execute failed or unsupported column, only support const 
column");
+        }
+        const ColumnConst& rhs_value_column =
+                static_cast<const 
ColumnConst&>(*block.get_by_position(arguments[2]).column.get());
+        ColumnPtr result_ptr;
+        RETURN_IF_ERROR(_execute(*src_nested_column, nested_type, src_offsets, 
condition,
+                                 rhs_value_column, &result_ptr));
+        block.replace_by_position(result, std::move(result_ptr));
+        return Status::OK();
+    }
+
+private:
+    enum class ApplyOp {
+        UNKNOWN = 0,
+        EQ = 1,
+        NE = 2,
+        LT = 3,
+        LE = 4,
+        GT = 5,
+        GE = 6,
+    };
+    template <typename T, ApplyOp op>
+    bool apply(T data, T comp) {
+        if constexpr (op == ApplyOp::EQ) {
+            return data == comp;
+        }
+        if constexpr (op == ApplyOp::NE) {
+            return data != comp;
+        }
+        if constexpr (op == ApplyOp::LT) {
+            return data < comp;
+        }
+        if constexpr (op == ApplyOp::LE) {
+            return data <= comp;
+        }
+        if constexpr (op == ApplyOp::GT) {
+            return data > comp;
+        }
+        if constexpr (op == ApplyOp::GE) {
+            return data >= comp;
+        }
+        __builtin_unreachable();
+    }
+
+    template <typename T, ApplyOp op>
+    ColumnPtr _apply_internal(const IColumn& src_column, const 
ColumnArray::Offsets64& src_offsets,
+                              const ColumnConst& cmp) {
+        T rhs_val = *reinterpret_cast<const T*>(cmp.get_data_at(0).data);
+        auto column_filter = ColumnUInt8::create(src_column.size(), 0);
+        auto& column_filter_data = column_filter->get_data();
+        const char* src_column_data_ptr = nullptr;
+        if (!src_column.is_nullable()) {
+            src_column_data_ptr = src_column.get_raw_data().data;
+        } else {
+            src_column_data_ptr = 
check_and_get_column<ColumnNullable>(src_column)
+                                          ->get_nested_column()
+                                          .get_raw_data()
+                                          .data;
+        }
+        for (size_t i = 0; i < src_column.size(); ++i) {
+            T lhs_val = *reinterpret_cast<const T*>(src_column_data_ptr);
+            if (apply<T, op>(lhs_val, rhs_val)) {
+                column_filter_data[i] = 1;
+            }
+            src_column_data_ptr += sizeof(T);
+        }
+        const IColumn::Filter& filter = column_filter_data;
+        ColumnPtr filtered = src_column.filter(filter, -1);

Review Comment:
   actually we know the result element number, we should better not use -1 here.



-- 
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