zhangstar333 commented on a change in pull request #8138:
URL: https://github.com/apache/incubator-doris/pull/8138#discussion_r810741885



##########
File path: be/src/vec/functions/function_bitmap_variadic.cpp
##########
@@ -0,0 +1,229 @@
+// 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_complex.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_bitmap.h"
+#include "vec/functions/function_const.h"
+#include "vec/functions/function_string.h"
+#include "vec/functions/function_totype.h"
+#include "vec/functions/simple_function_factory.h"
+
+namespace doris::vectorized {
+
+struct BitmapAnd {
+    static constexpr auto name = "bitmap_and";
+    using ResultDataType = DataTypeBitMap;
+
+    static Status vector_vector(ColumnPtr argument_columns[], size_t col_size,
+                                size_t input_rows_count, 
std::vector<BitmapValue>& res) {
+
+        res = assert_cast<const 
ColumnBitmap*>(argument_columns[0].get())->get_data();
+
+        for (size_t col = 1; col < col_size; ++col) {
+            auto& col_data =
+                    assert_cast<const 
ColumnBitmap*>(argument_columns[col].get())->get_data();
+            for (size_t row = 0; row < input_rows_count; ++row) {
+                res[row] &= col_data[row];
+            }
+        }
+        return Status::OK();
+    }
+};
+
+struct BitmapOr {
+    static constexpr auto name = "bitmap_or";
+    using ResultDataType = DataTypeBitMap;
+
+    static Status vector_vector(ColumnPtr argument_columns[], size_t col_size,
+                                size_t input_rows_count, 
std::vector<BitmapValue>& res) {
+
+        res = assert_cast<const 
ColumnBitmap*>(argument_columns[0].get())->get_data();
+        for (size_t col = 1; col < col_size; ++col) {
+            auto& col_data =
+                    assert_cast<const 
ColumnBitmap*>(argument_columns[col].get())->get_data();
+            for (size_t row = 0; row < input_rows_count; ++row) {
+                res[row] |= col_data[row];
+            }
+        }
+        return Status::OK();
+    }
+};
+
+struct BitmapXor {
+    using ResultDataType = DataTypeBitMap;
+    static constexpr auto name = "bitmap_xor";
+
+    static Status vector_vector(ColumnPtr argument_columns[], size_t col_size,
+                                size_t input_rows_count, 
std::vector<BitmapValue>& res) {
+
+        res = assert_cast<const 
ColumnBitmap*>(argument_columns[0].get())->get_data();
+        for (size_t col = 1; col < col_size; ++col) {
+            auto& col_data =
+                    assert_cast<const 
ColumnBitmap*>(argument_columns[col].get())->get_data();
+            for (size_t row = 0; row < input_rows_count; ++row) {
+                res[row] ^= col_data[row];
+            }
+        }
+        return Status::OK();
+    }
+};
+
+struct BitmapOrCount {
+    static constexpr auto name = "bitmap_or_count";
+    using ResultDataType = DataTypeInt64;
+    using TData = std::vector<BitmapValue>;
+    using ResTData = typename ColumnVector<Int64>::Container;
+
+    static Status vector_vector(ColumnPtr argument_columns[], size_t col_size,
+                                size_t input_rows_count, ResTData& res) {
+
+        TData vals(input_rows_count);
+        for (size_t col = 0; col < col_size; ++col) {
+            auto& col_data =
+                    assert_cast<const 
ColumnBitmap*>(argument_columns[col].get())->get_data();
+            for (size_t row = 0; row < input_rows_count; ++row) {
+                vals[row] |= col_data[row];
+            }
+        }
+
+        for (size_t row = 0; row < input_rows_count; ++row) {
+            res[row] = vals[row].cardinality();
+        }
+        return Status::OK();
+    }
+};
+
+struct BitmapAndCount {
+    static constexpr auto name = "bitmap_and_count";
+    using ResultDataType = DataTypeInt64;
+    using TData = std::vector<BitmapValue>;
+    using ResTData = typename ColumnVector<Int64>::Container;
+
+    static Status vector_vector(ColumnPtr argument_columns[], size_t col_size,
+                                size_t input_rows_count, ResTData& res) {
+
+        TData vals(input_rows_count);
+        vals = assert_cast<const 
ColumnBitmap*>(argument_columns[0].get())->get_data();
+
+        for (size_t col = 1; col < col_size; ++col) {
+            auto& col_data =
+                    assert_cast<const 
ColumnBitmap*>(argument_columns[col].get())->get_data();
+            for (size_t row = 0; row < input_rows_count; ++row) {
+                vals[row] &= col_data[row];
+            }
+        }
+
+        for (size_t row = 0; row < input_rows_count; ++row) {
+            res[row] = vals[row].cardinality();
+        }
+        return Status::OK();
+    }
+};
+
+struct BitmapXorCount {
+    static constexpr auto name = "bitmap_xor_count";
+    using ResultDataType = DataTypeInt64;
+    using TData = std::vector<BitmapValue>;
+    using ResTData = typename ColumnVector<Int64>::Container;
+
+    static Status vector_vector(ColumnPtr argument_columns[], size_t col_size,
+                                size_t input_rows_count, ResTData& res) {
+
+        TData vals(input_rows_count);
+        vals = assert_cast<const 
ColumnBitmap*>(argument_columns[0].get())->get_data();
+
+        for (size_t col = 1; col < col_size; ++col) {
+            auto& col_data =
+                    assert_cast<const 
ColumnBitmap*>(argument_columns[col].get())->get_data();
+            for (size_t row = 0; row < input_rows_count; ++row) {
+                vals[row] ^= col_data[row];
+            }
+        }
+
+        for (size_t row = 0; row < input_rows_count; ++row) {
+            res[row] = vals[row].cardinality();
+        }
+        return Status::OK();
+    }
+};
+
+
+
+template <typename Impl>
+class FunctionBitMapVariadic : public IFunction {
+public:
+    static constexpr auto name = Impl::name;
+
+    static FunctionPtr create() { return 
std::make_shared<FunctionBitMapVariadic>(); }
+
+    String get_name() const override { return name; }
+
+    size_t get_number_of_arguments() const override { return 0; }
+
+    bool is_variadic() const override { return true; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        using ResultDataType = typename Impl::ResultDataType;
+        return std::make_shared<ResultDataType>();
+    }
+
+    bool use_default_implementation_for_constants() const override { return 
true; }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        size_t argument_size = arguments.size();
+        ColumnPtr argument_columns[argument_size];
+
+        for (size_t i = 0; i < argument_size; ++i) {
+            argument_columns[i] =
+                    
block.get_by_position(arguments[i]).column->convert_to_full_column_if_const();
+        }
+
+        using ResultDataType = typename Impl::ResultDataType;  
//DataTypeBitMap or DataTypeInt64
+        using ResultType = typename ResultDataType::FieldType; //BitmapValue 
or Int64
+        using ColVecResult =
+                std::conditional_t<is_complex_v<ResultType>, 
ColumnComplexType<ResultType>,
+                                   ColumnVector<ResultType>>;
+        typename ColVecResult::MutablePtr col_res = nullptr;
+        col_res = ColVecResult::create();
+
+        auto& vec_res = col_res->get_data();
+        vec_res.resize(input_rows_count);
+
+        Impl::vector_vector(argument_columns, argument_size, input_rows_count, 
vec_res);
+        block.replace_by_position(result, std::move(col_res));
+        return Status::OK();
+    }
+};
+
+using FunctionBitmapOr = FunctionBitMapVariadic<BitmapOr>;

Review comment:
       now need support bitmap_or(bitmap, bitmap, ...);




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