Skip to content

Commit 9987a65

Browse files
committed
[fix](be) Evaluate the first FOLLOWING window as a single range
### What problem does this PR solve? Issue Number: None Related PR: #68210 Problem Summary: An unbounded-preceding window with a FOLLOWING end initialized the first row with a separate prefix call. LEAD derives its default-value row from the frame end, so an offset larger than the partition size made that call read row -1. Copying the value into the owning single-value state then crashed when a string column accessed offsets[-2]. Folded NULL UUID arguments also reach this path. Evaluate the complete first frame once, retaining its prefix for incremental aggregates and using the correct current-row default for LEAD. ### Release note Fix a crash in LEAD when its offset exceeds the partition size. ### Check List (For Author) - Test: Unit Test / Regression test / Manual test - Reproduced the original crash with the new STRING BE unit test before the fix. - Passed 19 related ASAN BE unit tests and the ASAN BE build. - Passed test_uuid_window_matrix (126 result groups), test_lead_following_default, test_lead_lag_nullable and test_lag_lead_window. - Generated the new golden file with the regression runner and independently verified all 16 queries / 80 rows. - Passed clang-format 16 and build hygiene checks. clang-tidy was blocked by the existing unmatched NOLINTEND in core/types.h; no diagnostics on new lines. - Behavior changed: Yes (valid LEAD queries no longer access an invalid default row) - Does this need documentation: No
1 parent 1305bc1 commit 9987a65

4 files changed

Lines changed: 242 additions & 5 deletions

File tree

