suhuruli commented on code in PR #60462:
URL: https://github.com/apache/spark/pull/60462#discussion_r4617942220
##########
sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/graph/MaterializeTablesSuite.scala:
##########
@@ -1610,17 +1626,321 @@ abstract class MaterializeTablesSuite extends
BaseCoreExecutionTest {
}
}
- test("re-materializing with a dropped property neither removes it nor issues
an alterTable") {
+ test("SPARK-57670: re-materializing with a dropped property removes it") {
withRecordingCatalog {
val schema = new StructType().add("id", IntegerType)
- // This test locks in the current buggy behavior where dropped
properties do not materialize
- // against the catalog table entity. See SPARK-57670.
materializeStreamingTable("t", schema, Map("p.keep" -> "v", "p.stale" ->
"old"))
assert(recordingCatalog.recordedAlters.isEmpty)
+ // A property the pipeline never declared, e.g. one managed by the
catalog.
+ spark.sql(
+ s"ALTER TABLE $recordingCatalogName.$recordingNamespace.t " +
+ "SET TBLPROPERTIES ('p.external' = 'e')")
+ recordingCatalog.recordedAlters.clear()
+
materializeStreamingTable("t", schema, Map("p.keep" -> "v"))
+ assert(recordingCatalog.recordedAlters.size == 1)
+ val changes = recordingCatalog.recordedAlters.flatten
+ assert(changes.collect { case r: TableChange.RemoveProperty =>
r.property() } ==
+ Seq("p.stale"))
+
+ val props = loadTableFromRecordingCatalog("t").properties()
+ assert(!props.containsKey("p.stale"))
+ assert(props.get("p.keep") == "v")
+ assert(props.get("p.external") == "e")
+ assert(props.get(PipelinesTableProperties.declaredPropertyKeys) ==
"""["p.keep"]""")
+ }
+ }
+
+ test("SPARK-57670: re-materializing with all properties dropped removes them
and the record " +
+ "in one alterTable") {
+ withRecordingCatalog {
+ val schema = new StructType().add("id", IntegerType)
+ materializeStreamingTable("t", schema, Map("p.a" -> "1", "p.b" -> "2"))
+ recordingCatalog.recordedAlters.clear()
+
+ materializeStreamingTable("t", schema, Map.empty)
+ assert(recordingCatalog.recordedAlters.size == 1)
+ val changes = recordingCatalog.recordedAlters.head
+ assert(changes.forall(_.isInstanceOf[TableChange.RemoveProperty]))
+ assert(changes.collect { case r: TableChange.RemoveProperty =>
r.property() }.toSet ==
+ Set("p.a", "p.b", PipelinesTableProperties.declaredPropertyKeys))
+
+ val props = loadTableFromRecordingCatalog("t").properties()
+ assert(!props.containsKey("p.a"))
+ assert(!props.containsKey("p.b"))
+ assert(!props.containsKey(PipelinesTableProperties.declaredPropertyKeys))
+
+ // The table is now in sync with its definition, so re-materializing is
a no-op.
+ recordingCatalog.recordedAlters.clear()
+ materializeStreamingTable("t", schema, Map.empty)
assert(recordingCatalog.recordedAlters.isEmpty)
+ }
+ }
+
+ test("SPARK-57670: a definition without table properties writes no
declared-keys record") {
+ withRecordingCatalog {
+ val schema = new StructType().add("id", IntegerType)
+ materializeStreamingTable("t", schema, Map.empty)
+ assert(!loadTableFromRecordingCatalog("t").properties()
+ .containsKey(PipelinesTableProperties.declaredPropertyKeys))
+ }
+ }
+
+ test("SPARK-57670: a table without properties and without a record is
re-materialized " +
+ "without an alterTable") {
+ withRecordingCatalog {
+ val schema = new StructType().add("id", IntegerType)
+ materializeStreamingTable("t", schema, Map.empty)
+ // Stand in for a table materialized before the record was introduced.
+ spark.sql(
+ s"ALTER TABLE $recordingCatalogName.$recordingNamespace.t UNSET
TBLPROPERTIES IF EXISTS " +
+ s"('${PipelinesTableProperties.declaredPropertyKeys}')")
+ recordingCatalog.recordedAlters.clear()
+
+ materializeStreamingTable("t", schema, Map.empty)
+ assert(recordingCatalog.recordedAlters.isEmpty)
+ assert(!loadTableFromRecordingCatalog("t").properties()
+ .containsKey(PipelinesTableProperties.declaredPropertyKeys))
+ }
+ }
- assert(loadTableFromRecordingCatalog("t").properties().get("p.stale") ==
"old")
+ test("SPARK-57670: dropped properties are not removed from a table without a
declared-keys " +
+ "record") {
+ withRecordingCatalog {
+ val schema = new StructType().add("id", IntegerType)
+ materializeStreamingTable("t", schema, Map("p.keep" -> "v", "p.stale" ->
"old"))
+ // Simulate a table materialized before the declared-keys record was
introduced.
+ spark.sql(
+ s"ALTER TABLE $recordingCatalogName.$recordingNamespace.t " +
+ s"UNSET TBLPROPERTIES
('${PipelinesTableProperties.declaredPropertyKeys}')")
+
+ materializeStreamingTable("t", schema, Map("p.keep" -> "v"))
+ val props = loadTableFromRecordingCatalog("t").properties()
+ assert(props.get("p.stale") == "old")
+ assert(props.get(PipelinesTableProperties.declaredPropertyKeys) ==
"""["p.keep"]""")
+ }
+ }
+
+ test("SPARK-57670: a user-declared value for the declared-keys property is
ignored") {
+ withRecordingCatalog {
+ val schema = new StructType().add("id", IntegerType)
+ materializeStreamingTable("t", schema, Map(
+ "p.a" -> "1",
+ PipelinesTableProperties.declaredPropertyKeys -> """["p.other"]"""))
+ assert(loadTableFromRecordingCatalog("t").properties()
+ .get(PipelinesTableProperties.declaredPropertyKeys) == """["p.a"]""")
+ }
+ }
+
+ test("SPARK-57670: a dropped property is removed from a materialized view") {
+ materializeGraph(new TestGraphRegistrationContext(spark) {
+ registerMaterializedView(
+ "mv",
+ query = dfFlowFunc(Seq(1).toDF("id")),
+ properties = Map("p.keep" -> "v", "p.stale" -> "old"))
+ }.resolveToDataflowGraph(), storageRoot = storageRoot)
+
+ materializeGraph(new TestGraphRegistrationContext(spark) {
+ registerMaterializedView(
+ "mv",
+ query = dfFlowFunc(Seq(1).toDF("id")),
+ properties = Map("p.keep" -> "v"))
+ }.resolveToDataflowGraph(), storageRoot = storageRoot)
+
+ val props =
spark.sessionState.catalogManager.currentCatalog.asInstanceOf[TableCatalog]
+
.loadTable(Identifier.of(Array(TestGraphRegistrationContext.DEFAULT_DATABASE),
"mv"))
+ .properties()
+ assert(!props.containsKey("p.stale"))
+ assert(props.get("p.keep") == "v")
+ }
+
+ test("SPARK-57670: a dropped comment declared as a table property is
removed") {
+ withRecordingCatalog {
+ val schema = new StructType().add("id", IntegerType)
+ materializeStreamingTable(
+ "t", schema, Map("p.a" -> "1", TableCatalog.PROP_COMMENT -> "old"))
+ assert(
+
loadTableFromRecordingCatalog("t").properties().get(TableCatalog.PROP_COMMENT)
== "old")
+
+ materializeStreamingTable("t", schema, Map("p.a" -> "1"))
+ val props = loadTableFromRecordingCatalog("t").properties()
+ assert(!props.containsKey(TableCatalog.PROP_COMMENT))
+ assert(props.get("p.a") == "1")
+ assert(props.get(PipelinesTableProperties.declaredPropertyKeys) ==
"""["p.a"]""")
+ }
+ }
+
+ test("SPARK-57670: a comment moved from the table properties to the comment
field stays") {
+ withRecordingCatalog {
+ val schema = new StructType().add("id", IntegerType)
+ materializeStreamingTable("t", schema, Map(TableCatalog.PROP_COMMENT ->
"kept"))
+ recordingCatalog.recordedAlters.clear()
+
+ materializeStreamingTable("t", schema, Map.empty, comment =
Option("kept"))
+ // The definition still supplies the comment, so only the record goes
away.
+ assert(recordingCatalog.recordedAlters.flatten.map {
+ case r: TableChange.RemoveProperty => r.property()
+ case other => other.toString
+ } == Seq(PipelinesTableProperties.declaredPropertyKeys))
+ val props = loadTableFromRecordingCatalog("t").properties()
+ assert(props.get(TableCatalog.PROP_COMMENT) == "kept")
+ assert(!props.containsKey(PipelinesTableProperties.declaredPropertyKeys))
+ }
+ }
+
+ test("SPARK-57670: a dropped reserved key other than comment declared as a
table property " +
+ "stays on the table") {
+ withRecordingCatalog {
+ val schema = new StructType().add("id", IntegerType)
+ materializeStreamingTable(
+ "t", schema, Map("p.a" -> "1", TableCatalog.PROP_OWNER -> "someone"))
+ assert(loadTableFromRecordingCatalog("t").properties()
+ .get(PipelinesTableProperties.declaredPropertyKeys) ==
"""["owner","p.a"]""")
+
+ materializeStreamingTable("t", schema, Map("p.a" -> "1"))
+ val props = loadTableFromRecordingCatalog("t").properties()
+ assert(props.get(TableCatalog.PROP_OWNER) == "someone")
+ assert(props.get(PipelinesTableProperties.declaredPropertyKeys) ==
"""["p.a"]""")
+ }
+ }
+
+ test("SPARK-57670: a dropped comment declared as a table property is removed
on the session " +
+ "catalog") {
+ val schema = new StructType().add("id", IntegerType)
+ materializeStreamingTable(
+ "t", schema, Map(TableCatalog.PROP_COMMENT -> "old"), catalog = None,
database = None)
+ assert(sessionCatalogTable("t").comment.contains("old"))
+
+ materializeStreamingTable("t", schema, Map.empty, catalog = None, database
= None)
+ assert(sessionCatalogTable("t").comment.isEmpty)
+
assert(!sessionCatalogTable("t").properties.contains(TableCatalog.PROP_COMMENT))
+
+ // A comment the definition keeps supplying through its comment field
stays.
+ materializeStreamingTable(
+ "t", schema, Map(TableCatalog.PROP_COMMENT -> "again"), catalog = None,
database = None)
+ assert(sessionCatalogTable("t").comment.contains("again"))
+ materializeStreamingTable(
+ "t", schema, Map.empty, comment = Option("again"), catalog = None,
database = None)
+ assert(sessionCatalogTable("t").comment.contains("again"))
+ }
+
+ test("SPARK-57670: table comments survive the declared-keys bootstrap and
removal alters") {
+ val schema = new StructType().add("id", IntegerType)
+ def materialize(properties: Map[String, String]): Unit = {
+ materializeStreamingTable(
+ "declared", schema, properties, comment = Option("declared comment"),
+ catalog = None, database = None)
+ materializeStreamingTable("external", schema, properties, catalog =
None, database = None)
+ }
+ def checkComments(): Unit = {
+ assert(sessionCatalogTable("declared").comment.contains("declared
comment"))
+ assert(sessionCatalogTable("external").comment.contains("external
comment"))
+ }
+ val db = s"${TestGraphRegistrationContext.DEFAULT_CATALOG}." +
+ TestGraphRegistrationContext.DEFAULT_DATABASE
+
+ materialize(Map("p.keep" -> "v", "p.stale" -> "old"))
+ // A comment set outside the pipeline.
+ spark.sql(s"COMMENT ON TABLE $db.external IS 'external comment'")
+ // Stand in for tables materialized before the record was introduced.
+ Seq("declared", "external").foreach { name =>
+ spark.sql(
+ s"ALTER TABLE $db.$name UNSET TBLPROPERTIES " +
+ s"('${PipelinesTableProperties.declaredPropertyKeys}')")
+ }
+ checkComments()
+
+ // The run whose only change is writing the record on the otherwise
unchanged tables.
+ materialize(Map("p.keep" -> "v", "p.stale" -> "old"))
+ checkComments()
+ assert(sessionCatalogTable("declared").properties
+
.get(PipelinesTableProperties.declaredPropertyKeys).contains("""["p.keep","p.stale"]"""))
+
+ // The run whose only change is dropping a recorded key.
+ materialize(Map("p.keep" -> "v"))
+ checkComments()
+ assert(!sessionCatalogTable("declared").properties.contains("p.stale"))
+ assert(!sessionCatalogTable("external").properties.contains("p.stale"))
+
assert(sessionCatalogTable("external").properties.get("p.keep").contains("v"))
+ }
+
+ test("SPARK-57670: a malformed declared-keys record removes nothing and is
rewritten") {
+ withRecordingCatalog {
+ val schema = new StructType().add("id", IntegerType)
+ Seq("t1" -> "not-json", "t2" -> """["p.stale", 1]""").foreach { case
(name, malformed) =>
+ materializeStreamingTable(name, schema, Map("p.a" -> "1", "p.stale" ->
"old"))
+ spark.sql(
+ s"ALTER TABLE $recordingCatalogName.$recordingNamespace.$name SET
TBLPROPERTIES " +
+ s"('${PipelinesTableProperties.declaredPropertyKeys}' =
'$malformed')")
+
+ materializeStreamingTable(name, schema, Map("p.a" -> "1"))
+ val props = loadTableFromRecordingCatalog(name).properties()
+ assert(props.get("p.stale") == "old", s"record: $malformed")
+ assert(props.get(PipelinesTableProperties.declaredPropertyKeys) ==
"""["p.a"]""",
+ s"record: $malformed")
+ }
+ }
+ }
+
+ test("SPARK-57670: a declared-keys record with trailing content is malformed
and removes " +
Review Comment:
The two malformed-record tests are now one table-driven test, "SPARK-57670:
a malformed declared-keys record removes nothing, is rewritten and is logged".
It has one row for each of the five inputs the two tests covered (`not-json`, a
mixed-type array, and the three arrays followed by more input). Each row
asserts that `p.stale` stays, that the record is rewritten to `["p.a"]`, and
that the malformed-record warning is logged. The new suite-local helpers
`declaredKeysRecord(name)` and `setRecord(name, value)` replace the repeated
record lookups and `ALTER ... SET TBLPROPERTIES` snippets in the
recording-catalog record tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]