Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-48618][SQL] Utilize the
ErrorCode
and SQLState
returned in…
… SQLException to make errors more accurate ### What changes were proposed in this pull request? The pr aims to use the `ErrorCode` and `SQLState` returned in SQLException to make errors more accurate. ### Why are the changes needed? - eg for `mysql` ``` withTable(s"$catalogName.tbl1", s"$catalogName.tbl2") { sql(s"CREATE TABLE $catalogName.tbl1 (col1 INT, col2 INT)") sql(s"CREATE TABLE $catalogName.tbl2 (col1 INT, col2 INT)") sql(s"ALTER TABLE $catalogName.tbl2 RENAME TO tbl1") } ``` - Before: ``` [FAILED_JDBC.UNCLASSIFIED] Failed JDBC jdbc: on the operation: Failed table renaming from tbl2 to tbl1 SQLSTATE: HV000 org.apache.spark.sql.AnalysisException: [FAILED_JDBC.UNCLASSIFIED] Failed JDBC jdbc: on the operation: Failed table renaming from tbl2 to tbl1 SQLSTATE: HV000 at org.apache.spark.sql.jdbc.JdbcDialect.classifyException(JdbcDialects.scala:767) at org.apache.spark.sql.jdbc.JdbcDialect.classifyException(JdbcDialects.scala:751) at org.apache.spark.sql.jdbc.MySQLDialect.classifyException(MySQLDialect.scala:348) at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.classifyException(JdbcUtils.scala:1271) at org.apache.spark.sql.execution.datasources.v2.jdbc.JDBCTableCatalog.$anonfun$renameTable$1(JDBCTableCatalog.scala:125) at org.apache.spark.sql.execution.datasources.v2.jdbc.JDBCTableCatalog.$anonfun$renameTable$1$adapted(JDBCTableCatalog.scala:116) at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.withConnection(JdbcUtils.scala:1279) ``` - After: ``` [TABLE_OR_VIEW_ALREADY_EXISTS] Cannot create table or view `tbl1` because it already exists. Choose a different name, drop or replace the existing object, or add the IF NOT EXISTS clause to tolerate pre-existing objects. SQLSTATE: 42P07 org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException: [TABLE_OR_VIEW_ALREADY_EXISTS] Cannot create table or view `tbl1` because it already exists. Choose a different name, drop or replace the existing object, or add the IF NOT EXISTS clause to tolerate pre-existing objects. SQLSTATE: 42P07 at org.apache.spark.sql.errors.QueryCompilationErrors$.tableAlreadyExistsError(QueryCompilationErrors.scala:2643) at org.apache.spark.sql.jdbc.MySQLDialect.classifyException(MySQLDialect.scala:343) at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.classifyException(JdbcUtils.scala:1271) at org.apache.spark.sql.execution.datasources.v2.jdbc.JDBCTableCatalog.$anonfun$renameTable$1(JDBCTableCatalog.scala:125) at org.apache.spark.sql.execution.datasources.v2.jdbc.JDBCTableCatalog.$anonfun$renameTable$1$adapted(JDBCTableCatalog.scala:116) at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.withConnection(JdbcUtils.scala:1279) ``` - Aligning similar logic to `PostgresDialect` https://github.com/apache/spark/blob/08e741b92b8fc9e43c838d0849317916218414ce/sql/core/src/main/scala/org/apache/spark/sql/jdbc/PostgresDialect.scala#L273-L275 ### Does this PR introduce _any_ user-facing change? Yes, End users will accurately know the cause of the error. ### How was this patch tested? Add new UT. ### Was this patch authored or co-authored using generative AI tooling? No. Closes apache#46969 from panbingkun/fine-grained_jdbc_error. Authored-by: panbingkun <panbingkun@baidu.com> Signed-off-by: Max Gekk <max.gekk@gmail.com>
- Loading branch information