Skip to content

Commit af6a803

Browse files
committed
[fix](be) Reserve nullable spill cuts and fill peer results by range
### What problem does this PR solve? Issue Number: None Related PR: #68500 Problem Summary: Window spill replay estimated a retained input slice from serialized row bytes. A NULL fixed-width nullable value serializes without its nested value, but ColumnNullable::cut copies both the null map and nested storage, so a wide all-NULL slice could be admitted with too little memory. Use the in-memory type width for non-constant fixed-width columns while preserving the existing variable-width and constant paths. Replay also evaluated PERCENT_RANK and CUME_DIST once per row even though each peer/output intersection has a single result; fill that intersection in one operation while retaining cancellation checks. Add focused storage-budget and cross-Block/sidecar peer tests. ### Release note Window spill replay accounts for fixed-width nullable input slices and fills peer results by range. ### Check List (For Author) - Test: Regression test and compile checks - Regression test: spill_p0/window_function_spill passed with an isolated ASAN BE and FE cluster. - Unit Test: focused test source compiled; execution was blocked by pre-existing function_string_test.cpp compile errors in the standard BE UT runner. - Manual test: None. - Behavior changed: Yes (spill replay reservation and peer-result fill strategy; SQL results unchanged). - Does this need documentation: No.
1 parent cccbed2 commit af6a803

2 files changed

Lines changed: 126 additions & 7 deletions

File tree

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <utility>
2626

