Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Kernel] Added Domain Metadata support to Delta Kernel #3835

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"commitInfo":{"timestamp":1730671956424,"operation":"CREATE TABLE","operationParameters":{"partitionBy":"[]","clusterBy":"[]","description":null,"isManaged":"false","properties":"{\"delta.checkpointInterval\":\"3\"}"},"isolationLevel":"Serializable","isBlindAppend":true,"operationMetrics":{},"engineInfo":"Apache-Spark/3.5.3 Delta-Lake/3.3.0-SNAPSHOT","txnId":"90158bef-da23-4500-aafc-d2932e80f8cb"}}
{"metaData":{"id":"04e4bf27-b577-4f7d-b002-08b3bbc00ce5","format":{"provider":"parquet","options":{}},"schemaString":"{\"type\":\"struct\",\"fields\":[{\"name\":\"id\",\"type\":\"long\",\"nullable\":true,\"metadata\":{}}]}","partitionColumns":[],"configuration":{"delta.checkpointInterval":"3"},"createdTime":1730671956256}}
{"protocol":{"minReaderVersion":1,"minWriterVersion":2}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"commitInfo":{"timestamp":1730671958509,"operation":"SET TBLPROPERTIES","operationParameters":{"properties":"{\"delta.feature.domainmetadata\":\"enabled\"}"},"readVersion":0,"isolationLevel":"Serializable","isBlindAppend":true,"operationMetrics":{},"engineInfo":"Apache-Spark/3.5.3 Delta-Lake/3.3.0-SNAPSHOT","txnId":"4b1dc72c-432c-4753-b9d3-68ab89f3cb91"}}
{"metaData":{"id":"04e4bf27-b577-4f7d-b002-08b3bbc00ce5","format":{"provider":"parquet","options":{}},"schemaString":"{\"type\":\"struct\",\"fields\":[{\"name\":\"id\",\"type\":\"long\",\"nullable\":true,\"metadata\":{}}]}","partitionColumns":[],"configuration":{"delta.checkpointInterval":"3"},"createdTime":1730671956256}}
{"protocol":{"minReaderVersion":1,"minWriterVersion":7,"writerFeatures":["domainMetadata","appendOnly","invariants"]}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"commitInfo":{"timestamp":1730671958797,"operation":"Manual Update","operationParameters":{},"readVersion":1,"isolationLevel":"Serializable","isBlindAppend":true,"operationMetrics":{},"engineInfo":"Apache-Spark/3.5.3 Delta-Lake/3.3.0-SNAPSHOT","txnId":"9a6324de-800e-4c8f-9ce3-7766d0462474"}}
{"domainMetadata":{"domain":"testDomain1","configuration":"{\"key1\":\"1\"}","removed":false}}
{"domainMetadata":{"domain":"testDomain2","configuration":"","removed":false}}
{"domainMetadata":{"domain":"testDomain3","configuration":"","removed":false}}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"commitInfo":{"timestamp":1730671959801,"operation":"WRITE","operationParameters":{"mode":"Append","partitionBy":"[]"},"readVersion":2,"isolationLevel":"Serializable","isBlindAppend":true,"operationMetrics":{"numFiles":"2","numOutputRows":"2","numOutputBytes":"956"},"engineInfo":"Apache-Spark/3.5.3 Delta-Lake/3.3.0-SNAPSHOT","txnId":"54aecdaa-e039-40c8-ac9b-bcd7f30183fa"}}
{"add":{"path":"test%25file%25prefix-part-00000-48cf7913-43ae-45bf-ab2c-94eb2fe77358-c000.snappy.parquet","partitionValues":{},"size":478,"modificationTime":1730671959767,"dataChange":true,"stats":"{\"numRecords\":1,\"minValues\":{\"id\":0},\"maxValues\":{\"id\":0},\"nullCount\":{\"id\":0}}"}}
{"add":{"path":"test%25file%25prefix-part-00001-071539c0-ef9e-478c-b550-035c6b5a31c2-c000.snappy.parquet","partitionValues":{},"size":478,"modificationTime":1730671959767,"dataChange":true,"stats":"{\"numRecords\":1,\"minValues\":{\"id\":1},\"maxValues\":{\"id\":1},\"nullCount\":{\"id\":0}}"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"commitInfo":{"timestamp":1730671962209,"operation":"Manual Update","operationParameters":{},"readVersion":3,"isolationLevel":"Serializable","isBlindAppend":true,"operationMetrics":{},"engineInfo":"Apache-Spark/3.5.3 Delta-Lake/3.3.0-SNAPSHOT","txnId":"063e4cd4-4091-4a30-a089-4e8a0fc5c0ac"}}
{"domainMetadata":{"domain":"testDomain1","configuration":"{\"key1\":\"10\"}","removed":false}}
{"domainMetadata":{"domain":"testDomain2","configuration":"","removed":true}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":3,"size":7,"sizeInBytes":16337,"numOfAddFiles":2,"checkpointSchema":{"type":"struct","fields":[{"name":"txn","type":{"type":"struct","fields":[{"name":"appId","type":"string","nullable":true,"metadata":{}},{"name":"version","type":"long","nullable":true,"metadata":{}},{"name":"lastUpdated","type":"long","nullable":true,"metadata":{}}]},"nullable":true,"metadata":{}},{"name":"add","type":{"type":"struct","fields":[{"name":"path","type":"string","nullable":true,"metadata":{}},{"name":"partitionValues","type":{"type":"map","keyType":"string","valueType":"string","valueContainsNull":true},"nullable":true,"metadata":{}},{"name":"size","type":"long","nullable":true,"metadata":{}},{"name":"modificationTime","type":"long","nullable":true,"metadata":{}},{"name":"dataChange","type":"boolean","nullable":true,"metadata":{}},{"name":"tags","type":{"type":"map","keyType":"string","valueType":"string","valueContainsNull":true},"nullable":true,"metadata":{}},{"name":"deletionVector","type":{"type":"struct","fields":[{"name":"storageType","type":"string","nullable":true,"metadata":{}},{"name":"pathOrInlineDv","type":"string","nullable":true,"metadata":{}},{"name":"offset","type":"integer","nullable":true,"metadata":{}},{"name":"sizeInBytes","type":"integer","nullable":true,"metadata":{}},{"name":"cardinality","type":"long","nullable":true,"metadata":{}},{"name":"maxRowIndex","type":"long","nullable":true,"metadata":{}}]},"nullable":true,"metadata":{}},{"name":"baseRowId","type":"long","nullable":true,"metadata":{}},{"name":"defaultRowCommitVersion","type":"long","nullable":true,"metadata":{}},{"name":"clusteringProvider","type":"string","nullable":true,"metadata":{}},{"name":"stats","type":"string","nullable":true,"metadata":{}}]},"nullable":true,"metadata":{}},{"name":"remove","type":{"type":"struct","fields":[{"name":"path","type":"string","nullable":true,"metadata":{}},{"name":"deletionTimestamp","type":"long","nullable":true,"metadata":{}},{"name":"dataChange","type":"boolean","nullable":true,"metadata":{}},{"name":"extendedFileMetadata","type":"boolean","nullable":true,"metadata":{}},{"name":"partitionValues","type":{"type":"map","keyType":"string","valueType":"string","valueContainsNull":true},"nullable":true,"metadata":{}},{"name":"size","type":"long","nullable":true,"metadata":{}},{"name":"deletionVector","type":{"type":"struct","fields":[{"name":"storageType","type":"string","nullable":true,"metadata":{}},{"name":"pathOrInlineDv","type":"string","nullable":true,"metadata":{}},{"name":"offset","type":"integer","nullable":true,"metadata":{}},{"name":"sizeInBytes","type":"integer","nullable":true,"metadata":{}},{"name":"cardinality","type":"long","nullable":true,"metadata":{}},{"name":"maxRowIndex","type":"long","nullable":true,"metadata":{}}]},"nullable":true,"metadata":{}},{"name":"baseRowId","type":"long","nullable":true,"metadata":{}},{"name":"defaultRowCommitVersion","type":"long","nullable":true,"metadata":{}}]},"nullable":true,"metadata":{}},{"name":"metaData","type":{"type":"struct","fields":[{"name":"id","type":"string","nullable":true,"metadata":{}},{"name":"name","type":"string","nullable":true,"metadata":{}},{"name":"description","type":"string","nullable":true,"metadata":{}},{"name":"format","type":{"type":"struct","fields":[{"name":"provider","type":"string","nullable":true,"metadata":{}},{"name":"options","type":{"type":"map","keyType":"string","valueType":"string","valueContainsNull":true},"nullable":true,"metadata":{}}]},"nullable":true,"metadata":{}},{"name":"schemaString","type":"string","nullable":true,"metadata":{}},{"name":"partitionColumns","type":{"type":"array","elementType":"string","containsNull":true},"nullable":true,"metadata":{}},{"name":"configuration","type":{"type":"map","keyType":"string","valueType":"string","valueContainsNull":true},"nullable":true,"metadata":{}},{"name":"createdTime","type":"long","nullable":true,"metadata":{}}]},"nullable":true,"metadata":{}},{"name":"protocol","type":{"type":"struct","fields":[{"name":"minReaderVersion","type":"integer","nullable":true,"metadata":{}},{"name":"minWriterVersion","type":"integer","nullable":true,"metadata":{}},{"name":"readerFeatures","type":{"type":"array","elementType":"string","containsNull":true},"nullable":true,"metadata":{}},{"name":"writerFeatures","type":{"type":"array","elementType":"string","containsNull":true},"nullable":true,"metadata":{}}]},"nullable":true,"metadata":{}},{"name":"domainMetadata","type":{"type":"struct","fields":[{"name":"domain","type":"string","nullable":true,"metadata":{}},{"name":"configuration","type":"string","nullable":true,"metadata":{}},{"name":"removed","type":"boolean","nullable":true,"metadata":{}}]},"nullable":true,"metadata":{}}]},"checksum":"7a97cc187ffb0604cc50e51bddb3cbfa"}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,46 @@ class GoldenTables extends QueryTest with SharedSparkSession {
Row(0, 0) :: Nil,
schema)
}

