Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions include/paimon/table/source/table_scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#pragma once

#include <cstddef>
#include <limits>
#include <map>
#include <memory>
#include <string>
Expand All @@ -33,6 +35,25 @@
namespace paimon {
class ScanContext;

/// Per-plan limits for an uncached managed append-only batch scan of a full snapshot with Avro
/// manifests. Row ranges are unsupported. A default limit imposes no count restriction.
/// Set explicit finite limits to bound objects. Each zero allows no such objects.
///
/// These counts exclude table and snapshot discovery, schema and index/statistics buffers,
/// decoding buffers (including Arrow and Avro), variable-length field bytes, and container
/// capacity.
struct PlanningLimits {
/// Total rows in the base and delta manifest lists, charged before conversion to objects.
std::size_t max_manifest_list_rows = std::numeric_limits<std::size_t>::max();
/// Rows charged across all selected manifests. With lazy entry decoding, only rows that
/// pass the early partition row filter are charged. Otherwise each decoded Arrow batch
/// is charged in full. Data-column predicates run after these charges.
std::size_t max_manifest_entry_rows = std::numeric_limits<std::size_t>::max();
/// Actual split count before applying the row limit, checked after a bucket is packed and
/// before creating its split objects. Packing groups are bounded by the admitted entries.
std::size_t max_splits = std::numeric_limits<std::size_t>::max();
};

/// A scanner interface for reading table's meta and create a plan.
class PAIMON_EXPORT TableScan {
public:
Expand All @@ -42,6 +63,11 @@ class PAIMON_EXPORT TableScan {
/// @return A Result containing a unique pointer to the `TableScan` instance.
static Result<std::unique_ptr<TableScan>> Create(std::unique_ptr<ScanContext> context);

/// Opt in to planning object limits using the existing executor.
/// Unsupported routes return `NotImplemented`. A limit refusal returns `CapacityError`.
static Result<std::unique_ptr<TableScan>> Create(std::unique_ptr<ScanContext> context,
const PlanningLimits& limits);

virtual ~TableScan() = default;

/// Create a scan plan.
Expand Down
13 changes: 8 additions & 5 deletions src/paimon/core/manifest/manifest_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ Status ManifestFile::ReadSelectedEntries(
const std::string& file_name, std::optional<int64_t> file_size, const RowRangeIndex* row_ranges,
const std::function<Result<bool>(const InternalRow&)>& row_filter,
const std::function<Result<bool>(const ManifestEntry&)>& entry_filter,
std::vector<ManifestEntry>* entries) const {
std::vector<ManifestEntry>* entries, ManifestRowLimit* row_limit) const {
if (!row_ranges && !row_filter) {
return Read(file_name, entry_filter, file_size, entries);
return Read(file_name, entry_filter, file_size, entries, row_limit);
}
return ReadArrowBatches(
file_name, file_size,
[this, row_ranges, &row_filter, &entry_filter,
entries](const std::shared_ptr<arrow::StructArray>& batch) -> Status {
[this, row_ranges, &row_filter, &entry_filter, entries,
row_limit](const std::shared_ptr<arrow::StructArray>& batch) -> Status {
// ManifestMetaReader has aligned both the entry and its nested file schema.
// Only row-range scans need to probe the file columns before entry decoding.
std::shared_ptr<arrow::StructArray> files;
Expand Down Expand Up @@ -194,6 +194,9 @@ Status ManifestFile::ReadSelectedEntries(
continue;
}
}
if (row_limit) {
PAIMON_RETURN_NOT_OK(row_limit->Admit(1));
}
PAIMON_ASSIGN_OR_RAISE(ManifestEntry entry, serializer_->FromRow(row));
if (entry_filter) {
PAIMON_ASSIGN_OR_RAISE(bool keep, entry_filter(entry));
Expand All @@ -205,7 +208,7 @@ Status ManifestFile::ReadSelectedEntries(
}
return Status::OK();
},
/*prepare_reader=*/nullptr);
/*prepare_reader=*/nullptr, /*allow_cache=*/!row_limit);
}

Status ManifestFile::PrepareBucketRead(int32_t bucket,
Expand Down
4 changes: 3 additions & 1 deletion src/paimon/core/manifest/manifest_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
Expand Down Expand Up @@ -82,12 +83,13 @@ class ManifestFile : public ObjectsFile<ManifestEntry> {
/// A null row_ranges or row_filter disables that selection. Unknown ranges are retained.
/// Add and Delete entries use the same selection. The entry filter runs after decoding.
/// The reader validates the entry version before inspecting row ranges or invoking the filter.
/// The optional row_limit limits the selected entries before materializing each object.
Status ReadSelectedEntries(
const std::string& file_name, std::optional<int64_t> file_size,
const RowRangeIndex* row_ranges,
const std::function<Result<bool>(const InternalRow&)>& row_filter,
const std::function<Result<bool>(const ManifestEntry&)>& entry_filter,
std::vector<ManifestEntry>* entries) const;
std::vector<ManifestEntry>* entries, ManifestRowLimit* row_limit = nullptr) const;

private:
Status PrepareBucketRead(int32_t bucket, const std::optional<int32_t>& expected_total_buckets,
Expand Down
95 changes: 95 additions & 0 deletions src/paimon/core/manifest/manifest_file_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,101 @@ TEST_F(ManifestFileTest, TestReadBucketEntriesMaterializesOnlySelectedBucket) {
ASSERT_EQ(1, manifest_cache->SupplierCallCount());
}

TEST_F(ManifestFileTest, SelectedEntryLimitAppliesAfterRowSelection) {
auto pool = GetDefaultPool();
auto dir = UniqueTestDirectory::Create();
ASSERT_TRUE(dir);
// Rewrite a pair of known buckets as an ordinary Avro manifest in the test directory.
const auto entries = ReadManifestEntry("orc", GetDataDir() + "/orc/append_09.db/append_09",
"manifest-3a44a0da-1008-463c-914e-28d271375e24-0", pool);
ASSERT_EQ(2, entries.size());
ASSERT_NE(entries[0].Bucket(), entries[1].Bucket());
auto partition_schema = arrow::schema({arrow::field("f1", arrow::int32())});
ASSERT_OK_AND_ASSIGN(std::shared_ptr<FileFormat> avro, FileFormatFactory::Get("avro", {}));
ASSERT_OK_AND_ASSIGN(
std::shared_ptr<FileStorePathFactory> paths,
FileStorePathFactory::Create(
dir->Str(), partition_schema, {"f1"}, /*default_part_value=*/"", avro->Identifier(),
/*data_file_prefix=*/"data-", /*legacy_partition_name_enabled=*/true,
/*external_paths=*/{}, /*global_index_external_paths=*/{},
/*index_file_in_data_file_dir=*/false, /*external_path_weights=*/{}, pool));
ASSERT_OK_AND_ASSIGN(CoreOptions options, CoreOptions::FromMap({}));
ASSERT_OK_AND_ASSIGN(
std::unique_ptr<ManifestFile> file,
ManifestFile::Create(dir->GetFileSystem(), avro, "zstd", paths,
/*target_file_size=*/1024 * 1024, pool, options, partition_schema));
ASSERT_OK_AND_ASSIGN(std::vector<ManifestFileMeta> metas, file->Write(entries));
ASSERT_EQ(1, metas.size());
const auto& meta = metas.front();
const auto matches_second = [bucket =
entries[1].Bucket()](const InternalRow& row) -> Result<bool> {
return ManifestEntrySerializer::GetBucket(row) == bucket;
};

std::vector<ManifestEntry> selected;
int decoded = 0;
const auto count_decoded = [&decoded](const ManifestEntry&) -> Result<bool> {
++decoded;
return true;
};
ManifestRowLimit reject_selected(0);
auto refusal =
file->ReadSelectedEntries(meta.FileName(), meta.FileSize(), nullptr, matches_second,
count_decoded, &selected, &reject_selected);
EXPECT_TRUE(refusal.IsCapacityError()) << refusal.ToString();
EXPECT_EQ(0, decoded);
EXPECT_TRUE(selected.empty());

ManifestRowLimit admit_selected(1);
ASSERT_OK(file->ReadSelectedEntries(meta.FileName(), meta.FileSize(), nullptr, matches_second,
count_decoded, &selected, &admit_selected));
EXPECT_EQ(1, decoded);
EXPECT_EQ(std::vector<ManifestEntry>({entries[1]}), selected);

ASSERT_OK_AND_ASSIGN(std::vector<ManifestFileMeta> later_metas, file->Write(entries));
ASSERT_EQ(1, later_metas.size());
ASSERT_NE(meta.FileName(), later_metas[0].FileName());
std::vector<ManifestEntry> later_entries;
const auto& later_meta = later_metas[0];
const auto later_refusal =
file->ReadSelectedEntries(later_meta.FileName(), later_meta.FileSize(), nullptr,
matches_second, count_decoded, &later_entries, &admit_selected);
EXPECT_TRUE(later_refusal.IsCapacityError()) << later_refusal.ToString();
EXPECT_TRUE(later_entries.empty());
EXPECT_EQ(1, decoded);

// Filtering out an already decoded entry cannot restore its admission allowance.
std::vector<ManifestEntry> post_filtered;
ManifestRowLimit admit_one(1);
const auto admit_either = [](const InternalRow&) -> Result<bool> { return true; };
const int32_t kept_bucket = entries[1].Bucket();
const auto post_filter = [kept_bucket](const ManifestEntry& entry) -> Result<bool> {
return entry.Bucket() == kept_bucket;
};
const auto post_filter_refusal =
file->ReadSelectedEntries(meta.FileName(), meta.FileSize(), nullptr, admit_either,
post_filter, &post_filtered, &admit_one);
EXPECT_TRUE(post_filter_refusal.IsCapacityError()) << post_filter_refusal.ToString();
EXPECT_TRUE(post_filtered.empty());

// The row passes selection, but materializing it would reject its file kind.
ManifestEntry invalid(FileKind(static_cast<int8_t>(2)), entries[1].Partition(),
entries[1].Bucket(), entries[1].TotalBuckets(), entries[1].File());
using WrittenFile = std::pair<std::string, int64_t>;
ASSERT_OK_AND_ASSIGN(WrittenFile malformed, file->WriteWithoutRolling({invalid}));
ManifestRowLimit reject_before_decode(0);
auto rejected =
file->ReadSelectedEntries(malformed.first, malformed.second, nullptr, matches_second,
count_decoded, &selected, &reject_before_decode);
EXPECT_TRUE(rejected.IsCapacityError()) << rejected.ToString();
ManifestRowLimit allow_decode(1);
ASSERT_NOK_WITH_MSG(
file->ReadSelectedEntries(malformed.first, malformed.second, nullptr, matches_second,
count_decoded, &selected, &allow_decode),
"Unsupported byte value 2 for file kind.");
EXPECT_EQ(std::vector<ManifestEntry>({entries[1]}), selected);
}

// A scan reads manifests whose length the manifest list already recorded. Handing that length over
// is what lets the store skip the metadata request a bare open issues, which on a remote store is
// a round trip paid before any of the file is read.
Expand Down
12 changes: 6 additions & 6 deletions src/paimon/core/manifest/manifest_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,21 @@ class ManifestList : public ObjectsFile<ManifestFileMeta> {
/// @param snapshot The input snapshot.
/// @param manifests The vector to store the manifest file metadata.
/// @return Status indicating whether the operation was successful or not.
Status ReadBaseManifests(const Snapshot& snapshot,
std::vector<ManifestFileMeta>* manifests) const {
Status ReadBaseManifests(const Snapshot& snapshot, std::vector<ManifestFileMeta>* manifests,
ManifestRowLimit* row_limit = nullptr) const {
return Read(snapshot.BaseManifestList(), /*filter=*/nullptr,
snapshot.BaseManifestListSize(), manifests);
snapshot.BaseManifestListSize(), manifests, row_limit);
}

/// Return a `ManifestFileMeta` for each delta manifest in this snapshot.
///
/// @param snapshot The input snapshot.
/// @param manifests The vector to store the manifest file metadata.
/// @return Status indicating whether the operation was successful or not.
Status ReadDeltaManifests(const Snapshot& snapshot,
std::vector<ManifestFileMeta>* manifests) const {
Status ReadDeltaManifests(const Snapshot& snapshot, std::vector<ManifestFileMeta>* manifests,
ManifestRowLimit* row_limit = nullptr) const {
return Read(snapshot.DeltaManifestList(), /*filter=*/nullptr,
snapshot.DeltaManifestListSize(), manifests);
snapshot.DeltaManifestListSize(), manifests, row_limit);
}

/// Return a `ManifestFileMeta` for each changelog manifest in this snapshot.
Expand Down
22 changes: 22 additions & 0 deletions src/paimon/core/manifest/manifest_list_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,28 @@ class ManifestListTest : public testing::Test {
}
};

TEST_F(ManifestListTest, LimitsRowsBeforeReservingManifestMetadata) {
auto dir = UniqueTestDirectory::Create();
ASSERT_TRUE(dir);
auto manifest_list = CreateManifestList("avro", dir->Str(), GetDefaultPool());
const std::vector<ManifestFileMeta> expected = {MakeMeta("manifest-a", 100, 1, 0),
MakeMeta("manifest-b", 200, 1, 0)};
using WrittenFile = std::pair<std::string, int64_t>;
ASSERT_OK_AND_ASSIGN(WrittenFile written, manifest_list->Write(expected));

std::vector<ManifestFileMeta> metas;
const auto initial_capacity = metas.capacity();
ManifestRowLimit reject_two(1);
auto refusal = manifest_list->Read(written.first, nullptr, written.second, &metas, &reject_two);
EXPECT_TRUE(refusal.IsCapacityError()) << refusal.ToString();
EXPECT_TRUE(metas.empty());
EXPECT_EQ(initial_capacity, metas.capacity());

ManifestRowLimit admit_two(2);
ASSERT_OK(manifest_list->Read(written.first, nullptr, written.second, &metas, &admit_two));
EXPECT_EQ(expected, metas);
}

TEST_F(ManifestListTest, DecodedCacheReusesIpcAcrossReaders) {
auto dir = UniqueTestDirectory::Create();
ASSERT_TRUE(dir);
Expand Down
Loading