morningman commented on a change in pull request #8475:
URL: https://github.com/apache/incubator-doris/pull/8475#discussion_r829198100



##########
File path: be/src/vec/functions/array/function_array_element.h
##########
@@ -0,0 +1,224 @@
+// 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.
+// This file is copied from
+// 
https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/array/arrayElement.cpp
+// and modified by Doris
+#pragma once
+
+#include <string_view>
+
+#include "vec/columns/column_array.h"
+#include "vec/columns/column_const.h"
+#include "vec/columns/column_string.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"
+
+namespace doris::vectorized {
+
+class NullMapBuilder {
+public:
+    explicit operator bool() const { return src_null_map; }
+    bool operator!() const { return !src_null_map; }
+
+    void init_source(const UInt8* src_null_map_) {
+        src_null_map = src_null_map_;
+    }
+
+    void init_sink(size_t size) {
+        auto sink = ColumnUInt8::create(size);
+        sink_null_map = sink->get_data().data();
+        sink_null_map_holder = std::move(sink);
+    }
+
+    void update(size_t from) {
+        sink_null_map[index] = bool(src_null_map && src_null_map[from]);
+        ++index;
+    }
+
+    void update() {
+        sink_null_map[index] = true;
+        ++index;
+    }
+
+    ColumnPtr get_null_map_column_ptr() && { return 
std::move(sink_null_map_holder); }
+
+private:
+    const UInt8* src_null_map = nullptr;
+    UInt8* sink_null_map = nullptr;
+    MutableColumnPtr sink_null_map_holder;
+    size_t index = 0;
+};
+
+class FunctionArrayElement : public IFunction
+{
+public:
+    static constexpr auto name = "element_at";
+    static FunctionPtr create() { return 
std::make_shared<FunctionArrayElement>(); }
+
+    /// 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 2; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        DCHECK(is_array(arguments[0])) << "first argument for function: " << 
name << " should be DataTypeArray";
+        DCHECK(is_integer(arguments[1])) << "second argument for function: " 
<< name << " should be Integer";
+        return 
make_nullable(check_and_get_data_type<DataTypeArray>(arguments[0].get())->get_nested_type());
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        NullMapBuilder builder;
+        ColumnsWithTypeAndName args;
+        auto col_left = block.get_by_position(arguments[0]);
+        if (col_left.column->is_nullable()) {
+            auto null_col = 
check_and_get_column<ColumnNullable>(*col_left.column);
+            
builder.init_source(null_col->get_null_map_column().get_data().data());
+            args = {{null_col->get_nested_column_ptr(), 
remove_nullable(col_left.type), col_left.name},
+                    block.get_by_position(arguments[1])};
+        } else {
+            args = {col_left,  block.get_by_position(arguments[1])};
+        }
+        builder.init_sink(input_rows_count);
+
+        auto result_type = 
remove_nullable(check_and_get_data_type<DataTypeArray>(args[0].type.get())->get_nested_type());
+
+        auto res_column = perform(args, result_type, builder, 
input_rows_count);
+        if (!res_column) {
+            return Status::RuntimeError(fmt::format("unsupported types for 
function {}({}, {})", get_name(),
+                                        
block.get_by_position(arguments[0]).type->get_name(),
+                                        
block.get_by_position(arguments[1]).type->get_name()));
+        }
+        block.replace_by_position(result, 
ColumnNullable::create(std::move(res_column), 
std::move(builder).get_null_map_column_ptr()));
+        return Status::OK();
+    }
+
+    ColumnPtr perform(const ColumnsWithTypeAndName& arguments, const 
DataTypePtr& result_type, NullMapBuilder& builder, size_t input_rows_count) {
+        ColumnPtr res;
+        if (!((res = execute_number<Int8>(arguments, result_type, builder, 
input_rows_count))
+             || (res = execute_number<UInt8>(arguments, result_type, builder, 
input_rows_count))

Review comment:
       Do we need these unsigned type? cause we do not actually support these 
types




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