Copilot commented on code in PR #62835: URL: https://github.com/apache/doris/pull/62835#discussion_r3141642565
########## regression-test/suites/query_p0/adaptive_batch_size/adaptive_batch_size.groovy: ########## @@ -0,0 +1,211 @@ +// 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. + +// Regression tests for the Adaptive Batch Size feature. +// +// Design notes: +// - Each case runs the same query with the feature enabled and disabled, and +// asserts that results are identical (correctness check). +// - We do NOT directly assert internal block byte sizes, because the storage +// layer does not expose them via SQL result columns. Correctness is the +// primary requirement; performance / memory reduction is verified manually +// or via profile counters in a separate benchmark. + +suite("adaptive_batch_size") { + + // ── helpers ──────────────────────────────────────────────────────────────── + + def set_adaptive = { enabled -> + if (enabled) { + set_be_param("enable_adaptive_batch_size", "true") + sql "set preferred_block_size_bytes = 8388608" // 8 MB (default) + sql "set batch_size = 4096" + sql "set preferred_max_column_in_block_size_bytes = 1048576" // 1 MB + } else { + set_be_param("enable_adaptive_batch_size", "false") + sql "set preferred_block_size_bytes = 8388608" + sql "set batch_size = 4096" + sql "set preferred_max_column_in_block_size_bytes = 1048576" + } + } + + try { + // ── Test 1: wide table (VARCHAR columns) ────────────────────────────────── + // Each row is ~10 KB; with 4096 rows that is ~40 MB/batch which OOM-risks. + // With adaptive=on the batch is trimmed to ~8 MB worth of rows. + + sql "drop table if exists abs_wide_table" + sql """ + create table abs_wide_table ( + id int not null, + c1 varchar(4096), + c2 varchar(4096), + c3 varchar(4096) + ) + ENGINE=OLAP + DUPLICATE KEY(id) + DISTRIBUTED BY HASH(id) BUCKETS 1 + PROPERTIES ("replication_allocation" = "tag.location.default: 1") + """ + + // Insert 1000 rows with ~3 KB data each. + def wide_rows = (1..1000).collect { i -> + "(${i}, '${('a' * 1000)}', '${('b' * 1000)}', '${('c' * 1000)}')" + } + sql "insert into abs_wide_table values ${wide_rows.join(',')}" + + // Run query with adaptive enabled and collect result. + set_adaptive(true) + def res_enabled = sql "select id, length(c1) as l1, length(c2) as l2, length(c3) as l3 from abs_wide_table order by 1, 2, 3, 4" + + order_qt_wide "select id, length(c1) as l1, length(c2) as l2, length(c3) as l3 from abs_wide_table order by 1, 2, 3, 4 limit 50" + + // Run query with adaptive disabled and collect result. + set_adaptive(false) + def res_disabled = sql "select id, length(c1) as l1, length(c2) as l2, length(c3) as l3 from abs_wide_table order by 1, 2, 3, 4" + + // Results must be identical. + assertEquals(res_enabled.size(), res_disabled.size()) + for (int i = 0; i < res_enabled.size(); i++) { + assertEquals(res_enabled[i].toString(), res_disabled[i].toString()) + } + + + // ── Test 2: narrow table (INT columns) ─────────────────────────────────── + // Rows are ~12 bytes each; with adaptive=on the predictor should converge + // toward returning close to batch_size (batch is still row-limited). + + sql "drop table if exists abs_narrow_table" + sql """ + create table abs_narrow_table ( + id int not null, + c1 int, + c2 int, + c3 int + ) + ENGINE=OLAP + DUPLICATE KEY(id) + DISTRIBUTED BY HASH(id) BUCKETS 1 + PROPERTIES ("replication_allocation" = "tag.location.default: 1") + """ + + sql "insert into abs_narrow_table select number, number*2, number*3, number*4 from numbers('number'='5000')" + + set_adaptive(true) + def narrow_on = sql "select sum(c1), sum(c2), sum(c3) from abs_narrow_table" + + order_qt_narrow "select sum(c1), sum(c2), sum(c3) from abs_narrow_table" + + set_adaptive(false) + def narrow_off = sql "select sum(c1), sum(c2), sum(c3) from abs_narrow_table" + + assertEquals(narrow_on.toString(), narrow_off.toString()) + + + // ── Test 3: AGG_KEYS table ──────────────────────────────────────────────── + // Verifies that adaptive batch size does not break aggregation correctness + // (the byte check in _agg_key_next_block must only trigger at group boundaries). + + sql "drop table if exists abs_agg_table" + sql """ + create table abs_agg_table ( + id int not null, + val bigint replace + ) + ENGINE=OLAP + AGGREGATE KEY(id) + DISTRIBUTED BY HASH(id) BUCKETS 1 + PROPERTIES ("replication_allocation" = "tag.location.default: 1") + """ + + // 2000 distinct keys, 3 rows per key → 6000 rows total. + def agg_rows = [] + for (int k = 1; k <= 2000; k++) { + agg_rows << "(${k}, ${k})" + agg_rows << "(${k}, ${k * 2})" + agg_rows << "(${k}, ${k * 3})" + } + sql "insert into abs_agg_table values ${agg_rows.join(',')}" + + set_adaptive(true) + def agg_on = sql "select id, val from abs_agg_table order by 1, 2 limit 10" + + order_qt_agg "select id, val from abs_agg_table order by 1, 2 limit 10" + + set_adaptive(false) + def agg_off = sql "select id, val from abs_agg_table order by 1, 2 limit 10" + + assertEquals(agg_on.toString(), agg_off.toString()) + + + // ── Test 4: UNIQUE_KEYS table ───────────────────────────────────────────── + // Verifies that adaptive byte-stop in _unique_key_next_block does not + // cause duplicate or missing rows. + + sql "drop table if exists abs_unique_table" + sql """ + create table abs_unique_table ( + id int not null, + name varchar(200) + ) + ENGINE=OLAP + UNIQUE KEY(id) + DISTRIBUTED BY HASH(id) BUCKETS 1 + PROPERTIES ("replication_allocation" = "tag.location.default: 1") + """ + + sql "insert into abs_unique_table select number, repeat('x', 100) from numbers('number'='3000')" + + set_adaptive(true) + def uniq_on = sql "select count(*), sum(id) from abs_unique_table" + + order_qt_unique "select count(*), sum(id) from abs_unique_table" + + set_adaptive(false) + def uniq_off = sql "select count(*), sum(id) from abs_unique_table" + + assertEquals(uniq_on.toString(), uniq_off.toString()) + + + // ── Test 5: verify setting enable_adaptive_batch_size = false disables adaptive sizing ── + + sql "drop table if exists abs_flag_table" + sql """ + create table abs_flag_table (id int not null, v int) + ENGINE=OLAP + DUPLICATE KEY(id) + DISTRIBUTED BY HASH(id) BUCKETS 1 + PROPERTIES ("replication_allocation" = "tag.location.default: 1") + """ + sql "insert into abs_flag_table select number, number from numbers('number'='100')" + + set_be_param("enable_adaptive_batch_size", "false") + def flag_off = sql "select sum(v) from abs_flag_table" + + order_qt_flag "select sum(v) from abs_flag_table" + + set_be_param("enable_adaptive_batch_size", "true") + def flag_on = sql "select sum(v) from abs_flag_table" + + assertEquals(flag_off.toString(), flag_on.toString()) + } finally { + reset_be_param("enable_adaptive_batch_size") + sql "set preferred_block_size_bytes = 8388608" + sql "set preferred_max_column_in_block_size_bytes = 1048576" + sql "set batch_size = 8160" + } Review Comment: `reset_be_param("enable_adaptive_batch_size")` is not defined in the regression-test Suite DSL (only `get_be_param` / `set_be_param` / `set_original_be_param` exist), so this suite will fail at runtime with a missing-method/property error. Capture the original BE config via `get_be_param("enable_adaptive_batch_size")` (like other suites in this PR) and restore it in `finally` using `set_original_be_param`, or wrap the body in `setBeConfigTemporary([enable_adaptive_batch_size:false])` so cleanup is guaranteed. ########## be/test/storage/iterator/vcollect_iterator_collected_enough_test.cpp: ########## @@ -0,0 +1,383 @@ +// 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. + +#include <gtest/gtest.h> + +#include <cstddef> +#include <cstdint> +#include <limits> +#include <string> + +#include "common/config.h" +#include "core/block/block.h" +#include "core/column/column_string.h" +#include "core/column/column_vector.h" +#include "storage/iterator/block_reader.h" +#include "storage/iterator/vcollect_iterator.h" + +namespace doris { + +// ============================================================================ +// Part 1: Pure-computation tests for estimate_collected_enough() +// ============================================================================ + +class EstimateCollectedEnoughTest : public testing::Test {}; + +// Budget is 0 → always false (feature disabled). +TEST_F(EstimateCollectedEnoughTest, BudgetZeroReturnsFalse) { + EXPECT_FALSE(estimate_collected_enough(/*present_bytes=*/1000, /*present_rows=*/10, + /*rows_to_merge=*/5, + /*preferred_block_size_bytes=*/0)); +} + +// No rows collected yet → always false (cannot estimate). +TEST_F(EstimateCollectedEnoughTest, ZeroRowsReturnsFalse) { + EXPECT_FALSE(estimate_collected_enough(/*present_bytes=*/0, /*present_rows=*/0, + /*rows_to_merge=*/5, + /*preferred_block_size_bytes=*/1024)); +} + +// Present bytes already exceed budget → true. +TEST_F(EstimateCollectedEnoughTest, PresentBytesExceedBudget) { + EXPECT_TRUE(estimate_collected_enough(/*present_bytes=*/2048, /*present_rows=*/10, + /*rows_to_merge=*/0, + /*preferred_block_size_bytes=*/1024)); +} + +// Present bytes exactly equal budget → true. +TEST_F(EstimateCollectedEnoughTest, PresentBytesEqualBudget) { + EXPECT_TRUE(estimate_collected_enough(/*present_bytes=*/1024, /*present_rows=*/10, + /*rows_to_merge=*/0, + /*preferred_block_size_bytes=*/1024)); +} + +// Prediction: 500 bytes / 10 rows = 50 bytes/row. +// With 10 pending rows → predicted 500 + 500 = 1000 < 1024 → false. +TEST_F(EstimateCollectedEnoughTest, PredictionBelowBudget) { + EXPECT_FALSE(estimate_collected_enough(/*present_bytes=*/500, /*present_rows=*/10, + /*rows_to_merge=*/10, + /*preferred_block_size_bytes=*/1024)); +} + +// Prediction: 500 bytes / 10 rows = 50 bytes/row. +// With 11 pending rows → predicted 500 * 21 / 10 = 1050 >= 1024 → true. +TEST_F(EstimateCollectedEnoughTest, PredictionMeetsBudget) { + EXPECT_TRUE(estimate_collected_enough(/*present_bytes=*/500, /*present_rows=*/10, + /*rows_to_merge=*/11, + /*preferred_block_size_bytes=*/1024)); +} + +// Zero pending rows: prediction = present_bytes (no pending rows to flush). +// 500 < 1024 → false. +TEST_F(EstimateCollectedEnoughTest, ZeroPendingRows) { + EXPECT_FALSE(estimate_collected_enough(/*present_bytes=*/500, /*present_rows=*/10, + /*rows_to_merge=*/0, + /*preferred_block_size_bytes=*/1024)); +} + +// Exact boundary: 512 bytes / 8 rows = 64 bytes/row. With 8 pending rows → +// predicted 512 * 16 / 8 = 1024 = budget → true. +TEST_F(EstimateCollectedEnoughTest, ExactBudgetBoundary) { + EXPECT_TRUE(estimate_collected_enough(/*present_bytes=*/512, /*present_rows=*/8, + /*rows_to_merge=*/8, + /*preferred_block_size_bytes=*/1024)); +} + +// One below boundary: 512 bytes / 8 rows = 64 bytes/row. With 7 pending rows → +// predicted 512 * 15 / 8 = 960 < 1024 → false. +TEST_F(EstimateCollectedEnoughTest, OneBelowBudgetBoundary) { + EXPECT_FALSE(estimate_collected_enough(/*present_bytes=*/512, /*present_rows=*/8, + /*rows_to_merge=*/7, + /*preferred_block_size_bytes=*/1024)); +} + +// Overflow guard: present_bytes close to SIZE_MAX; multiplication would wrap → true. +TEST_F(EstimateCollectedEnoughTest, OverflowGuardReturnsTrueForHugeBytes) { + const size_t huge = std::numeric_limits<size_t>::max() / 2 + 1; + EXPECT_TRUE(estimate_collected_enough(/*present_bytes=*/huge, /*present_rows=*/1, + /*rows_to_merge=*/1, + /*preferred_block_size_bytes=*/1024)); +} + +// Large but no overflow: present_bytes * total_rows fits in size_t. +TEST_F(EstimateCollectedEnoughTest, LargeButNoOverflow) { + // 1GB present, 100 rows, 100 pending → 2GB total predicted. + const size_t one_gb = 1ULL << 30; + EXPECT_TRUE(estimate_collected_enough(/*present_bytes=*/one_gb, /*present_rows=*/100, + /*rows_to_merge=*/100, + /*preferred_block_size_bytes=*/one_gb + 1)); +} + +// Single present row with many pending rows should scale correctly. +// 100 bytes / 1 row → 100 bytes/row. With 99 pending → predicted 100 * 100 / 1 = 10000 >= 5000. +TEST_F(EstimateCollectedEnoughTest, SingleRowScalesCorrectly) { + EXPECT_TRUE(estimate_collected_enough(/*present_bytes=*/100, /*present_rows=*/1, + /*rows_to_merge=*/99, + /*preferred_block_size_bytes=*/5000)); +} + +// ============================================================================ +// Part 2: Integration tests — real MutableColumns + estimate_collected_enough +// +// These tests exercise the same code path as collected_enough_rows(): +// present_bytes = Block::columns_byte_size(columns) +// present_rows = columns[0]->size() +// → estimate_collected_enough(present_bytes, present_rows, rows_to_merge, budget) +// +// Level1Iterator::collected_enough_rows() is a private inner class method, so +// we replicate its logic here with real columns to verify end-to-end correctness. +// ============================================================================ + +class CollectedEnoughWithColumnsTest : public testing::Test { +protected: + void SetUp() override { _saved_adaptive = config::enable_adaptive_batch_size; } + void TearDown() override { config::enable_adaptive_batch_size = _saved_adaptive; } + + // Replicate the logic of Level1Iterator::collected_enough_rows() with a + // configurable budget, so we can test the column-byte integration path + // without instantiating a Level1Iterator (private inner class). + static bool collected_enough_rows_sim(const MutableColumns& columns, int rows_to_merge, + size_t preferred_block_size_bytes) { + if (!config::enable_adaptive_batch_size) { + return false; + } + if (preferred_block_size_bytes == 0) { + return false; + } + const auto present_bytes = Block::columns_byte_size(columns); + const auto present_rows = columns.empty() ? 0 : columns[0]->size(); + return estimate_collected_enough(present_bytes, present_rows, rows_to_merge, + preferred_block_size_bytes); + } + + // Build a MutableColumns with N_cols ColumnInt32 columns, each having `nrows` rows. + // Each Int32 is 4 bytes → total = 4 * nrows * ncols bytes. + static MutableColumns make_int32_columns(size_t ncols, size_t nrows) { + MutableColumns cols; + for (size_t c = 0; c < ncols; ++c) { + auto col = ColumnInt32::create(); + for (size_t r = 0; r < nrows; ++r) { + col->insert_value(static_cast<Int32>(r)); + } + cols.push_back(std::move(col)); + } + return cols; + } + + bool _saved_adaptive = false; +}; + +// Config disabled → always false regardless of data. +TEST_F(CollectedEnoughWithColumnsTest, DisabledConfigReturnsFalse) { + config::enable_adaptive_batch_size = false; + auto cols = make_int32_columns(2, 100); + // 2 cols × 100 rows × 4 bytes = 800 bytes; budget = 100 bytes (well below) + EXPECT_FALSE(collected_enough_rows_sim(cols, 0, /*budget=*/100)); +} + +// Config enabled, budget = 0 → always false. +TEST_F(CollectedEnoughWithColumnsTest, ZeroBudgetReturnsFalse) { + config::enable_adaptive_batch_size = true; + auto cols = make_int32_columns(2, 100); + EXPECT_FALSE(collected_enough_rows_sim(cols, 0, /*budget=*/0)); +} + +// Empty columns → present_rows = 0 → cannot estimate → false. +TEST_F(CollectedEnoughWithColumnsTest, EmptyColumnsReturnsFalse) { + config::enable_adaptive_batch_size = true; + MutableColumns empty; + EXPECT_FALSE(collected_enough_rows_sim(empty, 10, /*budget=*/1024)); +} + +// Columns with zero rows → present_rows = 0 → false. +TEST_F(CollectedEnoughWithColumnsTest, ZeroRowColumnsReturnsFalse) { + config::enable_adaptive_batch_size = true; + auto cols = make_int32_columns(3, 0); + EXPECT_FALSE(collected_enough_rows_sim(cols, 5, /*budget=*/100)); +} + +// Single Int32 column, 256 rows → 1024 bytes. +// Budget = 1024 → already met → true (no pending rows needed). +TEST_F(CollectedEnoughWithColumnsTest, SingleColumnExactBudgetMet) { + config::enable_adaptive_batch_size = true; + auto cols = make_int32_columns(1, 256); + // 256 rows × 4 bytes = 1024 bytes + EXPECT_EQ(Block::columns_byte_size(cols), 1024); + EXPECT_TRUE(collected_enough_rows_sim(cols, 0, /*budget=*/1024)); +} + +// Single Int32 column, 255 rows → 1020 bytes < 1024 budget. +// No pending rows → not enough → false. +TEST_F(CollectedEnoughWithColumnsTest, SingleColumnBelowBudgetNoPending) { + config::enable_adaptive_batch_size = true; + auto cols = make_int32_columns(1, 255); + EXPECT_EQ(Block::columns_byte_size(cols), 1020); + EXPECT_FALSE(collected_enough_rows_sim(cols, 0, /*budget=*/1024)); +} + +// Single Int32 column, 255 rows → 1020 bytes. With 1 pending row: +// bytes_per_row = 1020/255 = 4, predicted = 1020 * 256 / 255 = 1024 → meets budget. +TEST_F(CollectedEnoughWithColumnsTest, SingleColumnBelowBudgetWithPending) { + config::enable_adaptive_batch_size = true; + auto cols = make_int32_columns(1, 255); + EXPECT_TRUE(collected_enough_rows_sim(cols, 1, /*budget=*/1024)); +} + +// Multi-column: 4 Int32 columns × 100 rows = 1600 bytes. +// Budget = 2000 → not met. With 25 pending rows: +// predicted = 1600 * 125 / 100 = 2000 → meets budget. +TEST_F(CollectedEnoughWithColumnsTest, MultiColumnPredictionMeetsBudget) { + config::enable_adaptive_batch_size = true; + auto cols = make_int32_columns(4, 100); + EXPECT_EQ(Block::columns_byte_size(cols), 1600); + EXPECT_FALSE(collected_enough_rows_sim(cols, 0, /*budget=*/2000)); + EXPECT_TRUE(collected_enough_rows_sim(cols, 25, /*budget=*/2000)); +} + +// Multi-column: just one row below the prediction threshold. +// 4 cols × 100 rows = 1600 bytes, 24 pending rows: +// predicted = 1600 * 124 / 100 = 1984 < 2000 → false. +TEST_F(CollectedEnoughWithColumnsTest, MultiColumnOneBelowPrediction) { + config::enable_adaptive_batch_size = true; + auto cols = make_int32_columns(4, 100); + EXPECT_FALSE(collected_enough_rows_sim(cols, 24, /*budget=*/2000)); +} + +// Variable-width column: ColumnString with known data sizes. +// ColumnString::byte_size() = chars_size + offsets_size. +// Each offset is sizeof(IColumn::Offset) = 8 bytes (64-bit). +TEST_F(CollectedEnoughWithColumnsTest, StringColumnByteSizeIntegration) { + config::enable_adaptive_batch_size = true; + + auto str_col = ColumnString::create(); + // Insert 10 strings of 10 chars each → chars = 100 bytes, offsets = 10 * 8 = 80. + // Total byte_size = 180 bytes. + for (int i = 0; i < 10; ++i) { + std::string s(10, 'A' + (i % 26)); + str_col->insert_data(s.data(), s.size()); + } + + const size_t expected_bytes = 10 * 10 + 10 * sizeof(IColumn::Offset); + EXPECT_EQ(str_col->byte_size(), expected_bytes); + + MutableColumns cols; + cols.push_back(std::move(str_col)); + + // Budget = expected_bytes → met → true. + EXPECT_TRUE(collected_enough_rows_sim(cols, 0, /*budget=*/expected_bytes)); + // Budget = expected_bytes + 1 → not met with 0 pending → false. + EXPECT_FALSE(collected_enough_rows_sim(cols, 0, /*budget=*/expected_bytes + 1)); +} + +// Mixed columns: Int32 + String together. +TEST_F(CollectedEnoughWithColumnsTest, MixedColumnTypes) { + config::enable_adaptive_batch_size = true; + + auto int_col = ColumnInt32::create(); + auto str_col = ColumnString::create(); + for (int i = 0; i < 50; ++i) { + int_col->insert_value(static_cast<Int32>(i)); + std::string s(20, 'x'); + str_col->insert_data(s.data(), s.size()); + } + // Int32: 50 × 4 = 200 bytes + // String: 50 × 20 chars + 50 × 8 offsets = 1000 + 400 = 1400 bytes + // Total: 1600 bytes + const size_t int_bytes = 50 * sizeof(Int32); + const size_t str_bytes = 50 * 20 + 50 * sizeof(IColumn::Offset); + EXPECT_EQ(int_col->byte_size(), int_bytes); + EXPECT_EQ(str_col->byte_size(), str_bytes); + + MutableColumns cols; + cols.push_back(std::move(int_col)); + cols.push_back(std::move(str_col)); + + const size_t total = int_bytes + str_bytes; + EXPECT_EQ(Block::columns_byte_size(cols), total); + + // Budget met exactly → true. + EXPECT_TRUE(collected_enough_rows_sim(cols, 0, /*budget=*/total)); + // Budget slightly above → not met with 0 pending → false. + EXPECT_FALSE(collected_enough_rows_sim(cols, 0, /*budget=*/total + 1)); + // With 1 pending row: predicted = total * 51 / 50 = total + total/50. + // So meets budget = total + total/50. + EXPECT_TRUE(collected_enough_rows_sim(cols, 1, /*budget=*/total + total / 50)); +} + +// Large number of rows to verify no integer issues with real column byte sizes. +TEST_F(CollectedEnoughWithColumnsTest, LargeRowCountInt32) { + config::enable_adaptive_batch_size = true; + auto cols = make_int32_columns(1, 100000); + // 100000 × 4 = 400000 bytes + EXPECT_EQ(Block::columns_byte_size(cols), 400000); + EXPECT_TRUE(collected_enough_rows_sim(cols, 0, /*budget=*/400000)); + EXPECT_FALSE(collected_enough_rows_sim(cols, 0, /*budget=*/400001)); + // With 1 pending: predicted = 400000 * 100001 / 100000 = 400004 < 400005 → false + EXPECT_FALSE(collected_enough_rows_sim(cols, 1, /*budget=*/400005)); + // predicted = 400004 >= 400004 → true + EXPECT_TRUE(collected_enough_rows_sim(cols, 1, /*budget=*/400004)); +} + +// ============================================================================ +// Part 3: BlockReader.preferred_block_size_bytes() override tests +// +// BlockReader overrides TabletReader::preferred_block_size_bytes() to gate on +// config::enable_adaptive_batch_size. These tests verify that behavior and +// ensure collected_enough_rows() would receive the correct budget value. +// ============================================================================ + +class BlockReaderByteBudgetTest : public testing::Test { +protected: + void SetUp() override { _saved_adaptive = config::enable_adaptive_batch_size; } + void TearDown() override { config::enable_adaptive_batch_size = _saved_adaptive; } + bool _saved_adaptive = false; +}; + +// When adaptive is enabled, preferred_block_size_bytes() returns the configured value. +TEST_F(BlockReaderByteBudgetTest, ReturnsConfiguredBytesWhenEnabled) { + config::enable_adaptive_batch_size = true; + BlockReader reader; + reader._reader_context.preferred_block_size_bytes = 65536; + EXPECT_EQ(reader.preferred_block_size_bytes(), 65536); +} + +// When adaptive is disabled, preferred_block_size_bytes() returns 0 regardless. +TEST_F(BlockReaderByteBudgetTest, ReturnsZeroWhenDisabled) { + config::enable_adaptive_batch_size = false; + BlockReader reader; + reader._reader_context.preferred_block_size_bytes = 65536; + EXPECT_EQ(reader.preferred_block_size_bytes(), 0); +} + +// Default value of preferred_block_size_bytes in reader context is 8MB. +TEST_F(BlockReaderByteBudgetTest, DefaultIsZero) { + config::enable_adaptive_batch_size = true; + BlockReader reader; + EXPECT_EQ(reader.preferred_block_size_bytes(), 8388608UL); Review Comment: The test name `DefaultIsZero` is misleading: the assertion expects the default byte budget to be 8MB when adaptive batch size is enabled. Rename the test to reflect the expected behavior (e.g. `DefaultIs8MB` / `DefaultIsDefaultBudget`) so failures are easier to interpret. ########## fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java: ########## @@ -1294,14 +1297,35 @@ public void checkQuerySlotCount(String slotCnt) { @VarAttrDef.VarAttr(name = HAVE_QUERY_CACHE, flag = VarAttrDef.READ_ONLY) public boolean haveQueryCache = false; - // 4096 minus 16 + 16 bytes padding that in padding pod array + // 8192 minus 16 + 16 bytes padding that in padding pod array. + // This remains the row cap for output blocks even when adaptive byte budgeting is enabled. @VarAttrDef.VarAttr(name = BATCH_SIZE, fuzzy = true, checker = "checkBatchSize", needForward = true) public int batchSize = 8160; Review Comment: The comment explaining `batchSize = 8160` looks mathematically inconsistent: “8192 minus 16 + 16” equals 8192, not 8160. Please correct the explanation (likely 8192 minus 16 minus 16, or otherwise clarify where the 32-row reduction comes from) so future readers understand why 8160 is the default cap. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
