You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by "eldenmoon (via GitHub)" <gi...@apache.org> on 2023/04/20 11:28:58 UTC

[GitHub] [doris] eldenmoon commented on a diff in pull request #18557: [vectorized](function) support array_count function

eldenmoon commented on code in PR #18557:
URL: https://github.com/apache/doris/pull/18557#discussion_r1172421060


##########
be/src/vec/functions/array/function_array_count.cpp:
##########
@@ -0,0 +1,93 @@
+// 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_count([1, 2, 3, 0, 0]) -> [3]

Review Comment:
   the comment should be changed



##########
be/src/vec/functions/array/function_array_count.cpp:
##########
@@ -0,0 +1,93 @@
+// 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_count([1, 2, 3, 0, 0]) -> [3]
+class FunctionArrayCount : public IFunction {
+public:
+    static constexpr auto name = "array_count";
+
+    static FunctionPtr create() { return std::make_shared<FunctionArrayCount>(); }
+
+    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 use_default_implementation_for_nulls() const override { return false; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
+        return std::make_shared<DataTypeInt64>();
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        auto src_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 (src_column->is_nullable()) {
+            auto nullable_array = assert_cast<const 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());
+        }
+
+        auto& src_nested_data = array_column->get_data();
+        auto& src_offset = array_column->get_offsets();
+
+        auto result_data_col = ColumnInt64::create(input_rows_count, 0);
+        auto& result_data = result_data_col->get_data();
+        
+        for (size_t i = 0; i < input_rows_count; ++i) {
+            if (array_null_map && array_null_map[i]) {
+                continue;
+            }
+            
+            // default count is 0 if no-zero elment is not found
+            Int64 count = 0;
+            for (size_t off = src_offset[i - 1]; off < src_offset[i]; ++off) {
+                if (!src_nested_data.is_null_at(off) && src_nested_data.get_bool(off)) {
+                    ++count;

Review Comment:
   get_bool will call virtual function, and lead to performance issue, we should modify not call virtual function



##########
be/src/vec/functions/array/function_array_count.cpp:
##########
@@ -0,0 +1,93 @@
+// 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_count([1, 2, 3, 0, 0]) -> [3]
+class FunctionArrayCount : public IFunction {
+public:
+    static constexpr auto name = "array_count";
+
+    static FunctionPtr create() { return std::make_shared<FunctionArrayCount>(); }
+
+    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 use_default_implementation_for_nulls() const override { return false; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
+        return std::make_shared<DataTypeInt64>();
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        auto src_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 (src_column->is_nullable()) {
+            auto nullable_array = assert_cast<const 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());
+        }
+
+        auto& src_nested_data = array_column->get_data();
+        auto& src_offset = array_column->get_offsets();
+
+        auto result_data_col = ColumnInt64::create(input_rows_count, 0);
+        auto& result_data = result_data_col->get_data();
+        
+        for (size_t i = 0; i < input_rows_count; ++i) {
+            if (array_null_map && array_null_map[i]) {
+                continue;
+            }
+            
+            // default count is 0 if no-zero elment is not found

Review Comment:
   this comment should alse be modified



##########
be/src/vec/functions/array/function_array_count.cpp:
##########
@@ -0,0 +1,154 @@
+// 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_count([1, 2, 3, 0, 0]) -> 3
+class FunctionArrayCount : public IFunction {
+public:
+    static constexpr auto name = "array_count";
+
+    static FunctionPtr create() { return std::make_shared<FunctionArrayCount>(); }
+
+    /// Get function name.
+    String get_name() const override { return name; }
+
+    bool is_variadic() const override { return false; }
+
+    size_t get_number_of_arguments() const override { return 1; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
+        DCHECK(is_array(arguments[0]))
+                << "first argument for function: " << name << " should be DataTypeArray";
+        return make_nullable(std::make_shared<DataTypeInt64>());
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        // extract array offsets and nested data
+        auto left_column =
+                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();

Review Comment:
   remind



##########
be/src/vec/functions/array/function_array_count.cpp:
##########
@@ -0,0 +1,93 @@
+// 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_count([1, 2, 3, 0, 0]) -> [3]
+class FunctionArrayCount : public IFunction {
+public:
+    static constexpr auto name = "array_count";
+
+    static FunctionPtr create() { return std::make_shared<FunctionArrayCount>(); }
+
+    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 use_default_implementation_for_nulls() const override { return false; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
+        return std::make_shared<DataTypeInt64>();
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        auto src_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 (src_column->is_nullable()) {
+            auto nullable_array = assert_cast<const 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());
+        }
+
+        auto& src_nested_data = array_column->get_data();
+        auto& src_offset = array_column->get_offsets();
+
+        auto result_data_col = ColumnInt64::create(input_rows_count, 0);
+        auto& result_data = result_data_col->get_data();
+        
+        for (size_t i = 0; i < input_rows_count; ++i) {
+            if (array_null_map && array_null_map[i]) {
+                continue;
+            }
+            
+            // default count is 0 if no-zero elment is not found
+            Int64 count = 0;
+            for (size_t off = src_offset[i - 1]; off < src_offset[i]; ++off) {
+                if (!src_nested_data.is_null_at(off) && src_nested_data.get_bool(off)) {
+                    ++count;

Review Comment:
   same as is_null_at



##########
be/src/vec/functions/array/function_array_count.cpp:
##########
@@ -0,0 +1,93 @@
+// 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_count([1, 2, 3, 0, 0]) -> [3]
+class FunctionArrayCount : public IFunction {
+public:
+    static constexpr auto name = "array_count";
+
+    static FunctionPtr create() { return std::make_shared<FunctionArrayCount>(); }
+
+    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 use_default_implementation_for_nulls() const override { return false; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
+        return std::make_shared<DataTypeInt64>();
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        auto src_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 (src_column->is_nullable()) {
+            auto nullable_array = assert_cast<const 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());
+        }
+
+        auto& src_nested_data = array_column->get_data();
+        auto& src_offset = array_column->get_offsets();
+
+        auto result_data_col = ColumnInt64::create(input_rows_count, 0);
+        auto& result_data = result_data_col->get_data();
+        
+        for (size_t i = 0; i < input_rows_count; ++i) {
+            if (array_null_map && array_null_map[i]) {
+                continue;
+            }
+            
+            // default count is 0 if no-zero elment is not found
+            Int64 count = 0;
+            for (size_t off = src_offset[i - 1]; off < src_offset[i]; ++off) {
+                if (!src_nested_data.is_null_at(off) && src_nested_data.get_bool(off)) {
+                    ++count;

Review Comment:
   we could do some cast 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-unsubscribe@doris.apache.org

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


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