Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scottsand-db committed Oct 31, 2024
1 parent a39f973 commit ba61062
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
Expand Down Expand Up @@ -146,21 +147,27 @@ public static KernelException mismatchedProtocolVersionFeatureSet(
int tableFeatureVersion,
int minRequiredVersion,
Set<String> tableFeatures) {
final String featureTypeStr = featureType.toString().toLowerCase(Locale.ROOT);
final String message =
String.format(
"Found %s Features %s in protocol version %s but these are only supported in "
+ "protocol version %s and above.",
featureType, tableFeatureVersion, minRequiredVersion, tableFeatures);
"Found %s features %s in %s protocol version %s but these are only supported in "
+ "%s protocol version %s and above.",
featureTypeStr,
tableFeatures,
featureTypeStr,
tableFeatureVersion,
featureTypeStr,
minRequiredVersion);
return new KernelException(message);
}

public static KernelException tableFeatureReadRequiresWrite(
int tableReaderVersion, int tableWriterVersion) {
final String message =
String.format(
"Table Reader Features are supported (current reader protocol version is %s) yet "
+ "table Writer Features are not (current writer protocol version is %s). Writer "
+ "protocol version must be at least %s to proceed",
"Table reader features are supported (current reader protocol version is %s) yet "
+ "table writer features are not (current writer protocol version is %s). Writer "
+ "protocol version must be at least %s to proceed.",
tableReaderVersion,
tableWriterVersion,
TableFeatures.TABLE_FEATURES_MIN_WRITER_VERSION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ public class TableFeatures {
public enum TableFeatureType {
READER,
WRITER;

@Override
public String toString() {
return name().charAt(0) + name().substring(1).toLowerCase();
}
}

/** Min reader version that supports reader features. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ import scala.collection.JavaConverters._
* Suite that tests Kernel throws error when it receives a unsupported protocol and metadata
*/
class TableFeaturesSuite extends AnyFunSuite {
test("throws when table does not support reader features but readerFeatures is non-empty") {
val exMsg = intercept[KernelException] {
new Protocol(1, 7, Set("columnMapping").asJava, Collections.emptySet())
}.getMessage
assert(exMsg == "Found reader features [columnMapping] in reader protocol version 1 but " +
"these are only supported in reader protocol version 3 and above.")
}

test("throws when table does not support writer features but writerFeatures is non-empty") {
val exMsg = intercept[KernelException] {
new Protocol(1, 1, Collections.emptySet(), Set("appendOnly").asJava)
}.getMessage
assert(exMsg == "Found writer features [appendOnly] in writer protocol version 1 but these " +
"are only supported in writer protocol version 7 and above.")
}

test("throws when table supports reader features but does not support writer features") {
val exMsg = intercept[KernelException] {
new Protocol(3, 1, Collections.emptySet(), Collections.emptySet())
}.getMessage
assert(exMsg == "Table reader features are supported (current reader protocol version is 3) " +
"yet table writer features are not (current writer protocol version is 1). Writer protocol " +
"version must be at least 7 to proceed.")
}

test("validate write supported: protocol 1") {
checkSupported(createTestProtocol(minWriterVersion = 1))
}
Expand Down Expand Up @@ -70,37 +95,41 @@ class TableFeaturesSuite extends AnyFunSuite {

Seq("appendOnly", "inCommitTimestamp", "columnMapping", "typeWidening-preview", "typeWidening")
.foreach { supportedWriterFeature =>
test(s"validateWriteSupported: protocol 7 with $supportedWriterFeature") {
checkSupported(createTestProtocol(minWriterVersion = 7, supportedWriterFeature))
test(s"validateWriteSupported: protocol 7 with $supportedWriterFeature") {
checkSupported(createTestProtocol(minWriterVersion = 7, supportedWriterFeature))
}
}
}

Seq("invariants", "checkConstraints", "generatedColumns", "allowColumnDefaults", "changeDataFeed",
"identityColumns", "deletionVectors", "rowTracking", "timestampNtz",
"domainMetadata", "v2Checkpoint", "icebergCompatV1", "icebergCompatV2", "clustering",
"vacuumProtocolCheck").foreach { unsupportedWriterFeature =>
"identityColumns", "deletionVectors", "rowTracking", "timestampNtz",
"domainMetadata", "v2Checkpoint", "icebergCompatV1", "icebergCompatV2", "clustering",
"vacuumProtocolCheck").foreach { unsupportedWriterFeature =>
test(s"validateWriteSupported: protocol 7 with $unsupportedWriterFeature") {
checkUnsupported(createTestProtocol(minWriterVersion = 7, unsupportedWriterFeature))
}
}

def checkSupported(
protocol: Protocol,
metadata: Metadata = null,
schema: StructType = createTestSchema()): Unit = {
////////////////////
// Helper Methods //
////////////////////

private def checkSupported(
protocol: Protocol,
metadata: Metadata = null,
schema: StructType = createTestSchema()): Unit = {
validateWriteSupportedTable(protocol, metadata, schema, "/test/table")
}

def checkUnsupported(
protocol: Protocol,
metadata: Metadata = null,
schema: StructType = createTestSchema()): Unit = {
private def checkUnsupported(
protocol: Protocol,
metadata: Metadata = null,
schema: StructType = createTestSchema()): Unit = {
intercept[KernelException] {
validateWriteSupportedTable(protocol, metadata, schema, "/test/table")
}
}

def createTestProtocol(minWriterVersion: Int, writerFeatures: String*): Protocol = {
private def createTestProtocol(minWriterVersion: Int, writerFeatures: String*): Protocol = {
new Protocol(
// minReaderVersion - it doesn't matter as the read fails anyway before the writer check
0,
Expand All @@ -111,7 +140,7 @@ class TableFeaturesSuite extends AnyFunSuite {
)
}

def createTestMetadata(withAppendOnly: Boolean = false): Metadata = {
private def createTestMetadata(withAppendOnly: Boolean = false): Metadata = {
var config: Map[String, String] = Map()
if (withAppendOnly) {
config = Map("delta.appendOnly" -> "true");
Expand Down Expand Up @@ -140,8 +169,8 @@ class TableFeaturesSuite extends AnyFunSuite {
)
}

def createTestSchema(
includeInvariant: Boolean = false): StructType = {
private def createTestSchema(
includeInvariant: Boolean = false): StructType = {
var structType = new StructType()
.add("c1", IntegerType.INTEGER)
.add("c2", StringType.STRING)
Expand Down

0 comments on commit ba61062

Please sign in to comment.