Repository navigation
Description
Problem
Polars summary stats run one pl.Series at a time through StatPipeline, and on any frame with more than 1,000,000 cells (FAST_SUMMARY_WHEN_GREATER, buckaroo/pluggable_analysis_framework/utils.py:9) they run on a 50,000-row sample (df_stats_v2.py:93). Nothing marks the result as sampled. A 52,814-row, 27-column tallyman entry is sampled: its stats report length 50,000. On a 10.8M-row frame, distinct_count for a column of unique IDs reads 50,000.
Bypassing the sample gives exact stats, but they take 12.4 s on that 10.8M-row frame (#997), against 3.4 s for XorqServerDataflow on the same file.
Measurements
The stats that need no group-by (length, null count, distinct count, empty-string count, min/max/mean/std, date min/max) fit in one select over every column. Polars then runs all columns in parallel, and the same select works on a pl.scan_parquet LazyFrame. Measured on an Apple M4 Pro (14 cores), polars 1.35.2:
| frame | variant | time | peak RSS |
|---|---|---|---|
| 10.8M rows × 43 cols, in memory | one select, approx_n_unique, incl. median, 1%/99% tails, numeric histogram |
0.81 s | — |
| same | same, exact n_unique |
2.8 s | — |
78M rows × 43 cols, scan_parquet, streaming |
one select, exact n_unique |
4.4 s | 13.7 GB |
| same | one select, approx_n_unique |
2.6 s | 4.9 GB |
| same | + median, tails, numeric histogram | 34.2 s | 18.3 GB |
| same | + top-7 value_counts on the 21 columns with ≤1,000 distinct values |
+17–20 s | 17.4 GB |
approx_n_unique under the streaming engine was off by −9.4%, −5.6% and +10.7% on three columns (a 28-value column reported 31). Under the in-memory engine the same three were off by −1.3%, −0.9% and +3.6% (29 for 28).
The size check has to be made in Python, before the select is built. pl.when(pl.len() > N).then(lit).otherwise(expensive) evaluates both branches: mode() on all 43 columns of the 10.8M-row frame took 1.13 s unconditionally, 1.07 s behind the when, and 0.11 s when the expression list was chosen in Python.
Suggested design
Mirror the xorq batch phase (xorq_stat_pipeline.py:370-433) on the polars side:
- A
PlColumnmarker instat_func.py, the polars twin ofXorqColumn. A stat taking it returns apl.Expr, annotated with the result type as inxorq_stats_v2.py:156. - A batch phase in the polars pipeline that calls every
PlColumnstat withpl.col(name), runs oneselect, and hands the values toprocess_columnthroughinitial_stats(stat_pipeline.py:251-261). This replaces the 50,000-row sample. @stat(max_rows=...), stored onStatFuncbesidecolumn_filterand applied inbuild_column_dag(typed_dag.py:118-122). The existing cascade removal (typed_dag.py:124-141) then drops dependents of a gated stat, e.g.computed_default_summary_statsandhistogramwhenvalue_countsis gated.- Gated keys are recorded and reported as not computed, rather than silently missing.
pl_base_summary_stats(pl_stats_v2.py:77) is split, solength,null_count,mode,minandmaxcome from batch stats andvalue_countsis the one gated stat.most_freq/modebreak ties:mode().first()picks among tied values in plan-dependent order, so the same entry reported "Kyle Peko" or "Lil'Jordan Humphrey" (37 rows each) depending on the query.mode().sort().first()is deterministic.
A RowCount marker that lets a stat branch on the row count itself was considered. It works for batch stats, but a downstream per-column stat would receive the "not computed" value instead of a real value_counts and fail the type check at stat_pipeline.py:108-118, reporting an error instead of a skip.
Open questions
- How "not computed" reaches the client: a marker value in each stat cell, or one flag in
df_meta. - Exact or approximate distinct count above the threshold.
- Whether median and the numeric histogram stay above the threshold at 34 s on 78M rows.
Context
Found while working out what /load with backend: "polars" needs for tallyman (#992, #993). Builds on #997. Prototype and benchmarks were run on main at 992fdb3 against a 10.8M-row and a 78M-row NYC parking-ticket parquet.
Activity
paddymul commentedon Oct 2, 2026
Comparison against the xorq path and against today's polars stats. Medians of 5, Apple M4 Pro, main at 992fdb3.
I've corrected the 10.8M row in the table above: the 0.81 s figure used
approx_n_unique, and with exactn_uniquethe same select takes 2.8 s.52,814-row × 27-column tallyman entry, stats for each search (ms)
XorqStatPipeline(28 queries)COUNT(*) FILTERper binto_pyarrow()→ one polars selectPlDfStatsV2, 50k-row sample)10.8M rows × 43 columns, no search
COUNT(*) FILTERper bindistinct_countcaps at 50,000)value_countsfor every columnThe "one select" here covers length, null count, exact distinct, empty-string count, min/max/mean/std, median, 1%/99% tails and a numeric histogram, plus top-10
value_countsfor every column in the NFL table. It doesn't produce every key today's pipeline does (unique_count, for example). The polars rows assume the frame is already in memory, as after/load; the xorq rows include reading the parquet. "No search" for polars today is dataflow construction, which includes the stats.On the xorq side, folding every histogram into one query helps on small frames (2.2× at best, with two queries) and not at all at 10.8M rows. A single query is no better than two, because it has to build both numeric bins and top-10 counts for integer columns before
distinct_countis known. The 0-row "zzqx" search, a single query, still costs 157–160 ms, 110 ms of it in DataFusion execution. None of the xorq rewrites has been checked for output parity withXorqStatPipelineyet.An option for xorq sessions whose filtered result is small: run only the filter in xorq, pull the rows to Arrow, and compute the stats with the polars batch select. That costs 102–115 ms on this entry, almost all of it the pull.
to_pyarrow()took about 105 ms even when 0 rows matched, whilecount()over the same filtered expression took 15 ms. Where the other 90 ms goes isn't known yet.Related: #911 asks the same row-count question for the xorq batch phase, and
max_rowshere could be the shared mechanism. #814 covers reruns for a repeated search term.perf(stats): polars batch stat phase, no 50k-row sample, value_counts…
perf(stats): break value_counts ties by run-length encoding sorted va…
perf(stats): compute polars scalar stats in one select, gate value_co…
test(stats): failing test for pl.Object distinct stats under the pre-…
fix(stats): make pl_distinct_stats safe for pl.Object columns (#999)
test(stats): failing tests for regex-like column names and per-expres…
fix(stats): address batch columns by position, retry failing expressi…