Skip to content

Commit b12f81f

Browse files
tsreaperqwencoder
andcommitted
[core] Add listTableDetails method to Catalog interface
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
1 parent 9573fcd commit b12f81f

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed

paimon-api/src/main/java/org/apache/paimon/rest/RESTApi.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,28 @@ public PagedList<GetTableResponse> listTableDetailsPaged(
420420
return new PagedList<>(tables, response.getNextPageToken());
421421
}
422422

423+
/**
424+
* List table details for a database.
425+
*
426+
* <p>Gets an array of table details for a database. There is no guarantee of a specific
427+
* ordering of the elements in the array.
428+
*
429+
* @param databaseName name of database.
430+
* @return a list of table details.
431+
* @throws NoSuchResourceException Exception thrown on HTTP 404 means the database not exists
432+
* @throws ForbiddenException Exception thrown on HTTP 403 means don't have the permission for
433+
* this database
434+
*/
435+
public List<GetTableResponse> listTableDetails(String databaseName) {
436+
return listDataFromPageApi(
437+
queryParams ->
438+
client.get(
439+
resourcePaths.tableDetails(databaseName),
440+
queryParams,
441+
ListTableDetailsResponse.class,
442+
restAuthFunction));
443+
}
444+
423445
/**
424446
* List table for a catalog.
425447
*

paimon-core/src/main/java/org/apache/paimon/catalog/AbstractCatalog.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,19 @@ protected PagedList<Table> listTableDetailsPagedImpl(
330330
pagedTableNames.getNextPageToken());
331331
}
332332

333+
@Override
334+
public List<Table> listTableDetails(String databaseName) throws DatabaseNotExistException {
335+
List<Table> result = new ArrayList<>();
336+
for (String tableName : listTables(databaseName)) {
337+
try {
338+
result.add(getTable(Identifier.create(databaseName, tableName)));
339+
} catch (TableNotExistException e) {
340+
// ignore
341+
}
342+
}
343+
return result;
344+
}
345+
333346
@Override
334347
public void dropTable(Identifier identifier, boolean ignoreIfNotExists)
335348
throws TableNotExistException {

paimon-core/src/main/java/org/apache/paimon/catalog/Catalog.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,17 @@ PagedList<Table> listTableDetailsPaged(
232232
@Nullable String tableType)
233233
throws DatabaseNotExistException;
234234

235+
/**
236+
* Get list of table details under this database. An empty list is returned if none exists.
237+
*
238+
* <p>NOTE: System tables will not be listed.
239+
*
240+
* @param databaseName Name of the database to list table details.
241+
* @return a list of the details of all tables in this database.
242+
* @throws DatabaseNotExistException if the database does not exist
243+
*/
244+
List<Table> listTableDetails(String databaseName) throws DatabaseNotExistException;
245+
235246
/**
236247
* Gets an array of tables for a catalog.
237248
*

paimon-core/src/main/java/org/apache/paimon/catalog/DelegateCatalog.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public PagedList<Table> listTableDetailsPaged(
126126
databaseName, maxResults, pageToken, tableNamePattern, tableType);
127127
}
128128

129+
@Override
130+
public List<Table> listTableDetails(String databaseName) throws DatabaseNotExistException {
131+
return wrapped.listTableDetails(databaseName);
132+
}
133+
129134
@Override
130135
public PagedList<Identifier> listTablesPagedGlobally(
131136
String databaseNamePattern,

paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalog.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,16 @@ public PagedList<Table> listTableDetailsPaged(
266266
}
267267
}
268268

269+
@Override
270+
public List<Table> listTableDetails(String databaseName) throws DatabaseNotExistException {
271+
try {
272+
List<GetTableResponse> tables = api.listTableDetails(databaseName);
273+
return tables.stream().map(t -> toTable(databaseName, t)).collect(Collectors.toList());
274+
} catch (NoSuchResourceException e) {
275+
throw new DatabaseNotExistException(databaseName);
276+
}
277+
}
278+
269279
@Override
270280
public PagedList<Identifier> listTablesPagedGlobally(
271281
@Nullable String databaseNamePattern,

paimon-core/src/test/java/org/apache/paimon/rest/RESTCatalogTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,32 @@ public void testListTableDetailsPagedWithTableType() throws Exception {
736736
assertThat(nonExistentTypeWithMaxResults.getNextPageToken()).isNull();
737737
}
738738

739+
@Test
740+
public void testListTableDetails() throws Exception {
741+
// List table details returns an empty list when there are no tables in the database
742+
String databaseName = "table_details_db";
743+
catalog.createDatabase(databaseName, false);
744+
List<Table> tableDetails = catalog.listTableDetails(databaseName);
745+
assertThat(tableDetails).isEmpty();
746+
747+
String[] tableNames = {"table1", "table2", "table3", "abd", "def", "opr", "table_name"};
748+
String[] expectedTableNames = Arrays.stream(tableNames).sorted().toArray(String[]::new);
749+
for (String tableName : tableNames) {
750+
catalog.createTable(
751+
Identifier.create(databaseName, tableName), DEFAULT_TABLE_SCHEMA, false);
752+
}
753+
754+
tableDetails = catalog.listTableDetails(databaseName);
755+
assertThat(tableDetails).hasSize(tableNames.length);
756+
List<String> actualTableNames =
757+
tableDetails.stream().map(Table::name).sorted().collect(Collectors.toList());
758+
assertThat(actualTableNames).containsExactly(expectedTableNames);
759+
760+
// List table details throws DatabaseNotExistException when the database does not exist
761+
assertThatExceptionOfType(Catalog.DatabaseNotExistException.class)
762+
.isThrownBy(() -> catalog.listTableDetails("non_existing_db"));
763+
}
764+
739765
@Test
740766
public void testListTablesPagedWithTableType() throws Exception {
741767
String databaseName = "tables_paged_table_type_db";

0 commit comments

Comments
 (0)