← Back to Pull requests

Add nodes to convert between csp structs and arrow record batches #680

Closed
unknown closed this pull request without merging on Jul 21, 2026 15:08 UTC
View on GitHub
unknown unknown commented on Feb 19, 2026 19:31 UTC
Author

Add record_batches_to_struct and struct_to_record_batches nodes

Two new C++-backed nodes for bidirectional conversion between csp.Struct and Arrow RecordBatch:

- struct_to_record_batches — ts[List[T]] → ts[List[pa.RecordBatch]]
- record_batches_to_struct — ts[List[pa.RecordBatch]] → ts[List[T]]

Both use the Arrow C Data Interface (PyCapsule protocol) to cross the Python/C++ boundary without serialization overhead. Scalar and temporal fields are read/written entirely in C++. Numpy array fields use bulk memory transfers rather than per-element iteration.

### Supported types

bool, int8/16/32/64, uint8/16/32/64, double, str, bytes, datetime, timedelta, date, time, csp.Enum, nested csp.Struct, Numpy1DArray[T], and NumpyNDArray[T].

### Performance

Read and write paths are columnar: each column is processed in a single readAll() / writeAll() call per batch rather than row-by-row virtual dispatch, reducing virtual call count from N×M (rows × columns) to M. The readAll() path uses a null_count==0 fast path that skips per-row validity checks entirely. Fixed-size writers use Unsa
feAppend
after a single Reserve() to skip per-row capacity and status checks. Nested struct readers pre-allocate all child structs before delegating to child readAll() calls column by column.

Throughput on 100k numeric rows (int64 + float64): ~6.4M rows/sec read, ~800k rows/sec write (write dominated by CSP graph overhead).

Stress tests comparing reading compressed parquet files through both the Parquet adapter (ParquetReader.subscribe_all) and the Arrow path (RecordBatchPullInputAdapter → record_batches_to_struct):

| Scenario | Arrow | Parquet | Speedup |
|---|---|---|---|
| 500k rows, 10 timestamps (50k rows/ts) | 4.0M rows/s | 1.8M rows/s | 2.3x |
| 100k rows, 1k timestamps (100 rows/ts) | 2.6M rows/s | 2.0M rows/s | 1.3x |
| 1M rows, 10k timestamps (100 rows/ts) | 2.4M rows/s | 2.1M rows/s | 1.15x |
| 100k rows, 100k timestamps (1 row/ts) | 76k rows/s | 2.0M rows/s | 26x slower |

Arrow's advantage grows with larger batches per timestamp. The 1-row-per-timestamp case exposes per-tick overhead from RecordBatchPullInputAdapter creating and converting individual single-row batches — the Parquet adapter handles this natively since its C++ reader emits one struct per row without intermediate Arrow batches.

### Example

  import csp
  import pyarrow as pa
  from csp.adapters.arrow import record_batches_to_struct, struct_to_record_batches


  class SensorReading(csp.Struct):
      temperature: float
      humidity: float
      station: str


  # field_map is always {struct_field_name: arrow_column_name}
  field_map = {
      "temperature": "temp_c",
      "humidity": "rel_humidity",
      "station": "station_id",
  }

  schema = pa.schema([
      ("temp_c", pa.float64()),
      ("rel_humidity", pa.float64()),
      ("station_id", pa.utf8()),
  ])


  @csp.graph
  def roundtrip(readings: csp.ts[csp.typing.List[SensorReading]]):
      batches = struct_to_record_batches(readings, SensorReading, field_map)
      restored = record_batches_to_struct(batches, SensorReading, field_map, schema)

 # Numpy fields are auto-detected from struct metadata. 1D arrays are stored as list<T>, NDArrays as list<T> plus a list<int64> shape column (default name: <column>_csp_dimensions).
  

Diff: 447 lines (+335 / -112) across 5 files Reviewer: AdamGlustein Review duration: 218616m
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 447 lines diff across 5 files
0.45
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.365
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #680"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 45.4% (MEDIUM) · Higher = larger blast radius
45.4%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 17.9% · Safe to merge
17.9%
Repository