This is an automated email from the ASF dual-hosted git repository.

jmclean pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/main by this push:
     new 5c2047d062 [#8166] fix: treat PostgreSQL SQLSTATE as string (#8260)
5c2047d062 is described below

commit 5c2047d0620e8c23baefe3ebbdd9d25251678473
Author: Joonha <[email protected]>
AuthorDate: Mon Aug 25 22:16:29 2025 +0900

    [#8166] fix: treat PostgreSQL SQLSTATE as string (#8260)
    
    ### What changes were proposed in this pull request?
    
    Fix bug in `PostgreSQLExceptionConverter` where SQLSTATE codes were
    parsed as numbers.
    Now SQLSTATE is treated as a string to avoid `NumberFormatException`
    (e.g., "28P01").
    
    ### Why are the changes needed?
    
    PostgreSQL SQLSTATE codes can include letters. Parsing them as integers
    causes runtime errors.
    
    Fix: #8166
    
    ### Does this PR introduce _any_ user-facing change?
    
    No
    
    ### How was this patch tested?
    
    1. Added unit tests for numeric code ("23505") and non-numeric code
    ("28P01").
    2. Verified tests pass successfully.
    
    Co-authored-by: 이준하 <[email protected]>
    Co-authored-by: Justin Mclean <[email protected]>
---
 .../converters/PostgreSQLExceptionConverter.java   |  4 +--
 .../TestPostgreSQLExceptionConverter.java}         | 39 +++++++++++-----------
 2 files changed, 22 insertions(+), 21 deletions(-)

diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/converters/PostgreSQLExceptionConverter.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/converters/PostgreSQLExceptionConverter.java
index 7414760930..318e3865d9 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/converters/PostgreSQLExceptionConverter.java
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/converters/PostgreSQLExceptionConverter.java
@@ -29,13 +29,13 @@ import org.apache.gravitino.EntityAlreadyExistsException;
  * href="https://www.postgresql.org/docs/8.4/errcodes-appendix.html";>error 
code of PostgreSQL</a>
  */
 public class PostgreSQLExceptionConverter implements SQLExceptionConverter {
-  private static final int DUPLICATED_ENTRY_ERROR_CODE = 23505;
+  private static final String DUPLICATED_ENTRY_ERROR_CODE = "23505";
 
   @Override
   @SuppressWarnings("FormatStringAnnotation")
   public void toGravitinoException(SQLException sqlException, 
Entity.EntityType type, String name)
       throws IOException {
-    int errorCode = Integer.valueOf(sqlException.getSQLState());
+    String errorCode = sqlException.getSQLState();
     switch (errorCode) {
       case DUPLICATED_ENTRY_ERROR_CODE:
         throw new EntityAlreadyExistsException(
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/converters/PostgreSQLExceptionConverter.java
 
b/core/src/test/java/org/apache/gravitino/storage/relational/converters/TestPostgreSQLExceptionConverter.java
similarity index 51%
copy from 
core/src/main/java/org/apache/gravitino/storage/relational/converters/PostgreSQLExceptionConverter.java
copy to 
core/src/test/java/org/apache/gravitino/storage/relational/converters/TestPostgreSQLExceptionConverter.java
index 7414760930..9c6d9c4ea2 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/converters/PostgreSQLExceptionConverter.java
+++ 
b/core/src/test/java/org/apache/gravitino/storage/relational/converters/TestPostgreSQLExceptionConverter.java
@@ -22,26 +22,27 @@ import java.io.IOException;
 import java.sql.SQLException;
 import org.apache.gravitino.Entity;
 import org.apache.gravitino.EntityAlreadyExistsException;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
 
-/**
- * Exception converter to Apache Gravitino exception for PostgreSQL. The 
definition of error codes
- * can be found in the document: <a
- * href="https://www.postgresql.org/docs/8.4/errcodes-appendix.html";>error 
code of PostgreSQL</a>
- */
-public class PostgreSQLExceptionConverter implements SQLExceptionConverter {
-  private static final int DUPLICATED_ENTRY_ERROR_CODE = 23505;
+public class TestPostgreSQLExceptionConverter {
+
+  @Test
+  public void testConvertDuplicatedEntryException() {
+    SQLException sqlException = new SQLException("duplicate", "23505");
+    PostgreSQLExceptionConverter converter = new 
PostgreSQLExceptionConverter();
+    Assertions.assertThrows(
+        EntityAlreadyExistsException.class,
+        () -> converter.toGravitinoException(sqlException, 
Entity.EntityType.METALAKE, "test"),
+        String.format("The %s entity: %s already exists.", 
Entity.EntityType.METALAKE, "test"));
+  }
 
-  @Override
-  @SuppressWarnings("FormatStringAnnotation")
-  public void toGravitinoException(SQLException sqlException, 
Entity.EntityType type, String name)
-      throws IOException {
-    int errorCode = Integer.valueOf(sqlException.getSQLState());
-    switch (errorCode) {
-      case DUPLICATED_ENTRY_ERROR_CODE:
-        throw new EntityAlreadyExistsException(
-            sqlException, "The %s entity: %s already exists.", type.name(), 
name);
-      default:
-        throw new IOException(sqlException);
-    }
+  @Test
+  public void testNonNumericSqlStateHandledAsIOException() {
+    SQLException sqlException = new SQLException("error", "28P01");
+    PostgreSQLExceptionConverter converter = new 
PostgreSQLExceptionConverter();
+    Assertions.assertThrows(
+        IOException.class,
+        () -> converter.toGravitinoException(sqlException, 
Entity.EntityType.METALAKE, "test"));
   }
 }

Reply via email to