-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support time travel using temporal version in DeltaLake
- Loading branch information
1 parent
76d1e6f
commit 2c9389f
Showing
9 changed files
with
980 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
...a-lake/src/main/java/io/trino/plugin/deltalake/transactionlog/TemporalTimeTravelUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* Licensed 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 io.trino.plugin.deltalake.transactionlog; | ||
|
||
import io.airlift.units.DataSize; | ||
import io.trino.filesystem.TrinoFileSystem; | ||
import io.trino.plugin.deltalake.transactionlog.checkpoint.LastCheckpoint; | ||
import io.trino.plugin.deltalake.transactionlog.checkpoint.TransactionLogTail; | ||
import io.trino.spi.TrinoException; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import static io.trino.plugin.deltalake.transactionlog.TransactionLogParser.readLastCheckpoint; | ||
import static io.trino.plugin.deltalake.transactionlog.TransactionLogUtil.getTransactionLogDir; | ||
import static io.trino.plugin.deltalake.transactionlog.TransactionLogUtil.getTransactionLogJsonEntryPath; | ||
import static io.trino.plugin.deltalake.transactionlog.checkpoint.TransactionLogTail.getEntriesFromJson; | ||
import static io.trino.plugin.deltalake.transactionlog.checkpoint.TransactionLogTail.loadNewTail; | ||
import static io.trino.spi.StandardErrorCode.INVALID_ARGUMENTS; | ||
import static java.lang.String.format; | ||
|
||
public final class TemporalTimeTravelUtil | ||
{ | ||
private TemporalTimeTravelUtil() {} | ||
|
||
public static long findLatestVersionUsingTemporal(TrinoFileSystem fileSystem, String tableLocation, long epochMillis) | ||
throws IOException | ||
{ | ||
Optional<LastCheckpoint> lastCheckpoint = readLastCheckpoint(fileSystem, tableLocation); | ||
long entryNumber = lastCheckpoint.map(LastCheckpoint::version).orElse(0L); | ||
|
||
String transactionLogDir = getTransactionLogDir(tableLocation); | ||
|
||
Optional<TransactionLogEntries> checkpointEntries = getEntriesFromJson(entryNumber, transactionLogDir, fileSystem, DataSize.ofBytes(0)); | ||
if (checkpointEntries.isEmpty()) { | ||
throw new MissingTransactionLogException(getTransactionLogJsonEntryPath(transactionLogDir, entryNumber).toString()); | ||
} | ||
|
||
Optional<CommitInfoEntry> commitInfo = checkpointEntries.get().getEntries(fileSystem) | ||
.map(DeltaLakeTransactionLogEntry::getCommitInfo) | ||
.filter(Objects::nonNull) | ||
.findFirst(); | ||
|
||
// We can not derive useful info from the last check point | ||
if (commitInfo.isEmpty()) { | ||
return findLatestVersionFromWholeTransactions(fileSystem, tableLocation, epochMillis); | ||
} | ||
|
||
if (commitInfo.get().timestamp() == epochMillis) { | ||
return entryNumber; | ||
} | ||
|
||
if (commitInfo.get().timestamp() < epochMillis) { | ||
long tail = searchTowardsTail(fileSystem, entryNumber + 1, transactionLogDir, epochMillis); | ||
if (tail >= 0) { | ||
return tail; | ||
} | ||
return entryNumber; | ||
} | ||
else { | ||
long head = searchTowardsHead(fileSystem, entryNumber - 1, transactionLogDir, epochMillis); | ||
if (head >= 0) { | ||
return head; | ||
} | ||
} | ||
|
||
throw new TrinoException(INVALID_ARGUMENTS, format("No temporal version history at or before %s", Instant.ofEpochMilli(epochMillis))); | ||
} | ||
|
||
private static long searchTowardsTail(TrinoFileSystem fileSystem, long entryNumber, String transactionLogDir, long epochMillis) | ||
throws IOException | ||
{ | ||
long version = -1; | ||
Optional<TransactionLogEntries> entries = getEntriesFromJson(entryNumber, transactionLogDir, fileSystem, DataSize.ofBytes(0)); | ||
while (entries.isPresent()) { | ||
Optional<CommitInfoEntry> commitInfo = entries.get().getEntries(fileSystem) | ||
.map(DeltaLakeTransactionLogEntry::getCommitInfo) | ||
.filter(Objects::nonNull) | ||
.findFirst(); | ||
|
||
if (commitInfo.isEmpty()) { | ||
entryNumber++; | ||
entries = getEntriesFromJson(entryNumber, transactionLogDir, fileSystem, DataSize.ofBytes(0)); | ||
continue; | ||
} | ||
|
||
if (commitInfo.get().timestamp() > epochMillis) { | ||
break; | ||
} | ||
|
||
version = entryNumber; | ||
|
||
entryNumber++; | ||
entries = getEntriesFromJson(entryNumber, transactionLogDir, fileSystem, DataSize.ofBytes(0)); | ||
} | ||
|
||
return version; | ||
} | ||
|
||
private static long searchTowardsHead(TrinoFileSystem fileSystem, long entryNumber, String transactionLogDir, long epochMillis) | ||
throws IOException | ||
{ | ||
Optional<TransactionLogEntries> entries = getEntriesFromJson(entryNumber, transactionLogDir, fileSystem, DataSize.ofBytes(0)); | ||
while (entries.isPresent()) { | ||
Optional<CommitInfoEntry> commitInfo = entries.get().getEntries(fileSystem) | ||
.map(DeltaLakeTransactionLogEntry::getCommitInfo) | ||
.filter(Objects::nonNull) | ||
.findFirst(); | ||
|
||
if (commitInfo.isEmpty()) { | ||
entryNumber--; | ||
if (entryNumber < 0) { | ||
break; | ||
} | ||
entries = getEntriesFromJson(entryNumber, transactionLogDir, fileSystem, DataSize.ofBytes(0)); | ||
continue; | ||
} | ||
|
||
if (commitInfo.get().timestamp() <= epochMillis) { | ||
return commitInfo.get().version(); | ||
} | ||
|
||
entryNumber--; | ||
if (entryNumber < 0) { | ||
break; | ||
} | ||
entries = getEntriesFromJson(entryNumber, transactionLogDir, fileSystem, DataSize.ofBytes(0)); | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
private static long findLatestVersionFromWholeTransactions(TrinoFileSystem fileSystem, String tableLocation, long epochMillis) | ||
throws IOException | ||
{ | ||
TransactionLogTail transactionLogTail = loadNewTail(fileSystem, tableLocation, Optional.empty(), Optional.empty(), DataSize.ofBytes(0)); | ||
|
||
long version = -1; | ||
for (Transaction transaction : transactionLogTail.getTransactions()) { | ||
Optional<CommitInfoEntry> commitInfo = transaction.transactionEntries().getEntries(fileSystem) | ||
.map(DeltaLakeTransactionLogEntry::getCommitInfo) | ||
.filter(Objects::nonNull) | ||
.filter(commitInfoEntry -> commitInfoEntry.timestamp() <= epochMillis) | ||
.findFirst(); | ||
if (commitInfo.isEmpty()) { | ||
continue; | ||
} | ||
version = Math.max(version, commitInfo.get().version()); | ||
} | ||
|
||
if (version >= 0) { | ||
return version; | ||
} | ||
|
||
throw new TrinoException(INVALID_ARGUMENTS, format("No temporal version history at or before %s", Instant.ofEpochMilli(epochMillis))); | ||
} | ||
} |
Oops, something went wrong.