generateGoldenTable("kernel-domain-metadata") { tablePath =>
withSQLConf(
("spark.databricks.delta.properties.defaults.checkpointInterval", "3")
) {
val tbl = "tbl"

sql(s"CREATE TABLE $tbl (id LONG) USING delta LOCATION '$tablePath'")
sql(s"ALTER TABLE $tbl SET TBLPROPERTIES('delta.feature.domainMetadata' = 'enabled')")

val deltaLog = DeltaLog.forTable(spark, new Path(tablePath))

deltaLog
.startTransaction()
.commitManually(
List(
DomainMetadata("testDomain1", "{\"key1\":\"1\"}", removed = false),
DomainMetadata("testDomain2", "", removed = false),
DomainMetadata("testDomain3", "", removed = false)
): _*
)

spark.range(0, 2).write.format("delta").mode("append").save(tablePath) // Checkpoint created

deltaLog
.startTransaction()
.commitManually(
List(
DomainMetadata("testDomain1", "{\"key1\":\"10\"}".stripMargin, removed = false),
DomainMetadata("testDomain2", "", removed = true)
): _*
)

// In the end, we need to read 1 checkpoint file and 1 log file to replay the golden table
// The state of the domain metadata should be:
// testDomain1: "\"key1\":\"10\"", removed = false
// testDomain2: "", removed = true
// testDomain3: "", removed = false
}
}
}

