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

fix: sort result set by sql type value in metadata typeinfo #910

Closed
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java
Expand Up @@ -14,6 +14,7 @@
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;

import java.io.IOException;
import java.sql.Array;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
Expand All @@ -24,6 +25,8 @@
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -2227,6 +2230,20 @@ public ResultSet getTypeInfo() throws SQLException {
rs.close();
stmt.close();

// Per JDBC API, we should sort result by SqlType value
Collections.sort(v, new Comparator<byte[][]>() {
@Override
public int compare(byte[][] o1, byte[][] o2) {
try {
int i1 = Integer.parseInt(connection.getEncoding().decode(o1[1]));
Copy link
Member

Choose a reason for hiding this comment

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

Would you please perform parseInt/decode stuff outside of Comparator to avoid repeated decode/parse in the sort loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @vlsi, The decode part is needed in the loop because we need the byte[1] "sqlType" as input byte. Moving it outside means I have to create new list of byte array and associate the origingal sqlType int value as one element. And again to remove the int sqlType as final result after the sorting for return value. Can you please confirm is that what you like to see?

int i2 = Integer.parseInt(connection.getEncoding().decode(o2[1]));
return (i1 < i2) ? -1 : ((i1 == i2) ? 0 : 1);
} catch (IOException e) {
return -1; // Should not happen, but if we do, we should avoid breaking the operation.
}
}
});

return ((BaseStatement) createMetaDataStatement()).createDriverResultSet(f, v);
}

Expand Down
Expand Up @@ -80,4 +80,18 @@ public void testGetSchemas() throws SQLException {
assertNull(rs.getString("TABLE_CATALOG"));
assertTrue(!rs.next());
}

@Test
public void testSortedDataTypes() throws SQLException {
// https://github.com/pgjdbc/pgjdbc/issues/716
DatabaseMetaData dbmd = _conn.getMetaData();
ResultSet rs = dbmd.getTypeInfo();
int lastType = Integer.MIN_VALUE;
while (rs.next()) {
int type = rs.getInt("DATA_TYPE");
assertTrue(lastType <= type);
lastType = type;
}
rs.close();
}
}