Обсуждение: [PATCH] Empty arrays cause SQLExceptions when using Array.getArray
I have fixed a minor bug in empty arrays as described below - Unified Context Diff provided at the end: I have the following table and values: =# \d array_test Table "array_test" Attribute | Type | Modifier -----------+-----------+------------------------------------------------------- id | integer | not null default nextval('"array_test_id_seq"'::text) first | integer[] | second | integer[] | number | integer | Index: array_test_pkey =# select * from array_test; id | first | second | number ----+------------+-----------------------------------+-------- 1 | {1,2,3} | {4,5,6} | 7 2 | {10,20,30} | {40,50,60} | 70 3 | {100,200} | {300,400,500,600} | 700 4 | {1000} | {2000,3000,4000,5000,6000} | 7000 5 | {} | {10000,2000,3000,4000,5000,6000} | 70000 6 | | {100000,2000,3000,4000,5000,6000} | 700000 7 | {9,8,7} | {6,5,4} | 8482 (7 rows) When I am getting row 5, the ResultSet.getArray().getArray() call fails: DbArrayTest.doTest: SQLException: Bad Integer DbArrayTest.doTest: SQLState: null DbArrayTest.doTest: VendorError: 0 Bad Integer at org.postgresql.jdbc2.ResultSet.toInt(ResultSet.java:1481) at org.postgresql.jdbc2.Array.getArray(Array.java:127) at org.postgresql.jdbc2.Array.getArray(Array.java:54) at com.pexicom.test.DbArrayTest.doTest(DbArrayTest.java:55) Code snippet in question: Array arrayObj; int[] array; while (rs.next()) { System.out.println("\nColumn 1: " + rs.getInt(1)); arrayObj = rs.getArray(2); if (arrayObj != null) { array = (int[])arrayObj.getArray(); System.out.println("Column 2: " + arrayObj); It fails at the arrayObj.getArray() call. This shouldn't happen; it should return a zero-length array. The diffs for my fix against the 7.2 source code: diff -ru5 org-orig/postgresql/jdbc2/Array.java org/postgresql/jdbc2/Array.java --- org-orig/postgresql/jdbc2/Array.java Mon Nov 19 18:16:46 2001 +++ org/postgresql/jdbc2/Array.java Wed Feb 20 19:50:41 2002 @@ -72,11 +72,15 @@ if (index < 1) throw new PSQLException("postgresql.arr.range"); Object retVal = null; ArrayList array = new ArrayList(); - if ( rawString != null ) + /* Check if the String is also not an empty array + * otherwise there will be an exception thrown below + * in the ResultSet.toX with an empty string. + * -- Doug Fields <dfields-pg-jdbc@pexicom.com> Feb 20, 2002 */ + if ( rawString != null && !rawString.equals("{}") ) { char[] chars = rawString.toCharArray(); StringBuffer sbuf = new StringBuffer(); boolean foundOpen = false; boolean insideString = false;
This has been saved for the 7.3 release: http://candle.pha.pa.us/cgi-bin/pgpatches2 --------------------------------------------------------------------------- Doug Fields wrote: > I have fixed a minor bug in empty arrays as described below - Unified > Context Diff provided at the end: > > I have the following table and values: > > =# \d array_test > Table "array_test" > Attribute > | Type | Modifier > -----------+-----------+------------------------------------------------------- > id | integer | not null default nextval('"array_test_id_seq"'::text) > first | integer[] | > second | integer[] | > number | integer | > Index: array_test_pkey > > =# select * from array_test; > id | first | second | number > ----+------------+-----------------------------------+-------- > 1 | {1,2,3} | {4,5,6} | 7 > 2 | {10,20,30} | {40,50,60} | 70 > 3 | {100,200} | {300,400,500,600} | 700 > 4 | {1000} | {2000,3000,4000,5000,6000} | 7000 > 5 | {} | {10000,2000,3000,4000,5000,6000} | 70000 > 6 | | {100000,2000,3000,4000,5000,6000} | 700000 > 7 | {9,8,7} | {6,5,4} | 8482 > (7 rows) > > When I am getting row 5, the ResultSet.getArray().getArray() call fails: > > DbArrayTest.doTest: SQLException: Bad Integer > DbArrayTest.doTest: SQLState: null > DbArrayTest.doTest: VendorError: 0 > Bad Integer > at org.postgresql.jdbc2.ResultSet.toInt(ResultSet.java:1481) > at org.postgresql.jdbc2.Array.getArray(Array.java:127) > at org.postgresql.jdbc2.Array.getArray(Array.java:54) > at com.pexicom.test.DbArrayTest.doTest(DbArrayTest.java:55) > > Code snippet in question: > > Array arrayObj; > int[] array; > while (rs.next()) { > System.out.println("\nColumn 1: " + rs.getInt(1)); > arrayObj = rs.getArray(2); > if (arrayObj != null) { > array = (int[])arrayObj.getArray(); > System.out.println("Column 2: " + arrayObj); > > It fails at the arrayObj.getArray() call. > > This shouldn't happen; it should return a zero-length array. > > The diffs for my fix against the 7.2 source code: > > diff -ru5 org-orig/postgresql/jdbc2/Array.java org/postgresql/jdbc2/Array.java > --- org-orig/postgresql/jdbc2/Array.java Mon Nov 19 18:16:46 2001 > +++ org/postgresql/jdbc2/Array.java Wed Feb 20 19:50:41 2002 > @@ -72,11 +72,15 @@ > if (index < 1) > throw new PSQLException("postgresql.arr.range"); > Object retVal = null; > > ArrayList array = new ArrayList(); > - if ( rawString != null ) > + /* Check if the String is also not an empty array > + * otherwise there will be an exception thrown below > + * in the ResultSet.toX with an empty string. > + * -- Doug Fields <dfields-pg-jdbc@pexicom.com> Feb 20, 2002 */ > + if ( rawString != null && !rawString.equals("{}") ) > { > char[] chars = rawString.toCharArray(); > StringBuffer sbuf = new StringBuffer(); > boolean foundOpen = false; > boolean insideString = false; > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org > -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 853-3000 + If your life is a hard drive, | 830 Blythe Avenue + Christ can be your backup. | Drexel Hill, Pennsylvania 19026
Your patch has been added to the PostgreSQL unapplied patches list at: http://candle.pha.pa.us/cgi-bin/pgpatches I will try to apply it within the next 48 hours. --------------------------------------------------------------------------- Doug Fields wrote: > I have fixed a minor bug in empty arrays as described below - Unified > Context Diff provided at the end: > > I have the following table and values: > > =# \d array_test > Table "array_test" > Attribute > | Type | Modifier > -----------+-----------+------------------------------------------------------- > id | integer | not null default nextval('"array_test_id_seq"'::text) > first | integer[] | > second | integer[] | > number | integer | > Index: array_test_pkey > > =# select * from array_test; > id | first | second | number > ----+------------+-----------------------------------+-------- > 1 | {1,2,3} | {4,5,6} | 7 > 2 | {10,20,30} | {40,50,60} | 70 > 3 | {100,200} | {300,400,500,600} | 700 > 4 | {1000} | {2000,3000,4000,5000,6000} | 7000 > 5 | {} | {10000,2000,3000,4000,5000,6000} | 70000 > 6 | | {100000,2000,3000,4000,5000,6000} | 700000 > 7 | {9,8,7} | {6,5,4} | 8482 > (7 rows) > > When I am getting row 5, the ResultSet.getArray().getArray() call fails: > > DbArrayTest.doTest: SQLException: Bad Integer > DbArrayTest.doTest: SQLState: null > DbArrayTest.doTest: VendorError: 0 > Bad Integer > at org.postgresql.jdbc2.ResultSet.toInt(ResultSet.java:1481) > at org.postgresql.jdbc2.Array.getArray(Array.java:127) > at org.postgresql.jdbc2.Array.getArray(Array.java:54) > at com.pexicom.test.DbArrayTest.doTest(DbArrayTest.java:55) > > Code snippet in question: > > Array arrayObj; > int[] array; > while (rs.next()) { > System.out.println("\nColumn 1: " + rs.getInt(1)); > arrayObj = rs.getArray(2); > if (arrayObj != null) { > array = (int[])arrayObj.getArray(); > System.out.println("Column 2: " + arrayObj); > > It fails at the arrayObj.getArray() call. > > This shouldn't happen; it should return a zero-length array. > > The diffs for my fix against the 7.2 source code: > > diff -ru5 org-orig/postgresql/jdbc2/Array.java org/postgresql/jdbc2/Array.java > --- org-orig/postgresql/jdbc2/Array.java Mon Nov 19 18:16:46 2001 > +++ org/postgresql/jdbc2/Array.java Wed Feb 20 19:50:41 2002 > @@ -72,11 +72,15 @@ > if (index < 1) > throw new PSQLException("postgresql.arr.range"); > Object retVal = null; > > ArrayList array = new ArrayList(); > - if ( rawString != null ) > + /* Check if the String is also not an empty array > + * otherwise there will be an exception thrown below > + * in the ResultSet.toX with an empty string. > + * -- Doug Fields <dfields-pg-jdbc@pexicom.com> Feb 20, 2002 */ > + if ( rawString != null && !rawString.equals("{}") ) > { > char[] chars = rawString.toCharArray(); > StringBuffer sbuf = new StringBuffer(); > boolean foundOpen = false; > boolean insideString = false; > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org > -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 853-3000 + If your life is a hard drive, | 830 Blythe Avenue + Christ can be your backup. | Drexel Hill, Pennsylvania 19026