Skip to content

Commit d61e847

Browse files
committed
[core] Remove branch merge from catalog API
1 parent f41c7af commit d61e847

10 files changed

Lines changed: 120 additions & 209 deletions

File tree

paimon-core/src/main/java/org/apache/paimon/catalog/AbstractCatalog.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,12 +631,6 @@ public void fastForward(Identifier identifier, String branch) throws BranchNotEx
631631
throw new UnsupportedOperationException();
632632
}
633633

634-
@Override
635-
public void mergeBranch(Identifier identifier, String sourceBranch, String targetBranch)
636-
throws BranchNotExistException {
637-
throw new UnsupportedOperationException();
638-
}
639-
640634
@Override
641635
public List<String> listBranches(Identifier identifier) throws TableNotExistException {
642636
throw new UnsupportedOperationException();

paimon-core/src/main/java/org/apache/paimon/catalog/Catalog.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -920,19 +920,6 @@ void renameBranch(Identifier identifier, String fromBranch, String toBranch)
920920
*/
921921
void fastForward(Identifier identifier, String branch) throws BranchNotExistException;
922922

923-
/**
924-
* Merge source branch into target branch.
925-
*
926-
* @param identifier path of the table, cannot be system or branch name.
927-
* @param sourceBranch the source branch name
928-
* @param targetBranch the target branch name
929-
* @throws BranchNotExistException if the source or target branch doesn't exist
930-
* @throws UnsupportedOperationException if the catalog does not {@link
931-
* #supportsVersionManagement()}
932-
*/
933-
void mergeBranch(Identifier identifier, String sourceBranch, String targetBranch)
934-
throws BranchNotExistException;
935-
936923
/**
937924
* List all branches of the table.
938925
*

paimon-core/src/main/java/org/apache/paimon/catalog/DelegateCatalog.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,6 @@ public void fastForward(Identifier identifier, String branch) throws BranchNotEx
263263
wrapped.fastForward(identifier, branch);
264264
}
265265

266-
@Override
267-
public void mergeBranch(Identifier identifier, String sourceBranch, String targetBranch)
268-
throws BranchNotExistException {
269-
wrapped.mergeBranch(identifier, sourceBranch, targetBranch);
270-
}
271-
272266
@Override
273267
public List<String> listBranches(Identifier identifier) throws TableNotExistException {
274268
return wrapped.listBranches(identifier);

paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalog.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -783,12 +783,6 @@ public void fastForward(Identifier identifier, String branch) throws BranchNotEx
783783
}
784784
}
785785

786-
@Override
787-
public void mergeBranch(Identifier identifier, String sourceBranch, String targetBranch)
788-
throws BranchNotExistException {
789-
throw new UnsupportedOperationException("Branch merge is not supported via REST catalog.");
790-
}
791-
792786
@Override
793787
public List<String> listBranches(Identifier identifier) throws TableNotExistException {
794788
try {

paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@
5555
import org.apache.paimon.table.source.snapshot.TimeTravelUtil;
5656
import org.apache.paimon.tag.TagAutoManager;
5757
import org.apache.paimon.utils.BranchManager;
58+
import org.apache.paimon.utils.BranchMergeHandler;
5859
import org.apache.paimon.utils.CatalogBranchManager;
5960
import org.apache.paimon.utils.ChangelogManager;
6061
import org.apache.paimon.utils.DVMetaCache;
61-
import org.apache.paimon.utils.DefaultBranchMergeHandler;
6262
import org.apache.paimon.utils.FileSystemBranchManager;
6363
import org.apache.paimon.utils.Preconditions;
6464
import org.apache.paimon.utils.SegmentsCache;
@@ -760,7 +760,7 @@ public BranchManager branchManager() {
760760
snapshotManager(),
761761
tagManager(),
762762
schemaManager(),
763-
new DefaultBranchMergeHandler(this::switchToBranch));
763+
new BranchMergeHandler(this::switchToBranch));
764764
}
765765

766766
@Override

paimon-core/src/main/java/org/apache/paimon/utils/BranchMergeHandler.java

Lines changed: 115 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,127 @@
1818

1919
package org.apache.paimon.utils;
2020

21+
import org.apache.paimon.CoreOptions;
22+
import org.apache.paimon.Snapshot;
23+
import org.apache.paimon.data.BinaryRow;
24+
import org.apache.paimon.io.CompactIncrement;
25+
import org.apache.paimon.io.DataFileMeta;
26+
import org.apache.paimon.io.DataIncrement;
2127
import org.apache.paimon.manifest.FileEntry;
28+
import org.apache.paimon.manifest.ManifestCommittable;
2229
import org.apache.paimon.manifest.ManifestEntry;
30+
import org.apache.paimon.manifest.ManifestFile;
31+
import org.apache.paimon.manifest.ManifestList;
32+
import org.apache.paimon.operation.FileStoreCommit;
33+
import org.apache.paimon.table.FileStoreTable;
34+
import org.apache.paimon.table.sink.CommitMessageImpl;
2335

36+
import java.util.ArrayList;
37+
import java.util.Collections;
38+
import java.util.LinkedHashMap;
2439
import java.util.List;
2540
import java.util.Map;
41+
import java.util.Objects;
42+
import java.util.UUID;
43+
import java.util.function.Function;
2644

27-
/** Handler for branch merge data operations (manifest reading, committing). */
28-
public interface BranchMergeHandler {
45+
import static org.apache.paimon.utils.Preconditions.checkArgument;
2946

30-
/** Read all active data files from the given branch. */
31-
Map<FileEntry.Identifier, ManifestEntry> readBranchFiles(String branch);
47+
/** Branch merge handler backed by {@link FileStoreTable}. */
48+
public class BranchMergeHandler {
3249

33-
/** Commit the given files to the target branch. */
34-
void commit(String targetBranch, List<ManifestEntry> filesToMerge);
50+
private final Function<String, FileStoreTable> branchTableFactory;
51+
52+
public BranchMergeHandler(Function<String, FileStoreTable> branchTableFactory) {
53+
this.branchTableFactory = branchTableFactory;
54+
}
55+
56+
public Map<FileEntry.Identifier, ManifestEntry> readBranchFiles(String branch) {
57+
FileStoreTable branchTable = branchTableFactory.apply(branch);
58+
Snapshot snapshot = branchTable.snapshotManager().latestSnapshot();
59+
checkArgument(
60+
snapshot != null,
61+
"Cannot read branch '%s', because it does not have any snapshot.",
62+
branch);
63+
ManifestList manifestList = branchTable.store().manifestListFactory().create();
64+
ManifestFile manifestFile = branchTable.store().manifestFileFactory().create();
65+
Map<FileEntry.Identifier, ManifestEntry> files = new LinkedHashMap<>();
66+
FileEntry.mergeEntries(manifestFile, manifestList.readDataManifests(snapshot), files, null);
67+
return files;
68+
}
69+
70+
public void commit(String targetBranch, List<ManifestEntry> filesToMerge) {
71+
FileStoreTable branchTable = branchTableFactory.apply(targetBranch);
72+
boolean rowTrackingEnabled =
73+
new CoreOptions(branchTable.schema().options()).rowTrackingEnabled();
74+
75+
Map<MergeKey, List<DataFileMeta>> grouped = new LinkedHashMap<>();
76+
for (ManifestEntry entry : filesToMerge) {
77+
DataFileMeta file = prepareFileForTargetCommit(entry.file(), rowTrackingEnabled);
78+
grouped.computeIfAbsent(
79+
new MergeKey(
80+
entry.partition().copy(), entry.bucket(), entry.totalBuckets()),
81+
k -> new ArrayList<>())
82+
.add(file);
83+
}
84+
85+
String commitUser = UUID.randomUUID().toString();
86+
ManifestCommittable committable = new ManifestCommittable(0);
87+
for (Map.Entry<MergeKey, List<DataFileMeta>> e : grouped.entrySet()) {
88+
MergeKey key = e.getKey();
89+
CommitMessageImpl message =
90+
new CommitMessageImpl(
91+
key.partition,
92+
key.bucket,
93+
key.totalBuckets,
94+
new DataIncrement(
95+
e.getValue(), Collections.emptyList(), Collections.emptyList()),
96+
CompactIncrement.emptyIncrement());
97+
committable.addFileCommittable(message);
98+
}
99+
100+
try (FileStoreCommit commit = branchTable.store().newCommit(commitUser, branchTable)) {
101+
commit.appendCommitCheckConflict(true).commit(committable, true);
102+
}
103+
}
104+
105+
private DataFileMeta prepareFileForTargetCommit(DataFileMeta file, boolean rowTrackingEnabled) {
106+
if (rowTrackingEnabled && file.firstRowId() != null) {
107+
// Source files already have row ids assigned in their branch. Clear them so the
108+
// target branch commit path assigns fresh, non-overlapping row ids.
109+
return file.newFirstRowId(null);
110+
}
111+
return file;
112+
}
113+
114+
private static class MergeKey {
115+
final BinaryRow partition;
116+
final int bucket;
117+
final int totalBuckets;
118+
119+
MergeKey(BinaryRow partition, int bucket, int totalBuckets) {
120+
this.partition = partition;
121+
this.bucket = bucket;
122+
this.totalBuckets = totalBuckets;
123+
}
124+
125+
@Override
126+
public boolean equals(Object o) {
127+
if (this == o) {
128+
return true;
129+
}
130+
if (!(o instanceof MergeKey)) {
131+
return false;
132+
}
133+
MergeKey that = (MergeKey) o;
134+
return bucket == that.bucket
135+
&& totalBuckets == that.totalBuckets
136+
&& Objects.equals(partition, that.partition);
137+
}
138+
139+
@Override
140+
public int hashCode() {
141+
return Objects.hash(partition, bucket, totalBuckets);
142+
}
143+
}
35144
}

paimon-core/src/main/java/org/apache/paimon/utils/CatalogBranchManager.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,7 @@ public void fastForward(String branchName) {
110110

111111
@Override
112112
public void mergeBranch(String sourceBranch, String targetBranch) {
113-
executePost(
114-
catalog -> {
115-
BranchManager.mergeValidate(sourceBranch, targetBranch);
116-
catalog.mergeBranch(identifier, sourceBranch, targetBranch);
117-
});
113+
throw new UnsupportedOperationException("Branch merge is not supported via catalog.");
118114
}
119115

120116
@Override

paimon-core/src/main/java/org/apache/paimon/utils/DefaultBranchMergeHandler.java

Lines changed: 0 additions & 146 deletions
This file was deleted.

0 commit comments

Comments
 (0)