Rerun Catalog Queries rerun-catalog-queries
Performance patterns and gotchas for querying a Rerun catalog from Python. Reach for this when exploring an unfamiliar dataset, when a CatalogClient/dataset query is unexpectedly slow, or when shaping a per-segment / per-episode pipeline that hits the catalog from many places.
- Our test
- —
- not tested yet
- GitHub stars
- 12k
- rerun-io/rerun
- Added by
- 0
- members
- Updated
- Dec 6
- commit 6c0d8ef
- Content
- 1 file
- no scripts · 15 KB
- License
- Apache 2.0
- free to use
What our test found
Not tested yet. Our model tries every skill on a realistic request and scores the result, the most popular skills first. Until then, the safety checks on the right still apply.
name: rerun-catalog-queries description: Performance patterns and gotchas for querying a Rerun catalog from Python. Reach for this when exploring an unfamiliar dataset, when a CatalogClient/dataset query is unexpectedly slow, or when shaping a per-segment / per-episode pipeline that hits the catalog from many places. user_invocable: true allowed-tools: Read, Grep, Bash
Rerun catalog queries
Practical performance patterns for querying a Rerun catalog from Python (rerun.catalog.CatalogClient → dataset.reader(...) → DataFusion DataFrame). The DataFusion side of the stack is covered by the `datafusion-python` skill — load that for DataFrame / SessionContext / expression-API references. This skill focuses on catalog-specific behaviors and the round-trip costs that catch teams off guard.
Install and version
Always install with the catalog extra. Plain rerun-sdk fails at CatalogClient(…) with a missing datafusion dependency error.
uv run --with 'rerun-sdk[catalog]' python script.pyMatch the SDK to the viewer or server. Pin rerun-sdk[catalog]~=0.X.0, using the minor version of the viewer's viewer_version. An inherited VIRTUAL_ENV or a cached resolve can pick an old release, such as 0.32, so print rerun.__version__ first.
Only if the user asks for the latest unreleased SDK, install it from the main build index:
uv pip install --index https://build.rerun.io/version/main/simple --reinstall-package rerun-sdk 'rerun-sdk[catalog]'Connect with a rerun:// URL
from rerun.catalog import CatalogClient
client = CatalogClient("rerun+https://api.example.cloud.rerun.io:443")
dataset = client.get_dataset(name=dataset_name) # or: client.get_dataset(id=dataset_id)CatalogClient takes a rerun:// or rerun+https:// URL, or rerun+http:// for a local server. Do not pass a plain https:// URL. A segment URL such as rerun://host:443/dataset/<dataset-id>?segment_id=<segment-id> gives the server, the dataset id and the segment id.
The client reads the token from RERUN_DB_TOKEN, or from the stored credentials of rerun auth login, so token= is usually not needed.
The query cost model in one sentence
Every materialization of a dataset.reader(...) DataFrame is one cloud round-trip, with most of the cost being the network/decode pair, not the compute. Plan for round-trip count and payload bytes, in that order.
A typical catalog round-trip is a few seconds even for a tiny result. So:
- 30 segments × 1 query each ≈ 90s. (Naive per-segment loop.)
- 30 segments × 4 queries each ≈ 6 minutes. (A splitter that runs
count+collect_columnfor both starts and stops.) - 1 query covering all 30 segments ≈ 3s.
The same fan-out happens along the entity axis: 10 entities × 1 filter_contents([one_entity]).reader() each ≈ 30s, vs one filter_contents([all_entities]).reader() ≈ 3s. Push as much as you can into one round-trip — across segments and across entities.
Always apply filter_contents and time-window filters before .reader(...)
The single biggest lever:
filter_contents([entity_globs])restricts which entity-path columns the reader produces. Without it, every entity in the dataset is read.- Time-window filters push down to storage and dramatically reduce bytes scanned when they compare the bare index column with a literal:
df.filter(col(index) >= lit(start)).filter(col(index) <= lit(end)). Integer, timestamp and duration literals of any unit work. Acastor arithmetic on the index column is not pushed down.
Combined: dataset.filter_segments(seg).filter_contents(entities).reader(...) .filter(in_window).select(...).
df.cache() is your friend for repeated probes
When the same materialization gets used by multiple downstream filter/count/collect calls, materialize once with DataFrame.cache() and operate on the cached frame:
cached = (
dataset
.filter_segments(seg)
.filter_contents([entity])
.reader(index=index_col)
.select(col(index_col).cast(pa.int64()).alias(index_col), value.alias("v"))
.cache() # one network round-trip, materializes into in-memory batches
)
starts = cached.filter(col("v") == start_val).collect_column(index_col)
stops = cached.filter(stop_pred(col("v"))).collect_column(index_col)Without cache(), each count() / collect_column() re-executes the whole reader chain.
When NOT to cache. cache() forces materialization into Arrow batches, breaking laziness. If downstream code keeps composing more DataFusion ops on top (joins, windows, further filters) and only materializes once at the end, caching mid-pipeline turns one execution into two and pre-empts whatever physical-plan optimizations the engine could have done across the boundary. Reach for cache() when the consumers are terminal (count(), collect_column(), to_arrow_table()), not when they're another lazy DataFrame.
Cross-segment batching: drop filter_segments, group by rerun_segment_id
For pipelines that need the same query on many segments, omit filter_segments(...) entirely and pull a single cross-segment table. Every reader row carries a rerun_segment_id column — group locally:
df = dataset.filter_contents(entities).reader(index=index_col)
cached = df.select(
"rerun_segment_id",
col(index_col).cast(pa.int64()).alias(index_col),
value.alias("v"),
).cache()
# Now N filter/aggregate calls are local, not network.
starts = cached.filter(col("v") == start_val).select("rerun_segment_id", index_col).to_arrow_table()Trigger / event columns are tiny enough that pulling all segments at once dominates per-segment looping by an order of magnitude.
Per-entity fan-out within a segment
Symmetric to cross-segment batching, along the entity axis. If you need data from N entities of a single segment, don't loop:
# Anti-pattern: N reader setups, N round-trips.
for entity in entities:
df = dataset.filter_segments(seg).filter_contents([entity]).reader(index=ix)
...Instead pull all N at once and project per entity locally. Per "Reader row layout" below, every row carries data for one entity and NULLs for the others, so col("<entity>:<archetype>:<component>").is_not_null() is the per-entity filter:
shared = (
dataset
.filter_segments(seg)
.filter_contents(sorted(set(entities)))
.reader(index=ix)
.filter(col(ix).cast(pa.int64()).between(start_ns, end_ns))
)
# Each downstream consumer narrows to its entity's rows lazily.
src_a = shared.filter(col(f"{ent_a}:{comp_a}").is_not_null()).select(ix, f"{ent_a}:{comp_a}")
src_b = shared.filter(col(f"{ent_b}:{comp_b}").is_not_null()).select(ix, f"{ent_b}:{comp_b}")DataFusion can share the underlying scan across the per-entity projections when it builds the physical plan, so this stays a single catalog round-trip even though there are N logical consumers. Works inside generators that build per-source DataFusion plans (resampling, bracket lookup, nearest-in-time joins) — collapse the network fan-out without changing the per-source logic.
A trap when refactoring: if a downstream query uses a reader column's fully-qualified name (col(f"{entity}:{archetype}:{component}")), you don't need to alias the column in shared. The shared reader's output schema preserves native column names, so existing per-entity projection helpers keep working unchanged.
count() is not free
Counter-intuitive: df.count() and df.aggregate([], [F.count(col)]) do not always push down. Aggregate plans can force the engine to materialize the underlying column data server-side, then count on the client. F.count(col) over wide entity-columns can ship full struct or blob payloads to count nullity.
Alternatives, in order of preference for "is anything here":
| Need | Use | |---|---| | "any row in this filter?" | df.select(col(index)).limit(1).to_arrow_table().num_rows > 0 — server short-circuits on first match | | "count rows in a tiny window" | df.filter(window).select(col(index)).count() after the time filter | | "count each entity in a wide query" | per-entity limit(1) probes, threaded — not one big count(col) aggregate |
A trap that fooled us: assuming bool_or(col.is_not_null()) would only need nullity buffers. It does not — the operator still touches payload data on most plans.
using_index_values + fill_latest_at is great for resampling, not for presence
.reader(index=index_col, using_index_values=targets, fill_latest_at=True)- Returns one row per target timestamp, each entity column carrying its latest non-null value at-or-before the target.
- Excellent for nearest-prior resampling (no DataFusion required).
- Don't use it as a presence check. Two reasons:
- Semantics are "ever emitted before T", not "emitted in [start, T]".
- The server still ships the full struct/blob payload for every entity to compute the latest-known value; a downstream
is_not_null()projection runs post-transfer and doesn't reduce wire bytes.
For a strict in-window presence check, prefer per-entity limit(1) over the time-filtered reader, run concurrently.
Explore an unfamiliar dataset with dataset.info()
info = dataset.info()
print(info) # compact text overviewIt reports the segment count and size, each layer with how many segments have it, each timeline with its kind, value range and segment coverage, and the property column names. It reads only the segment table and the schema, no recording data, so it stays cheap on large datasets.
Use it to choose reader(index=…) and to bound time-window filters before the first real query. TimelineInfo.min / .max are numpy values that work directly as literals:
ts = next(t for t in info.timelines if t.name == "capture_time")
df = dataset.reader(index=ts.name).filter(col(ts.name) >= lit(ts.min))Properties found here can be filtered via dataset.segment_table(), and layer names tell you what register(…, layer_name=…) already attached.
Schema introspection is cheap; use it before probing
schema = dataset.filter_segments(seg).schema()
available = {(c.entity_path, c.component) for c in schema.component_columns()}This is a single round-trip and tells you which (entity, component) pairs the segment registered. If a column isn't in the schema, you can drop it from your manifest without any further cloud queries. This is often a complete substitute for "does this entity have events" probing.
Caveat: schema presence ≠ events. The MCAP records the topic schema even for unused topics. If your pipeline cares about distinguishing "registered but never emitted" vs "registered with events", you have to probe — see "is anything here" patterns above.
Reader row layout: entities are columns, not rows
Every row from dataset.<filters>.reader(...) corresponds to a single event on a single entity. Other entities' columns are null on that row. Implications:
select("rerun_segment_id", "<entity>:<archetype>:<component>")works — quote the entity column when using SQL.- There is no
rerun_entity_pathrow attribute. To attribute rows to entities you either filter to one entity at a time, or pick a per-entity column (e.g.:McapChannel:id) whose non-null pattern identifies the source. df.count()returns total events across all entities, not per-entity counts.
Field access on a null struct returns 0.0, not null
A DataFusion gotcha that bites pipelines reading struct messages:
col("/some/entity:msg.MyType:message")[0]["sub"]["x"]
# When the parent struct is null on a row, this evaluates to 0.0
# (and "" for strings), not null.Wrap struct-walk projections with a null guard:
parent = col("/some/entity:msg.MyType:message")
leaf = parent[0]["sub"]["x"]
guard = parent.is_null() | parent[0].is_null() | parent[0]["sub"].is_null()
expr = F.when(guard, lit(None)).otherwise(leaf)Only apply this to struct sources. For scalar / blob columns (Scalars:scalars, EncodedImage:blob, etc.) the wrap is a no-op at best and at worst rewrites the plan in ways that change downstream join behavior. Gate the guard on whether the source actually walks through a struct boundary.
Common debug recipe
When a query stage is slower than expected:
- Count round-trips. Wrap each
to_arrow_table()/collect_column()/count()withtime.perf_counter(). Each is a round-trip. If you see N segment queries, that's N × few-seconds minimum. - Split build vs materialize timing. Time the lazy DataFrame construction separately from the terminal
to_arrow_table()call. If "build" takes seconds, something inside is materializing eagerly (an Arrow round-trip in a join helper, acache()in a generator, a.collect()hidden in a chained-join utility). A correctly lazy plan should build in ~milliseconds regardless of result size. - Measure bytes.
tbl.nbytesafterto_arrow_table()reveals when "I projectedis_not_null()" actually shipped megabytes. If bytes are large despite a small projection, the operator didn't push down. - Cross-segment first, then cross-entity. If the per-segment query is fundamentally the same (just scoped by id), drop
filter_segmentsand group byrerun_segment_idlocally. If you also have a per-entity loop within a segment, collapse it the same way (onefilter_contents([all])reader, per-entityis_not_null()filters downstream). - Cache before terminal re-use. If two
count()/collect_column()calls share the same reader,df.cache()between them. Don't cache if the consumers are themselves lazy DataFrames being composed further — caching breaks plan-wide optimization. - Window first. Always push the time filter before any projection or aggregate that touches payload columns.
Refresh the server in the viewer after catalog writes
<!-- TODO(RR-4859, RR-4853): remove this section once the Rerun DB server sends WatchEvents for every change, so the viewer refreshes by itself. -->
The viewer caches the contents of each server. After any change to the catalog (rename, create, register, upload, delete), the viewer's sidebar and open dataset or table pages stay stale until refreshed.
Run viewer_run_command with server.refresh after each write. Use table.refresh to refresh only the dataset or table on screen.
See also
datafusion-pythonskill — DataFrame API, SQL parity, expression building, common pitfalls (boolean operators, immutability, etc.). Not installed? Ask the user to install it globally:npx skills add apache/datafusion-python.