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
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@

import java.io.FileNotFoundException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -476,7 +477,8 @@ public TableCommitImpl newCommit(String commitUser) {
options.snapshotExpireExecutionMode(),
name(),
options.forceCreatingSnapshot(),
options.fileOperationThreadNum());
options.fileOperationThreadNum(),
createPartitionValidators(this));
}

@Override
Expand Down Expand Up @@ -744,6 +746,14 @@ protected RowKindGenerator rowKindGenerator() {
return RowKindGenerator.create(schema(), store().options());
}

private List<PartitionValidator> createPartitionValidators(FileStoreTable table) {
List<PartitionValidator> validators = new ArrayList<>();
if (table.coreOptions().isChainTable()) {
validators.add(new ChainPartitionDropValidator(table));
}
return validators;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* 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.
*/

package org.apache.paimon.table;

import org.apache.paimon.CoreOptions;
import org.apache.paimon.codegen.CodeGenUtils;
import org.apache.paimon.codegen.RecordComparator;
import org.apache.paimon.data.BinaryRow;
import org.apache.paimon.manifest.PartitionEntry;
import org.apache.paimon.predicate.Predicate;
import org.apache.paimon.predicate.PredicateBuilder;
import org.apache.paimon.table.source.snapshot.SnapshotReader;
import org.apache.paimon.utils.ChainTableUtils;
import org.apache.paimon.utils.InternalRowPartitionComputer;
import org.apache.paimon.utils.RowDataToObjectArrayConverter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.apache.paimon.partition.PartitionPredicate.createBinaryPartitions;

/**
* Validator for chain table partition drop.
*
* <p>Checks if a partition can be dropped based on the chain table's snapshot partition
* dependencies.
*/
public class ChainPartitionDropValidator implements PartitionValidator {

private static final Logger LOG = LoggerFactory.getLogger(ChainPartitionDropValidator.class);

private final FileStoreTable table;
private final CoreOptions coreOptions;

public ChainPartitionDropValidator(FileStoreTable table) {
this.table = table;
this.coreOptions = table.coreOptions();
}

@Override
public void validatePartitionDrop(List<Map<String, String>> partitionSpecs) {
if (!ChainTableUtils.isScanFallbackSnapshotBranch(coreOptions)) {
return;
}
FileStoreTable candidateTable = table;
if (table instanceof FallbackReadFileStoreTable) {
candidateTable =
((ChainGroupReadTable) ((FallbackReadFileStoreTable) table).fallback())
.wrapped();
}
FileStoreTable deltaTable =
candidateTable.switchToBranch(coreOptions.scanFallbackDeltaBranch());
List<BinaryRow> partitions =
createBinaryPartitions(
partitionSpecs,
table.schema().logicalPartitionType(),
coreOptions.partitionDefaultName());
RowDataToObjectArrayConverter partitionConverter =
new RowDataToObjectArrayConverter(table.schema().logicalPartitionType());
InternalRowPartitionComputer partitionComputer =
new InternalRowPartitionComputer(
coreOptions.partitionDefaultName(),
table.schema().logicalPartitionType(),
table.schema().partitionKeys().toArray(new String[0]),
coreOptions.legacyPartitionName());
RecordComparator partitionComparator =
CodeGenUtils.newRecordComparator(
table.schema().logicalPartitionType().getFieldTypes());
List<BinaryRow> snapshotPartitions =
table.newSnapshotReader().partitionEntries().stream()
.map(PartitionEntry::partition)
.sorted(partitionComparator)
.collect(Collectors.toList());
SnapshotReader deltaSnapshotReader = deltaTable.newSnapshotReader();
PredicateBuilder builder = new PredicateBuilder(table.schema().logicalPartitionType());
for (BinaryRow partition : partitions) {
Optional<BinaryRow> preSnapshotPartition =
findPreSnapshotPartition(snapshotPartitions, partition, partitionComparator);
Optional<BinaryRow> nextSnapshotPartition =
findNextSnapshotPartition(snapshotPartitions, partition, partitionComparator);
Predicate deltaFollowingPredicate =
ChainTableUtils.createTriangularPredicate(
partition, partitionConverter, builder::equal, builder::greaterThan);
List<BinaryRow> deltaFollowingPartitions =
deltaSnapshotReader.withPartitionFilter(deltaFollowingPredicate)
.partitionEntries().stream()
.map(PartitionEntry::partition)
.filter(
deltaPartition ->
isNextIntervalPartition(
deltaPartition,
nextSnapshotPartition,
partitionComparator))
.collect(Collectors.toList());
boolean canDrop =
deltaFollowingPartitions.isEmpty() || preSnapshotPartition.isPresent();
LOG.info(
"Drop partition, partition={}, canDrop={}, preSnapshotPartition={}, nextSnapshotPartition={}",
partitionComputer.generatePartValues(partition),
canDrop,
generatePartitionValues(preSnapshotPartition, partitionComputer),
generatePartitionValues(nextSnapshotPartition, partitionComputer));
if (!canDrop) {
throw new RuntimeException("Snapshot partition cannot be dropped.");
}
}
}

private Optional<BinaryRow> findPreSnapshotPartition(
List<BinaryRow> snapshotPartitions,
BinaryRow partition,
RecordComparator partitionComparator) {
BinaryRow pre = null;
for (BinaryRow snapshotPartition : snapshotPartitions) {
if (partitionComparator.compare(snapshotPartition, partition) < 0) {
pre = snapshotPartition;
} else {
break;
}
}
return Optional.ofNullable(pre);
}

private Optional<BinaryRow> findNextSnapshotPartition(
List<BinaryRow> snapshotPartitions,
BinaryRow partition,
RecordComparator partitionComparator) {
for (BinaryRow snapshotPartition : snapshotPartitions) {
if (partitionComparator.compare(snapshotPartition, partition) > 0) {
return Optional.of(snapshotPartition);
}
}
return Optional.empty();
}

private boolean isNextIntervalPartition(
BinaryRow partition,
Optional<BinaryRow> nextSnapshotPartition,
RecordComparator partitionComparator) {
return !nextSnapshotPartition.isPresent()
|| partitionComparator.compare(partition, nextSnapshotPartition.get()) < 0;
}

private String generatePartitionValues(
Optional<BinaryRow> partition, InternalRowPartitionComputer partitionComputer) {
if (!partition.isPresent()) {
return "<none>";
}
return partitionComputer.generatePartValues(partition.get()).toString();
}

@Override
public void close() throws Exception {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.
*/

package org.apache.paimon.table;

import java.util.List;
import java.util.Map;

/** Validator to validate partition operations. */
public interface PartitionValidator extends AutoCloseable {

void validatePartitionDrop(List<Map<String, String>> partitionSpecs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.paimon.operation.PartitionExpire;
import org.apache.paimon.operation.metrics.CommitMetrics;
import org.apache.paimon.stats.Statistics;
import org.apache.paimon.table.PartitionValidator;
import org.apache.paimon.tag.TagAutoCreation;
import org.apache.paimon.tag.TagAutoManager;
import org.apache.paimon.tag.TagTimeExpire;
Expand Down Expand Up @@ -92,6 +93,8 @@ public class TableCommitImpl implements InnerTableCommit {
private boolean batchCommitted = false;
private boolean expireForEmptyCommit = true;

private final List<PartitionValidator> partitionValidators;

public TableCommitImpl(
FileStoreCommit commit,
@Nullable Runnable expireSnapshots,
Expand All @@ -102,7 +105,8 @@ public TableCommitImpl(
ExpireExecutionMode expireExecutionMode,
String tableName,
boolean forceCreatingSnapshot,
int threadNum) {
int threadNum,
List<PartitionValidator> partitionValidators) {
if (partitionExpire != null) {
commit.withPartitionExpire(partitionExpire);
}
Expand All @@ -126,6 +130,10 @@ public TableCommitImpl(
this.tableName = tableName;
this.forceCreatingSnapshot = forceCreatingSnapshot;
this.fileCheckExecutor = FileOperationThreadPool.getExecutorService(threadNum);
this.partitionValidators =
partitionValidators == null
? Collections.emptyList()
: Collections.unmodifiableList(new ArrayList<>(partitionValidators));
}

public boolean forceCreatingSnapshot() {
Expand Down Expand Up @@ -184,6 +192,7 @@ public void truncateTable() {

@Override
public void truncatePartitions(List<Map<String, String>> partitionSpecs) {
partitionValidators.forEach(validator -> validator.validatePartitionDrop(partitionSpecs));
commit.dropPartitions(partitionSpecs, COMMIT_IDENTIFIER);
}

Expand Down Expand Up @@ -409,6 +418,13 @@ public void expireSnapshots() {
public void close() throws Exception {
commit.close();
maintainExecutor.shutdownNow();
for (PartitionValidator validator : partitionValidators) {
try {
validator.close();
} catch (Exception e) {
LOG.warn("Failed to close partition validator.", e);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,9 @@ public static boolean isScanFallbackDeltaBranch(CoreOptions options) {
return options.isChainTable()
&& options.scanFallbackDeltaBranch().equalsIgnoreCase(options.branch());
}

public static boolean isScanFallbackSnapshotBranch(CoreOptions options) {
return options.isChainTable()
&& options.scanFallbackSnapshotBranch().equalsIgnoreCase(options.branch());
}
}
Loading
Loading