Skip to content

Harden, speed up, and clean up measurement-result Parquet conversion - #191

Draft
rok wants to merge 1 commit into
benchx-dev:mainfrom
rok:parquet-conversion-cleanup
Draft

rok wants to merge 1 commit into
benchx-dev:mainfrom
rok:parquet-conversion-cleanup

Conversation

@rok

@rok rok commented Dec 3, 2026

Copy link
Copy Markdown
Member

Summary

Follow-up cleanup and hardening from reviewing how measurement-result JSON documents are converted into Parquet:

Schema mapping & conversion hardening (src/benchx/parquet.py)

  • Reject unsupported schema constructs loudly instead of silently dropping data or falling back to JSON:
    • Validate $ref targets (chained #/$defs/ refs, cycle detection, reject external/missing refs).
    • Validate allOf[1:] only contains assertion/annotation keywords rather than silently ignoring extra properties/$refs; reject empty allOf.
    • Require items on array nodes; reject bare {}/anyOf/null instead of falling through to JSON.
    • Ensure oneOf with bare numbers only allows "value" as a required field on the structured object branch.
  • Gate bare-number struct wrapping on numeric (non-bool) scalars when "value" is a struct field; raise TypeError otherwise.
  • Use zip(..., strict=True) in results_table.

Performance & write-safety improvements

  • Compile the row-shaping converter once from the derived Arrow schema (_compile_row, shared with to_row) instead of re-walking the schema, padding missing optional keys with None, and allocating pa.json_() per scalar leaf.
  • Add catalog.schema() (src/benchx/core/catalog.py) so benchx.parquet reads schema.json without constructing a full Draft202012Validator, Registry, and FormatChecker at import time.
  • Cache core.canonical(order).decode() per work_order_ref in ingest_documents (src/benchx/results.py).
  • Project dotted Parquet leaf columns in Store._index, Store._documents, and Store.head (src/benchx/store.py) so heavy nested columns (observations, summaries, protocol, system, etc.) are skipped when not needed.
  • Use unique tempfile.NamedTemporaryFile siblings in Store._atomic_write and tools/json_to_parquet.py with fsync, atomic os.replace, and temp-file cleanup on failure.
  • Update store._fields to walk list<struct> elements ([]), and log warnings on schema_uri mismatch or removed fields when appending in Store._write_parquet.

Deslopping

  • Consolidate duplicated PyArrow availability/import guards (is_pyarrow_missing, has_pyarrow, require_pyarrow) into src/benchx/_pyarrow.py (with zero top-level pyarrow import) and reuse JSON_SUFFIXES in views/inputs.py.
  • Replace arrow_type's json_type threading and double derivation with a single _FINAL derivation + _storage_schema().
  • Use core.hashing.canonical consistently in parquet.py.
  • Update pip install -e . -> pip install -e '.[parquet]' in tools/json_to_parquet.py and docs/design/measurement-result-parquet.md.

Tests & verification

  • Expanded tests/test_parquet.py with negative schema-node tests, chained/cyclic $ref tests, empty/mismatched message_table/results_table tests, sub-microsecond timestamp truncation, optional schema branches (constraint, derivation, source_bounds, standard_uncertainty, expanded_uncertainty, quality.validation, full structuredObservation), _pyarrow edge cases, _fields list-of-struct traversal, and atomic write cleanup/warning tests.
  • Updated tests/test_logging_silent.py for the projected provenance.run_key column name in debug logs.
  • Verified:
    • uv run --with ruff==0.17.6 ruff check src tests tools & ruff format --check src tests tools
    • uv run --extra parquet --extra test --with coverage python -m coverage run -m pytest tests -ra (1171 passed, 18 skipped; 93% total coverage, parquet.py 99%, _pyarrow.py 100%)
    • uv run --isolated --extra test python -m pytest tests/test_minimal_deps.py tests/test_parquet.py -ra (34 passed, 4 skipped without pyarrow)

Note: A separate CI compaction performance improvement (benchx-ci/scripts/compact.py running bx ingest once per suite unit instead of batching) lives in the separate benchx-ci repository and is out of scope for this PR.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant