gengliangwang commented on code in PR #50561: URL: https://github.com/apache/spark/pull/50561#discussion_r2044866814
########## sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala: ########## @@ -296,6 +297,49 @@ private[sql] object CatalogV2Util { } } + /** + * Apply Table Constraints changes to an existing table and return the result. + */ + def applyConstraintChanges( + table: Table, + changes: Seq[TableChange]): Array[Constraint] = { + val constraints = table.constraints() + + def findExistingConstraint(name: String): Option[Constraint] = { + constraints.find(_.name.toLowerCase(Locale.ROOT) == name.toLowerCase(Locale.ROOT)) + } + + changes.foldLeft(constraints) { (constraints, change) => + change match { + case add: AddConstraint => + val newConstraint = add.constraint + val existingConstraint = findExistingConstraint(newConstraint.name) + if (existingConstraint.isDefined) { + throw new AnalysisException( + errorClass = "CONSTRAINT_ALREADY_EXISTS", + messageParameters = + Map("constraintName" -> existingConstraint.get.name, + "oldConstraint" -> existingConstraint.get.toDDL)) Review Comment: It would be easier for users to know what is in the old constraint without running a describe table command and find details. Constraints should be simple. We will show all the constraints in describe table command anyway. -- 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