zclllyybb commented on code in PR #59223:
URL: https://github.com/apache/doris/pull/59223#discussion_r2637853674


##########
be/src/vec/functions/array/function_array_cross_product.h:
##########
@@ -0,0 +1,204 @@
+// 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.
+
+#pragma once
+
+#include <gen_cpp/Types_types.h>
+
+#include "common/exception.h"
+#include "common/status.h"
+#include "runtime/primitive_type.h"
+#include "vec/columns/column.h"
+#include "vec/columns/column_array.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/common/assert_cast.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_array.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/data_types/data_type_number.h"
+#include "vec/functions/array/function_array_utils.h"
+#include "vec/functions/function.h"
+#include "vec/utils/util.hpp"
+
+namespace doris::vectorized {
+
+class FunctionArrayCrossProduct : public IFunction {
+public:
+    using DataType = PrimitiveTypeTraits<TYPE_FLOAT>::DataType;
+    using ColumnType = PrimitiveTypeTraits<TYPE_FLOAT>::ColumnType;
+
+    static constexpr auto name = "cross_product";
+    String get_name() const override { return name; }
+    static FunctionPtr create() { return 
std::make_shared<FunctionArrayCrossProduct>(); }
+    size_t get_number_of_arguments() const override { return 2; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        if (arguments.size() != 2) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Invalid number of arguments for function 
{}", get_name());
+        }
+
+        if (arguments[0]->get_primitive_type() != TYPE_ARRAY ||
+            arguments[1]->get_primitive_type() != TYPE_ARRAY) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Arguments for function {} must be arrays", 
get_name());
+        }
+
+        // return ARRAY<FLOAT>
+        return std::make_shared<DataTypeArray>(
+                std::make_shared<DataTypeFloat32>());
+    }
+
+    // strict semantics: do not allow NULL
+    bool use_default_implementation_for_nulls() const override { return false; 
}
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        uint32_t result, size_t input_rows_count) const 
override {
+        const auto& arg1 = block.get_by_position(arguments[0]);
+        const auto& arg2 = block.get_by_position(arguments[1]);
+
+        auto col1 = arg1.column->convert_to_full_column_if_const();

Review Comment:
   use `vector_const` and `const_vector` to deal constancy



##########
regression-test/suites/query_p0/sql_functions/array_functions/test_array_cross_product_function.groovy:
##########
@@ -0,0 +1,62 @@
+// 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.
+
+suite("test_array_cross_product_function") {
+    // normal test cases
+    qt_sql "SELECT cross_product([1, 2, 3], [2, 3, 4])"
+    qt_sql "SELECT cross_product([1, 2, 3], [0, 0, 0])"
+    qt_sql "SELECT cross_product([0, 0, 0], [1, 2, 3])"
+    qt_sql "SELECT cross_product([1, 0, 0], [0, 1, 0])"
+    qt_sql "SELECT cross_product([0, 1, 0], [1, 0, 0])"
+
+    // abnormal test cases
+    try {

Review Comment:
   dont use this. we have ``` test {sql, exception} ``` for abnormal test



##########
be/src/vec/functions/array/function_array_cross_product.h:
##########
@@ -0,0 +1,204 @@
+// 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.
+
+#pragma once
+
+#include <gen_cpp/Types_types.h>
+
+#include "common/exception.h"
+#include "common/status.h"
+#include "runtime/primitive_type.h"
+#include "vec/columns/column.h"
+#include "vec/columns/column_array.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/common/assert_cast.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_array.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/data_types/data_type_number.h"
+#include "vec/functions/array/function_array_utils.h"
+#include "vec/functions/function.h"
+#include "vec/utils/util.hpp"
+
+namespace doris::vectorized {
+
+class FunctionArrayCrossProduct : public IFunction {
+public:
+    using DataType = PrimitiveTypeTraits<TYPE_FLOAT>::DataType;
+    using ColumnType = PrimitiveTypeTraits<TYPE_FLOAT>::ColumnType;
+
+    static constexpr auto name = "cross_product";
+    String get_name() const override { return name; }
+    static FunctionPtr create() { return 
std::make_shared<FunctionArrayCrossProduct>(); }
+    size_t get_number_of_arguments() const override { return 2; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        if (arguments.size() != 2) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Invalid number of arguments for function 
{}", get_name());
+        }
+
+        if (arguments[0]->get_primitive_type() != TYPE_ARRAY ||
+            arguments[1]->get_primitive_type() != TYPE_ARRAY) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Arguments for function {} must be arrays", 
get_name());
+        }
+
+        // return ARRAY<FLOAT>
+        return std::make_shared<DataTypeArray>(
+                std::make_shared<DataTypeFloat32>());
+    }
+
+    // strict semantics: do not allow NULL
+    bool use_default_implementation_for_nulls() const override { return false; 
}
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        uint32_t result, size_t input_rows_count) const 
override {
+        const auto& arg1 = block.get_by_position(arguments[0]);
+        const auto& arg2 = block.get_by_position(arguments[1]);
+
+        auto col1 = arg1.column->convert_to_full_column_if_const();
+        auto col2 = arg2.column->convert_to_full_column_if_const();
+
+        if (col1->size() != col2->size()) {
+            return Status::RuntimeError(
+                    fmt::format("function {} have different input array sizes: 
{} and {}",
+                                get_name(), col1->size(), col2->size()));
+        }
+
+        const ColumnArray* arr1 = nullptr;
+        const ColumnArray* arr2 = nullptr;
+
+        if (const auto* nullable = 
+                typeid_cast<const ColumnNullable*>(col1.get())) {
+            if (nullable->has_null()) {
+                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                       "First argument for function {} cannot 
be null", get_name());
+            }
+            arr1 = assert_cast<const 
ColumnArray*>(nullable->get_nested_column_ptr().get());
+        } else {
+            arr1 = assert_cast<const ColumnArray*>(col1.get());
+        }
+
+        if (const auto* nullable = 
+                typeid_cast<const ColumnNullable*>(col2.get())) {
+            if (nullable->has_null()) {
+                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                       "Second argument for function {} cannot 
be null",
+                                       get_name());
+            }
+            arr2 = assert_cast<const 
ColumnArray*>(nullable->get_nested_column_ptr().get());
+        } else {
+            arr2 = assert_cast<const ColumnArray*>(col2.get());
+        }
+
+        const ColumnFloat32* float1 = nullptr;
+        const ColumnFloat32* float2 = nullptr;
+
+        if (const auto* nullable = 
+                typeid_cast<const 
ColumnNullable*>(arr1->get_data_ptr().get())) {
+            if (nullable->has_null()) {
+                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                       "First argument for function {} cannot 
have null elements",
+                                       get_name());
+            }
+            float1 = assert_cast<const 
ColumnFloat32*>(nullable->get_nested_column_ptr().get());
+        } else {
+            float1 = assert_cast<const 
ColumnFloat32*>(arr1->get_data_ptr().get());
+        }
+
+        if (const auto* nullable = 
+                typeid_cast<const 
ColumnNullable*>(arr2->get_data_ptr().get())) {
+            if (nullable->has_null()) {
+                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                       "Second argument for function {} cannot 
have null elements",
+                                       get_name());
+            }
+            float2 = assert_cast<const 
ColumnFloat32*>(nullable->get_nested_column_ptr().get());
+        } else {
+            float2 = assert_cast<const 
ColumnFloat32*>(arr2->get_data_ptr().get());
+        }
+
+        const auto* offset1 =
+                assert_cast<const 
ColumnArray::ColumnOffsets*>(arr1->get_offsets_ptr().get());
+        const auto* offset2 =
+                assert_cast<const 
ColumnArray::ColumnOffsets*>(arr2->get_offsets_ptr().get());
+
+        // prepare result data
+        auto nested_res = ColumnFloat32::create();
+        auto offsets_res = ColumnArray::ColumnOffsets::create();
+        auto& offsets_data = offsets_res->get_data();
+        offsets_data.reserve(input_rows_count);
+        size_t current_offset = 0;
+
+        size_t row_cnt = offset1->size();
+        size_t prev_offset1 = 0;
+        size_t prev_offset2 = 0;
+        for (ssize_t row = 0; row < row_cnt; ++row) {
+            ssize_t size1 = offset1->get_data()[row] - prev_offset1;
+            ssize_t size2 = offset2->get_data()[row] - prev_offset2;
+
+            if (size1 != size2) [[unlikely]] {
+                return Status::InvalidArgument(
+                        "function {} have different input element sizes of 
array: {} and {}",
+                        get_name(), size1, size2);
+            }
+
+            if (size1 != 3 || size2 != 3) {
+                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                       "function {} requires arrays of size 3",
+                                       get_name());
+            }
+
+            ssize_t base1 = prev_offset1;
+            ssize_t base2 = prev_offset2;
+
+            float a1 = float1->get_data()[base1];
+            float a2 = float1->get_data()[base1 + 1];
+            float a3 = float1->get_data()[base1 + 2];
+
+            float b1 = float2->get_data()[base2];
+            float b2 = float2->get_data()[base2 + 1];
+            float b3 = float2->get_data()[base2 + 2];
+
+            float c1 = a2 * b3 - a3 * b2;
+            float c2 = a3 * b1 - a1 * b3;
+            float c3 = a1 * b2 - a2 * b1;
+
+            nested_res->insert_value(c1);

