-
Notifications
You must be signed in to change notification settings - Fork 0
feat: parquet sharding #4
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from rationai.mlkit.data.shard_parquet import shard_parquet | ||
|
|
||
|
|
||
| __all__ = ["shard_parquet"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| import pyarrow.parquet as pq | ||
|
|
||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def shard_parquet( | ||
| input_file: str | Path, | ||
| output_dir: str | Path, | ||
| rows_per_shard: int = 100_000, | ||
| row_group_size: int = 5000, | ||
| ) -> None: | ||
|
JakubPekar marked this conversation as resolved.
|
||
| """Splits a large Parquet file into smaller Parquet files (shards). | ||
|
|
||
| This function reads a single Parquet file in memory-efficient batches and writes | ||
| it out into multiple smaller files. Each output file will contain exactly | ||
| `rows_per_shard` rows, except potentially the final shard. | ||
|
|
||
| Args: | ||
| input_file (str | Path): The path to the source Parquet file. | ||
| output_dir (str | Path): The directory where the output shards will be saved. | ||
| rows_per_shard (int, optional): The target number of rows per shard. | ||
| Defaults to 100,000. | ||
| row_group_size (int, optional): The number of rows to read/write per batch. | ||
| Defaults to 5,000. | ||
|
|
||
| Raises: | ||
| AssertionError: If `rows_per_shard` or `row_group_size` are not strictly positive, | ||
| or if `rows_per_shard` is not perfectly divisible by `row_group_size`. | ||
| """ | ||
| # --- Input Validation --- | ||
| assert rows_per_shard > 0, "rows_per_shard must be greater than 0" | ||
| assert row_group_size > 0, "row_group_size must be greater than 0" | ||
|
|
||
| # Ensure exact chunks can be written without remainder | ||
| assert rows_per_shard % row_group_size == 0, ( | ||
| "rows_per_shard must be divisible by row_group_size" | ||
| ) | ||
|
JakubPekar marked this conversation as resolved.
|
||
|
|
||
| # --- Setup Output Directory --- | ||
| output_dir = Path(output_dir) | ||
| output_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # --- Read and Shard Process --- | ||
| with pq.ParquetFile(input_file) as parquet_file: | ||
| _logger.info(f"Total rows in source: {parquet_file.metadata.num_rows}") | ||
|
|
||
| # Initialize tracking variables | ||
| shard_idx = 0 | ||
| current_shard_rows = 0 | ||
| writer = None | ||
|
|
||
| try: | ||
| # Iterate through the source file in memory-efficient chunks (batches) | ||
| for batch in parquet_file.iter_batches(batch_size=row_group_size): | ||
| if writer is None: | ||
| out_path = output_dir / f"shard_{shard_idx:05d}.parquet" | ||
| writer = pq.ParquetWriter(out_path, batch.schema) | ||
|
|
||
|
JakubPekar marked this conversation as resolved.
|
||
| # Write the current batch | ||
| writer.write_batch(batch) | ||
| current_shard_rows += batch.num_rows | ||
|
|
||
| # Check if the current shard has reached its maximum capacity | ||
| if current_shard_rows >= rows_per_shard: | ||
| writer.close() | ||
| writer = None | ||
|
|
||
| _logger.info(f"Finished writing shard {shard_idx:05d}") | ||
|
|
||
| shard_idx += 1 | ||
| current_shard_rows = 0 | ||
|
|
||
| if writer is not None: | ||
| _logger.info(f"Finished writing final shard {shard_idx:05d}") | ||
|
|
||
| finally: | ||
| # Ensure the active writer is properly closed | ||
| if writer is not None: | ||
| writer.close() | ||
|
|
||
| _logger.info("Sharding complete!") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.