Skip to content

Commit

Permalink
Return empty table list in Iceberg REST Catalog when invalid namespac…
Browse files Browse the repository at this point in the history
…e is provided
  • Loading branch information
amogh-jahagirdar authored and findepi committed Jan 8, 2024
1 parent 8fb9730 commit 2be084c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,7 @@ public void renameNamespace(ConnectorSession session, String source, String targ
public List<SchemaTableName> listTables(ConnectorSession session, Optional<String> namespace)
{
SessionContext sessionContext = convert(session);
List<Namespace> namespaces;

if (namespace.isPresent() && namespaceExists(session, namespace.get())) {
namespaces = ImmutableList.of(Namespace.of(namespace.get()));
}
else {
namespaces = listNamespaces(session).stream()
.map(Namespace::of)
.collect(toImmutableList());
}
List<Namespace> namespaces = listNamespaces(session, namespace);

ImmutableList.Builder<SchemaTableName> tables = ImmutableList.builder();
for (Namespace restNamespace : namespaces) {
Expand Down Expand Up @@ -555,4 +546,15 @@ private static TableIdentifier toIdentifier(SchemaTableName schemaTableName)
{
return TableIdentifier.of(schemaTableName.getSchemaName(), schemaTableName.getTableName());
}

private List<Namespace> listNamespaces(ConnectorSession session, Optional<String> namespace)
{
if (namespace.isEmpty()) {
return listNamespaces(session).stream()
.map(Namespace::of)
.collect(toImmutableList());
}

return ImmutableList.of(Namespace.of(namespace.get()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,47 @@ public void testView()
}
}

@Test
public void testListTables()
throws Exception
{
TrinoCatalog catalog = createTrinoCatalog(false);
TrinoPrincipal principal = new TrinoPrincipal(PrincipalType.USER, SESSION.getUser());
String ns1 = "ns1";
String ns2 = "ns2";

catalog.createNamespace(SESSION, ns1, defaultNamespaceProperties(ns1), principal);
catalog.createNamespace(SESSION, ns2, defaultNamespaceProperties(ns2), principal);
SchemaTableName table1 = new SchemaTableName(ns1, "t1");
SchemaTableName table2 = new SchemaTableName(ns2, "t2");
catalog.newCreateTableTransaction(
SESSION,
table1,
new Schema(Types.NestedField.of(1, true, "col1", Types.LongType.get())),
PartitionSpec.unpartitioned(),
SortOrder.unsorted(),
arbitraryTableLocation(catalog, SESSION, table1),
ImmutableMap.of())
.commitTransaction();

catalog.newCreateTableTransaction(
SESSION,
table2,
new Schema(Types.NestedField.of(1, true, "col1", Types.LongType.get())),
PartitionSpec.unpartitioned(),
SortOrder.unsorted(),
arbitraryTableLocation(catalog, SESSION, table2),
ImmutableMap.of())
.commitTransaction();

// No namespace provided, all tables across all namespaces should be returned
assertThat(catalog.listTables(SESSION, Optional.empty())).containsAll(ImmutableList.of(table1, table2));
// Namespace is provided and exists
assertThat(catalog.listTables(SESSION, Optional.of(ns1))).isEqualTo(ImmutableList.of(table1));
// Namespace is provided and does not exist
assertThat(catalog.listTables(SESSION, Optional.of("non_existing"))).isEmpty();
}

private String arbitraryTableLocation(TrinoCatalog catalog, ConnectorSession session, SchemaTableName schemaTableName)
throws Exception
{
Expand Down

0 comments on commit 2be084c

Please sign in to comment.