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

[Spark][Test-Only] Add Materialized Row Tracking columns CLONE tests #2744

Merged
merged 1 commit into from
Mar 19, 2024
Merged
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 @@ -16,7 +16,7 @@

package org.apache.spark.sql.delta.rowtracking

import org.apache.spark.sql.delta.{DeltaConfigs, DeltaLog, DeltaRuntimeException, MaterializedRowCommitVersion, MaterializedRowId, RowTracking}
import org.apache.spark.sql.delta.{DeltaConfigs, DeltaIllegalStateException, DeltaLog, DeltaRuntimeException, MaterializedRowCommitVersion, MaterializedRowId, RowTracking}
import org.apache.spark.sql.delta.rowid.RowIdTestUtils

import org.apache.spark.sql.catalyst.TableIdentifier
Expand Down Expand Up @@ -183,5 +183,80 @@ class MaterializedColumnSuite extends RowIdTestUtils
}
}
}

test(s"clone gives new materialized $name column name for existing empty target table") {
val sourceTableName = "source"
val targetTableName = "target"

withTable(sourceTableName, targetTableName) {
withRowTrackingEnabled(enabled = true) {
spark.range(end = 1).toDF("col1").write.format("delta").saveAsTable(sourceTableName)
spark.range(end = 0).toDF("col2").write.format("delta").saveAsTable(targetTableName)
val oldMaterializedColumnName = getMaterializedColumnName(targetTableName).get

sql(s"CREATE OR REPLACE TABLE $targetTableName SHALLOW CLONE $sourceTableName")

val newMaterializedColumnName = getMaterializedColumnName(targetTableName).get
assert(oldMaterializedColumnName === newMaterializedColumnName)
val sourceMaterializedColName = getMaterializedColumnName(sourceTableName).get
assert(sourceMaterializedColName !== newMaterializedColumnName)
}
}
}

test("double clone from an empty source table maintains the same " +
s"materialized $name column name") {
val sourceTableName = "source"
val targetTableName = "target"

withTable(sourceTableName, targetTableName) {
withRowTrackingEnabled(enabled = true) {
spark.range(end = 0).toDF("col1").write.format("delta").saveAsTable(sourceTableName)

sql(s"CREATE OR REPLACE TABLE $targetTableName SHALLOW CLONE $sourceTableName " +
s"TBLPROPERTIES ('${DeltaConfigs.ROW_TRACKING_ENABLED.key}' = 'true')")
val materializedColumnNameBefore = getMaterializedColumnName(targetTableName)
val sourceMaterializedColName = getMaterializedColumnName(sourceTableName)
assert(sourceMaterializedColName !== materializedColumnNameBefore)

sql(s"CREATE OR REPLACE TABLE $targetTableName SHALLOW CLONE $sourceTableName")
val materializedColumnNameAfter = getMaterializedColumnName(targetTableName)
assert(materializedColumnNameBefore === materializedColumnNameAfter)
}
}
}

test(s"self clone of an empty table maintains the same materialized $name column name") {
withRowTrackingEnabled(enabled = true) {
withTestTable {
spark.range(end = 0).toDF(testDataColumnName)
.write.format("delta").mode("overwrite").saveAsTable(testTableName)

val materializedColumnNameBefore = getMaterializedColumnName(testTableName)
sql(s"CREATE OR REPLACE TABLE $testTableName SHALLOW CLONE $testTableName")
val materializedColumnNameAfter = getMaterializedColumnName(testTableName)
assert(materializedColumnNameBefore === materializedColumnNameAfter)

val (_, snapshot) = DeltaLog.forTableWithSnapshot(spark, TableIdentifier(testTableName))
assert(RowTracking.isEnabled(snapshot.protocol, snapshot.metadata))
}
}
}

test(s"can't clone materialized $name column name for existing non-empty target") {
val sourceTableName = "source"
val targetTableName = "target"

withTable(sourceTableName, targetTableName) {
withRowTrackingEnabled(enabled = true) {
spark.range(end = 1).toDF("col1").write.format("delta").saveAsTable(sourceTableName)
spark.range(end = 1).toDF("col2").write.format("delta").saveAsTable(targetTableName)

assert(intercept[DeltaIllegalStateException] {
sql(s"CREATE OR REPLACE TABLE $targetTableName SHALLOW CLONE $sourceTableName")
}.getErrorClass === "DELTA_UNSUPPORTED_NON_EMPTY_CLONE")
}
}
}
}
}
Loading