From 5139c344245b8df46aef6a7566afb5ec8c37a008 Mon Sep 17 00:00:00 2001 From: Colin Lee Date: Thu, 26 Nov 2026 11:16:21 +0800 Subject: [PATCH] fix(cpp): return E_INVALID_ARG when a result block cannot hold one row A table query with a TimeBetween filter and a tsblock_max_memory_ smaller than one result row spun forever in TsFileSeriesScanIterator::get_next: an E_OK with 0 rows was retried while has_more_data() stayed true with no progress. Detect an unusable block (zero max rows) before decoding and surface E_INVALID_ARG, matching the serial decode paths. --- cpp/src/reader/tsfile_series_scan_iterator.cc | 9 +++ .../aligned_field_filter_regression_test.cc | 75 +++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/cpp/src/reader/tsfile_series_scan_iterator.cc b/cpp/src/reader/tsfile_series_scan_iterator.cc index adc1bf130..ba7834e0b 100644 --- a/cpp/src/reader/tsfile_series_scan_iterator.cc +++ b/cpp/src/reader/tsfile_series_scan_iterator.cc @@ -365,6 +365,15 @@ int TsFileSeriesScanIterator::get_next(TsBlock*& ret_tsblock, bool alloc, if (alloc && ret_tsblock == nullptr) { ret_tsblock = alloc_or_reuse_tsblock(); } + // A block whose memory budget cannot hold even one row has no + // usable capacity: get_next_page() would report E_OK with 0 rows + // and no progress, and the 0-row retry below would spin forever. + if (ret_tsblock == nullptr) { + return common::E_OOM; + } + if (ret_tsblock->get_max_row_count() == 0) { + return common::E_INVALID_ARG; + } ret = chunk_reader_->get_next_page(ret_tsblock, filter, *data_pa_, min_time_hint, row_offset_, row_limit_); diff --git a/cpp/test/reader/aligned_field_filter_regression_test.cc b/cpp/test/reader/aligned_field_filter_regression_test.cc index 265a075aa..4abce065a 100644 --- a/cpp/test/reader/aligned_field_filter_regression_test.cc +++ b/cpp/test/reader/aligned_field_filter_regression_test.cc @@ -1034,6 +1034,81 @@ TEST_P(AlignedFieldFilterRegressionTest, UnusableTsBlockCapacityReportsError) { EXPECT_EQ(ret, E_INVALID_ARG); } +// A wide table queried through a TimeBetween filter used to spin forever when +// tsblock_max_memory_ was smaller than one result row: the SSI's 0-row retry +// branch saw has_more_data() stay true without any progress. The read must +// surface E_INVALID_ARG instead of looping. +TEST_P(AlignedFieldFilterRegressionTest, + TimeBetweenFilterUnusableCapacityReportsError) { + g_config_value_.tsblock_max_memory_ = 8; + const int kTags = 10; + const int kFields = 100; + const int kRows = 100; + const int64_t kBase = 1000000; + + std::vector columns; + for (int t = 0; t < kTags; ++t) + columns.emplace_back("tag" + std::to_string(t), STRING, UNCOMPRESSED, + PLAIN, ColumnCategory::TAG); + for (int f = 0; f < kFields; ++f) + columns.emplace_back("f" + std::to_string(f), INT64, UNCOMPRESSED, + PLAIN, ColumnCategory::FIELD); + TableSchema schema("t", columns); + + WriteFile file; + create(file); + TsFileTableWriter writer(&file, &schema); + std::vector names; + std::vector types; + std::vector categories; + for (int t = 0; t < kTags; ++t) { + names.push_back("tag" + std::to_string(t)); + types.push_back(STRING); + categories.push_back(ColumnCategory::TAG); + } + for (int f = 0; f < kFields; ++f) { + names.push_back("f" + std::to_string(f)); + types.push_back(INT64); + categories.push_back(ColumnCategory::FIELD); + } + Tablet tablet("t", names, types, categories, kRows); + for (int r = 0; r < kRows; ++r) { + ASSERT_EQ(tablet.add_timestamp(r, kBase + r), E_OK); + for (int t = 0; t < kTags; ++t) { + const std::string v = "tag" + std::to_string(t) + "_v"; + ASSERT_EQ(tablet.add_value(r, t, v.c_str()), E_OK); + } + for (int f = 0; f < kFields; ++f) + ASSERT_EQ( + tablet.add_value(r, kTags + f, static_cast(r + f)), + E_OK); + } + ASSERT_EQ(writer.write_table(tablet), E_OK); + ASSERT_EQ(writer.close(), E_OK); + + std::vector query_columns; + for (int t = 0; t < kTags; ++t) + query_columns.push_back("tag" + std::to_string(t)); + for (int f = 0; f < kFields; ++f) + query_columns.push_back("f" + std::to_string(f)); + + LocalRandomAccessReadFile rfile; + ASSERT_EQ(rfile.open(filename_), E_OK); + TableQueryExecutor executor(&rfile, std::get<1>(GetParam())); + Filter* tf = new TimeBetween(kBase + 10, kBase + 80, false); + ResultSet* result = nullptr; + int ret = + executor.query("t", query_columns, tf, nullptr, nullptr, 0, -1, result); + if (ret == E_OK) { + ASSERT_NE(result, nullptr); + bool next = false; + ret = result->next(next); + EXPECT_FALSE(next); + executor.destroy_query_data_set(result); + } + EXPECT_EQ(ret, E_INVALID_ARG); +} + INSTANTIATE_TEST_SUITE_P(RowAndBatchParallelModes, AlignedFieldFilterRegressionTest, ::testing::Combine(::testing::Bool(),