Review Comment:
   could pre-alloc all result length. and just set the value here.(`resize()` 
first, `operator[]` every time insert)



##########
be/src/vec/functions/array/function_array_cross_product.h:
##########
@@ -0,0 +1,204 @@
+// 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.
+
+#pragma once
+
+#include <gen_cpp/Types_types.h>
+
+#include "common/exception.h"
+#include "common/status.h"
+#include "runtime/primitive_type.h"
+#include "vec/columns/column.h"
+#include "vec/columns/column_array.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/common/assert_cast.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_array.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/data_types/data_type_number.h"
+#include "vec/functions/array/function_array_utils.h"
+#include "vec/functions/function.h"
+#include "vec/utils/util.hpp"
+
+namespace doris::vectorized {
+
+class FunctionArrayCrossProduct : public IFunction {
+public:
+    using DataType = PrimitiveTypeTraits<TYPE_FLOAT>::DataType;
+    using ColumnType = PrimitiveTypeTraits<TYPE_FLOAT>::ColumnType;
+
+    static constexpr auto name = "cross_product";
+    String get_name() const override { return name; }
+    static FunctionPtr create() { return 
std::make_shared<FunctionArrayCrossProduct>(); }
+    size_t get_number_of_arguments() const override { return 2; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        if (arguments.size() != 2) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Invalid number of arguments for function 
{}", get_name());
+        }
+
+        if (arguments[0]->get_primitive_type() != TYPE_ARRAY ||
+            arguments[1]->get_primitive_type() != TYPE_ARRAY) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Arguments for function {} must be arrays", 
get_name());
+        }
+
+        // return ARRAY<FLOAT>
+        return std::make_shared<DataTypeArray>(
+                std::make_shared<DataTypeFloat32>());
+    }
+
+    // strict semantics: do not allow NULL
+    bool use_default_implementation_for_nulls() const override { return false; 
}
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        uint32_t result, size_t input_rows_count) const 
override {
+        const auto& arg1 = block.get_by_position(arguments[0]);
+        const auto& arg2 = block.get_by_position(arguments[1]);
+
+        auto col1 = arg1.column->convert_to_full_column_if_const();
+        auto col2 = arg2.column->convert_to_full_column_if_const();
+
+        if (col1->size() != col2->size()) {
+            return Status::RuntimeError(
+                    fmt::format("function {} have different input array sizes: 
{} and {}",
+                                get_name(), col1->size(), col2->size()));
+        }
+
+        const ColumnArray* arr1 = nullptr;
+        const ColumnArray* arr2 = nullptr;
+
+        if (const auto* nullable = 
+                typeid_cast<const ColumnNullable*>(col1.get())) {
+            if (nullable->has_null()) {
+                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                       "First argument for function {} cannot 
be null", get_name());
+            }
+            arr1 = assert_cast<const 
ColumnArray*>(nullable->get_nested_column_ptr().get());
+        } else {
+            arr1 = assert_cast<const ColumnArray*>(col1.get());
+        }
+
+        if (const auto* nullable = 
+                typeid_cast<const ColumnNullable*>(col2.get())) {
+            if (nullable->has_null()) {
+                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                       "Second argument for function {} cannot 
be null",
+                                       get_name());
+            }
+            arr2 = assert_cast<const 
ColumnArray*>(nullable->get_nested_column_ptr().get());
+        } else {
+            arr2 = assert_cast<const ColumnArray*>(col2.get());
+        }
+
+        const ColumnFloat32* float1 = nullptr;
+        const ColumnFloat32* float2 = nullptr;
+
+        if (const auto* nullable = 
+                typeid_cast<const 
ColumnNullable*>(arr1->get_data_ptr().get())) {
+            if (nullable->has_null()) {
+                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                       "First argument for function {} cannot 
have null elements",
+                                       get_name());
+            }
+            float1 = assert_cast<const 
ColumnFloat32*>(nullable->get_nested_column_ptr().get());
+        } else {
+            float1 = assert_cast<const 
ColumnFloat32*>(arr1->get_data_ptr().get());
+        }
+
+        if (const auto* nullable = 
+                typeid_cast<const 
ColumnNullable*>(arr2->get_data_ptr().get())) {
+            if (nullable->has_null()) {
+                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                       "Second argument for function {} cannot 
have null elements",
+                                       get_name());
+            }
+            float2 = assert_cast<const 
ColumnFloat32*>(nullable->get_nested_column_ptr().get());
+        } else {
+            float2 = assert_cast<const 
ColumnFloat32*>(arr2->get_data_ptr().get());
+        }
+
+        const auto* offset1 =
+                assert_cast<const 
ColumnArray::ColumnOffsets*>(arr1->get_offsets_ptr().get());
+        const auto* offset2 =
+                assert_cast<const 
ColumnArray::ColumnOffsets*>(arr2->get_offsets_ptr().get());
+
+        // prepare result data
+        auto nested_res = ColumnFloat32::create();
+        auto offsets_res = ColumnArray::ColumnOffsets::create();
+        auto& offsets_data = offsets_res->get_data();
+        offsets_data.reserve(input_rows_count);
+        size_t current_offset = 0;
+
+        size_t row_cnt = offset1->size();
+        size_t prev_offset1 = 0;
+        size_t prev_offset2 = 0;
+        for (ssize_t row = 0; row < row_cnt; ++row) {
+            ssize_t size1 = offset1->get_data()[row] - prev_offset1;
+            ssize_t size2 = offset2->get_data()[row] - prev_offset2;
+
+            if (size1 != size2) [[unlikely]] {
+                return Status::InvalidArgument(
+                        "function {} have different input element sizes of 
array: {} and {}",
+                        get_name(), size1, size2);
+            }
+
+            if (size1 != 3 || size2 != 3) {

Review Comment:
   L156 and L162 are same check?



##########
be/src/vec/functions/array/function_array_cross_product.cpp:
##########


Review Comment:
   could combine .h and .cpp files



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/CrossProduct.java:
##########
@@ -0,0 +1,75 @@
+// 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.
+
+package org.apache.doris.nereids.trees.expressions.functions.scalar;
+
+import org.apache.doris.catalog.FunctionSignature;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.functions.AlwaysNotNullable;
+import 
org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
+import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
+import org.apache.doris.nereids.types.ArrayType;
+import org.apache.doris.nereids.types.FloatType;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+
+/**
+ * cosine_distance function
+ */
+public class CrossProduct extends ScalarFunction implements 
ExplicitlyCastableSignature,
+        BinaryExpression, AlwaysNotNullable {
+
+    public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
+            FunctionSignature.ret(ArrayType.of(FloatType.INSTANCE))
+                    .args(ArrayType.of(FloatType.INSTANCE), 
ArrayType.of(FloatType.INSTANCE))

Review Comment:
   why not double type?



##########
be/src/vec/functions/array/function_array_cross_product.h:
##########
@@ -0,0 +1,204 @@
+// 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.
+
+#pragma once
+
+#include <gen_cpp/Types_types.h>
+
+#include "common/exception.h"
+#include "common/status.h"
+#include "runtime/primitive_type.h"
+#include "vec/columns/column.h"
+#include "vec/columns/column_array.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/common/assert_cast.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_array.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/data_types/data_type_number.h"
+#include "vec/functions/array/function_array_utils.h"
+#include "vec/functions/function.h"
+#include "vec/utils/util.hpp"
+
+namespace doris::vectorized {
+
+class FunctionArrayCrossProduct : public IFunction {
+public:
+    using DataType = PrimitiveTypeTraits<TYPE_FLOAT>::DataType;
+    using ColumnType = PrimitiveTypeTraits<TYPE_FLOAT>::ColumnType;
+
+    static constexpr auto name = "cross_product";
+    String get_name() const override { return name; }
+    static FunctionPtr create() { return 
std::make_shared<FunctionArrayCrossProduct>(); }
+    size_t get_number_of_arguments() const override { return 2; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        if (arguments.size() != 2) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Invalid number of arguments for function 
{}", get_name());
+        }
+
+        if (arguments[0]->get_primitive_type() != TYPE_ARRAY ||
+            arguments[1]->get_primitive_type() != TYPE_ARRAY) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Arguments for function {} must be arrays", 
get_name());
+        }
+
+        // return ARRAY<FLOAT>
+        return std::make_shared<DataTypeArray>(
+                std::make_shared<DataTypeFloat32>());
+    }
+
+    // strict semantics: do not allow NULL
+    bool use_default_implementation_for_nulls() const override { return false; 
}
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        uint32_t result, size_t input_rows_count) const 
override {
+        const auto& arg1 = block.get_by_position(arguments[0]);
+        const auto& arg2 = block.get_by_position(arguments[1]);
+
+        auto col1 = arg1.column->convert_to_full_column_if_const();
+        auto col2 = arg2.column->convert_to_full_column_if_const();
+
+        if (col1->size() != col2->size()) {
+            return Status::RuntimeError(
+                    fmt::format("function {} have different input array sizes: 
{} and {}",
+                                get_name(), col1->size(), col2->size()));
+        }
+
+        const ColumnArray* arr1 = nullptr;
+        const ColumnArray* arr2 = nullptr;
+
+        if (const auto* nullable = 
+                typeid_cast<const ColumnNullable*>(col1.get())) {

Review Comment:
   dont use `typeid_cast`



##########
be/src/vec/functions/array/function_array_cross_product.h:
##########
@@ -0,0 +1,204 @@
+// 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.
+
+#pragma once
+
+#include <gen_cpp/Types_types.h>
+
+#include "common/exception.h"
+#include "common/status.h"
+#include "runtime/primitive_type.h"
+#include "vec/columns/column.h"
+#include "vec/columns/column_array.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/common/assert_cast.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_array.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/data_types/data_type_number.h"
+#include "vec/functions/array/function_array_utils.h"
+#include "vec/functions/function.h"
+#include "vec/utils/util.hpp"
+
+namespace doris::vectorized {
+
+class FunctionArrayCrossProduct : public IFunction {
+public:
+    using DataType = PrimitiveTypeTraits<TYPE_FLOAT>::DataType;
+    using ColumnType = PrimitiveTypeTraits<TYPE_FLOAT>::ColumnType;
+
+    static constexpr auto name = "cross_product";
+    String get_name() const override { return name; }
+    static FunctionPtr create() { return 
std::make_shared<FunctionArrayCrossProduct>(); }
+    size_t get_number_of_arguments() const override { return 2; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        if (arguments.size() != 2) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Invalid number of arguments for function 
{}", get_name());
+        }
+
+        if (arguments[0]->get_primitive_type() != TYPE_ARRAY ||
+            arguments[1]->get_primitive_type() != TYPE_ARRAY) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Arguments for function {} must be arrays", 
get_name());
+        }
+
+        // return ARRAY<FLOAT>
+        return std::make_shared<DataTypeArray>(
+                std::make_shared<DataTypeFloat32>());
+    }
+
+    // strict semantics: do not allow NULL
+    bool use_default_implementation_for_nulls() const override { return false; 
}
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        uint32_t result, size_t input_rows_count) const 
override {
+        const auto& arg1 = block.get_by_position(arguments[0]);
+        const auto& arg2 = block.get_by_position(arguments[1]);
+
+        auto col1 = arg1.column->convert_to_full_column_if_const();
+        auto col2 = arg2.column->convert_to_full_column_if_const();
+
+        if (col1->size() != col2->size()) {

Review Comment:
   these sizes are not array's size ,but array column's size. should be 
`FatalError` and make errmsg more proper



##########
regression-test/suites/query_p0/sql_functions/array_functions/test_array_cross_product_function.groovy:
##########
@@ -0,0 +1,62 @@
+// 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.
+
+suite("test_array_cross_product_function") {
+    // normal test cases

Review Comment:
   should create a table and test.



##########
be/src/vec/functions/array/function_array_cross_product.h:
##########
@@ -0,0 +1,204 @@
+// 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.
+
+#pragma once
+
+#include <gen_cpp/Types_types.h>
+
+#include "common/exception.h"
+#include "common/status.h"
+#include "runtime/primitive_type.h"
+#include "vec/columns/column.h"
+#include "vec/columns/column_array.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/common/assert_cast.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_array.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/data_types/data_type_number.h"
+#include "vec/functions/array/function_array_utils.h"
+#include "vec/functions/function.h"
+#include "vec/utils/util.hpp"
+
+namespace doris::vectorized {
+
+class FunctionArrayCrossProduct : public IFunction {
+public:
+    using DataType = PrimitiveTypeTraits<TYPE_FLOAT>::DataType;
+    using ColumnType = PrimitiveTypeTraits<TYPE_FLOAT>::ColumnType;
+
+    static constexpr auto name = "cross_product";
+    String get_name() const override { return name; }
+    static FunctionPtr create() { return 
std::make_shared<FunctionArrayCrossProduct>(); }
+    size_t get_number_of_arguments() const override { return 2; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        if (arguments.size() != 2) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Invalid number of arguments for function 
{}", get_name());
+        }
+
+        if (arguments[0]->get_primitive_type() != TYPE_ARRAY ||
+            arguments[1]->get_primitive_type() != TYPE_ARRAY) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Arguments for function {} must be arrays", 
get_name());
+        }
+
+        // return ARRAY<FLOAT>
+        return std::make_shared<DataTypeArray>(
+                std::make_shared<DataTypeFloat32>());
+    }
+
+    // strict semantics: do not allow NULL
+    bool use_default_implementation_for_nulls() const override { return false; 
}
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        uint32_t result, size_t input_rows_count) const 
override {
+        const auto& arg1 = block.get_by_position(arguments[0]);
+        const auto& arg2 = block.get_by_position(arguments[1]);
+
+        auto col1 = arg1.column->convert_to_full_column_if_const();
+        auto col2 = arg2.column->convert_to_full_column_if_const();
+
+        if (col1->size() != col2->size()) {
+            return Status::RuntimeError(
+                    fmt::format("function {} have different input array sizes: 
{} and {}",
+                                get_name(), col1->size(), col2->size()));
+        }
+
+        const ColumnArray* arr1 = nullptr;
+        const ColumnArray* arr2 = nullptr;
+
+        if (const auto* nullable = 
+                typeid_cast<const ColumnNullable*>(col1.get())) {
+            if (nullable->has_null()) {
+                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                       "First argument for function {} cannot 
be null", get_name());
+            }
+            arr1 = assert_cast<const 
ColumnArray*>(nullable->get_nested_column_ptr().get());
+        } else {
+            arr1 = assert_cast<const ColumnArray*>(col1.get());
+        }
+
+        if (const auto* nullable = 
+                typeid_cast<const ColumnNullable*>(col2.get())) {
+            if (nullable->has_null()) {
+                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                       "Second argument for function {} cannot 
be null",
+                                       get_name());
+            }
+            arr2 = assert_cast<const 
ColumnArray*>(nullable->get_nested_column_ptr().get());
+        } else {
+            arr2 = assert_cast<const ColumnArray*>(col2.get());
+        }
+
+        const ColumnFloat32* float1 = nullptr;
+        const ColumnFloat32* float2 = nullptr;
+
+        if (const auto* nullable = 
+                typeid_cast<const 
ColumnNullable*>(arr1->get_data_ptr().get())) {

Review Comment:
   in Doris BE, column inside column_array should always be ColumnNullable



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


Reply via email to