‎be/src/exec/operator/analytic_sink_operator.cpp‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,13 @@ bool AnalyticSinkLocalState::_get_next_for_unbounded_rows(int64_t current_block_
237237
_need_more_data = true;
238238
break;
239239
}
240-
if (is_n_following_frame && _current_row_position == _partition_by_pose.start) {
241-
_execute_for_function(_partition_by_pose.start, _partition_by_pose.end,
242-
_partition_by_pose.start, current_row_end - 1);
243-
}
244-
_execute_for_function(_partition_by_pose.start, _partition_by_pose.end, current_row_end - 1,
240+
// Initialize the first FOLLOWING frame in one call. LEAD derives the current row
241+
// from frame_end, so a separate prefix call would read the previous row's default.
242+
const int64_t current_row_start =
243+
is_n_following_frame && _current_row_position == _partition_by_pose.start
244+
? _partition_by_pose.start
245+
: current_row_end - 1;
246+
_execute_for_function(_partition_by_pose.start, _partition_by_pose.end, current_row_start,
245247
current_row_end);
246248
int64_t pos = current_pos_in_block();
247249
_insert_result_info(pos, pos + 1);

‎be/test/exec/operator/analytic_sink_operator_test.cpp‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,68 @@ TEST_F(AnalyticSinkOperatorTest, UnboundedRowsRetainsNextUnreadRowDuringEviction
494494
EXPECT_TRUE(eos);
495495
}
496496

497+
TEST_F(AnalyticSinkOperatorTest, LeadBeyondPartitionUsesDefaultFromCurrentRow) {
498+
Initialize(3);
499+
const auto string_type = std::make_shared<DataTypeString>();
500+
const DataTypes argument_types {string_type, std::make_shared<DataTypeInt64>(), string_type};
501+
create_operator(true, 1, "lead", argument_types, string_type);
502+
sink->_num_agg_input[0] = argument_types.size();
503+
sink->_agg_expr_ctxs = {MockSlotRef::create_mock_contexts(argument_types)};
504+
505+
TAnalyticWindow window;
506+
window.type = TAnalyticWindowType::ROWS;
507+
TAnalyticWindowBoundary window_end;
508+
window_end.type = TAnalyticWindowBoundaryType::FOLLOWING;
509+
window_end.__set_rows_offset_value(100);
510+
window.__set_window_end(window_end);
511+
create_window_type(false, true, window);
512+
create_local_state();
513+
514+
const auto defaults = ColumnHelper::create_column<DataTypeString>({"first", "second", "third"});
515+
Block input {{ColumnHelper::create_column<DataTypeString>({"a", "b", "c"}), string_type, "v"},
516+
{ColumnHelper::create_column<DataTypeInt64>({100, 100, 100}), argument_types[1],
517+
"offset"},
518+
{defaults, string_type, "default"}};
519+
ASSERT_TRUE(sink->sink(state.get(), &input, true).ok());
520+
521+
Block result;
522+
bool eos = false;
523+
ASSERT_TRUE(source->get_block(state.get(), &result, &eos).ok());
524+
ASSERT_EQ(result.columns(), 4);
525+
EXPECT_TRUE(ColumnHelper::column_equal(result.get_by_position(3).column, defaults));
526+
}
527+
528+
TEST_F(AnalyticSinkOperatorTest, FirstFollowingFrameIncludesPrefixAcrossBlocks) {
529+
Initialize(2);
530+
const auto type = std::make_shared<DataTypeInt64>();
531+
create_operator(true, 1, "sum", {type}, type);
532+
sink->_agg_expr_ctxs = {MockSlotRef::create_mock_contexts(type)};
533+
534+
TAnalyticWindow window;
535+
window.type = TAnalyticWindowType::ROWS;
536+
TAnalyticWindowBoundary window_end;
537+
window_end.type = TAnalyticWindowBoundaryType::FOLLOWING;
538+
window_end.__set_rows_offset_value(1);
539+
window.__set_window_end(window_end);
540+
create_window_type(false, true, window);
541+
create_local_state();
542+
543+
Block first = ColumnHelper::create_block<DataTypeInt64>({1, 2});
544+
ASSERT_TRUE(sink->sink(state.get(), &first, false).ok());
545+
Block second = ColumnHelper::create_block<DataTypeInt64>({3, 4});
546+
ASSERT_TRUE(sink->sink(state.get(), &second, true).ok());
547+
548+
Block result;
549+
bool eos = false;
550+
ASSERT_TRUE(source->get_block(state.get(), &result, &eos).ok());
551+
EXPECT_TRUE(ColumnHelper::block_equal(
552+
result, ColumnHelper::create_block<DataTypeInt64>({1, 2}, {3, 6})));
553+
result.clear();
554+
ASSERT_TRUE(source->get_block(state.get(), &result, &eos).ok());
555+
EXPECT_TRUE(ColumnHelper::block_equal(
556+
result, ColumnHelper::create_block<DataTypeInt64>({3, 4}, {10, 10})));
557+
}
558+
497559
TEST_F(AnalyticSinkOperatorTest, SlidingRowsSumRetainsOutgoingRowDuringEviction) {
498560
int batch_size = 2;
499561
Initialize(batch_size);
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !defaults --
3+
1 00000000-0000-0000-0000-000000000001 00000000-0000-0000-0000-000000000001 00000000-0000-0000-0000-000000000001
4+
2 \N \N \N
5+
3 80000000-0000-0000-0000-000000000003 80000000-0000-0000-0000-000000000003 80000000-0000-0000-0000-000000000003
6+
4 ffffffff-ffff-ffff-ffff-ffffffffffff ffffffff-ffff-ffff-ffff-ffffffffffff ffffffff-ffff-ffff-ffff-ffffffffffff
7+
5 \N \N \N
8+
9+
-- !defaults --
10+
1 \N 80000000-0000-0000-0000-000000000001 \N
11+
2 80000000-0000-0000-0000-000000000003 00000000-0000-0000-0000-000000000001 80000000-0000-0000-0000-000000000003
12+
3 00000000-0000-0000-0000-000000000003 \N \N
13+
4 \N 00000000-0000-0000-0000-000000000004 \N
14+
5 00000000-0000-0000-0000-000000000005 ffffffff-ffff-ffff-ffff-ffffffffffff \N
15+
16+
-- !defaults --
17+
1 80000000-0000-0000-0000-000000000001 80000000-0000-0000-0000-000000000001 \N
18+
2 \N \N \N
19+
3 00000000-0000-0000-0000-000000000003 00000000-0000-0000-0000-000000000003 \N
20+
4 00000000-0000-0000-0000-000000000004 00000000-0000-0000-0000-000000000004 \N
21+
5 00000000-0000-0000-0000-000000000005 00000000-0000-0000-0000-000000000005 \N
22+
23+
-- !defaults --
24+
1 80000000-0000-0000-0000-000000000001 80000000-0000-0000-0000-000000000001 \N
25+
2 \N \N \N
26+
3 00000000-0000-0000-0000-000000000003 00000000-0000-0000-0000-000000000003 \N
27+
4 00000000-0000-0000-0000-000000000004 00000000-0000-0000-0000-000000000004 \N
28+
5 00000000-0000-0000-0000-000000000005 00000000-0000-0000-0000-000000000005 \N
29+
30+
-- !defaults --
31+
1 a a a
32+
2 \N \N \N
33+
3 c c c
34+
4 d d d
35+
5 \N \N \N
36+
37+
-- !defaults --
38+
1 \N d1 \N
39+
2 c a c
40+
3 d3 \N \N
41+
4 \N d4 \N
42+
5 d5 d \N
43+
44+
-- !defaults --
45+
1 d1 d1 \N
46+
2 \N \N \N
47+
3 d3 d3 \N
48+
4 d4 d4 \N
49+
5 d5 d5 \N
50+
51+
-- !defaults --
52+
1 d1 d1 \N
53+
2 \N \N \N
54+
3 d3 d3 \N
55+
4 d4 d4 \N
56+
5 d5 d5 \N
57+
58+
-- !defaults --
59+
1 10 10 10
60+
2 \N \N \N
61+
3 30 30 30
62+
4 40 40 40
63+
5 \N \N \N
64+
65+
-- !defaults --
66+
1 \N 101 \N
67+
2 30 10 30
68+
3 103 \N \N
69+
4 \N 104 \N
70+
5 105 40 \N
71+
72+
-- !defaults --
73+
1 101 101 \N
74+
2 \N \N \N
75+
3 103 103 \N
76+
4 104 104 \N
77+
5 105 105 \N
78+
79+
-- !defaults --
80+
1 101 101 \N
81+
2 \N \N \N
82+
3 103 103 \N
83+
4 104 104 \N
84+
5 105 105 \N
85+
86+
-- !null_uuid --
87+
1 \N \N
88+
2 \N \N
89+
3 \N \N
90+
4 \N \N
91+
5 \N \N
92+
93+
-- !following_frame --
94+
1 \N 3 2 a \N
95+
2 c 6 3 a c
96+
3 d3 6 3 a c
97+
4 \N 9 2 d \N
98+
5 d5 9 2 d \N
99+
100+
-- !following_frame --
101+
1 d1 6 3 a c
102+
2 \N 6 3 a c
103+
3 d3 6 3 a c
104+
4 d4 9 2 d \N
105+
5 d5 9 2 d \N
106+
107+
-- !nth_following --
108+
1 \N
109+
2 c
110+
3 c
111+
4 \N
112+
5 \N
113+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
suite("test_lead_following_default", "p0") {
19+
sql "DROP TABLE IF EXISTS lead_following_default"
20+
sql """CREATE TABLE lead_following_default (
21+
id INT, p INT, u UUID, ud UUID, s STRING, sd STRING, v INT, vd INT
22+
) DUPLICATE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES('replication_num'='1')"""
23+
sql """INSERT INTO lead_following_default VALUES
24+
(1,0,'00000000-0000-0000-0000-000000000001','80000000-0000-0000-0000-000000000001','a','d1',10,101),
25+
(2,0,NULL,NULL,NULL,NULL,NULL,NULL),
26+
(3,0,'80000000-0000-0000-0000-000000000003','00000000-0000-0000-0000-000000000003','c','d3',30,103),
27+
(4,1,'ffffffff-ffff-ffff-ffff-ffffffffffff','00000000-0000-0000-0000-000000000004','d','d4',40,104),
28+
(5,1,NULL,'00000000-0000-0000-0000-000000000005',NULL,'d5',NULL,105)"""
29+
30+
// An out-of-partition LEAD must use the current row's default, including the first row
31+
// of each partition. Exercise offset zero, in-range targets and targets beyond the partition.
32+
for (String column : ['u', 's', 'v']) {
33+
for (int offset : [0, 1, 3, 100]) {
34+
qt_defaults """SELECT id,
35+
LEAD(${column},${offset},${column}d) OVER (PARTITION BY p ORDER BY id),
36+
LAG(${column},${offset},${column}d) OVER (PARTITION BY p ORDER BY id),
37+
LEAD(${column},${offset},NULL) OVER (PARTITION BY p ORDER BY id)
38+
FROM lead_following_default ORDER BY id"""
39+
}
40+
}
41+
42+
// The original crash also occurs with folded NULL UUID arguments.
43+
qt_null_uuid """SELECT id,
44+
LEAD(CAST(NULL AS UUID),100,CAST(NULL AS UUID)) OVER (ORDER BY id),
45+
LAG(CAST(NULL AS UUID),100,CAST(NULL AS UUID)) OVER (ORDER BY id)
46+
FROM lead_following_default ORDER BY id"""
47+
48+
// Prefix initialization is still needed for aggregates sharing a FOLLOWING frame.
49+
for (int offset : [1, 100]) {
50+
String frame = "OVER (PARTITION BY p ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND ${offset} FOLLOWING)"
51+
qt_following_frame """SELECT id,
52+
LEAD(s,${offset},sd) OVER (PARTITION BY p ORDER BY id),
53+
SUM(id) ${frame}, COUNT(*) ${frame},
54+
FIRST_VALUE(s) ${frame}, LAST_VALUE(s) ${frame}
55+
FROM lead_following_default ORDER BY id"""
56+
}
57+
qt_nth_following """SELECT id, NTH_VALUE(s,3) OVER
58+
(PARTITION BY p ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)
59+
FROM lead_following_default ORDER BY id"""
60+
}

0 commit comments

Comments
 (0)