Skip to content

Commit 6ac1f1a

Browse files
committed
[fix](function) Bind enclosing lambda arguments in array_sorted_merge
The merge lambda may reference an argument of an enclosing lambda, for example array_map(x -> array_sorted_merge((a, b) -> IF(a = b AND a < x, a, NULL), l, r, true), xs). The function executes its lambda against a block of its own, and that block's frame hides the parent bindings, so the reference could not be resolved and the query failed with a missing column. Key changes: - Collect the named lambda bindings visible at the current execution point and publish them under their names in the one-row merge block, before the two merge arguments so that a merge argument with the same name shadows the outer one - Read the enclosing values at the same row as the arrays, so short-circuit evaluation and batched lambda execution keep working - Cover an enclosing array_map argument in test_array_sorted_merge Unit Test: - regression-test/suites/query_p0/sql_functions/array_functions/test_array_sorted_merge.groovy
1 parent 48370fe commit 6ac1f1a

4 files changed

Lines changed: 64 additions & 3 deletions

File tree

‎be/src/exprs/lambda_function/lambda_execution_context.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ class LambdaExecutionContext {
110110
}
111111
}
112112

113+
/// Collects the named bindings visible at the current execution point, innermost first so
114+
/// that an inner binding shadows an outer one with the same name. A function that executes
115+
/// its lambda against a block of its own can publish these values under their names, which
116+
/// is how a lambda references an enclosing lambda's argument.
117+
void collect_visible_bindings(std::vector<Binding>& bindings) const {
118+
std::set<std::string> seen_names;
119+
for (auto frame_it = _frames.rbegin(); frame_it != _frames.rend(); ++frame_it) {
120+
for (auto binding_it = frame_it->argument_bindings.rbegin();
121+
binding_it != frame_it->argument_bindings.rend(); ++binding_it) {
122+
if (seen_names.insert(binding_it->name).second) {
123+
bindings.push_back(*binding_it);
124+
}
125+
}
126+
if (!frame_it->parent_bindings_visible) {
127+
break;
128+
}
129+
}
130+
}
131+
113132
private:
114133
std::vector<Frame> _frames;
115134
};

‎be/src/exprs/lambda_function/varray_sorted_merge_function.cpp‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,13 @@ class ArraySortedMergeFunction : public LambdaFunction {
169169
// take the positions after the last captured column and are bound by name.
170170
std::set<int> captured_ids;
171171
children[0]->collect_slot_column_ids(captured_ids);
172-
if (!captured_ids.empty() && block == nullptr) {
172+
// An enclosing lambda's argument is a name reference rather than a slot, and this
173+
// function executes its lambda against a block of its own that hides the parent frames.
174+
// Publish the visible binding values under their names in that block, so the reference
175+
// resolves by name like the merge arguments do.
176+
std::vector<LambdaExecutionContext::Binding> enclosing_bindings;
177+
context->lambda_execution_context().collect_visible_bindings(enclosing_bindings);
178+
if ((!captured_ids.empty() || !enclosing_bindings.empty()) && block == nullptr) {
173179
return Status::InternalError(
174180
"array_sorted_merge lambda references outer columns but has no input block");
175181
}
@@ -198,6 +204,25 @@ class ArraySortedMergeFunction : public LambdaFunction {
198204
ColumnUInt8::create(1, 0), std::make_shared<DataTypeUInt8>(), "temp"));
199205
}
200206
}
207+
// The enclosing bindings go before the merge arguments, and the name lookup scans the
208+
// block backwards, so a merge argument with the same name shadows the outer one.
209+
std::vector<ColumnPtr> enclosing_columns;
210+
std::vector<int> enclosing_positions;
211+
for (const auto& binding : enclosing_bindings) {
212+
if (binding.column_position < 0 ||
213+
static_cast<size_t>(binding.column_position) >= block->columns()) {
214+
return Status::InternalError(
215+
"array_sorted_merge enclosing lambda argument '{}' is outside the input "
216+
"block (position {})",
217+
binding.name, binding.column_position);
218+
}
219+
const auto& source = block->get_by_position(binding.column_position);
220+
enclosing_columns.push_back(source.column->convert_to_full_column_if_const());
221+
enclosing_positions.push_back(static_cast<int>(lambda_block.columns()));
222+
lambda_block.insert(ColumnWithTypeAndName(enclosing_columns.back()->clone_empty(),
223+
source.type, binding.name));
224+
}
225+
const int merge_argument_base = static_cast<int>(lambda_block.columns());
201226
for (int i = 0; i < 2; ++i) {
202227
const std::string argument_name =
203228
argument_names != nullptr && i < argument_names->size()
@@ -209,8 +234,8 @@ class ArraySortedMergeFunction : public LambdaFunction {
209234
// The lambda arguments are bound by name (the column refs carry the argument names),
210235
// so the body reads these two one-row columns.
211236
IColumn* argument_columns[2] = {
212-
lambda_block.get_by_position(lambda_argument_base).column->assert_mutable().get(),
213-
lambda_block.get_by_position(lambda_argument_base + 1)
237+
lambda_block.get_by_position(merge_argument_base).column->assert_mutable().get(),
238+
lambda_block.get_by_position(merge_argument_base + 1)
214239
.column->assert_mutable()
215240
.get()};
216241

@@ -258,6 +283,10 @@ class ArraySortedMergeFunction : public LambdaFunction {
258283
for (const auto& [id, column] : captured_columns) {
259284
lambda_block.get_by_position(id).column = column->cut(source_row, 1);
260285
}
286+
for (size_t i = 0; i < enclosing_columns.size(); ++i) {
287+
lambda_block.get_by_position(enclosing_positions[i]).column =
288+
enclosing_columns[i]->cut(source_row, 1);
289+
}
261290

262291
size_t left = row == 0 ? 0 : offsets0[row - 1];
263292
const size_t left_end = offsets0[row];

‎regression-test/data/query_p0/sql_functions/array_functions/test_array_sorted_merge.out‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
-- !capture_column_two --
2828
[1]
2929

30+
-- !capture_enclosing_lambda --
31+
[[1, 1], [1]]
32+
3033
-- !nonnullable_body --
3134
[1, 2]
3235

‎regression-test/suites/query_p0/sql_functions/array_functions/test_array_sorted_merge.groovy‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ suite("test_array_sorted_merge") {
143143
FROM array_sorted_merge_capture_2
144144
"""
145145

146+
// The merge lambda may also reference an enclosing lambda's argument. array_map binds x by
147+
// name, while this function executes the merge lambda against a block of its own, so the
148+
// outer binding is published under its name there. x=0 keeps both values, x=2 merges them.
149+
order_qt_capture_enclosing_lambda """
150+
SELECT array_map(x -> array_sorted_merge((a, b) -> IF(a = b AND a < x, a, NULL),
151+
CAST([1] AS ARRAY<INT>),
152+
CAST([1] AS ARRAY<INT>), true),
153+
[0, 2])
154+
"""
155+
146156
// A non-nullable lambda body (COALESCE) must be wrapped into the nullable element
147157
// representation instead of being inserted directly.
148158
order_qt_nonnullable_body """

0 commit comments

Comments
 (0)