Skip to content

Commit ec4bb3d

Browse files
Backport #97213 to 26.1: Fix FunctionVariantAdaptor exception when nested function returns Nothing type
1 parent fd397f2 commit ec4bb3d

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/Functions/FunctionVariantAdaptor.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ ColumnPtr ExecutableFunctionVariantAdaptor::executeImpl(
7171
DataTypePtr nested_result_type = func_base->getResultType();
7272
ColumnPtr nested_result = func_base->execute(new_arguments, nested_result_type, variant_column.size(), dry_run);
7373

74-
/// If result is Nullable(Nothing), just return column filled with NULLs.
75-
if (nested_result_type->onlyNull())
74+
/// If result is Nullable(Nothing) or Nothing, just return column filled with NULLs/defaults.
75+
/// Nothing can appear when the function is executed on an empty type (e.g. arrayElement on Array(Nothing)).
76+
if (nested_result_type->onlyNull() || isNothing(nested_result_type))
7677
{
7778
auto res = result_type->createColumn();
7879
res->insertManyDefaults(variant_column.size());
@@ -171,8 +172,8 @@ ColumnPtr ExecutableFunctionVariantAdaptor::executeImpl(
171172
ColumnPtr nested_result = func_base->execute(new_arguments, nested_result_type, new_arguments[0].column->size(), dry_run)
172173
->convertToFullColumnIfConst();
173174

174-
/// If result is Nullable(Nothing), just return column filled with NULLs.
175-
if (nested_result_type->onlyNull())
175+
/// If result is Nullable(Nothing) or Nothing, just return column filled with NULLs/defaults.
176+
if (nested_result_type->onlyNull() || isNothing(nested_result_type))
176177
{
177178
auto res = result_type->createColumn();
178179
res->insertManyDefaults(variant_column.size());
@@ -348,8 +349,8 @@ ColumnPtr ExecutableFunctionVariantAdaptor::executeImpl(
348349

349350
variants_result_types[i] = nested_result_type;
350351

351-
/// Set nullptr in case of only NULL values, we will insert NULL for rows of this selector.
352-
if (nested_result_type->onlyNull())
352+
/// Set nullptr in case of only NULL or Nothing values, we will insert NULL for rows of this selector.
353+
if (nested_result_type->onlyNull() || isNothing(nested_result_type))
353354
{
354355
variants_results[i] = nullptr;
355356
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
\N
2+
(2,'b')
3+
1
4+
\N
5+
(1,'a')
6+
(3,'c')
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Regression test: FunctionVariantAdaptor should handle Nothing type result
2+
-- when a function like arrayElement is executed on Array(Nothing) inside a Variant context.
3+
-- Previously this caused a logical error exception because Nothing type could not be cast to a Variant type.
4+
5+
SET enable_analyzer = 1;
6+
SET allow_suspicious_variant_types = 1;
7+
SET allow_suspicious_types_in_order_by = 1;
8+
9+
SELECT arrayElement(arr, 1) FROM (SELECT [(2, 'b'), (1, 'a')] AS arr UNION ALL SELECT [1, (3, 'c')] AS arr UNION ALL SELECT [] AS arr) ORDER BY arr;
10+
SELECT arrayElement(arr, 2) FROM (SELECT [(2, 'b'), (1, 'a')] AS arr UNION ALL SELECT [1, (3, 'c')] AS arr UNION ALL SELECT [] AS arr) ORDER BY arr;

0 commit comments

Comments
 (0)