anishshri-db commented on code in PR #49277:
URL: https://github.com/apache/spark/pull/49277#discussion_r1899784571


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/StateSchemaCompatibilityChecker.scala:
##########
@@ -163,25 +157,65 @@ class StateSchemaCompatibilityChecker(
   private def check(
       oldSchema: StateStoreColFamilySchema,
       newSchema: StateStoreColFamilySchema,
-      ignoreValueSchema: Boolean) : Unit = {
-    val (storedKeySchema, storedValueSchema) = (oldSchema.keySchema,
-      oldSchema.valueSchema)
+      ignoreValueSchema: Boolean,
+      schemaEvolutionEnabled: Boolean): StateStoreColFamilySchema = {
+
+    val (storedKeySchema, storedValueSchema) = (oldSchema.keySchema, 
oldSchema.valueSchema)
     val (keySchema, valueSchema) = (newSchema.keySchema, newSchema.valueSchema)
 
+    def incrementSchemaId(id: Short): Short = (id + 1).toShort
+
+    // Initialize with old schema IDs
+    var resultSchema = newSchema.copy(
+      keySchemaId = oldSchema.keySchemaId,
+      valueSchemaId = oldSchema.valueSchemaId
+    )
+
     if (storedKeySchema.equals(keySchema) &&
       (ignoreValueSchema || storedValueSchema.equals(valueSchema))) {
-      // schema is exactly same
-    } else if (!schemasCompatible(storedKeySchema, keySchema)) {
-      throw 
StateStoreErrors.stateStoreKeySchemaNotCompatible(storedKeySchema.toString,
-        keySchema.toString)
-    } else if (!ignoreValueSchema && !schemasCompatible(storedValueSchema, 
valueSchema)) {
-      throw 
StateStoreErrors.stateStoreValueSchemaNotCompatible(storedValueSchema.toString,
-        valueSchema.toString)
+      // Schema is exactly same - return old schema
+      oldSchema
+    } else if (!ignoreValueSchema && schemaEvolutionEnabled) {
+      // Check value schema evolution
+      val oldAvroSchema = SchemaConverters.toAvroType(storedValueSchema)
+      val newAvroSchema = SchemaConverters.toAvroType(valueSchema)
+
+      val validator = new 
SchemaValidatorBuilder().canReadStrategy.validateAll()
+      validator.validate(newAvroSchema, Iterable(oldAvroSchema).asJava)
+
+      // Schema evolved - increment value schema ID
+      resultSchema.copy(valueSchemaId = 
incrementSchemaId(oldSchema.valueSchemaId))
     } else {
-      logInfo("Detected schema change which is compatible. Allowing to put 
rows.")
+      // Check compatibility
+      if (!schemasCompatible(storedKeySchema, keySchema)) {

Review Comment:
   Lets ensure that key schema evolution is still blocked ?



-- 
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