fsspeckit.core.maintenance¶
maintenance
¶
Backend-neutral maintenance layer for parquet dataset operations.
This module provides shared functionality for dataset discovery, statistics, and grouping algorithms used by both DuckDB and PyArrow maintenance operations. It serves as the authoritative implementation for maintenance planning, ensuring consistent behavior across different backends.
Key responsibilities: 1. Dataset discovery and file-level statistics 2. Compaction grouping algorithms with streaming execution 3. Optimization planning with z-order validation 4. Canonical statistics structures 5. Partition filtering and edge case handling
Architecture: - Functions accept both dict format (legacy) and FileInfo objects for backward compatibility - All planning functions return structured results with canonical MaintenanceStats - Backend implementations delegate to this core for consistent behavior - Streaming design avoids materializing entire datasets in memory
Core components: - FileInfo: Canonical file information with validation - MaintenanceStats: Canonical statistics structure across backends - CompactionGroup: Logical grouping of files for processing - collect_dataset_stats: Dataset discovery with partition filtering - plan_compaction_groups: Shared compaction planning algorithm - plan_optimize_groups: Shared optimization planning with z-order validation
Usage: Backend functions should delegate to this module rather than implementing their own discovery and planning logic. This ensures that DuckDB and PyArrow produce identical grouping decisions and statistics structures.
Classes¶
fsspeckit.core.maintenance.CompactionGroup
dataclass
¶
CompactionGroup(files: list[FileInfo])
A group of files to be compacted or optimized together.
This dataclass represents a logical grouping of files that will be processed together during maintenance operations. It enables streaming execution by bounding the amount of data processed at once.
Attributes:
| Name | Type | Description |
|---|---|---|
files |
list[FileInfo]
|
List of FileInfo objects in this group. |
total_size_bytes |
int
|
Total size of all files in this group (computed). |
total_rows |
int
|
Total rows across all files in this group (computed). |
Note
Must contain at least one file. The total_size_bytes and total_rows are computed during initialization and used for planning decisions. This structure enables per-group streaming processing without materializing entire datasets.
fsspeckit.core.maintenance.FileInfo
dataclass
¶
Information about a single parquet file with validation.
This canonical data structure represents file metadata across all backends. It enables consistent file information handling and size-based planning.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
File path relative to the dataset root. |
size_bytes |
int
|
File size in bytes; must be >= 0. |
num_rows |
int
|
Number of rows in the file; must be >= 0. |
Note
The size_bytes and num_rows values are validated to be non-negative. This class is used throughout the maintenance planning pipeline for consistent file metadata representation.
fsspeckit.core.maintenance.MaintenanceStats
dataclass
¶
MaintenanceStats(
before_file_count: int,
after_file_count: int,
before_total_bytes: int,
after_total_bytes: int,
compacted_file_count: int,
rewritten_bytes: int,
compression_codec: Union[str, None] = None,
dry_run: bool = False,
zorder_columns: list[str] | None = None,
planned_groups: list[list[str]] | None = None,
)
Canonical statistics structure for maintenance operations.
This dataclass provides the authoritative statistics format for all maintenance operations across DuckDB and PyArrow backends. It ensures consistent reporting and enables unified testing and validation.
Attributes:
| Name | Type | Description |
|---|---|---|
before_file_count |
int
|
Number of files before the operation. |
after_file_count |
int
|
Number of files after the operation. |
before_total_bytes |
int
|
Total bytes before the operation. |
after_total_bytes |
int
|
Total bytes after the operation. |
compacted_file_count |
int
|
Number of files that were compacted/rewritten. |
rewritten_bytes |
int
|
Total bytes rewritten during the operation. |
compression_codec |
Union[str, None]
|
Compression codec used (None if unchanged). |
dry_run |
bool
|
Whether this was a dry run operation. |
zorder_columns |
list[str] | None
|
Z-order columns used (for optimization operations). |
planned_groups |
list[list[str]] | None
|
File groupings planned during dry run. |
Note
All numeric fields are validated to be non-negative. The to_dict() method provides backward compatibility with existing code expecting dictionary format.
Functions¶
fsspeckit.core.maintenance.MaintenanceStats.to_dict
¶
Convert to dictionary format for backward compatibility.
Source code in src/fsspeckit/core/maintenance.py
Functions¶
fsspeckit.core.maintenance.collect_dataset_stats
¶
collect_dataset_stats(
path: str,
filesystem: AbstractFileSystem | None = None,
partition_filter: list[str] | None = None,
) -> dict[str, Any]
Collect file-level statistics for a parquet dataset.
This function walks the given dataset directory on the provided filesystem, discovers parquet files (recursively), and returns basic statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Root directory of the parquet dataset. |
required |
filesystem
|
AbstractFileSystem | None
|
Optional fsspec filesystem. If omitted, a local "file" filesystem is used. |
None
|
partition_filter
|
list[str] | None
|
Optional list of partition prefix filters
(e.g. ["date=2025-11-04"]). Only files whose path relative to
|
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with keys: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the path does not exist or no parquet files match the optional partition filter. |
Source code in src/fsspeckit/core/maintenance.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
fsspeckit.core.maintenance.plan_compaction_groups
¶
plan_compaction_groups(
file_infos: list[dict[str, Any]] | list[FileInfo],
target_mb_per_file: int | None,
target_rows_per_file: int | None,
) -> dict[str, Any]
Plan compaction groups based on size and row thresholds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_infos
|
list[dict[str, Any]] | list[FileInfo]
|
List of file information dictionaries or FileInfo objects. |
required |
target_mb_per_file
|
int | None
|
Target size in megabytes per output file. |
required |
target_rows_per_file
|
int | None
|
Target number of rows per output file. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If both target_mb_per_file and target_rows_per_file are None or <= 0. |
Source code in src/fsspeckit/core/maintenance.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
fsspeckit.core.maintenance.plan_optimize_groups
¶
plan_optimize_groups(
file_infos: list[dict[str, Any]] | list[FileInfo],
zorder_columns: list[str],
target_mb_per_file: int | None = None,
target_rows_per_file: int | None = None,
sample_schema: Any = None,
) -> dict[str, Any]
Plan optimization groups with z-order validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_infos
|
list[dict[str, Any]] | list[FileInfo]
|
List of file information dictionaries or FileInfo objects. |
required |
zorder_columns
|
list[str]
|
List of columns to use for z-order clustering. |
required |
target_mb_per_file
|
int | None
|
Target size in megabytes per output file. |
None
|
target_rows_per_file
|
int | None
|
Target number of rows per output file. |
None
|
sample_schema
|
Any
|
PyArrow schema or object with column_names method for validation. If None, schema validation will be skipped. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If thresholds are invalid or zorder_columns is empty. |
Source code in src/fsspeckit/core/maintenance.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | |