Skip to content

Commit

Permalink
Handle case of table with no supported columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Akanksha-kedia authored and tdcmeehan committed Dec 13, 2023
1 parent 8584c85 commit 333ae21
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,10 @@ public List<JdbcColumnHandle> getColumns(ConnectorSession session, JdbcTableHand
{
try (Connection connection = connectionFactory.openConnection(JdbcIdentity.from(session))) {
try (ResultSet resultSet = getColumns(tableHandle, connection.getMetaData())) {
int allColumns = 0;
List<JdbcColumnHandle> columns = new ArrayList<>();
while (resultSet.next()) {
allColumns++;
JdbcTypeHandle typeHandle = new JdbcTypeHandle(
resultSet.getInt("DATA_TYPE"),
resultSet.getString("TYPE_NAME"),
Expand All @@ -249,8 +251,12 @@ public List<JdbcColumnHandle> getColumns(ConnectorSession session, JdbcTableHand
}
}
if (columns.isEmpty()) {
// In rare cases (e.g. PostgreSQL) a table might have no columns.
throw new TableNotFoundException(tableHandle.getSchemaTableName());
// A table may have no supported columns. In rare cases (e.g. PostgreSQL) a table might have no columns at all.
// Throw an exception if the table has no supported columns.
// This can occur if all columns in the table are of unsupported types, or in rare cases, if the table has no columns at all.
throw new TableNotFoundException(
tableHandle.getSchemaTableName(),
format("Table '%s' has no supported columns (all %s columns are not supported)", tableHandle.getSchemaTableName(), allColumns));
}
return ImmutableList.copyOf(columns);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,13 @@ public void testTableWithNoSupportedColumns()
AutoCloseable ignore2 = withTable("tpch.supported_columns", format("(good %s)", supportedDataType));
AutoCloseable ignore3 = withTable("tpch.no_columns", "()")) {
assertThat(computeActual("SHOW TABLES").getOnlyColumnAsSet()).contains("orders", "no_supported_columns", "supported_columns", "no_columns");
assertQueryFails("SELECT c FROM tpch.no_supported_columns", "\\QTable 'tpch.no_supported_columns" + "' has no supported columns (all 1 columns are not supported)");
assertQueryFails("SELECT * FROM tpch.no_supported_columns", "\\QTable 'tpch.no_supported_columns" + "' has no supported columns (all 1 columns are not supported)");
assertQueryFails("SELECT 'a' FROM tpch.no_supported_columns", "\\QTable 'tpch.no_supported_columns" + "' has no supported columns (all 1 columns are not supported)");

assertQueryFails("SELECT c FROM no_supported_columns", "Table 'tpch.no_supported_columns' not found");
assertQueryFails("SELECT * FROM no_supported_columns", "Table 'tpch.no_supported_columns' not found");
assertQueryFails("SELECT 'a' FROM no_supported_columns", "Table 'tpch.no_supported_columns' not found");

assertQueryFails("SELECT c FROM no_columns", "Table 'tpch.no_columns' not found");
assertQueryFails("SELECT * FROM no_columns", "Table 'tpch.no_columns' not found");
assertQueryFails("SELECT 'a' FROM no_columns", "Table 'tpch.no_columns' not found");
assertQueryFails("SELECT c FROM tpch.no_columns", "\\QTable 'tpch.no_columns" + "' has no supported columns (all 0 columns are not supported)");
assertQueryFails("SELECT * FROM tpch.no_columns", "\\QTable 'tpch.no_columns" + "' has no supported columns (all 0 columns are not supported)");
assertQueryFails("SELECT 'a' FROM tpch.no_columns", "\\QTable 'tpch.no_columns" + "' has no supported columns (all 0 columns are not supported)");

assertQueryFails("SELECT c FROM non_existent", ".*Table .*tpch.non_existent.* does not exist");
assertQueryFails("SELECT * FROM non_existent", ".*Table .*tpch.non_existent.* does not exist");
Expand Down

0 comments on commit 333ae21

Please sign in to comment.