This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-4.1 by this push:
new 44da729a967 branch-4.1: [fix](function) Evaluate element_at over a
bare NULL container to NULL #68114 (#71388)
44da729a967 is described below
commit 44da729a967e6126eadfb11d0dc6f09ce6d367a8
Author: Jerry Hu <[email protected]>
AuthorDate: Sat Dec 5 22:17:31 2026 +0800
branch-4.1: [fix](function) Evaluate element_at over a bare NULL container
to NULL #68114 (#71388)
### What problem does this PR solve?
Issue Number: None
Cherry-picked from #68114 (74ae7d20b202afca27dc035f910d5fba6fefdea6).
When constant folding is skipped, a bare NULL container can reach
`element_at` as a null-literal type. Return-type inference previously
interpreted its placeholder type as BOOL and rejected it. Resolve this
case to `Nullable(Nothing)` so `element_at(NULL, ...)` consistently
evaluates to NULL.
### Changes
- Recognize a null-literal container before ARRAY/MAP return-type
dispatch and include `DataTypeNothing` directly.
- Reuse the existing NULL execution path and preserve ARRAY/MAP and
STRUCT/FILE handling.
- Adapt unit coverage to the branch-4.1 function-factory API. Include
SQL regression coverage for integer, string and NULL indexes, multiple
rows, and folded/non-folded evaluation.
### Check List (For Author)
- Test:
- Release BE/FE build and packaging (`--be --fe -j 48`), including
Checkstyle: passed.
- Targeted NULL-container GTest linked against Release production
libraries: 1/1 passed. The complete modified BE test translation unit
also passed syntax-only compilation with `BE_TEST`.
- `test_element_at_null_container` SQL regression: 1 suite passed, 0
failed, 0 fatal and 0 skipped. Additional typed ARRAY/MAP/STRUCT queries
passed.
- Regression framework compilation, targeted clang-format 16 and `git
diff --check`: passed.
- Full ASAN BE unit suite: not run. Native SQL validation used
`enable_java_support=false`; Java UDF execution was not covered.
- Behavior changed: Yes. A bare NULL container evaluates to NULL when
constant folding is skipped, matching the folded result.
- Does this need documentation: No.
### Release note
Fix `element_at(NULL, ...)` failing when constant folding is skipped.
---
.../exprs/function/array/function_array_element.h | 7 +++
.../exprs/function/function_array_element_test.cpp | 52 ++++++++++++++++++++++
.../test_element_at_null_container.out | 17 +++++++
.../test_element_at_null_container.groovy | 31 +++++++++++++
4 files changed, 107 insertions(+)
diff --git a/be/src/exprs/function/array/function_array_element.h
b/be/src/exprs/function/array/function_array_element.h
index f10c671fdbd..d5aa52260f3 100644
--- a/be/src/exprs/function/array/function_array_element.h
+++ b/be/src/exprs/function/array/function_array_element.h
@@ -51,6 +51,7 @@
#include "core/data_type/data_type_array.h"
#include "core/data_type/data_type_file.h"
#include "core/data_type/data_type_map.h"
+#include "core/data_type/data_type_nothing.h"
#include "core/data_type/data_type_nullable.h"
#include "core/data_type/data_type_number.h"
#include "core/data_type/data_type_struct.h"
@@ -111,6 +112,12 @@ public:
}
DataTypePtr get_return_type_impl(const DataTypes& arguments) const
override {
+ // A bare NULL container (element_at(NULL, x)) reaches the BE as a
null-literal type when
+ // the FE skips constant folding. It carries no array/map type, so
resolve it the way the
+ // default null implementation does (Nullable(Nothing)) instead of
reading it as BOOL.
+ if (arguments[0]->is_null_literal()) {
+ return make_nullable(std::make_shared<DataTypeNothing>());
+ }
DataTypePtr arg_0 = remove_nullable(arguments[0]);
DCHECK(arg_0->get_primitive_type() == TYPE_ARRAY ||
arg_0->get_primitive_type() == TYPE_MAP)
<< "first argument for function: " << name
diff --git a/be/test/exprs/function/function_array_element_test.cpp
b/be/test/exprs/function/function_array_element_test.cpp
index 784188f2e43..a9334f76507 100644
--- a/be/test/exprs/function/function_array_element_test.cpp
+++ b/be/test/exprs/function/function_array_element_test.cpp
@@ -15,15 +15,23 @@
// specific language governing permissions and limitations
// under the License.
+#include <memory>
#include <string>
+#include <utility>
+#include "core/block/block.h"
+#include "core/block/columns_with_type_and_name.h"
+#include "core/column/column_string.h"
+#include "core/column/column_vector.h"
#include "core/data_type/data_type_date.h"
#include "core/data_type/data_type_date_time.h"
#include "core/data_type/data_type_decimal.h"
+#include "core/data_type/data_type_factory.hpp"
#include "core/data_type/data_type_number.h"
#include "core/data_type/data_type_string.h"
#include "core/types.h"
#include "exprs/function/function_test_util.h"
+#include "exprs/function/simple_function_factory.h"
namespace doris {
@@ -131,4 +139,48 @@ TEST(function_array_element_test, element_at) {
}
}
+// element_at(NULL, idx): with constant folding skipped the FE hands the BE a
bare NULL container
+// typed as a null literal (TYPE_NULL). It carries no array/map type and must
evaluate to NULL
+// rather than being rejected as BOOL.
+TEST(function_array_element_test, element_at_null_literal_container) {
+ constexpr size_t N = 3;
+
+ // Mirror the FE plan: both the container argument and the result are
typed TYPE_NULL.
+ auto null_literal_type =
+
DataTypeFactory::instance().create_data_type(PrimitiveType::TYPE_NULL, true);
+ ASSERT_TRUE(null_literal_type->is_null_literal());
+ ColumnPtr const_null_container =
null_literal_type->create_column_const_with_default_value(N);
+
+ auto run_with_index = [&](ColumnPtr idx_col, DataTypePtr idx_type) {
+ ColumnsWithTypeAndName args = {{const_null_container,
null_literal_type, "container"},
+ {idx_col, idx_type, "index"}};
+ auto func =
SimpleFunctionFactory::instance().get_function("element_at", args,
+
null_literal_type);
+ ASSERT_NE(func, nullptr);
+ Block block(args);
+ block.insert({nullptr, null_literal_type, "result"});
+ ASSERT_TRUE(func->execute(nullptr, block, {0, 1}, 2, N).ok());
+ auto result = block.get_by_position(2).column;
+ ASSERT_EQ(result->size(), N);
+ auto full = result->convert_to_full_column_if_const();
+ for (size_t i = 0; i < N; ++i) {
+ EXPECT_TRUE(full->is_null_at(i)) << "row " << i;
+ }
+ };
+
+ // Array-style integer index.
+ auto idx_data = ColumnInt32::create();
+ for (Int32 v : {1, 2, 3}) {
+ idx_data->insert_value(v);
+ }
+ run_with_index(std::move(idx_data), std::make_shared<DataTypeInt32>());
+
+ // Map-style string key.
+ auto key_data = ColumnString::create();
+ for (const auto& v : {std::string("a"), std::string("b"),
std::string("c")}) {
+ key_data->insert_data(v.data(), v.size());
+ }
+ run_with_index(std::move(key_data), std::make_shared<DataTypeString>());
+}
+
} // namespace doris
diff --git
a/regression-test/data/query_p0/sql_functions/array_functions/test_element_at_null_container.out
b/regression-test/data/query_p0/sql_functions/array_functions/test_element_at_null_container.out
new file mode 100644
index 00000000000..8b772f5b8bd
--- /dev/null
+++
b/regression-test/data/query_p0/sql_functions/array_functions/test_element_at_null_container.out
@@ -0,0 +1,17 @@
+-- This file is automatically generated. You should know what you did if you
want to edit this
+-- !null_container_int_index --
+\N
+
+-- !null_container_string_key --
+\N
+
+-- !null_container_null_index --
+\N
+
+-- !null_container_from_rows --
+\N
+\N
+\N
+
+-- !null_container_folded --
+\N
diff --git
a/regression-test/suites/query_p0/sql_functions/array_functions/test_element_at_null_container.groovy
b/regression-test/suites/query_p0/sql_functions/array_functions/test_element_at_null_container.groovy
new file mode 100644
index 00000000000..337fb08e75b
--- /dev/null
+++
b/regression-test/suites/query_p0/sql_functions/array_functions/test_element_at_null_container.groovy
@@ -0,0 +1,31 @@
+// 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_element_at_null_container") {
+ // A bare NULL container carries no array/map type. When constant folding
is skipped the
+ // expression reaches the BE as a null-literal argument and must still
evaluate to NULL
+ // instead of being rejected as an unsupported BOOL container.
+ sql "set debug_skip_fold_constant = true"
+ qt_null_container_int_index "select element_at(NULL, 1)"
+ qt_null_container_string_key "select element_at(NULL, 'k')"
+ qt_null_container_null_index "select element_at(NULL, NULL)"
+ qt_null_container_from_rows "select element_at(NULL, number) from
numbers('number' = '3') order by number"
+
+ // The folded path must agree with the BE evaluation above.
+ sql "set debug_skip_fold_constant = false"
+ qt_null_container_folded "select element_at(NULL, 1)"
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]