diff --git a/cpp/src/common/container/bit_map.cc b/cpp/src/common/container/bit_map.cc index 3b1af6ab2..a83d66efa 100644 --- a/cpp/src/common/container/bit_map.cc +++ b/cpp/src/common/container/bit_map.cc @@ -24,7 +24,9 @@ namespace common { BitMap::~BitMap() { - if (bitmap_ && (size_ > 0)) { + // init(0) still allocates the allocator's 8-byte header block, so free on + // the pointer rather than on the size the bits need. + if (bitmap_ != nullptr) { mem_free(bitmap_); bitmap_ = nullptr; size_ = 0; diff --git a/cpp/src/common/container/byte_buffer.h b/cpp/src/common/container/byte_buffer.h index 0f313acdc..3f4954093 100644 --- a/cpp/src/common/container/byte_buffer.h +++ b/cpp/src/common/container/byte_buffer.h @@ -37,7 +37,9 @@ class ByteBuffer { reserved_size_(0) {} ~ByteBuffer() { - if (data_ && (reserved_size_ > 0)) { + // A zero-sized buffer still owns the allocator's header block, so the + // decision to free must key off the pointer, not the reserved size. + if (data_ != nullptr) { mem_free(data_); data_ = nullptr; real_data_size_ = 0; diff --git a/cpp/src/reader/aligned_chunk_reader.cc b/cpp/src/reader/aligned_chunk_reader.cc index 38252744b..828880b58 100644 --- a/cpp/src/reader/aligned_chunk_reader.cc +++ b/cpp/src/reader/aligned_chunk_reader.cc @@ -719,8 +719,15 @@ int AlignedChunkReader::decode_time_value_buf_into_tsblock( } value_page_col_notnull_bitmap_.clear(); value_page_col_notnull_bitmap_.shrink_to_fit(); - } else { + } else if (ret_tsblock != nullptr && ret_tsblock->get_row_count() > 0) { + // Block is full but holds rows: keep the unfinished page so the + // caller drains this block and comes back for the rest. ret = E_OK; + } else { + // E_OVERFLOW with nothing appended means the block could not hold a + // single row. No input was consumed either, so reporting success + // would make the caller retry the same decode without progress. + ret = E_INVALID_ARG; } return ret; } @@ -832,7 +839,15 @@ int AlignedChunkReader::decode_tv_batch(ByteStream& time_in, const uint32_t null_mask_base = 1 << 7; while (time_decoder_->has_remaining(time_in)) { - if (row_appender.remaining() < (uint32_t)BATCH) { + // Cap each pass to what the appender can still hold; mirrors + // multi_DECODE_TV_BATCH and ChunkReader's per-type batch loops. A + // blanket "remaining < BATCH" guard could never be satisfied by a + // TsBlock whose capacity is below BATCH rows, and it returned + // E_OVERFLOW before consuming any input, so the caller kept retrying + // a block it could never fill. + const int eff_batch = + std::min(BATCH, static_cast(row_appender.remaining())); + if (eff_batch <= 0) { ret = E_OVERFLOW; break; } @@ -872,8 +887,8 @@ int AlignedChunkReader::decode_tv_batch(ByteStream& time_in, } int time_count = 0; - if (RET_FAIL(time_decoder_->read_batch_int64(times, BATCH, time_count, - time_in))) { + if (RET_FAIL(time_decoder_->read_batch_int64(times, eff_batch, + time_count, time_in))) { break; } if (time_count == 0) break; @@ -2685,8 +2700,12 @@ int AlignedChunkReader::decode_time_value_buf_into_tsblock_multi( if (!prev_time_page_not_finish()) { time_in_.reset(); } - } else { + } else if (ret_tsblock != nullptr && ret_tsblock->get_row_count() > 0) { ret = E_OK; + } else { + // Same no-progress case as the single-column path: an empty block + // that cannot hold a row would otherwise be retried forever. + ret = E_INVALID_ARG; } return ret; } diff --git a/cpp/test/reader/aligned_field_filter_regression_test.cc b/cpp/test/reader/aligned_field_filter_regression_test.cc index 9b4540347..c2b7506d1 100644 --- a/cpp/test/reader/aligned_field_filter_regression_test.cc +++ b/cpp/test/reader/aligned_field_filter_regression_test.cc @@ -934,6 +934,91 @@ TEST_P(AlignedFieldFilterRegressionTest, } } +// A TsBlock whose capacity is below the decoder's fixed 129-row batch used to +// stall the first ResultSet::next() forever: the single-column aligned decode +// demanded a full batch before consuming input, so no block could ever be +// filled and the caller retried without progress. +TEST_P(AlignedFieldFilterRegressionTest, + SmallTsBlockCapacityResumesAlignedReads) { + g_config_value_.page_writer_max_point_num_ = 5; + g_config_value_.tsblock_max_memory_ = 128; + TableSchema schema( + "t", {{"id", STRING, UNCOMPRESSED, PLAIN, ColumnCategory::TAG}, + {"v", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"w", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"ghost", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}}); + WriteFile file; + create(file); + TsFileTableWriter writer(&file, &schema); + for (int chunk = 0; chunk < 3; ++chunk) + for (int device = 0; device < 3; ++device) { + std::vector names{"id", "w"}; + std::vector types{STRING, INT64}; + std::vector categories{ColumnCategory::TAG, + ColumnCategory::FIELD}; + if (device != 1) { + names.push_back("v"); + types.push_back(INT64); + categories.push_back(ColumnCategory::FIELD); + } + Tablet tablet("t", names, types, categories, 7); + const std::string id = "d" + std::to_string(device); + for (int r = 0; r < 7; ++r) { + const int t = chunk * 7 + r; + ASSERT_EQ(tablet.add_timestamp(r, t), E_OK); + ASSERT_EQ(tablet.add_value(r, "id", id.c_str()), E_OK); + if (t % 3 != 0) { + ASSERT_EQ( + tablet.add_value(r, "w", int64_t(device * 1000 + t)), + E_OK); + } + if (device != 1 && t % 4 != 0) { + ASSERT_EQ( + tablet.add_value(r, "v", int64_t(device * 100 + t)), + E_OK); + } + } + ASSERT_EQ(writer.write_table(tablet), E_OK); + ASSERT_EQ(writer.flush(), E_OK); + } + ASSERT_EQ(writer.close(), E_OK); + + Rows expected; + for (int device = 0; device < 3; ++device) + for (int t = 0; t < 21; ++t) { + const int64_t v = + (device != 1 && t % 4 != 0) ? device * 100 + t : kNull; + const int64_t w = t % 3 != 0 ? device * 1000 + t : kNull; + expected.push_back({t, v, kNull, w}); + } + // The bug is completeness, not the device interleaving of the output. + Rows actual = scan({"v", "ghost", "w"}, nullptr); + std::sort(actual.begin(), actual.end()); + std::sort(expected.begin(), expected.end()); + EXPECT_EQ(actual, expected); +} + +// A block that cannot hold even one row has no supported capacity; the read +// must surface an error instead of reporting empty successful blocks forever. +TEST_P(AlignedFieldFilterRegressionTest, UnusableTsBlockCapacityReportsError) { + g_config_value_.tsblock_max_memory_ = 8; + write_missing_columns(); + LocalRandomAccessReadFile file; + ASSERT_EQ(file.open(filename_), E_OK); + TableQueryExecutor executor(&file, std::get<1>(GetParam())); + ResultSet* result = nullptr; + int ret = executor.query("t", {"v", "ghost"}, nullptr, 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(),