Skip to content
Merged
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
32 changes: 23 additions & 9 deletions src/paimon/core/table/system/metadata_system_tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,21 @@ Result<std::shared_ptr<FileStorePathFactory>> CreatePathFactory(
return path_factory;
}

Result<std::optional<Snapshot>> LatestSnapshot(const MetadataSystemTableContext& context) {
// Reads the snapshot pinned by `scan.snapshot-id`, or the latest snapshot when the option is unset.
// A pinned snapshot that does not exist is an error instead of a fallback to the latest snapshot.
Result<std::optional<Snapshot>> SelectedSnapshot(const MetadataSystemTableContext& context,
const CoreOptions& core_options) {
SnapshotManager snapshot_manager(context.fs, context.table_path, context.branch);
return snapshot_manager.LatestSnapshot();
std::optional<int64_t> snapshot_id = core_options.GetScanSnapshotId();
if (snapshot_id == std::nullopt) {
return snapshot_manager.LatestSnapshot();
}
PAIMON_ASSIGN_OR_RAISE(bool exists, snapshot_manager.SnapshotExists(snapshot_id.value()));
if (!exists) {
return Status::NotExist(fmt::format("snapshot {} does not exist", snapshot_id.value()));
}
PAIMON_ASSIGN_OR_RAISE(Snapshot snapshot, snapshot_manager.LoadSnapshot(snapshot_id.value()));
return std::optional<Snapshot>(std::move(snapshot));
}

Result<std::vector<ManifestFileMeta>> ReadDataManifests(
Expand Down Expand Up @@ -255,11 +267,12 @@ Result<std::unique_ptr<ManifestFile>> CreateManifestFile(
partition_schema);
}

Result<std::vector<ManifestEntry>> ReadLatestManifestEntries(
Result<std::vector<ManifestEntry>> ReadSelectedManifestEntries(
const MetadataSystemTableContext& context,
const std::shared_ptr<FileStorePathFactory>& path_factory, const CoreOptions& core_options,
const std::shared_ptr<MemoryPool>& pool) {
PAIMON_ASSIGN_OR_RAISE(std::optional<Snapshot> snapshot, LatestSnapshot(context));
PAIMON_ASSIGN_OR_RAISE(std::optional<Snapshot> snapshot,
SelectedSnapshot(context, core_options));
if (!snapshot) {
return std::vector<ManifestEntry>();
}
Expand All @@ -276,12 +289,12 @@ Result<std::vector<ManifestEntry>> ReadLatestManifestEntries(
return entries;
}

Result<std::vector<ManifestEntry>> ReadLatestDataFiles(
Result<std::vector<ManifestEntry>> ReadSelectedDataFiles(
const MetadataSystemTableContext& context,
const std::shared_ptr<FileStorePathFactory>& path_factory, const CoreOptions& core_options,
const std::shared_ptr<MemoryPool>& pool) {
PAIMON_ASSIGN_OR_RAISE(std::vector<ManifestEntry> entries,
ReadLatestManifestEntries(context, path_factory, core_options, pool));
ReadSelectedManifestEntries(context, path_factory, core_options, pool));
std::vector<ManifestEntry> merged_entries;
PAIMON_RETURN_NOT_OK(FileEntry::MergeEntries(entries, &merged_entries));
return merged_entries;
Expand Down Expand Up @@ -798,13 +811,14 @@ Result<std::shared_ptr<arrow::Schema>> ManifestsSystemTable::ArrowSchema() const

Result<std::vector<GenericRow>> ManifestsSystemTable::BuildRows() const {
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<arrow::Schema> schema, ArrowSchema());
PAIMON_ASSIGN_OR_RAISE(std::optional<Snapshot> snapshot, LatestSnapshot(context_));
PAIMON_ASSIGN_OR_RAISE(CoreOptions core_options, CreateCoreOptions(context_));
PAIMON_ASSIGN_OR_RAISE(std::optional<Snapshot> snapshot,
SelectedSnapshot(context_, core_options));
if (!snapshot) {
return std::vector<GenericRow>();
}

std::shared_ptr<MemoryPool> pool = GetDefaultPool();
PAIMON_ASSIGN_OR_RAISE(CoreOptions core_options, CreateCoreOptions(context_));
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<FileStorePathFactory> path_factory,
CreatePathFactory(context_, core_options, pool));
PAIMON_ASSIGN_OR_RAISE(
Expand Down Expand Up @@ -884,7 +898,7 @@ Result<std::vector<GenericRow>> FilesSystemTable::BuildRows() const {
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<FileStorePathFactory> path_factory,
CreatePathFactory(context_, core_options, pool));
PAIMON_ASSIGN_OR_RAISE(std::vector<ManifestEntry> entries,
ReadLatestDataFiles(context_, path_factory, core_options, pool));
ReadSelectedDataFiles(context_, path_factory, core_options, pool));
std::shared_ptr<arrow::Schema> arrow_schema =
DataField::ConvertDataFieldsToArrowSchema(context_.table_schema->Fields());
PAIMON_ASSIGN_OR_RAISE(
Expand Down
55 changes: 55 additions & 0 deletions test/inte/read_inte_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,61 @@ TEST(SystemTableReadInteTest, TestReadMetadataSystemTables) {
ASSERT_FALSE(creation_time_array->IsNull(0));
}

TEST(SystemTableReadInteTest, TestReadMetadataSystemTablesAtPinnedSnapshot) {
arrow::FieldVector fields = {
arrow::field("pk", arrow::utf8()),
arrow::field("v", arrow::int32()),
};
auto schema = arrow::schema(fields);
std::map<std::string, std::string> options = {
{Options::FILE_SYSTEM, "local"}, {Options::FILE_FORMAT, "parquet"}, {Options::BUCKET, "1"}};
auto dir = UniqueTestDirectory::Create();
ASSERT_TRUE(dir);
ASSERT_OK_AND_ASSIGN(auto helper, TestHelper::Create(dir->Str(), schema,
/*partition_keys=*/{},
/*primary_keys=*/{"pk"}, options,
/*is_streaming_mode=*/true));
ASSERT_OK_AND_ASSIGN(std::unique_ptr<RecordBatch> batch_1,
TestHelper::MakeRecordBatch(arrow::struct_(fields), R"([["a", 1]])",
/*partition_map=*/{}, /*bucket=*/0, {}));
ASSERT_OK(helper->WriteAndCommit(std::move(batch_1), /*commit_identifier=*/0,
/*expected_commit_messages=*/std::nullopt));
ASSERT_OK_AND_ASSIGN(std::unique_ptr<RecordBatch> batch_2,
TestHelper::MakeRecordBatch(arrow::struct_(fields), R"([["b", 2]])",
/*partition_map=*/{}, /*bucket=*/0, {}));
ASSERT_OK(helper->WriteAndCommit(std::move(batch_2), /*commit_identifier=*/1,
/*expected_commit_messages=*/std::nullopt));
std::string table_path = PathUtil::JoinPath(dir->Str(), "foo.db/bar");

ASSERT_OK_AND_ASSIGN(auto latest_files, ReadSystemTable(table_path + "$files", options));
ASSERT_EQ(SingleStructChunk(latest_files)->length(), 2);
ASSERT_OK_AND_ASSIGN(auto latest_manifests,
ReadSystemTable(table_path + "$manifests", options));
const int64_t latest_manifest_count = SingleStructChunk(latest_manifests)->length();

std::map<std::string, std::string> pinned_options = options;
pinned_options[Options::SCAN_SNAPSHOT_ID] = "1";
ASSERT_OK_AND_ASSIGN(auto pinned_files, ReadSystemTable(table_path + "$files", pinned_options));
auto pinned_files_array = SingleStructChunk(pinned_files);
ASSERT_EQ(pinned_files_array->length(), 1);
auto record_count_array =
std::dynamic_pointer_cast<arrow::Int64Array>(pinned_files_array->field(6));
ASSERT_TRUE(record_count_array);
ASSERT_EQ(record_count_array->Value(0), 1);
ASSERT_OK_AND_ASSIGN(auto pinned_manifests,
ReadSystemTable(table_path + "$manifests", pinned_options));
ASSERT_EQ(SingleStructChunk(pinned_manifests)->length(), 1);
ASSERT_LT(SingleStructChunk(pinned_manifests)->length(), latest_manifest_count);

// A pinned snapshot that does not exist must not fall back to the latest snapshot.
std::map<std::string, std::string> missing_options = options;
missing_options[Options::SCAN_SNAPSHOT_ID] = "3";
ASSERT_NOK_WITH_MSG(ReadSystemTable(table_path + "$files", missing_options),
"snapshot 3 does not exist");
ASSERT_NOK_WITH_MSG(ReadSystemTable(table_path + "$manifests", missing_options),
"snapshot 3 does not exist");
}

TEST(SystemTableReadInteTest, TestReadOptimizedSystemTable) {
arrow::FieldVector fields = {
arrow::field("k", arrow::int32()),
Expand Down
Loading