case class TestStruct(f1: String, f2: Long)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static java.lang.String.format;

import io.delta.kernel.exceptions.*;
import io.delta.kernel.internal.actions.DomainMetadata;
import io.delta.kernel.types.DataType;
import io.delta.kernel.types.StructType;
import io.delta.kernel.utils.DataFileStatus;
Expand Down Expand Up @@ -274,6 +275,33 @@ public static KernelException invalidConfigurationValueException(
return new InvalidConfigurationValueException(key, value, helpMessage);
}

public static KernelException domainMetadataUnsupported() {
String message =
"Found DomainMetadata action(s) but table feature 'domainMetadata' "
+ "is not supported on this table.";
return new KernelException(message);
}

public static KernelException duplicateDomainMetadataAction(String action1, String action2) {
String message =
String.format(
"Multiple domain metadata actions detected in single transaction: '%s' and '%s'. "
qiyuandong-db marked this conversation as resolved.
Show resolved Hide resolved
+ "Only one action per domain is allowed.",
action1, action2);
return new KernelException(message);
}

public static ConcurrentWriteException concurrentDomainMetadataAction(
DomainMetadata domainMetadataAttempt, DomainMetadata winningDomainMetadata) {
String message =
String.format(
"A concurrent writer added a domainMetadata action for the same domain: %s. "
+ "No domain-specific conflict resolution available for this domain. "
+ "Attempted domainMetadata: %s. Winning domainMetadata: %s",
domainMetadataAttempt.getDomain(), domainMetadataAttempt, winningDomainMetadata);
return new ConcurrentWriteException(message);
}

