Skip to content

Commit

Permalink
Update Python client to use listAllTables REST API (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuansunxt committed Sep 29, 2021
1 parent 363f832 commit 6dcc0ab
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
13 changes: 11 additions & 2 deletions python/delta_sharing/delta_sharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ def __init__(self, profile: Union[str, BinaryIO, TextIO, Path, DeltaSharingProfi
self._profile = profile
self._rest_client = DataSharingRestClient(profile)

def __list_all_tables_in_share(self, share: Share) -> Sequence[Table]:
tables: List[Table] = []
page_token: Optional[str] = None
while True:
response = self._rest_client.list_all_tables(share=share, page_token=page_token)
tables.extend(response.tables)
page_token = response.next_page_token
if page_token is None:
return tables

def list_shares(self) -> Sequence[Share]:
"""
List shares that can be accessed by you in a Delta Sharing Server.
Expand Down Expand Up @@ -149,5 +159,4 @@ def list_all_tables(self) -> Sequence[Table]:
:return: all tables that can be accessed.
"""
shares = self.list_shares()
schemas = chain(*(self.list_schemas(share) for share in shares))
return list(chain(*(self.list_tables(schema) for schema in schemas)))
return list(chain(*(self.__list_all_tables_in_share(share) for share in shares)))
23 changes: 23 additions & 0 deletions python/delta_sharing/rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class ListTablesResponse:
next_page_token: Optional[str]


@dataclass(frozen=True)
class ListAllTablesResponse:
tables: Sequence[Table]
next_page_token: Optional[str]


@dataclass(frozen=True)
class QueryTableMetadataResponse:
protocol: Protocol
Expand Down Expand Up @@ -152,6 +158,23 @@ def list_tables(
next_page_token=tables_json.get("nextPageToken", None),
)

@retry_with_exponential_backoff
def list_all_tables(
self, share: Share, *, max_results: Optional[int] = None, page_token: Optional[str] = None
) -> ListAllTablesResponse:
data: Dict = {}
if max_results is not None:
data["maxResults"] = max_results
if page_token is not None:
data["pageToken"] = page_token

with self._get_internal(f"/shares/{share.name}/all-tables", data) as lines:
tables_json = json.loads(next(lines))
return ListAllTablesResponse(
tables=[Table.from_json(table_json) for table_json in tables_json.get("items", [])],
next_page_token=tables_json.get("nextPageToken", None),
)

@retry_with_exponential_backoff
def query_table_metadata(self, table: Table) -> QueryTableMetadataResponse:
with self._get_internal(
Expand Down
3 changes: 3 additions & 0 deletions python/delta_sharing/tests/test_delta_sharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_list_shares(sharing_client: SharingClient):
Share(name="share4"),
Share(name="share5"),
Share(name="share6"),
Share(name="share7"),
Share(name="share_azure"),
]

Expand Down Expand Up @@ -70,6 +71,8 @@ def test_list_all_tables(sharing_client: SharingClient):
Table(name="table4", share="share3", schema="default"),
Table(name="table5", share="share3", schema="default"),
Table(name="test_gzip", share="share4", schema="default"),
Table(name="table8", share="share7", schema="schema1"),
Table(name="table9", share="share7", schema="schema2"),
Table(name="table_wasb", share="share_azure", schema="default"),
Table(name="table_abfs", share="share_azure", schema="default"),
]
Expand Down
1 change: 1 addition & 0 deletions python/delta_sharing/tests/test_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def test_list_shares(rest_client: DataSharingRestClient):
Share(name="share4"),
Share(name="share5"),
Share(name="share6"),
Share(name="share7"),
Share(name="share_azure"),
]

Expand Down

0 comments on commit 6dcc0ab

Please sign in to comment.