Skip to content

Commit

Permalink
Fix DatabaseMetaData.getColumns method in JDBC driver
Browse files Browse the repository at this point in the history
The ResultSet returned from the method always threw an exception.
  • Loading branch information
electrum committed Oct 15, 2015
1 parent e1f89a1 commit 0c91d53
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
6 changes: 6 additions & 0 deletions presto-docs/src/main/sphinx/release/release-0.123.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ creating a Hive table you can specify the file format. To list all available ta
properties, run the following query::

SELECT * FROM system.metadata.table_properties

General Changes
---------------

* Fix exception when using the ``ResultSet`` returned from the
``DatabaseMetaData.getColumns`` method in the JDBC driver.
37 changes: 30 additions & 7 deletions presto-jdbc/src/test/java/com/facebook/presto/jdbc/TestDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -594,44 +594,67 @@ public void testGetColumns()
throws Exception
{
try (Connection connection = createConnection()) {
try (ResultSet rs = connection.getMetaData().getColumns(null, null, "tables", "column_name")) {
try (ResultSet rs = connection.getMetaData().getColumns(null, null, "tables", "table_name")) {
assertColumnMetadata(rs);
assertTrue(rs.next());
assertEquals(rs.getString("TABLE_CAT"), "system");
assertEquals(rs.getString("TABLE_SCHEM"), "information_schema");
assertEquals(rs.getString("TABLE_NAME"), "tables");
assertEquals(rs.getString("COLUMN_NAME"), "table_name");
assertEquals(rs.getInt("DATA_TYPE"), Types.LONGNVARCHAR);
assertTrue(rs.next());
assertEquals(rs.getString("TABLE_CAT"), "system");
assertEquals(rs.getString("TABLE_SCHEM"), "jdbc");
assertTrue(rs.next());
assertEquals(rs.getString("TABLE_CAT"), TEST_CATALOG);
assertEquals(rs.getString("TABLE_SCHEM"), "information_schema");
assertFalse(rs.next());
}
}

try (Connection connection = createConnection()) {
try (ResultSet rs = connection.getMetaData().getColumns(TEST_CATALOG, null, "tables", "column_name")) {
try (ResultSet rs = connection.getMetaData().getColumns(TEST_CATALOG, null, "tables", "table_name")) {
assertColumnMetadata(rs);
assertEquals(readRows(rs).size(), 1);
}
}

try (Connection connection = createConnection()) {
try (ResultSet rs = connection.getMetaData().getColumns(null, "information_schema", "tables", "column_name")) {
try (ResultSet rs = connection.getMetaData().getColumns(null, "information_schema", "tables", "table_name")) {
assertColumnMetadata(rs);
assertEquals(readRows(rs).size(), 2);
}
}

try (Connection connection = createConnection()) {
try (ResultSet rs = connection.getMetaData().getColumns(TEST_CATALOG, "information_schema", "tables", "column_name")) {
try (ResultSet rs = connection.getMetaData().getColumns(TEST_CATALOG, "information_schema", "tables", "table_name")) {
assertColumnMetadata(rs);
assertEquals(readRows(rs).size(), 1);
}
}

try (Connection connection = createConnection()) {
try (ResultSet rs = connection.getMetaData().getColumns(TEST_CATALOG, "inf%", "tables", "column_name")) {
try (ResultSet rs = connection.getMetaData().getColumns(TEST_CATALOG, "inf%", "tables", "table_name")) {
assertColumnMetadata(rs);
assertEquals(readRows(rs).size(), 1);
}
}

try (Connection connection = createConnection()) {
try (ResultSet rs = connection.getMetaData().getColumns(TEST_CATALOG, "information_schema", "tab%", "column_name")) {
try (ResultSet rs = connection.getMetaData().getColumns(TEST_CATALOG, "information_schema", "tab%", "table_name")) {
assertColumnMetadata(rs);
assertEquals(readRows(rs).size(), 1);
}
}

try (Connection connection = createConnection()) {
try (ResultSet rs = connection.getMetaData().getColumns(TEST_CATALOG, "information_schema", "tables", "col%")) {
try (ResultSet rs = connection.getMetaData().getColumns(TEST_CATALOG, "information_schema", "tables", "%m%")) {
assertColumnMetadata(rs);
assertTrue(rs.next());
assertEquals(rs.getString("COLUMN_NAME"), "table_schema");
assertTrue(rs.next());
assertEquals(rs.getString("COLUMN_NAME"), "table_name");
assertFalse(rs.next());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public long getLong(int field)
{
checkState(record != null, "no current record");
checkNotNull(record.get(field), "value is null");
return (Long) record.get(field);
return ((Number) record.get(field)).longValue();
}

@Override
Expand Down Expand Up @@ -226,7 +226,8 @@ public Builder addRow(Object... values)
checkArgument(value instanceof Boolean, "Expected value %d to be an instance of Boolean, but is a %s", i, value.getClass().getSimpleName());
}
else if (BIGINT.equals(type) || DATE.equals(type) || TIMESTAMP.equals(type) || TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
checkArgument(value instanceof Long, "Expected value %d to be an instance of Long, but is a %s", i, value.getClass().getSimpleName());
checkArgument(value instanceof Integer || value instanceof Long,
"Expected value %d to be an instance of Integer or Long, but is a %s", i, value.getClass().getSimpleName());
}
else if (DOUBLE.equals(type)) {
checkArgument(value instanceof Double, "Expected value %d to be an instance of Double, but is a %s", i, value.getClass().getSimpleName());
Expand Down Expand Up @@ -281,10 +282,7 @@ private static long sizeOf(List<?> record)
else if (value instanceof Boolean) {
completedBytes++;
}
else if (value instanceof Long) {
completedBytes += 8;
}
else if (value instanceof Double) {
else if (value instanceof Number) {
completedBytes += 8;
}
else if (value instanceof String) {
Expand Down

0 comments on commit 0c91d53

Please sign in to comment.