MaxGekk commented on code in PR #37887:
URL: https://github.com/apache/spark/pull/37887#discussion_r1014297862


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/AlreadyExistException.scala:
##########
@@ -20,66 +20,112 @@ package org.apache.spark.sql.catalyst.analysis
 import org.apache.spark.sql.AnalysisException
 import org.apache.spark.sql.catalyst.InternalRow
 import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec
-import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
-import org.apache.spark.sql.connector.catalog.Identifier
+import org.apache.spark.sql.catalyst.util.{quoteIdentifier, quoteNameParts }
 import org.apache.spark.sql.types.StructType
 
 /**
  * Thrown by a catalog when an item already exists. The analyzer will rethrow 
the exception
  * as an [[org.apache.spark.sql.AnalysisException]] with the correct position 
information.
  */
 class DatabaseAlreadyExistsException(db: String)
-  extends NamespaceAlreadyExistsException(s"Database '$db' already exists")
+  extends NamespaceAlreadyExistsException(Array(db))
 
-class NamespaceAlreadyExistsException(message: String)
-  extends AnalysisException(
-    message,
-    errorClass = Some("_LEGACY_ERROR_TEMP_1118"),
-    messageParameters = Map("msg" -> message)) {
+
+class NamespaceAlreadyExistsException(errorClass: String, messageParameters: 
Map[String, String])
+  extends AnalysisException(errorClass, messageParameters) {
   def this(namespace: Array[String]) = {
-    this(s"Namespace '${namespace.quoted}' already exists")
+    this(errorClass = "SCHEMA_ALREADY_EXISTS",
+      Map("schemaName" -> quoteNameParts(namespace)))
   }
 }
 
-class TableAlreadyExistsException(message: String, cause: Option[Throwable] = 
None)
-  extends AnalysisException(
-    message,
-    errorClass = Some("_LEGACY_ERROR_TEMP_1116"),
-    messageParameters = Map("msg" -> message),
-    cause = cause) {
+
+class TableAlreadyExistsException(errorClass: String, messageParameters: 
Map[String, String],
+  cause: Option[Throwable] = None)
+  extends AnalysisException(errorClass, messageParameters, cause = cause) {
   def this(db: String, table: String) = {
-    this(s"Table or view '$table' already exists in database '$db'")
+    this(errorClass = "TABLE_OR_VIEW_ALREADY_EXISTS",
+      messageParameters = Map("relationName" ->
+        (quoteIdentifier(db) + "." + quoteIdentifier(table))))
+  }
+
+  def this(table: String) = {
+    this(errorClass = "TABLE_OR_VIEW_ALREADY_EXISTS",
+      messageParameters = Map("relationName" ->
+        quoteNameParts(UnresolvedAttribute.parseAttributeName(table))))
   }
 
-  def this(tableIdent: Identifier) = {
-    this(s"Table ${tableIdent.quoted} already exists")
+  def this(table: Seq[String]) = {
+    this(errorClass = "TABLE_OR_VIEW_ALREADY_EXISTS",
+      messageParameters = Map("relationName" -> quoteNameParts(table)))
+  }
+}
+
+class TempTableAlreadyExistsException(errorClass: String, messageParameters: 
Map[String, String],
+                                      cause: Option[Throwable] = None)
+  extends AnalysisException(errorClass, messageParameters, cause = cause) {
+  def this(table: String) = {
+    this(errorClass = "TEMP_TABLE_OR_VIEW_ALREADY_EXISTS",
+      messageParameters = Map("relationName"
+        -> quoteNameParts(UnresolvedAttribute.parseAttributeName(table))))
   }
 }
 
-class TempTableAlreadyExistsException(table: String)
-  extends TableAlreadyExistsException(s"Temporary view '$table' already 
exists")
+class PartitionAlreadyExistsException(errorClass: String, messageParameters: 
Map[String, String])
+  extends AnalysisException(errorClass, messageParameters) {
+  def this(db: String, table: String, spec: TablePartitionSpec) = {
+    this(errorClass = "PARTITIONS_ALREADY_EXIST",
+      Map("partitionList" -> ("PARTITION (" +
+        spec.map( kv => quoteIdentifier(kv._1) + s" = ${kv._2}").mkString(", 
") + ")"),
+        "tableName" -> (quoteIdentifier(db) + "." + quoteIdentifier(table))))
+  }
+
+  def this(tableName: String, partitionIdent: InternalRow, partitionSchema: 
StructType) = {
+    this(errorClass = "PARTITIONS_ALREADY_EXIST",
+      Map("partitionList" ->
+        ("PARTITION (" + 
partitionIdent.toSeq(partitionSchema).zip(partitionSchema.map(_.name))
+        .map( kv => quoteIdentifier(s"${kv._2}") + s" = ${kv._1}").mkString(", 
") + ")"),
+        "tableName" -> 
quoteNameParts(UnresolvedAttribute.parseAttributeName(tableName))))
+  }
+}
 
-class PartitionsAlreadyExistException(message: String) extends 
AnalysisException(message) {
+class PartitionsAlreadyExistException(errorClass: String, messageParameters: 
Map[String, String])
+  extends AnalysisException(errorClass, messageParameters) {
   def this(db: String, table: String, specs: Seq[TablePartitionSpec]) = {
-    this(s"The following partitions already exist in table '$table' database 
'$db':\n"
-      + specs.mkString("\n===\n"))
+    this(errorClass = "PARTITIONS_ALREADY_EXIST",
+      Map("partitionList" ->
+        ("PARTITION ("
+        + specs.map(spec => spec.map(kv => quoteIdentifier(kv._1) + s" = 
${kv._2}").mkString(", "))
+        .mkString("), PARTITION (") + ")"),
+        "tableName" -> (quoteIdentifier(db) + "." + quoteIdentifier(table))))
   }
 
   def this(db: String, table: String, spec: TablePartitionSpec) =
     this(db, table, Seq(spec))
 
   def this(tableName: String, partitionIdents: Seq[InternalRow], 
partitionSchema: StructType) = {
-    this(s"The following partitions already exist in table $tableName:" +
-      partitionIdents.map(id => 
partitionSchema.map(_.name).zip(id.toSeq(partitionSchema))
-        .map( kv => s"${kv._1} -> 
${kv._2}").mkString(",")).mkString("\n===\n"))
+    this(errorClass = "PARTITIONS_ALREADY_EXIST",
+      Map("partitionList" ->
+        ("PARTITION (" +
+          
partitionIdents.map(_.toSeq(partitionSchema).zip(partitionSchema.map(_.name))
+            .map( kv => quoteIdentifier(s"${kv._2}") + s" = ${kv._1}")
+            .mkString(", ")).mkString("), PARTITION (") + ")"),
+        "tableName" -> 
quoteNameParts(UnresolvedAttribute.parseAttributeName(tableName))))
   }
 
   def this(tableName: String, partitionIdent: InternalRow, partitionSchema: 
StructType) =
     this(tableName, Seq(partitionIdent), partitionSchema)
 }
 
-class FunctionAlreadyExistsException(db: String, func: String)
-  extends AnalysisException(s"Function '$func' already exists in database 
'$db'")
+class FunctionAlreadyExistsException(errorClass: String, messageParameters: 
Map[String, String])
+  extends AnalysisException(errorClass, messageParameters) {
+
+  def this(function: Seq[String]) = {
+    this (errorClass = "ROUTINE_ALREADY_EXISTS",
+      Map("routineName" -> quoteNameParts(function)))
+  }
+}
 
 class IndexAlreadyExistsException(message: String, cause: Option[Throwable] = 
None)
-  extends AnalysisException(message, cause = cause)
+  extends AnalysisException(errorClass = "INDEX_NOT_FOUND",

Review Comment:
   This has been fixed in another PR as far as I remember. Please, check the 
master branch.



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

Reply via email to