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

feat: Generator for paged-queries #75

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions examples/dataframe/query_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from nisystemlink.clients.dataframe import DataFrameClient
from nisystemlink.clients.dataframe.models import QueryTablesRequest

client = DataFrameClient()

# List all tables with more than 100k rows
query = QueryTablesRequest(
filter="rowCount > 100000", order_by="CREATED_AT", order_by_descending=True
)

# Print the query results
for table in client.query_tables_generator(query):
print(f"{table.created_at} \t {table.name} \t Rows={table.row_count}")
28 changes: 27 additions & 1 deletion nisystemlink/clients/dataframe/_data_frame_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Implementation of DataFrameClient."""

from typing import List, Optional
from typing import Generator, List, Optional

from nisystemlink.clients import core
from nisystemlink.clients.core._uplink._base_client import BaseClient
Expand All @@ -15,6 +15,7 @@
from requests.models import Response
from uplink import Body, Field, Path, Query


from . import models


Expand Down Expand Up @@ -119,6 +120,31 @@ def query_tables(self, query: models.QueryTablesRequest) -> models.PagedTables:
"""
...

def query_tables_generator(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's weird to me that we're using the term _generator in the function name here. Shouldn't this just be query_tables and return Generator?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is not the final name, this PR was raised to discuss the aligned way to proceed

self, query: models.QueryTablesRequest
) -> Generator[models.TableMetadata, None, None]:
"""Queries available tables on the SystemLink DataFrame service and returns their metadata.

Args:
query: The request to query tables. `continuation_token` is ignored.

Yields:
`models.TableMetadata` Table Metadata
"""
_query = query.copy()
_query.continuation_token = None
while True:
paged_tables = self.query_tables(query=_query)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we include exponential backoff on this so that we protect users from running into rate limiting when they use this style for large queries?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementing the rate limiter at the client class level will automatically cover all methods; hence, additional handling is not required within the generator.

cont_token = paged_tables.continuation_token

for table in paged_tables.tables:
yield table

if cont_token is None:
break
else:
_query.continuation_token = cont_token

@get("tables/{id}")
def get_table_metadata(self, id: str) -> models.TableMetadata:
"""Retrieves the metadata and column information for a single table identified by its ID.
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/dataframe/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,25 @@ def test__query_tables__returns(
assert len(second_page.tables) == 1
assert second_page.continuation_token is None

def test__query_tables_generator__returns(
self, client: DataFrameClient, test_tables: List[str]
):
query = QueryTablesRequest(
filter="""(id == @0 or id == @1 or id == @2)
and createdWithin <= RelativeTime.CurrentWeek
and supportsAppend == @3 and rowCount < @4""",
substitutions=[test_tables[0], test_tables[1], test_tables[2], True, 1],
reference_time=datetime.now(tz=timezone.utc),
take=2,
order_by="NAME",
order_by_descending=True,
)
# Make the generator yield all values
tables = list(client.query_tables_generator(query))

assert len(tables) == 3
assert tables[0].id == test_tables[-1] # Asserts descending order

def test__modify_table__returns(self, client: DataFrameClient, create_table):
id = create_table(basic_table_model)

Expand Down