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

Iceberg Rest Catalog: Fix bug where listing namespaces was performed but non-existing namespace was provided #19980

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
amogh-jahagirdar marked this conversation as resolved.
Show resolved Hide resolved

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);
amogh-jahagirdar marked this conversation as resolved.
Show resolved Hide resolved

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
Loading