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
3 changes: 3 additions & 0 deletions docs/source/api/write.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Write
Interface
=========

.. doxygenclass:: paimon::AppendFileCompaction
:members:

.. doxygenclass:: paimon::FileStoreWrite
:members:
:undoc-members:
Expand Down
37 changes: 23 additions & 14 deletions docs/source/user_guide/compaction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,31 @@ respectively. Adjust ``target-file-size`` to move the threshold.

Custom Compaction
~~~~~~~~~~~~~~~~~
``FileStoreWrite::CompactFiles(partition, bucket, file_names)`` asynchronously
rewrites selected files, accepting partition values, a bucket, and distinct data
file basenames. As with ``Compact``, each partition-bucket restores the latest
snapshot when its writer is initialized and reuses that state. Files need not be
contiguous; compaction thresholds do not apply.
``AppendFileCompaction::Execute(table_path, branch, partition, bucket,
file_names, options, file_system, pool, executor)`` synchronously rewrites selected
files and returns commit messages. Each call creates a dedicated batch writer
and restores the latest snapshot for the specified partition-bucket. Input files
must be distinct data file basenames. Files need not be contiguous; compaction
thresholds do not apply.

Supports fixed-bucket append tables with or without deletion vectors. Requires
``bucket-append-ordered = false``, ``write-only = false`` and a dedicated writer
with the full schema. Real-time mode and data evolution are unsupported.

Use ``PrepareCommit`` to collect results and background errors, following the
usual ``Compact`` waiting rules. Commit or abort the returned messages through
``FileStoreCommit``.

Do not update deletion vectors or run another compaction for the same
partition-bucket until the custom compaction result is committed or discarded.
``bucket-append-ordered = false`` and ``write-only = false``. The internal writer
always uses the full schema. Real-time mode and data evolution are unsupported.

The call waits for compaction and closes its internal writer before returning.
Commit or abort the returned messages through ``FileStoreCommit``. All arguments
are explicit; pass an empty branch for main and ``nullptr`` for the default file
system, memory pool or executor. Failures may leave orphan files for later cleanup.

With deletion vectors enabled, callers must serialize compactions within each
partition-bucket, even when input files are disjoint. From the start of a compaction
until its returned messages have been committed or discarded, do not start another
compaction, update deletion vectors or overwrite the same partition-bucket.

Without deletion vectors, compactions of disjoint input files may run concurrently
in the same partition-bucket. For example, compacting ``f1, f2`` into ``a`` and
``f3, f4`` into ``b`` is allowed. Overlapping compaction or overwrite that removes
another task's input files may cause its commit to fail.

Dictionary Passthrough
~~~~~~~~~~~~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions include/paimon/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#pragma once

#include "paimon/append_file_compaction.h" // IWYU pragma: export
#include "paimon/commit_context.h" // IWYU pragma: export
#include "paimon/defs.h" // IWYU pragma: export
#include "paimon/factories/factory.h" // IWYU pragma: export
Expand Down
72 changes: 72 additions & 0 deletions include/paimon/append_file_compaction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.
*/

#pragma once

#include <cstdint>
#include <map>
#include <memory>
#include <string>
#include <vector>

#include "paimon/commit_message.h"
#include "paimon/result.h"
#include "paimon/visibility.h"

