Repository navigation
[Data] Add batch_size to MCAPDatasource for streaming reads - #66190
lorenzozanee wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a batch_size parameter to read_mcap and MCAPDatasource to allow yielding blocks of message data per batch instead of per file. The review feedback highlights two important issues: first, a potential schema compatibility error (e.g., ArrowInvalid) when a batch contains only messages without a schema, which can be resolved by casting null-type metadata columns to their expected types before yielding; second, the batch_size validation should explicitly disallow boolean values since bool is a subclass of int in Python.
| if self._batch_size is not None and builder.num_rows() >= self._batch_size: | ||
| yield builder.build() | ||
| builder = DelegatingBlockBuilder() |
There was a problem hiding this comment.
When streaming reads are batched using batch_size, different batches may contain different sets of messages. If a batch happens to contain only messages without a schema (e.g., when reading from channels that do not define schemas, or if schemas are only present on some channels), the columns schema_name, schema_encoding, and schema_data will be entirely None in that batch.
DelegatingBlockBuilder (which delegates to ArrowBlockBuilder / PyArrow) will infer these columns as null type for that block. However, other blocks that contain messages with schemas will have these columns inferred as string or binary types. When Ray Data attempts to merge or process these blocks downstream, PyArrow will raise a schema compatibility error (e.g., ArrowInvalid: Schema migration not supported) due to the mismatch between null type and string/binary types.
To prevent this, we should ensure schema consistency across all yielded blocks by casting any null-type metadata columns to their expected types (string for schema_name/schema_encoding, and binary for schema_data) before yielding.
Please apply this same casting logic to the final block yielded at the end of _read_stream as well (or refactor it into a helper method).
if self._batch_size is not None and builder.num_rows() >= self._batch_size:
block = builder.build()
import pyarrow as pa
if isinstance(block, pa.Table):
schema = block.schema
casts = {}
if "schema_name" in schema.names and schema.field("schema_name").type == pa.null():
casts["schema_name"] = pa.string()
if "schema_encoding" in schema.names and schema.field("schema_encoding").type == pa.null():
casts["schema_encoding"] = pa.string()
if "schema_data" in schema.names and schema.field("schema_data").type == pa.null():
casts["schema_data"] = pa.binary()
if casts:
new_fields = [pa.field(f.name, casts.get(f.name, f.type)) for f in schema]
block = block.cast(pa.schema(new_fields))
yield block
builder = DelegatingBlockBuilder()| if batch_size is not None: | ||
| if not isinstance(batch_size, int) or batch_size <= 0: | ||
| raise ValueError( | ||
| f"batch_size must be a positive integer, got {batch_size!r}" | ||
| ) |
There was a problem hiding this comment.
In Python, bool is a subclass of int (i.e., isinstance(True, int) is True). Consequently, passing batch_size=True will bypass this validation and be treated as batch_size=1, which can lead to severe performance degradation due to extremely small block sizes. We should explicitly disallow boolean values.
| if batch_size is not None: | |
| if not isinstance(batch_size, int) or batch_size <= 0: | |
| raise ValueError( | |
| f"batch_size must be a positive integer, got {batch_size!r}" | |
| ) | |
| if batch_size is not None: | |
| if isinstance(batch_size, bool) or not isinstance(batch_size, int) or batch_size <= 0: | |
| raise ValueError( | |
| f"batch_size must be a positive integer, got {batch_size!r}" | |
| ) |
|
This pull request has been automatically marked as stale because it has not had You can always ask for help on our discussion forum or Ray's public slack channel. If you'd like to keep this open, just leave any comment, and the stale label will be removed. |
|
Thanks for reviving this; the per-batch yield is the right shape and pairs well with opening the file for random access (the seeking reader can then prune chunks by topic and time, and with
For context, the stale label landed yesterday; a push resets it. |
|
FYI, #66653 lands the pieces this PR's yield pairs with: it opens the file for random access (so the seeking reader prunes chunks by topic and time), cuts blocks by estimated bytes against |
Description
MCAPDatasource._read_stream previously accumulated all messages of a file into a single block and yielded once at the end. For large MCAP recordings this makes peak memory scale with file size times concurrent read tasks and prevents downstream stages from overlapping with the read within a file.
This change adds an optional
batch_size(message count) toMCAPDatasourceandray.data.read_mcap. When set,_read_streamyields one block per batch instead of one block per file.batch_size=None(default) keeps the current behavior exactly; output schema and predicate pushdown are unchanged, only block boundaries change. The implementation follows the existingIterator[Block]protocol already used by CSV/JSON/Parquet datasources and aligns with Daft'sread_mcap(batch_size=1000)precedent.Related issues
Closes #65641
No open PR currently addresses this issue. The change is limited to the MCAP datasource and its public read API.
Additional information
python -m py_compileandgit diff --checkpass.Nonepreserves byte-identical behavior; existingtest_mcap.pyexpectations remain valid. Fulltest_mcap.pysuite requires a complete Ray build and is expected to run in CI.Supercedes #65787(原PR分支已删,按原提交重建)