This is an automated email from the ASF dual-hosted git repository.

HappenLee pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 0d16233c2c9 [opt](array function) Reuse prepared sets for constant 
arrays_overlap arguments (#70645)
0d16233c2c9 is described below

commit 0d16233c2c975ade321ce7676ed611c7121f1b8e
Author: foxtail463 <[email protected]>
AuthorDate: Wed Nov 25 12:11:08 2026 +0800

    [opt](array function) Reuse prepared sets for constant arrays_overlap 
arguments (#70645)
    
    Problem Summary:
    arrays_overlap constructs a hash set for every row, repeating
    allocations and insertions even when one array argument is constant.
    
    Solution:
    Build a read-only lookup set once per fragment when exactly one argument
    is constant, then reuse it to probe the other array. Fully constant
    expressions use the existing whole-expression result cache.
    
    ---------
    
    Co-authored-by: yangtao555 <[email protected]>
---
 be/benchmark/benchmark_arrays_overlap.hpp          | 101 +++++++++++
 be/benchmark/benchmark_main.cpp                    |   1 +
 .../function/array/function_arrays_overlap.cpp     |  39 +++++
 .../exprs/function/array/function_arrays_overlap.h |  40 ++++-
 .../function/function_arrays_overlap_test.cpp      | 185 +++++++++++++++++++++
 .../array_functions/test_arrays_overlap_const.out  |  37 +++++
 .../test_arrays_overlap_const.groovy               |  71 ++++++++
 7 files changed, 466 insertions(+), 8 deletions(-)

diff --git a/be/benchmark/benchmark_arrays_overlap.hpp 
b/be/benchmark/benchmark_arrays_overlap.hpp
new file mode 100644
index 00000000000..5112133de6d
--- /dev/null
+++ b/be/benchmark/benchmark_arrays_overlap.hpp
@@ -0,0 +1,101 @@
+// 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 <benchmark/benchmark.h>
+
+#include <cstddef>
+#include <memory>
+
+#include "core/column/column_array.h"
+#include "core/column/column_const.h"
+#include "core/column/column_nullable.h"
+#include "core/column/column_vector.h"
+#include "core/data_type/data_type_array.h"
+#include "core/data_type/data_type_nullable.h"
+#include "core/data_type/data_type_number.h"
+#include "exprs/function/simple_function_factory.h"
+#include "exprs/function_context.h"
+
+namespace doris {
+
+// Both variants execute the same constant-column input. Registering the 
constant
+// with FunctionContext enables preparation; omitting it measures the per-row 
path.
+// Time execute() only, including result allocation, but excluding input 
creation/open().
+template <bool Prepare>
+static void BM_ArraysOverlap(benchmark::State& state) {
+    constexpr size_t rows = 4096;
+    const size_t const_size = state.range(0);
+    const size_t array_size = state.range(1);
+    const bool match = state.range(2);
+    const auto type =
+            
std::make_shared<DataTypeArray>(make_nullable(std::make_shared<DataTypeInt32>()));
+    const auto result_type = make_nullable(std::make_shared<DataTypeUInt8>());
+    Block block;
+    for (size_t arg = 0; arg < 2; ++arg) {
+        const size_t count = arg == 0 ? rows : 1;
+        const size_t length = arg == 0 ? array_size : const_size;
+        auto values = ColumnInt32::create();
+        auto offsets = ColumnArray::ColumnOffsets::create();
+        for (size_t row = 0; row < count; ++row) {
+            for (size_t i = 0; i < length; ++i) {
+                if (arg == 1) {
+                    values->insert_value(static_cast<Int32>(i));
+                } else {
+                    const auto value = match && i + 1 == length ? const_size - 
1 : 1000 + row + i;
+                    values->insert_value(static_cast<Int32>(value));
+                }
+            }
+            offsets->insert_value(values->size());
+        }
+        auto nulls = ColumnUInt8::create(values->size(), 0);
+        ColumnPtr column = ColumnArray::create(
+                ColumnNullable::create(std::move(values), std::move(nulls)), 
std::move(offsets));
+        if (arg == 1) {
+            column = ColumnConst::create(std::move(column), rows);
+        }
+        block.insert({std::move(column), type, arg == 0 ? "array" : 
"constant"});
+    }
+    auto function = SimpleFunctionFactory::instance().get_function(
+            "arrays_overlap", block.get_columns_with_type_and_name(), 
result_type);
+    auto context = FunctionContext::create_context(nullptr, result_type, 
{type, type});
+    if constexpr (Prepare) {
+        context->set_constant_cols(
+                {nullptr, 
std::make_shared<ConstantExprResult>(block.get_by_position(1).column)});
+    }
+    auto status =
+            function->open(context.get(), 
FunctionContext::FunctionStateScope::FRAGMENT_LOCAL);
+    if (!status.ok()) {
+        state.SkipWithError(status.to_string().c_str());
+        return;
+    }
+    block.insert({nullptr, result_type, "result"});
+    for (auto _ : state) {
+        status = function->execute(context.get(), block, {0, 1}, 2, rows);
+        if (!status.ok()) {
+            state.SkipWithError(status.to_string().c_str());
+            break;
+        }
+        benchmark::DoNotOptimize(block.get_by_position(2).column);
+        benchmark::ClobberMemory();
+    }
+    state.SetItemsProcessed(state.iterations() * rows);
+}
+
+BENCHMARK_TEMPLATE(BM_ArraysOverlap, false)->ArgsProduct({{3, 64}, {8, 64}, 
{0, 1}});
+BENCHMARK_TEMPLATE(BM_ArraysOverlap, true)->ArgsProduct({{3, 64}, {8, 64}, {0, 
1}});
+
+} // namespace doris
diff --git a/be/benchmark/benchmark_main.cpp b/be/benchmark/benchmark_main.cpp
index d60b07a4e5b..a17a93226ff 100644
--- a/be/benchmark/benchmark_main.cpp
+++ b/be/benchmark/benchmark_main.cpp
@@ -27,6 +27,7 @@
 #else
 #include "benchmark_analyzer.hpp"
 #include "benchmark_array_range.hpp"
+#include "benchmark_arrays_overlap.hpp"
 #include "benchmark_arrow_validation.hpp"
 #include "benchmark_binary_arithmetic.hpp"
 #include "benchmark_bit_pack.hpp"
diff --git a/be/src/exprs/function/array/function_arrays_overlap.cpp 
b/be/src/exprs/function/array/function_arrays_overlap.cpp
index d65d3cf4fa3..29e7c51e61a 100644
--- a/be/src/exprs/function/array/function_arrays_overlap.cpp
+++ b/be/src/exprs/function/array/function_arrays_overlap.cpp
@@ -21,6 +21,45 @@
 
 namespace doris {
 
+Status FunctionArraysOverlap::open(FunctionContext* context,
+                                   FunctionContext::FunctionStateScope scope) {
+    if (scope != FunctionContext::FunctionStateScope::FRAGMENT_LOCAL) {
+        return Status::OK();
+    }
+    const bool left_const = context->is_col_constant(0);
+    const bool right_const = context->is_col_constant(1);
+    // Prepare only when exactly one argument is constant. With two constants,
+    // VectorizedFnCall caches the entire result; e.g. arrays_overlap([1, 2, 
...], [])
+    // must not build a set before the empty-array check.
+    if (left_const == right_const) {
+        return Status::OK();
+    }
+    const int const_arg = left_const ? 0 : 1;
+
+    const auto& column = context->get_constant_col(const_arg)->column_ptr();
+    const auto& array_type =
+            assert_cast<const 
DataTypeArray&>(*remove_nullable(context->get_arg_type(const_arg)));
+    const auto element_type = remove_nullable(array_type.get_nested_type());
+    auto prepare = [&](const auto& type) {
+        constexpr PrimitiveType PType = std::decay_t<decltype(type)>::PType;
+        const auto view = ColumnArrayView<PType>::create(column);
+        // A NULL array has no elements to prepare; execute propagates its 
outer NULL.
+        if (!view.is_null_at(0)) {
+            auto prepared = std::make_shared<PreparedSet<PType>>(const_arg);
+            prepared->set.insert_array(view[0]);
+            // For arrays_overlap(col, ['abc']), StringRefs point into the 
constant column.
+            // FunctionContext and its clones retain that column for the 
shared state's lifetime.
+            context->set_function_state(scope, std::move(prepared));
+        }
+        return true;
+    };
+    if (!dispatch_switch_all(element_type->get_primitive_type(), prepare)) {
+        return Status::InvalidArgument("not support type {} in function {}",
+                                       element_type->get_name(), get_name());
+    }
+    return Status::OK();
+}
+
 void register_function_arrays_overlap(SimpleFunctionFactory& factory) {
     factory.register_function<FunctionArraysOverlap>();
 }
diff --git a/be/src/exprs/function/array/function_arrays_overlap.h 
b/be/src/exprs/function/array/function_arrays_overlap.h
index c515c08a1ff..031cbe6a068 100644
--- a/be/src/exprs/function/array/function_arrays_overlap.h
+++ b/be/src/exprs/function/array/function_arrays_overlap.h
@@ -41,6 +41,7 @@
 #include "core/string_ref.h"
 #include "core/types.h"
 #include "exec/common/hash_table/hash.h"
+#include "exec/common/hash_table/phmap_fwd_decl.h"
 #include "exprs/function/array/function_array_index.h"
 #include "exprs/function/array/function_array_utils.h"
 #include "exprs/function/function.h"
@@ -58,12 +59,16 @@ template <typename T>
 class ColumnStr;
 using ColumnString = ColumnStr<UInt32>;
 
+// Keep phmap's equality semantics; doris::EqualTo treats floating-point NaNs 
as equal.
+template <typename T>
+using OverlapHashSet = doris::flat_hash_set<T, DefaultHash<T>, 
phmap::priv::hash_default_eq<T>>;
+
 template <PrimitiveType PType>
 struct OverlapSetImpl {
     using ArrayView = ArrayDataView<PType>;
     using ElementNativeType =
             typename NativeType<typename 
PrimitiveTypeTraits<PType>::ColumnType::value_type>::Type;
-    using Set = phmap::flat_hash_set<ElementNativeType, 
DefaultHash<ElementNativeType>>;
+    using Set = OverlapHashSet<ElementNativeType>;
     Set set;
     bool has_null = false;
 
@@ -77,7 +82,7 @@ struct OverlapSetImpl {
         }
     }
 
-    bool find_any(const ArrayView& array) {
+    bool find_any(const ArrayView& array) const {
         for (size_t i = 0; i < array.size(); ++i) {
             if (array.is_null_at(i)) {
                 if (has_null) {
@@ -99,7 +104,7 @@ template <>
 struct OverlapSetImpl<TYPE_DECIMALV2> {
     using ArrayView = ArrayDataView<TYPE_DECIMALV2>;
     using ElementNativeType = Int128;
-    using Set = phmap::flat_hash_set<ElementNativeType, 
DefaultHash<ElementNativeType>>;
+    using Set = OverlapHashSet<ElementNativeType>;
     Set set;
     bool has_null = false;
 
@@ -113,7 +118,7 @@ struct OverlapSetImpl<TYPE_DECIMALV2> {
         }
     }
 
-    bool find_any(const ArrayView& array) {
+    bool find_any(const ArrayView& array) const {
         for (size_t i = 0; i < array.size(); ++i) {
             if (array.is_null_at(i)) {
                 if (has_null) {
@@ -134,7 +139,7 @@ struct OverlapSetImpl<TYPE_DECIMALV2> {
 template <>
 struct OverlapSetImpl<TYPE_STRING> {
     using ArrayView = ArrayDataView<TYPE_STRING>;
-    using Set = phmap::flat_hash_set<StringRef, DefaultHash<StringRef>>;
+    using Set = OverlapHashSet<StringRef>;
     Set set;
     bool has_null = false;
 
@@ -148,7 +153,7 @@ struct OverlapSetImpl<TYPE_STRING> {
         }
     }
 
-    bool find_any(const ArrayView& array) {
+    bool find_any(const ArrayView& array) const {
         for (size_t i = 0; i < array.size(); ++i) {
             if (array.is_null_at(i)) {
                 if (has_null) {
@@ -180,6 +185,8 @@ public:
 
     size_t get_number_of_arguments() const override { return 2; }
 
+    Status open(FunctionContext* context, FunctionContext::FunctionStateScope 
scope) override;
+
     DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
         auto left_data_type = remove_nullable(arguments[0]);
         auto right_data_type = remove_nullable(arguments[1]);
@@ -301,8 +308,11 @@ public:
                     
ColumnArrayView<PType>::create(block.get_by_position(arguments[0]).column);
             auto right_view =
                     
ColumnArrayView<PType>::create(block.get_by_position(arguments[1]).column);
+            const auto* prepared =
+                    static_cast<const 
PreparedSet<PType>*>(context->get_function_state(
+                            
FunctionContext::FunctionStateScope::FRAGMENT_LOCAL));
             ret = _execute_internal<PType>(left_view, right_view, 
dst_null_map_data,
-                                           dst_nested_col->get_data().data());
+                                           dst_nested_col->get_data().data(), 
prepared);
             return true;
         };
         if (!dispatch_switch_all(left_element_type->get_primitive_type(), 
call)) {
@@ -319,6 +329,14 @@ public:
     }
 
 private:
+    template <PrimitiveType PType>
+    struct PreparedSet {
+        explicit PreparedSet(int arg) : const_arg(arg) {}
+
+        const int const_arg;
+        OverlapSetImpl<PType> set;
+    };
+
     template <PrimitiveType PType>
     static Status _execute_nullable(const ColumnArrayView<PType>& data, UInt8* 
dst_nullmap_data) {
         for (ssize_t row = 0; row < data.size(); ++row) {
@@ -337,7 +355,7 @@ private:
     template <PrimitiveType PType>
     Status _execute_internal(const ColumnArrayView<PType>& left_data,
                              const ColumnArrayView<PType>& right_data, UInt8* 
dst_nullmap_data,
-                             UInt8* dst_data) const {
+                             UInt8* dst_data, const PreparedSet<PType>* 
prepared) const {
         using ExecutorImpl = OverlapSetImpl<PType>;
         RETURN_IF_ERROR(_execute_nullable(left_data, dst_nullmap_data));
         RETURN_IF_ERROR(_execute_nullable(right_data, dst_nullmap_data));
@@ -354,6 +372,12 @@ private:
                 continue;
             }
 
+            if (prepared != nullptr) {
+                dst_data[row] =
+                        prepared->set.find_any(prepared->const_arg == 0 ? 
right_array : left_array);
+                continue;
+            }
+
             const auto& small_data =
                     right_array.size() < left_array.size() ? right_array : 
left_array;
             const auto& large_data =
diff --git a/be/test/exprs/function/function_arrays_overlap_test.cpp 
b/be/test/exprs/function/function_arrays_overlap_test.cpp
index 9aa0dd1dd73..0e4740e8f23 100644
--- a/be/test/exprs/function/function_arrays_overlap_test.cpp
+++ b/be/test/exprs/function/function_arrays_overlap_test.cpp
@@ -15,16 +15,84 @@
 // specific language governing permissions and limitations
 // under the License.
 
+#include "exprs/function/array/function_arrays_overlap.h"
+
 #include <gtest/gtest.h>
 
+#include <memory>
 #include <string>
+#include <utility>
+#include <vector>
 
+#include "core/column/column_const.h"
+#include "core/data_type/data_type_array.h"
 #include "core/data_type/data_type_number.h"
 #include "core/types.h"
 #include "exprs/function/function_test_util.h"
+#include "runtime/memory/mem_tracker_limiter.h"
+#include "runtime/thread_context.h"
 
 namespace doris {
 
+// Unlike check_function, store only one physical row for constant arguments 
while
+// executing multiple rows and blocks, as VectorizedFnCall does.
+static void check_const_arrays_overlap(const InputTypeSet& input_types, const 
DataSet& data_set) {
+    ut_type::UTDataTypeDescs descs;
+    ASSERT_TRUE(parse_ut_data_type(input_types, descs));
+    const auto result_type = make_nullable(std::make_shared<DataTypeUInt8>());
+    const auto rows = data_set.size();
+    DataTypes arg_types;
+    Block block;
+    std::vector<std::shared_ptr<ConstantExprResult>> constants(2);
+    for (size_t arg = 0; arg < 2; ++arg) {
+        const auto& desc = descs[arg];
+        auto column = desc.data_type->create_column();
+        for (size_t row = 0; row < (desc.is_const ? 1 : rows); ++row) {
+            ASSERT_TRUE(insert_cell(column, desc.data_type, 
data_set[row].first[arg]));
+        }
+        ColumnPtr input_column = std::move(column);
+        if (desc.is_const) {
+            input_column = ColumnConst::create(std::move(input_column), rows);
+            constants[arg] = 
std::make_shared<ConstantExprResult>(input_column);
+        }
+        block.insert({std::move(input_column), desc.data_type, desc.col_name});
+        arg_types.push_back(desc.data_type);
+    }
+
+    auto function = SimpleFunctionFactory::instance().get_function(
+            "arrays_overlap", block.get_columns_with_type_and_name(), 
result_type);
+    ASSERT_NE(function, nullptr);
+    auto context = FunctionContext::create_context(nullptr, result_type, 
arg_types);
+    context->set_constant_cols(constants);
+    constants.clear();
+    ASSERT_TRUE(function->open(context.get(), 
FunctionContext::FunctionStateScope::FRAGMENT_LOCAL)
+                        .ok());
+    if (descs[0].is_const && descs[1].is_const) {
+        
EXPECT_EQ(context->get_function_state(FunctionContext::FunctionStateScope::FRAGMENT_LOCAL),
+                  nullptr)
+                << "All-constant expressions must not build a lookup set";
+    }
+    ASSERT_TRUE(
+            function->open(context.get(), 
FunctionContext::FunctionStateScope::THREAD_LOCAL).ok());
+    auto expected = result_type->create_column();
+    for (const auto& row : data_set) {
+        ASSERT_TRUE(insert_cell(expected, result_type, row.second));
+    }
+    block.insert({nullptr, result_type, "result"});
+    for (int batch = 0; batch < 2; ++batch) {
+        ASSERT_TRUE(function->execute(context.get(), block, {0, 1}, 2, 
rows).ok());
+        const auto actual = 
block.get_by_position(2).column->convert_to_full_column_if_const();
+        ASSERT_EQ(actual->size(), rows);
+        for (size_t row = 0; row < rows; ++row) {
+            EXPECT_EQ(0, actual->compare_at(row, row, *expected, 1)) << "row " 
<< row;
+        }
+    }
+    ASSERT_TRUE(
+            function->close(context.get(), 
FunctionContext::FunctionStateScope::THREAD_LOCAL).ok());
+    ASSERT_TRUE(function->close(context.get(), 
FunctionContext::FunctionStateScope::FRAGMENT_LOCAL)
+                        .ok());
+}
+
 TEST(function_arrays_overlap_test, arrays_overlap) {
     std::string func_name = "arrays_overlap";
     TestArray empty_arr;
@@ -174,4 +242,121 @@ TEST(function_arrays_overlap_test, const_arguments) {
     }
 }
 
+TEST(function_arrays_overlap_test, prepared_set_nulls_and_empty_arrays) {
+    const TestArray constant = {Int32(1), Null(), Int32(3), Int32(1)};
+    const TestArray empty;
+    const DataSet data = {
+            {{constant, TestArray {Int32(3)}}, UInt8(1)},
+            {{constant, TestArray {Int32(4)}}, UInt8(0)},
+            {{constant, TestArray {Null()}}, UInt8(1)},
+            {{constant, empty}, UInt8(0)},
+            {{constant, Null()}, Null()},
+            {{constant, TestArray {Int32(4), Int32(5), Int32(6), Int32(7), 
Int32(1)}}, UInt8(1)}};
+    for (bool const_left : {true, false}) {
+        InputTypeSet types = {Consted {TYPE_ARRAY}, TYPE_INT, TYPE_ARRAY, 
TYPE_INT};
+        if (!const_left) {
+            types = {TYPE_ARRAY, TYPE_INT, Consted {TYPE_ARRAY}, TYPE_INT};
+        }
+        auto check_cases = [&](DataSet cases) {
+            if (!const_left) {
+                for (auto& row : cases) {
+                    std::swap(row.first[0], row.first[1]);
+                }
+            }
+            check_const_arrays_overlap(types, cases);
+        };
+        check_cases(data);
+        check_cases({{{empty, empty}, UInt8(0)},
+                     {{empty, TestArray {Null()}}, UInt8(0)},
+                     {{empty, Null()}, Null()}});
+        check_cases({{{Null(), Null()}, Null()},
+                     {{Null(), empty}, Null()},
+                     {{Null(), TestArray {Int32(1)}}, Null()}});
+        check_cases({{{TestArray {Int32(1)}, TestArray {Null()}}, UInt8(0)}});
+    }
+}
+
+TEST(function_arrays_overlap_test, both_const_arguments) {
+    const InputTypeSet types = {Consted {TYPE_ARRAY}, TYPE_INT, Consted 
{TYPE_ARRAY}, TYPE_INT};
+    check_const_arrays_overlap(types, {{{TestArray {Null()}, TestArray 
{Null()}}, UInt8(1)},
+                                       {{TestArray {Null()}, TestArray 
{Null()}}, UInt8(1)}});
+    check_const_arrays_overlap(types, {{{TestArray {}, TestArray {Null()}}, 
UInt8(0)}});
+    check_const_arrays_overlap(types, {{{Null(), TestArray {}}, Null()}});
+
+    TestArray large;
+    for (Int32 value = 0; value < 10000; ++value) {
+        large.emplace_back(value);
+    }
+    check_const_arrays_overlap(types, {{{large, TestArray {}}, UInt8(0)}});
+    check_const_arrays_overlap(types, {{{large, Null()}, Null()}});
+}
+
+TEST(function_arrays_overlap_test, hash_set_allocation_is_tracked) {
+    auto tracker = 
MemTrackerLimiter::create_shared(MemTrackerLimiter::Type::OTHER,
+                                                    "ArraysOverlapHashSet");
+    auto switch_tracker = SwitchThreadMemTrackerLimiter(tracker);
+    thread_context()->thread_mem_tracker_mgr->flush_untracked_mem();
+    const auto baseline = tracker->consumption();
+    {
+        OverlapHashSet<Int32> set;
+        for (Int32 value = 0; value < 1024; ++value) {
+            set.insert(value);
+        }
+        EXPECT_EQ(set.size(), 1024);
+        thread_context()->thread_mem_tracker_mgr->flush_untracked_mem();
+        EXPECT_GT(tracker->consumption(), baseline);
+    }
+    thread_context()->thread_mem_tracker_mgr->flush_untracked_mem();
+    EXPECT_EQ(tracker->consumption(), baseline);
+}
+
+TEST(function_arrays_overlap_test, prepared_decimal_set) {
+    const auto value = ut_type::DECIMALV2VALUE(123, 450000000, 9);
+    const auto other = ut_type::DECIMALV2VALUE(123, 460000000, 9);
+    check_const_arrays_overlap({TYPE_ARRAY, TYPE_DECIMALV2, Consted 
{TYPE_ARRAY}, TYPE_DECIMALV2},
+                               {{{TestArray {value}, TestArray {value, 
Null()}}, UInt8(1)},
+                                {{TestArray {other}, TestArray {value, 
Null()}}, UInt8(0)},
+                                {{TestArray {Null()}, TestArray {value, 
Null()}}, UInt8(1)}});
+}
+
+TEST(function_arrays_overlap_test, prepared_strings_survive_context_clone) {
+    const auto type =
+            
std::make_shared<DataTypeArray>(make_nullable(std::make_shared<DataTypeString>()));
+    const auto result_type = make_nullable(std::make_shared<DataTypeUInt8>());
+    auto context = FunctionContext::create_context(nullptr, result_type, 
{type, type});
+    auto constant = type->create_column();
+    ASSERT_TRUE(insert_cell(constant, type, TestArray {std::string(128, 'a'), 
std::string("")}));
+    context->set_constant_cols(
+            {nullptr, 
std::make_shared<ConstantExprResult>(std::move(constant))});
+    const auto function = SimpleFunctionFactory::instance().get_function(
+            "arrays_overlap", {{nullptr, type, "left"}, {nullptr, type, 
"right"}}, result_type);
+    ASSERT_NE(function, nullptr);
+    ASSERT_TRUE(function->open(context.get(), 
FunctionContext::FunctionStateScope::FRAGMENT_LOCAL)
+                        .ok());
+    
ASSERT_NE(context->get_function_state(FunctionContext::FunctionStateScope::FRAGMENT_LOCAL),
+              nullptr);
+    auto clone = context->clone();
+    context.reset();
+    ASSERT_TRUE(
+            function->open(clone.get(), 
FunctionContext::FunctionStateScope::THREAD_LOCAL).ok());
+
+    auto left = type->create_column();
+    ASSERT_TRUE(insert_cell(left, type, TestArray {std::string(128, 'a')}));
+    ASSERT_TRUE(insert_cell(left, type, TestArray {std::string("different")}));
+    ASSERT_TRUE(insert_cell(left, type, TestArray {std::string("")}));
+    Block block {{std::move(left), type, "left"},
+                 
{ColumnConst::create(clone->get_constant_col(1)->column_ptr(), 3), type, 
"right"},
+                 {nullptr, result_type, "result"}};
+    ASSERT_TRUE(function->execute(clone.get(), block, {0, 1}, 2, 3).ok());
+    auto expected = result_type->create_column();
+    for (auto value : {UInt8(1), UInt8(0), UInt8(1)}) {
+        ASSERT_TRUE(insert_cell(expected, result_type, value));
+    }
+    for (size_t row = 0; row < 3; ++row) {
+        EXPECT_EQ(0, block.get_by_position(2).column->compare_at(row, row, 
*expected, 1));
+    }
+    ASSERT_TRUE(
+            function->close(clone.get(), 
FunctionContext::FunctionStateScope::THREAD_LOCAL).ok());
+}
+
 } // namespace doris
diff --git 
a/regression-test/data/query_p0/sql_functions/array_functions/test_arrays_overlap_const.out
 
b/regression-test/data/query_p0/sql_functions/array_functions/test_arrays_overlap_const.out
new file mode 100644
index 00000000000..facb8fdca77
--- /dev/null
+++ 
b/regression-test/data/query_p0/sql_functions/array_functions/test_arrays_overlap_const.out
@@ -0,0 +1,37 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !projection --
+1      true    true    true    true    false   \N      true    false
+2      false   false   true    true    false   \N      true    false
+3      true    true    false   false   false   \N      true    false
+4      false   false   false   false   false   \N      true    false
+5      \N      \N      \N      \N      \N      \N      true    false
+6      true    true    false   false   false   \N      true    false
+
+-- !int_predicate --
+1
+2
+
+-- !string_predicate --
+1
+2
+
+-- !null_element_predicate --
+3
+6
+
+-- !empty_predicate --
+
+-- !int_predicate --
+1
+2
+
+-- !string_predicate --
+1
+2
+
+-- !null_element_predicate --
+3
+6
+
+-- !empty_predicate --
+
diff --git 
a/regression-test/suites/query_p0/sql_functions/array_functions/test_arrays_overlap_const.groovy
 
b/regression-test/suites/query_p0/sql_functions/array_functions/test_arrays_overlap_const.groovy
new file mode 100644
index 00000000000..5897b212354
--- /dev/null
+++ 
b/regression-test/suites/query_p0/sql_functions/array_functions/test_arrays_overlap_const.groovy
@@ -0,0 +1,71 @@
+// 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_arrays_overlap_const") {
+    sql "DROP TABLE IF EXISTS test_arrays_overlap_const"
+    sql """
+        CREATE TABLE test_arrays_overlap_const (
+            id INT,
+            ints ARRAY<INT> NULL,
+            strings ARRAY<STRING> NULL,
+            INDEX idx_ints(ints) USING INVERTED,
+            INDEX idx_strings(strings) USING INVERTED
+        ) DUPLICATE KEY(id)
+        DISTRIBUTED BY HASH(id) BUCKETS 1
+        PROPERTIES ("replication_num" = "1")
+    """
+    sql """
+        INSERT INTO test_arrays_overlap_const VALUES
+            (1, [1, 2], ['a', 'b']),
+            (2, [3, 4, 5, 6, 7], ['c', '', 'd', 'e']),
+            (3, [NULL, 8], [NULL, 'f']),
+            (4, [], []),
+            (5, NULL, NULL),
+            (6, [NULL], [NULL])
+    """
+
+    // Projection exercises the prepared expression even when the table has 
indexes.
+    order_qt_projection """
+        SELECT id, arrays_overlap(ints, [1, NULL, 1]),
+               arrays_overlap([1, NULL, 1], ints),
+               arrays_overlap(strings, ['a', '']),
+               arrays_overlap(['a', ''], strings),
+               arrays_overlap(ints, CAST([] AS ARRAY<INT>)),
+               arrays_overlap(CAST(NULL AS ARRAY<INT>), ints),
+               arrays_overlap([1, NULL], [NULL, 2]), arrays_overlap([], [1])
+        FROM test_arrays_overlap_const
+    """
+
+    // Cover both the index path and expression evaluation, including the 
NULL-element
+    // queries for which the inverted index falls back to evaluating the 
expression.
+    for (def enabled : [false, true]) {
+        sql "SET enable_inverted_index_query = ${enabled}"
+        order_qt_int_predicate """
+            SELECT id FROM test_arrays_overlap_const WHERE 
arrays_overlap(ints, [1, 7])
+        """
+        order_qt_string_predicate """
+            SELECT id FROM test_arrays_overlap_const WHERE 
arrays_overlap(strings, ['a', ''])
+        """
+        order_qt_null_element_predicate """
+            SELECT id FROM test_arrays_overlap_const WHERE 
arrays_overlap(ints, [NULL, 8])
+        """
+        order_qt_empty_predicate """
+            SELECT id FROM test_arrays_overlap_const
+            WHERE arrays_overlap(ints, CAST([] AS ARRAY<INT>))
+        """
+    }
+}


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

Reply via email to