/* ------------------------ HELPER METHODS ----------------------------- */
private static String formatTimestamp(long millisSinceEpochUTC) {
return new Timestamp(millisSinceEpochUTC).toInstant().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.delta.kernel.engine.CommitCoordinatorClientHandler;
import io.delta.kernel.engine.Engine;
import io.delta.kernel.internal.actions.CommitInfo;
import io.delta.kernel.internal.actions.DomainMetadata;
import io.delta.kernel.internal.actions.Metadata;
import io.delta.kernel.internal.actions.Protocol;
import io.delta.kernel.internal.fs.Path;
Expand All @@ -31,6 +32,7 @@
import io.delta.kernel.internal.snapshot.LogSegment;
import io.delta.kernel.internal.snapshot.TableCommitCoordinatorClientHandler;
import io.delta.kernel.types.StructType;
import java.util.Map;
import java.util.Optional;

/** Implementation of {@link Snapshot}. */
Expand Down Expand Up @@ -83,6 +85,10 @@ public Protocol getProtocol() {
return protocol;
}

public Map<String, DomainMetadata> getDomainMetadataMap() {
return logReplay.getDomainMetadataMap();
}

public CreateCheckpointIterator getCreateCheckpointIterator(Engine engine) {
long minFileRetentionTimestampMillis =
System.currentTimeMillis() - TOMBSTONE_RETENTION.fromMetadata(metadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class TableFeatures {
add("columnMapping");
add("typeWidening-preview");
add("typeWidening");
add("domainMetadata");
}
});

Expand Down Expand Up @@ -93,7 +94,7 @@ public static void validateReadSupportedTable(
* <li>protocol writer version 1.
* <li>protocol writer version 2 only with appendOnly feature enabled.
* <li>protocol writer version 7 with {@code appendOnly}, {@code inCommitTimestamp}, {@code
* columnMapping}, {@code typeWidening} feature enabled.
* columnMapping}, {@code typeWidening}, {@code domainMetadata} feature enabled.
* </ul>
*
* @param protocol Table protocol
Expand Down Expand Up @@ -137,6 +138,8 @@ public static void validateWriteSupportedTable(
break;
case "typeWidening":
break;
case "domainMetadata":
break;
default:
throw unsupportedWriterFeature(tablePath, writerFeature);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.delta.kernel.internal.util.ColumnMapping;
import io.delta.kernel.internal.util.FileNames;
import io.delta.kernel.internal.util.InCommitTimestampUtils;
import io.delta.kernel.internal.util.ValidateDomainMetadataIterator;
import io.delta.kernel.internal.util.VectorUtils;
import io.delta.kernel.types.StructType;
import io.delta.kernel.utils.CloseableIterable;
Expand Down Expand Up @@ -142,7 +143,8 @@ public TransactionCommitResult commit(Engine engine, CloseableIterable<Row> data
+ "Trying to resolve conflicts and retry commit.",
commitAsVersion);
TransactionRebaseState rebaseState =
ConflictChecker.resolveConflicts(engine, readSnapshot, commitAsVersion, this);
ConflictChecker.resolveConflicts(
engine, readSnapshot, commitAsVersion, this, dataActions);
long newCommitAsVersion = rebaseState.getLatestVersion() + 1;
checkArgument(
commitAsVersion < newCommitAsVersion,
Expand Down Expand Up @@ -221,7 +223,8 @@ private TransactionCommitResult doCommit(
}
setTxnOpt.ifPresent(setTxn -> metadataActions.add(createTxnSingleAction(setTxn.toRow())));

try (CloseableIterator<Row> stageDataIter = dataActions.iterator()) {
try (CloseableIterator<Row> stageDataIter =
new ValidateDomainMetadataIterator(protocol, dataActions.iterator(), FULL_SCHEMA)) {
Copy link
Collaborator

@scottsand-db scottsand-db Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. So this is just a thin wrapper over the actions-to-be-committed and validates the DomainMetadata on write.

@vkorukanti -- would we ever want to validate other things on write?

@qiyuandong-db -- I wonder if we should not have a ValidateDomainMetadataIterator but rather a ValidationIterator. This validation iterator lets you pass in different validationFunctions that take a row and decide to or not to throw an error. This would be a more extensible solution. It would let us avoid iterater wrappers on top of iterator wrappers on top of iterator wrappers ...

For example, I'd be fine removing the validation from this PR and coding that ^ up in a followup PR ... let's see what @vkorukanti thinks

qiyuandong-db marked this conversation as resolved.
Show resolved Hide resolved
// Create a new CloseableIterator that will return the metadata actions followed by the
// data actions.
CloseableIterator<Row> dataAndMetadataActions =
Expand Down
Loading
Loading