GitHub
08/28/2024, 6:15 PM_MAX_RECORDS_LIMIT
Issue created by kgpayne
Singer SDK Version
0.18.0
Python Version
3.10
Bug scope
Taps (catalog, state, stream maps, etc.)
Operating System
MacOS
Description
In testing MeltanoLabs/tap-stackexchange (PR here) with the upcoming test improvements, we noticed that the RESTStream implementation included in the SDK does not adhere to the _MAX_RECORDS_LIMIT internal attribute when returning records from get_records(). Specifically, get_records() returns more than the limit, causing the _check_max_record_limit() to fail and throw a MaxRecordsLimitException.
For connection testing (currently the only use of _MAX_RECORDS_LIMIT) this error is caught and passed. However, in the new testing framework, we need the stream to complete gracefully (emitting state etc.) so that we can test the records returned. Catching the raised error, whilst possible, is still unhelpful when records/messages are required for testing.
# Connection test:
@final
def run_connection_test(self) -> bool:
"""Run connection test.
Returns:
True if the test succeeded.
"""
for stream in self.streams.values():
# Initialize streams' record limits before beginning the sync test.
stream._MAX_RECORDS_LIMIT = 1
for stream in self.streams.values():
if stream.parent_stream_type:
self.logger.debug(
f"Child stream '{type(stream).name}' should be called by "
f"parent stream '{stream.parent_stream_type.name}'. "
"Skipping direct invocation."
)
continue
try:
stream.sync()
except MaxRecordsLimitException:
pass
return True
In the recent SQLStream implementation we added a snippet to "push down" the limit to the remote sql engine, to avoid materialising a complete dataset on the server but exiting early after a small number of records:
# Get records from stream
def get_records(self, context: dict | None) -> Iterable[dict[str, Any]]:
"""Return a generator of record-type dictionary objects.
If the stream has a replication_key value defined, records will be sorted by the
incremental key. If the stream also has an available starting bookmark, the
records will be filtered for values greater than or equal to the bookmark value.
Args:
context: If partition context is provided, will read specifically from this
data slice.
Yields:
One dict per record.
Raises:
NotImplementedError: If partition is passed in context and the stream does
not support partitioning.
"""
if context:
raise NotImplementedError(
f"Stream '{self.name}' does not support partitioning."
)
selected_column_names = self.get_selected_schema()["properties"].keys()
table = self.connector.get_table(
full_table_name=self.fully_qualified_name,
column_names=selected_column_names,
)
query = table.select()
if self.replication_key:
replication_key_col = table.columns[self.replication_key]
query = query.order_by(replication_key_col)
start_val = self.get_starting_replication_key_value(context)
if start_val:
query = query.where(
sqlalchemy.text(":replication_key >= :start_val").bindparams(
replication_key=replication_key_col, start_val=start_val
)
)
# limit records if _MAX_RECORDS_LIMIT is specified
if self._MAX_RECORDS_LIMIT is not None:
query = query.limit(self._MAX_RECORDS_LIMIT)
for record in self.connector.connection.execute(query):
yield dict(record)
By limiting the number of records fetched in get_records() we both limit load on the upstream system and avoid triggering the MaxRecordsLimitException thrown by _check_max_record_limit() if too many records are returned. We could apply the same approach to the other stream classes base implementation. We should then also document this approach for the cases where users override the get_records() implementations.
Code
No response
meltano/sdkGitHub
08/28/2024, 6:15 PM_MAX_RECORDS_LIMIT
Issue closed as completed by stale[bot]
meltano/sdk