Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-48618][SQL] Utilize the ErrorCode and SQLState returned in SQLException to make errors more accurate #46969

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -942,4 +942,19 @@ private[v2] trait V2JDBCTest extends SharedSparkSession with DockerIntegrationFu
assert(row(2).getDouble(0) === 0.0)
}
}

test("SPARK-48618: Renaming the table to the name of an existing table") {
withTable(s"$catalogName.tbl1", s"$catalogName.tbl2") {
sql(s"CREATE TABLE $catalogName.tbl1 (col1 INT, col2 INT)")
sql(s"CREATE TABLE $catalogName.tbl2 (col3 INT, col4 INT)")

checkError(
exception = intercept[AnalysisException] {
sql(s"ALTER TABLE $catalogName.tbl2 RENAME TO tbl1")
},
errorClass = "TABLE_OR_VIEW_ALREADY_EXISTS",
parameters = Map("relationName" -> "`tbl1`")
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.apache.spark.sql.catalyst.SQLConfHelper
import org.apache.spark.sql.catalyst.analysis.NonEmptyNamespaceException
import org.apache.spark.sql.connector.catalog.Identifier
import org.apache.spark.sql.connector.expressions.Expression
import org.apache.spark.sql.errors.QueryCompilationErrors
import org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions
import org.apache.spark.sql.types._

Expand Down Expand Up @@ -167,6 +168,9 @@ private case class DB2Dialect() extends JdbcDialect with SQLConfHelper {
namespace = messageParameters.get("namespace").toArray,
details = sqlException.getMessage,
cause = Some(e))
case "42710" if errorClass == "FAILED_JDBC.RENAME_TABLE" =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref:

errorCode: -601, sqlState: 42710, message: The name of the object to be created is identical to the existing name "DB2INST1.tbl1" of type "TABLE".. SQLCODE=-601, SQLSTATE=42710, DRIVER=4.33.31, errorClass: FAILED_JDBC.RENAME_TABLE, messageParameters: Map(url -> jdbc:db2://10.1.0.20:43213/foo:user=db2inst1;***;retrieveMessagesFromServerOnGetMessage=true;, oldName -> `tbl2`, newName -> `tbl1`),description: Failed table renaming from tbl2 to tbl1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val newTable = messageParameters("newName")
throw QueryCompilationErrors.tableAlreadyExistsError(newTable)
case _ => super.classifyException(e, errorClass, messageParameters, description)
}
case _ => super.classifyException(e, errorClass, messageParameters, description)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import org.apache.spark.sql.catalyst.analysis.NonEmptyNamespaceException
import org.apache.spark.sql.connector.catalog.Identifier
import org.apache.spark.sql.connector.expressions.{Expression, NullOrdering, SortDirection}
import org.apache.spark.sql.connector.expressions.filter.Predicate
import org.apache.spark.sql.errors.QueryExecutionErrors
import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryExecutionErrors}
import org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.jdbc.MsSqlServerDialect.{GEOGRAPHY, GEOMETRY}
Expand Down Expand Up @@ -216,6 +216,9 @@ private case class MsSqlServerDialect() extends JdbcDialect {
namespace = messageParameters.get("namespace").toArray,
details = sqlException.getMessage,
cause = Some(e))
case 15335 if errorClass == "FAILED_JDBC.RENAME_TABLE" =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref:

errorCode: 15335, sqlState: S0001, message: Error: The new name 'tbl1' is already in use as a object name and would cause a duplicate that is not permitted., errorClass: FAILED_JDBC.RENAME_TABLE, messageParameters: Map(url -> jdbc:sqlserver://10.1.0.20:38701;user=sa;***;encrypt=true;trustServerCertificate=true, oldName -> `tbl2`, newName -> `tbl1`),description: Failed table renaming from tbl2 to tbl1

val newTable = messageParameters("newName")
throw QueryCompilationErrors.tableAlreadyExistsError(newTable)
case _ => super.classifyException(e, errorClass, messageParameters, description)
}
case _ => super.classifyException(e, errorClass, messageParameters, description)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.apache.spark.sql.catalyst.analysis.{IndexAlreadyExistsException, NoSu
import org.apache.spark.sql.connector.catalog.Identifier
import org.apache.spark.sql.connector.catalog.index.TableIndex
import org.apache.spark.sql.connector.expressions.{Expression, FieldReference, NamedReference, NullOrdering, SortDirection}
import org.apache.spark.sql.errors.QueryExecutionErrors
import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryExecutionErrors}
import org.apache.spark.sql.execution.datasources.jdbc.{JDBCOptions, JdbcUtils}
import org.apache.spark.sql.types._

Expand Down Expand Up @@ -337,6 +337,9 @@ private case class MySQLDialect() extends JdbcDialect with SQLConfHelper {
case sqlException: SQLException =>
sqlException.getErrorCode match {
// ER_DUP_KEYNAME
case 1050 if errorClass == "FAILED_JDBC.RENAME_TABLE" =>
val newTable = messageParameters("newName")
throw QueryCompilationErrors.tableAlreadyExistsError(newTable)
case 1061 if errorClass == "FAILED_JDBC.CREATE_INDEX" =>
val indexName = messageParameters("indexName")
val tableName = messageParameters("tableName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@

package org.apache.spark.sql.jdbc

import java.sql.{Date, Timestamp, Types}
import java.sql.{Date, SQLException, Timestamp, Types}
import java.util.Locale

import scala.util.control.NonFatal

import org.apache.spark.SparkUnsupportedOperationException
import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst.SQLConfHelper
import org.apache.spark.sql.connector.expressions.Expression
import org.apache.spark.sql.errors.QueryCompilationErrors
import org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions
import org.apache.spark.sql.jdbc.OracleDialect._
import org.apache.spark.sql.types._
Expand Down Expand Up @@ -229,6 +231,23 @@ private case class OracleDialect() extends JdbcDialect with SQLConfHelper {
override def supportsLimit: Boolean = true

override def supportsOffset: Boolean = true

override def classifyException(
e: Throwable,
errorClass: String,
messageParameters: Map[String, String],
description: String): AnalysisException = {
e match {
case sqlException: SQLException =>
sqlException.getErrorCode match {
case 955 if errorClass == "FAILED_JDBC.RENAME_TABLE" =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref:

errorCode: 955, sqlState: 42000, message: ORA-00955: name is already used by an existing object
https://docs.oracle.com/error-help/db/ora-00955/, errorClass: FAILED_JDBC.RENAME_TABLE, messageParameters: Map(url -> jdbc:oracle:thin:system/Th1s1sThe0racle#Pass@//10.1.0.20:38407/freepdb1, oldName -> `tbl2`, newName -> `tbl1`),description: Failed table renaming from tbl2 to tbl1

val newTable = messageParameters("newName")
throw QueryCompilationErrors.tableAlreadyExistsError(newTable)
case _ => super.classifyException(e, errorClass, messageParameters, description)
}
case _ => super.classifyException(e, errorClass, messageParameters, description)
}
}
}

private[jdbc] object OracleDialect {
Expand Down