2727
#include "common/config.h"
28+
#include "core/column/column_const.h"
2829
#include "core/column/column_nullable.h"
2930
#include "core/column/column_vector.h"
3031
#include "exec/operator/operator.h"
@@ -257,8 +258,14 @@ size_t AnalyticLocalState::_next_output_reserve_bytes(RuntimeState* state) const
257258
// A retained Block is cut into the next output slice. Count the actual rows rather
258259
// than carrying the previous whole-record read peak into every small slice.
259260
for (const auto& column : *cut_block) {
260-
for (size_t row = cut_start; row < cut_start + rows; ++row) {
261-
bytes += 2 * column.column->serialize_size_at(row);
261+
if (!is_column_const(*column.column) && column.type->have_maximum_size_of_value()) {
262+
// A nullable cut copies both its null map and nested fixed-width values,
263+
// including values whose serialized NULL representation omits the nested bytes.
264+
bytes += 2 * rows * column.type->get_size_of_value_in_memory();
265+
} else {
266+
for (size_t row = cut_start; row < cut_start + rows; ++row) {
267+
bytes += 2 * column.column->serialize_size_at(row);
268+
}
262269
}
263270
}
264271
}
@@ -424,24 +431,28 @@ void AnalyticLocalState::_append_ntile_result(size_t function_index, size_t rows
424431
Status AnalyticLocalState::_append_peer_group_results(
425432
RuntimeState* state, size_t rows, const std::vector<IColumn*>& computed_result_columns) {
426433
const auto function_count = _current_batch->strategies.size();
427-
for (size_t row = 0; row < rows; ++row) {
428-
if ((row & 4095) == 0) {
434+
for (size_t offset = 0; offset < rows;) {
435+
if ((offset & 4095) == 0) {
429436
RETURN_IF_CANCELLED(state);
430437
}
431-
const int64_t batch_row = _batch_output_position + row;
438+
const int64_t batch_row = _batch_output_position + offset;
432439
while (batch_row >= _peer_group_end) {
433440
RETURN_IF_ERROR(_next_peer_group_end(state));
434441
}
435442
DCHECK_GE(_peer_group_start, _partition_start);
436443
DCHECK_LE(_peer_group_end, _partition_end);
444+
// Keep the existing cancellation cadence even when one peer spans many output rows.
445+
const size_t count =
446+
std::min({rows - offset, static_cast<size_t>(_peer_group_end - batch_row),
447+
size_t {4096} - (offset & 4095)});
437448
for (size_t i = 0; i < function_count; ++i) {
438449
if (_current_batch->strategies[i] != WindowSpillStrategy::PEER_GROUP) {
439450
continue;
440451
}
441452
assert_cast<ColumnFloat64&>(*computed_result_columns[i])
442-
.get_data()
443-
.push_back(_current_peer_group_result(i));
453+
.insert_many_vals(_current_peer_group_result(i), count);
444454
}
455+
offset += count;
445456
}
446457
return Status::OK();
447458
}

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
#include "core/block/block.h"
3333
#include "core/column/column_const.h"
3434
#include "core/column/column_dummy.h"
35+
#include "core/column/column_nullable.h"
3536
#include "core/cow.h"
3637
#include "core/data_type/data_type.h"
38+
#include "core/data_type/data_type_nullable.h"
3739
#include "core/data_type/data_type_string.h"
3840
#include "exec/operator/analytic_source_operator.h"
3941
#include "exec/operator/repeat_operator.h"
@@ -609,6 +611,56 @@ TEST_F(AnalyticSinkOperatorTest, SpillAdmissionIncludesFirstAndNextInMemoryCuts)
609611
EXPECT_EQ(source->get_reserve_mem_size(state.get()), first_cut);
610612
}
611613

614+
TEST_F(AnalyticSinkOperatorTest, SpillAdmissionCountsNullableFixedWidthCutStorage) {
615+
Initialize(65535);
616+
prepare_spilled_full_partition_sum();
617+
constexpr size_t block_rows = 65536;
618+
constexpr size_t columns = 32;
619+
const auto nullable_type = make_nullable(std::make_shared<DataTypeInt64>());
620+
const std::vector<int64_t> values(block_rows, 7);
621+
622+
for (bool mixed_nulls : {false, true}) {
623+
std::vector<UInt8> null_map(block_rows, 1);
624+
if (mixed_nulls) {
625+
for (size_t row = 0; row < block_rows; row += 2) {
626+
null_map[row] = 0;
627+
}
628+
}
629+
const auto column = ColumnHelper::create_nullable_column<DataTypeInt64>(values, null_map);
630+
Block block;
631+
for (size_t i = 0; i < columns; ++i) {
632+
block.insert({column, nullable_type, "nullable_input"});
633+
}
634+
auto batch = std::make_shared<AnalyticSpillBatch>();
635+
batch->rows = block_rows;
636+
batch->blocks.push_back(std::move(block));
637+
source_local_state->_current_batch = batch;
638+
639+
const size_t rows = state->batch_size();
640+
const size_t budget = source_local_state->_next_output_reserve_bytes(state.get());
641+
size_t cut_bytes = 0;
642+
for (const auto& input : batch->blocks.front()) {
643+
cut_bytes += input.column->cut(0, rows)->allocated_bytes();
644+
}
645+
EXPECT_EQ(budget, 2 * rows * columns * nullable_type->get_size_of_value_in_memory());
646+
EXPECT_GE(budget, cut_bytes);
647+
}
648+
649+
// A constant NULL remains one stored value after cut, not 65,535 nested Int64 values.
650+
auto null_column = nullable_type->create_column();
651+
null_column->insert_default();
652+
ColumnPtr const_column = ColumnConst::create(std::move(null_column), block_rows);
653+
Block const_block;
654+
const_block.insert({const_column, nullable_type, "constant_null"});
655+
auto batch = std::make_shared<AnalyticSpillBatch>();
656+
batch->rows = block_rows;
657+
batch->blocks.push_back(std::move(const_block));
658+
source_local_state->_current_batch = batch;
659+
const size_t budget = source_local_state->_next_output_reserve_bytes(state.get());
660+
EXPECT_EQ(budget, 2 * static_cast<size_t>(state->batch_size()));
661+
EXPECT_GE(budget, const_column->cut(0, state->batch_size())->allocated_bytes());
662+
}
663+
612664
TEST_F(AnalyticSinkOperatorTest, SpillCardinalityKeepsConstantArgumentsCompact) {
613665
Initialize(65535);
614666
auto local_state = AnalyticSinkLocalState::create_unique(sink.get(), state.get());
@@ -1350,6 +1402,38 @@ size_t replay_read_reserve(const AnalyticSpillBatch::RecordMemory& memory) {
13501402
memory.serialized_bytes + 2 * memory.decoded_bytes;
13511403
}
13521404

1405+
void expect_large_cume_dist_block(const Block& output, size_t* seen) {
1406+
constexpr size_t total_rows = 8002;
1407+
ASSERT_LE(output.rows(), 1024);
1408+
const auto& order = assert_cast<const ColumnInt64&>(*output.get_by_position(0).column);
1409+
const auto& result = assert_cast<const ColumnFloat64&>(*output.get_by_position(1).column);
1410+
for (size_t row = 0; row < output.rows(); ++row, ++(*seen)) {
1411+
int64_t expected_order = *seen < 5000 ? 1 : 2;
1412+
double expected_result = *seen < 5000 ? 5000.0 / total_rows : 8001.0 / total_rows;
1413+
if (*seen == 8001) {
1414+
expected_order = 3;
1415+
expected_result = 1.0;
1416+
}
1417+
EXPECT_EQ(order.get_data()[row], expected_order);
1418+
EXPECT_EQ(result.get_data()[row], expected_result);
1419+
}
1420+
}
1421+
1422+
void expect_large_cume_dist_peer_output(AnalyticSourceOperatorX* source, RuntimeState* state) {
1423+
size_t seen = 0;
1424+
bool eos = false;
1425+
while (!eos) {
1426+
Block output;
1427+
const auto status = source->get_block(state, &output, &eos);
1428+
ASSERT_TRUE(status.ok()) << status.to_string();
1429+
if (eos) {
1430+
break;
1431+
}
1432+
expect_large_cume_dist_block(output, &seen);
1433+
}
1434+
EXPECT_EQ(seen, 8002);
1435+
}
1436+
13531437
} // namespace
13541438

13551439
TEST_F(AnalyticSinkOperatorTest, SpillPathCoalescesSmallBlocksAndSlicesReplay) {
@@ -1601,6 +1685,30 @@ TEST_F(AnalyticSinkOperatorTest, SpillPathReplaysPeerGroupsFromSidecarFile) {
16011685
expect_spill_source_eos(source.get(), state.get());
16021686
}
16031687

1688+
TEST_F(AnalyticSinkOperatorTest, SpillPathFillsPeerRangesAcrossOutputBlocksAndSidecar) {
1689+
ASSERT_TRUE(spill_file_manager.init().ok());
1690+
Initialize(1024);
1691+
prepare_cume_dist_without_partition();
1692+
1693+
// The first large peer crosses output Blocks. Revocation writes its end to the sidecar;
1694+
// the next peer crosses both the data-record and sidecar-record boundaries.
1695+
std::vector<int64_t> first_values(5000, 1);
1696+
first_values.push_back(2);
1697+
Block first = ColumnHelper::create_block<DataTypeInt64>(first_values);
1698+
auto status = sink->sink(state.get(), &first, false);
1699+
ASSERT_TRUE(status.ok()) << status.to_string();
1700+
status = sink->revoke_memory(state.get());
1701+
ASSERT_TRUE(status.ok()) << status.to_string();
1702+
ASSERT_TRUE(sink_local_state->_batch_store->has_spilled_peer_groups());
1703+
1704+
std::vector<int64_t> second_values(3000, 2);
1705+
second_values.push_back(3);
1706+
Block second = ColumnHelper::create_block<DataTypeInt64>(second_values);
1707+
status = sink->sink(state.get(), &second, true);
1708+
ASSERT_TRUE(status.ok()) << status.to_string();
1709+
expect_large_cume_dist_peer_output(source.get(), state.get());
1710+
}
1711+
16041712
TEST_F(AnalyticSinkOperatorTest, SpillPathFlushesPeerGroupsAtSpillBufferSize) {
16051713
ASSERT_TRUE(spill_file_manager.init().ok());
16061714

0 commit comments

Comments
 (0)