namespace paimon {
class Executor;
class FileSystem;
class MemoryPool;

/// Rewrites explicitly selected files in a fixed-bucket append table.
// TODO(xinyu.lxy): Support catalog-based compaction.
class PAIMON_EXPORT AppendFileCompaction {
public:
AppendFileCompaction() = delete;
~AppendFileCompaction() = delete;

/// Synchronously compacts files and returns messages for FileStoreCommit::Commit or Abort.
/// Each call creates a dedicated batch writer and restores the latest snapshot for the bucket.
/// Supports deletion vectors; real-time mode and data evolution are unsupported.
/// Requires bucket-append-ordered=false and write-only=false.
/// With deletion vectors enabled, callers must serialize compactions within each
/// partition/bucket, even for disjoint inputs. Do not start another compaction, update deletion
/// vectors or overwrite this partition/bucket until the messages are committed or discarded.
/// Without deletion vectors, compactions of disjoint input files may run concurrently in the
/// same partition/bucket. Overlapping compaction or overwrite may cause commit conflicts.
/// Failures may leave orphan files for later cleanup.
///
/// @param table_path The root path of the table.
/// @param branch The branch to use; an empty string selects main.
/// @param partition Complete partition values; empty for an unpartitioned table.
/// @param bucket The bucket containing the input files.
/// @param file_names Nonempty, distinct data file basenames. Files need not be contiguous.
/// @param options Options overriding the table options.
/// @param file_system The file system; nullptr selects one from options.
/// @param pool The memory pool; nullptr uses the default pool.
/// @param executor The executor; nullptr uses the default executor.
static Result<std::vector<std::shared_ptr<CommitMessage>>> Execute(
const std::string& table_path, const std::string& branch,
const std::map<std::string, std::string>& partition, int32_t bucket,
const std::vector<std::string>& file_names,
const std::map<std::string, std::string>& options,
const std::shared_ptr<FileSystem>& file_system, const std::shared_ptr<MemoryPool>& pool,
const std::shared_ptr<Executor>& executor);
};

} // namespace paimon
12 changes: 0 additions & 12 deletions include/paimon/file_store_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,6 @@ class PAIMON_EXPORT FileStoreWrite {
virtual Status Compact(const std::map<std::string, std::string>& partition, int32_t bucket,
bool full_compaction) = 0;

/// Asynchronously compacts requested files from the writer's restored state, as with Compact().
/// Supports fixed-bucket append tables with or without deletion vectors.
/// Requires bucket-append-ordered=false, write-only=false and the full write schema.
/// Use a dedicated writer. Real-time mode and data evolution are unsupported.
/// Accepts complete partition values and distinct data file basenames.
/// Files need not be contiguous.
/// Collect results and background errors through PrepareCommit(), as with Compact().
/// Do not update deletion vectors or compact the same partition-bucket until commit or discard.
/// TODO(xinyu.lxy): Consider extracting this API into a separate interface.
virtual Status CompactFiles(const std::map<std::string, std::string>& partition, int32_t bucket,
const std::vector<std::string>& file_names);

/// Generate a list of commit messages with the latest generated data file meta
/// information of the current snapshot.
///
Expand Down
1 change: 1 addition & 0 deletions src/paimon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ set(PAIMON_CORE_SRCS
core/operation/data_evolution_split_read.cpp
core/operation/abstract_file_store_write.cpp
core/operation/abstract_split_read.cpp
core/operation/append_file_compaction.cpp
core/operation/append_only_file_store_scan.cpp
core/operation/append_only_file_store_write.cpp
core/operation/metrics/commit_metrics.cpp
Expand Down
66 changes: 66 additions & 0 deletions src/paimon/core/operation/append_file_compaction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 "paimon/append_file_compaction.h"

#include <utility>

#include "paimon/common/utils/scope_guard.h"
#include "paimon/core/operation/append_only_file_store_write.h"
#include "paimon/file_store_write.h"
#include "paimon/write_context.h"

namespace paimon {

Result<std::vector<std::shared_ptr<CommitMessage>>> AppendFileCompaction::Execute(
const std::string& table_path, const std::string& branch,
const std::map<std::string, std::string>& partition, int32_t bucket,
const std::vector<std::string>& file_names, const std::map<std::string, std::string>& options,
const std::shared_ptr<FileSystem>& file_system, const std::shared_ptr<MemoryPool>& pool,
const std::shared_ptr<Executor>& executor) {
WriteContextBuilder builder(table_path, "append-file-compaction");
builder.WithBranch(branch)
.SetOptions(options)
.WithStreamingMode(false)
.WithIgnorePreviousFiles(false)
.WithIgnoreNumBucketCheck(false)
.WithFileSystem(file_system);
if (pool) {
builder.WithMemoryPool(pool);
}
if (executor) {
builder.WithExecutor(executor);
}
PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<WriteContext> context, builder.Finish());
PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<FileStoreWrite> write,
FileStoreWrite::Create(std::move(context)));
ScopeGuard close_guard([&]() { (void)write->Close(); });
auto* append_write = dynamic_cast<AppendOnlyFileStoreWrite*>(write.get());
if (!append_write) {
return Status::NotImplemented("custom compaction only supports fixed-bucket append tables");
}
PAIMON_RETURN_NOT_OK(append_write->CompactFiles(partition, bucket, file_names));
PAIMON_ASSIGN_OR_RAISE(std::vector<std::shared_ptr<CommitMessage>> messages,
write->PrepareCommit());
// PrepareCommit drains the increments; Close leaves their files available for commit or abort.
close_guard.Release();
PAIMON_RETURN_NOT_OK(write->Close());
return messages;
}

} // namespace paimon
21 changes: 0 additions & 21 deletions src/paimon/core/operation/append_only_file_store_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,6 @@ AppendOnlyFileStoreWrite::AppendOnlyFileStoreWrite(

AppendOnlyFileStoreWrite::~AppendOnlyFileStoreWrite() = default;

Status AppendOnlyFileStoreWrite::Write(std::unique_ptr<RecordBatch>&& batch) {
if (custom_compaction_) {
return Status::Invalid("cannot mix custom compaction with Write or Compact");
}
regular_write_ = true;
return AbstractFileStoreWrite::Write(std::move(batch));
}

Status AppendOnlyFileStoreWrite::Compact(const std::map<std::string, std::string>& partition,
int32_t bucket, bool full_compaction) {
if (custom_compaction_) {
return Status::Invalid("cannot mix custom compaction with Write or Compact");
}
regular_write_ = true;
return AbstractFileStoreWrite::Compact(partition, bucket, full_compaction);
}

Status AppendOnlyFileStoreWrite::CompactFiles(const std::map<std::string, std::string>& partition,
int32_t bucket,
const std::vector<std::string>& file_names) {
Expand All @@ -163,9 +146,6 @@ Status AppendOnlyFileStoreWrite::CompactFiles(const std::map<std::string, std::s
if (write_cols_) {
return Status::Invalid("custom compaction requires the full write schema");
}
if (regular_write_) {
return Status::Invalid("cannot mix custom compaction with Write or Compact");
}
if (bucket < 0 || bucket >= options_.GetBucket()) {
return Status::Invalid("custom compaction bucket is out of range");
}
Expand All @@ -179,7 +159,6 @@ Status AppendOnlyFileStoreWrite::CompactFiles(const std::map<std::string, std::s
PAIMON_RETURN_NOT_OK(PathUtil::CheckSinglePathComponent("data file", name));
}
PAIMON_ASSIGN_OR_RAISE(BinaryRow part, file_store_path_factory_->ToBinaryRow(partition));
custom_compaction_ = true;
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<BatchWriter> writer, GetWriter(part, bucket));
auto append_writer = std::dynamic_pointer_cast<AppendOnlyWriter>(writer);
if (!append_writer) {
Expand Down
15 changes: 6 additions & 9 deletions src/paimon/core/operation/append_only_file_store_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#pragma once

#include <atomic>
#include <cstdint>
#include <map>
#include <memory>
Expand Down Expand Up @@ -92,12 +91,6 @@ class AppendOnlyFileStoreWrite : public AbstractFileStoreWrite {

Status RefreshCommittedSnapshot(int64_t snapshot_id) override;

Status Write(std::unique_ptr<RecordBatch>&& batch) override;
Status Compact(const std::map<std::string, std::string>& partition, int32_t bucket,
bool full_compaction) override;
Status CompactFiles(const std::map<std::string, std::string>& partition, int32_t bucket,
const std::vector<std::string>& file_names) override;

/// Rewrites the given files into new compacted files.
///
/// @param partition The partition of the files.
Expand All @@ -112,6 +105,12 @@ class AppendOnlyFileStoreWrite : public AbstractFileStoreWrite {
const std::shared_ptr<CancellationController>& cancellation_controller);

private:
friend class AppendFileCompaction;

// Only called on a fresh, dedicated writer owned by AppendFileCompaction.
Status CompactFiles(const std::map<std::string, std::string>& partition, int32_t bucket,
const std::vector<std::string>& file_names);

using WriterFactory =
std::shared_ptr<SingleFileWriterFactory<::ArrowArray*, std::shared_ptr<DataFileMeta>>>;

Expand Down Expand Up @@ -169,8 +168,6 @@ class AppendOnlyFileStoreWrite : public AbstractFileStoreWrite {
const std::shared_ptr<ShreddingWritePlanFactory>& plan_factory) const;

std::optional<std::vector<std::string>> write_cols_;
std::atomic<bool> custom_compaction_{false};
std::atomic<bool> regular_write_{false};
std::shared_ptr<RealtimeContext> realtime_context_;
std::unique_ptr<Logger> logger_;
};
Expand Down
Loading
Loading