You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by "amorynan (via GitHub)" <gi...@apache.org> on 2023/06/29 09:49:06 UTC

[GitHub] [doris] amorynan commented on a diff in pull request #21330: support array_contains_all function

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


##########
be/src/vec/functions/array/function_array_contains_all.cpp:
##########
@@ -0,0 +1,160 @@
+// 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 "common/status.h"
+ #include "vec/columns/column.h"
+ #include "vec/columns/column_array.h"
+ #include "vec/columns/column_const.h"
+ #include "vec/columns/column_nullable.h"
+ #include "vec/common/assert_cast.h"
+ #include "vec/core/block.h"
+ #include "vec/core/column_numbers.h"
+ #include "vec/core/types.h"
+ #include "vec/data_types/data_type.h"
+ #include "vec/data_types/data_type_number.h"
+ #include "vec/functions/function.h"
+ #include "vec/functions/simple_function_factory.h"
+
+ namespace doris::vectorized {
+
+ class FunctionArrayContainsAll : public IFunction {
+ public:
+     static constexpr auto name {"array_contains_all"};
+
+     static FunctionPtr create() { return std::make_shared<FunctionArrayContainsAll>(); }
+
+     String get_name() const override { return name; }
+
+     bool use_default_implementation_for_nulls() const override { return false; }
+
+     bool is_variadic() const override { return false; }
+
+     size_t get_number_of_arguments() const override { return 2; }
+
+     DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
+         return std::make_shared<DataTypeUInt8>();
+     }
+
+     Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                         size_t result, size_t input_rows_count) override {
+         // construct arrays from the input columns.
+         const Array left_array = from_input_column(block.get_by_position(arguments[0]).column);
+         const Array right_array = from_input_column(block.get_by_position(arguments[1]).column);
+
+         // construct a column to store the execution result.
+         auto result_column = ColumnUInt8::create(input_rows_count);
+         UInt8* result_data = result_column->get_data().data();
+
+         // check if the left array contains all of the right elements.
+         _execute_internal(left_array, right_array, result_data);
+
+         // store the result column in the specified `result` column of the block.
+         block.replace_by_position(result, std::move(result_column));
+
+         return Status::OK();
+     }
+
+ private:
+     // the internal array type.
+     struct Array {
+         ColumnPtr data {nullptr};
+         const NullMap& null_map;
+         const size_t num_elements;
+
+         Array(ColumnPtr data_, const NullMap& null_map_, const size_t num_elements_)
+                 : data {data_}, null_map {null_map_}, num_elements {num_elements_} {}
+     };
+
+     // construct an `Array` instance from the input column.
+     static Array from_input_column(ColumnPtr column) {
+         const auto& [nullable_column, _] = unpack_if_const(column);
+
+         // applying NULL checking on the nullable columns is somewhat the canonical way.
+         // however, the NULL checking is already performed in the frontend.
+         // so we simply fetch the nested array column without any further checking.
+         const ColumnArray* array_column = assert_cast<const ColumnArray*>(nullable_column.get());
+
+         // fetch the data and the corresponding null map.
+         const auto& nested_nullable_column =
+                 assert_cast<const ColumnNullable&>(array_column->get_data());
+         ColumnPtr data = nested_nullable_column.get_nested_column_ptr();
+         const NullMap& null_map = nested_nullable_column.get_null_map_data();
+
+         // count the number of elements in the array.
+         const auto& offsets = array_column->get_offsets();
+         // FIXME(niebayes): the usage pattern of `offsets` is somewhat confusing.
+         // maybe we can find another more elegant way to count the number of elements.
+         const size_t num_elements = offsets[0] - offsets[-1];

Review Comment:
   we just use offsets[cur_row] - offsets[cur_row-1] to get current row's array element , u can see this in column_array.cpp 



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