This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-4.2 by this push:
new 460fdcc8dc9 branch-4.2: [fix](function) Return NULL for a bare NULL
element_at container #68114 (#71412)
460fdcc8dc9 is described below
commit 460fdcc8dc9213c1ed8cd1fdb83263b96a9db6a9
Author: Jerry Hu <[email protected]>
AuthorDate: Sat Dec 5 22:12:43 2026 +0800
branch-4.2: [fix](function) Return NULL for a bare NULL element_at
container #68114 (#71412)
### What problem does this PR solve?
Issue Number: None
Related PR: #68114
Cherry-picked from #68114 (commit
74ae7d20b202afca27dc035f910d5fba6fefdea6) to `branch-4.2`.
When constant folding is skipped, `element_at(NULL, index)` reaches the
BE with a bare null-literal container rather than an ARRAY or MAP type.
The existing type resolution treats its mock UInt8 type as BOOL and
rejects the expression instead of returning NULL.
### Changes
- Recognize bare null-literal containers before ARRAY/MAP return-type
resolution and resolve them to `Nullable(Nothing)`.
- Return a constant NULL result with exactly `input_rows_count` logical
rows before complex-column handling.
- Keep the existing ARRAY/MAP/STRUCT paths unchanged and include the
`DataTypeNothing` definition directly.
- Retain the upstream NULL-container unit and SQL regression coverage.
Adapt only the required unit-test helper and direct includes for
`branch-4.2`; remove a redundant trailing blank line from the expected
output without changing result values.
### Release note
Fix `element_at` rejecting a bare NULL container when constant folding
is skipped.
### Validation
- Release FE/BE build: `FE_MAVEN_THREADS=8
DISABLE_BE_JAVA_EXTENSIONS=OFF ./build.sh --be --fe -j 48` passed,
including default Checkstyle and Java runtime packaging.
- ASAN BE unit tests: `./run-be-ut.sh --run
--filter=function_array_element_test.* -j 48` passed (2/2).
- SQL regression: `test_element_at_null_container` under
`query_p0/sql_functions/array_functions`, executed with
`./run-regression-test.sh` against an isolated single-FE/single-BE
configuration, passed (1 suite, 5 result groups; 0
failed/fatal/skipped).
- `clang-format --dry-run --Werror
be/src/exprs/function/array/function_array_element.h
be/test/exprs/function/function_array_element_test.cpp` passed.
- `git diff --check` passed for the complete patch.
Full FE unit tests, the full SQL regression suite,
multi-BE/mixed-version testing and performance benchmarks were not run.
### Check List (For Author)
- Test:
- [x] Regression test
- [x] Unit Test
- Behavior changed:
- [x] Yes. A bare NULL container returns NULL consistently with
constant-folded evaluation.
- Does this need documentation?
- [x] No.
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label
---
.../exprs/function/array/function_array_element.h | 13 +++++
.../exprs/function/function_array_element_test.cpp | 68 +++++++++++++++++++++-
.../test_element_at_null_container.out | 17 ++++++
.../test_element_at_null_container.groovy | 31 ++++++++++
4 files changed, 128 insertions(+), 1 deletion(-)
diff --git a/be/src/exprs/function/array/function_array_element.h
b/be/src/exprs/function/array/function_array_element.h
index 9d10034bb28..2d038567f2b 100644
--- a/be/src/exprs/function/array/function_array_element.h
+++ b/be/src/exprs/function/array/function_array_element.h
@@ -49,6 +49,7 @@
#include "core/data_type/data_type.h"
#include "core/data_type/data_type_array.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"
@@ -107,6 +108,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
@@ -130,6 +137,12 @@ public:
Status execute_impl(FunctionContext* context, Block& block, const
ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const
override {
+ if (block.get_by_position(arguments[0]).type->is_null_literal()) {
+ // Null-literal container: every row is NULL, matching the
Nullable(Nothing) return type.
+ auto& res = block.get_by_position(result);
+ res.column =
res.type->create_column_const_with_default_value(input_rows_count);
+ return Status::OK();
+ }
if
(remove_nullable(block.get_by_position(arguments[0]).type)->get_primitive_type()
==
TYPE_STRUCT) {
return _execute_struct(block, arguments, result, input_rows_count);
diff --git a/be/test/exprs/function/function_array_element_test.cpp
b/be/test/exprs/function/function_array_element_test.cpp
index 784188f2e43..a084aa91e79 100644
--- a/be/test/exprs/function/function_array_element_test.cpp
+++ b/be/test/exprs/function/function_array_element_test.cpp
@@ -15,15 +15,26 @@
// 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.h"
+#include "core/column/column_string.h"
+#include "core/column/column_vector.h"
+#include "core/data_type/data_type.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/data_type/primitive_type.h"
#include "core/types.h"
#include "exprs/function/function_test_util.h"
+#include "exprs/function/simple_function_factory.h"
namespace doris {
@@ -131,4 +142,59 @@ TEST(function_array_element_test, element_at) {
}
}
+// Invoke element_at(arr_col, idx_col) and return the result ColumnNullable.
+// arr_type must match arr_col; idx_type must match idx_col.
+static ColumnPtr run_element_at(ColumnPtr arr_col, DataTypePtr arr_type,
ColumnPtr idx_col,
+ DataTypePtr idx_type, DataTypePtr result_type,
+ size_t input_rows_count) {
+ ColumnsWithTypeAndName args = {{arr_col, arr_type, "arr"}, {idx_col,
idx_type, "idx"}};
+ auto func = SimpleFunctionFactory::instance().get_function("element_at",
args, result_type);
+ EXPECT_NE(func, nullptr);
+
+ Block block;
+ block.insert({arr_col, arr_type, "arr"});
+ block.insert({idx_col, idx_type, "idx"});
+ block.insert({nullptr, result_type, "result"});
+
+ EXPECT_TRUE(func->execute(nullptr, block, {0, 1}, 2,
input_rows_count).ok());
+ return block.get_by_position(2).column;
+}
+
+// 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) {
+ auto result = run_element_at(const_null_container, null_literal_type,
std::move(idx_col),
+ idx_type, null_literal_type, N);
+ 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]