HeartSaVioR commented on code in PR #47524:
URL: https://github.com/apache/spark/pull/47524#discussion_r1711118565


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/StateSchemaCompatibilityChecker.scala:
##########
@@ -166,21 +170,43 @@ class StateSchemaCompatibilityChecker(
   def validateAndMaybeEvolveStateSchema(
       newStateSchema: List[StateStoreColFamilySchema],
       ignoreValueSchema: Boolean,
-      stateSchemaVersion: Int): Boolean = {
+      stateSchemaVersion: Int): (Boolean, List[StateStoreColFamilySchema]) = {
     val existingStateSchemaList = 
getExistingKeyAndValueSchema().sortBy(_.colFamilyName)
-    val newStateSchemaList = newStateSchema.sortBy(_.colFamilyName)
+    // assign colFamilyIds based on position in list
+    var maxId: Short = 
existingStateSchemaList.map(_.colFamilyId).maxOption.getOrElse(0)
 
     if (existingStateSchemaList.isEmpty) {
       // write the schema file if it doesn't exist
+      val newStateSchemaList = 
newStateSchema.sortBy(_.colFamilyName).zipWithIndex.map {
+        case (schema, index) =>
+          schema.copy(colFamilyId = (index + 1).toShort)
+      }
       createSchemaFile(newStateSchemaList, stateSchemaVersion)
-      true
+      (true, newStateSchemaList)
     } else {
-      // validate if the new schema is compatible with the existing schema
-      existingStateSchemaList.lazyZip(newStateSchemaList).foreach {
-        case (existingStateSchema, newStateSchema) =>
-          check(existingStateSchema, newStateSchema, ignoreValueSchema)
+      // create a hashmap of name to schema from existing state schema list
+      val existingSchemas = existingStateSchemaList.map { schema =>
+        schema.colFamilyName -> schema
+      }.toMap
+      // For each column family we are creating, check if it existed in the
+      // previous run, and assign its ID to the old ID if the schema hasn't 
changed.
+      // If the schema has evolved, assign it a new ID.

Review Comment:
   And I don't see we migrate the existing data, so this seems to be a hole of 
data loss.



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to