Skip to content

Commit

Permalink
Fix iceberg hive metadata listTables for non existent schema
Browse files Browse the repository at this point in the history
  • Loading branch information
nmahadevuni authored and auden-woolfson committed Jan 17, 2025
1 parent ae68ae4 commit dfc6304
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public abstract class IcebergAbstractMetadata
implements ConnectorMetadata
{
private static final Logger log = Logger.get(IcebergAbstractMetadata.class);
protected static final String INFORMATION_SCHEMA = "information_schema";

protected final TypeManager typeManager;
protected final JsonCodec<CommitTaskData> commitTaskCodec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,20 @@ public List<String> listSchemaNames(ConnectorSession session)
public List<SchemaTableName> listTables(ConnectorSession session, Optional<String> schemaName)
{
MetastoreContext metastoreContext = getMetastoreContext(session);
if (schemaName.isPresent() && INFORMATION_SCHEMA.equals(schemaName.get())) {
return metastore.getAllDatabases(metastoreContext)
.stream()
.map(table -> new SchemaTableName(INFORMATION_SCHEMA, table))
.collect(toImmutableList());
}
// If schema name is not present, list tables from all schemas
List<String> schemaNames = schemaName
.map(ImmutableList::of)
.orElseGet(() -> ImmutableList.copyOf(listSchemaNames(session)));
return schemaNames.stream()
.flatMap(schema -> metastore
.getAllTables(metastoreContext, schema)
.orElseGet(() -> metastore.getAllDatabases(metastoreContext))
.orElseGet(() -> ImmutableList.of())
.stream()
.map(table -> new SchemaTableName(schema, table)))
.collect(toImmutableList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
public class IcebergNativeMetadata
extends IcebergAbstractMetadata
{
private static final String INFORMATION_SCHEMA = "information_schema";
private static final String VIEW_DIALECT = "presto";

private final IcebergNativeCatalogFactory catalogFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,29 @@ public void testHistogramReconstruction(Type type, Object[] values)
}
}

@Test
public void testInformationSchemaQueries()
{
assertQuerySucceeds("CREATE SCHEMA ICEBERG.TEST_SCHEMA1");
assertQuerySucceeds("CREATE SCHEMA ICEBERG.TEST_SCHEMA2");
assertQuerySucceeds("CREATE TABLE ICEBERG.TEST_SCHEMA1.ICEBERG_T1(i int)");
assertQuerySucceeds("CREATE TABLE ICEBERG.TEST_SCHEMA1.ICEBERG_T2(i int)");
assertQuerySucceeds("CREATE TABLE ICEBERG.TEST_SCHEMA2.ICEBERG_T3(i int)");
assertQuerySucceeds("CREATE TABLE ICEBERG.TEST_SCHEMA2.ICEBERG_T4(i int)");

assertQuery("SELECT table_name FROM iceberg.information_schema.tables WHERE table_schema ='test_schema1'", "VALUES 'iceberg_t1', 'iceberg_t2'");
assertQuery("SELECT table_name FROM iceberg.information_schema.tables WHERE table_schema ='test_schema2'", "VALUES 'iceberg_t3', 'iceberg_t4'");
//query on non-existing schema
assertQueryReturnsEmptyResult("SELECT table_name FROM iceberg.information_schema.tables WHERE table_schema = 'NON_EXISTING_SCHEMA'");

assertQuerySucceeds("DROP TABLE ICEBERG.TEST_SCHEMA1.ICEBERG_T1");
assertQuerySucceeds("DROP TABLE ICEBERG.TEST_SCHEMA1.ICEBERG_T2");
assertQuerySucceeds("DROP TABLE ICEBERG.TEST_SCHEMA2.ICEBERG_T3");
assertQuerySucceeds("DROP TABLE ICEBERG.TEST_SCHEMA2.ICEBERG_T4");
assertQuerySucceeds("DROP SCHEMA ICEBERG.TEST_SCHEMA1");
assertQuerySucceeds("DROP SCHEMA ICEBERG.TEST_SCHEMA2");
}

private void testCheckDeleteFiles(Table icebergTable, int expectedSize, List<FileContent> expectedFileContent)
{
// check delete file list
Expand Down

0 comments on commit dfc6304

Please sign in to comment.