Skip to content

polars stats: compute expression stats in one select, and gate expensive stats on row count #999

Open

Description

@paddymul

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 PlColumn marker in stat_func.py, the polars twin of XorqColumn. A stat taking it returns a pl.Expr, annotated with the result type as in xorq_stats_v2.py:156.
  • A batch phase in the polars pipeline that calls every PlColumn stat with pl.col(name), runs one select, and hands the values to process_column through initial_stats (stat_pipeline.py:251-261). This replaces the 50,000-row sample.
  • @stat(max_rows=...), stored on StatFunc beside column_filter and applied in build_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_stats and histogram when value_counts is 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, so length, null_count, mode, min and max come from batch stats and value_counts is the one gated stat.
  • most_freq/mode break 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

  1. paddymul commented on Oct 2, 2026

    @paddymul
    CollaboratorAuthor

    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 exact n_unique the same select takes 2.8 s.

    52,814-row × 27-column tallyman entry, stats for each search (ms)

    variant no search "a" "allen" "zzqx" (0 rows)
    xorq, current XorqStatPipeline (28 queries) 558 830 850 160
    xorq, scalar batch + every histogram in one query (2 queries) 366 400 389 157
    xorq, everything in one query, unpivot layout 452 489 483 471
    xorq, everything in one query, per-column subplans cross-joined 709 876 921 877
    xorq, everything in one query, COUNT(*) FILTER per bin 693 813 816 799
    xorq filter → to_pyarrow() → one polars select 115 113 106 102
    polars, today (PlDfStatsV2, 50k-row sample) 588 593 52 22
    polars, filter + one select 8.0 10.6 3.2 3.1

    10.8M rows × 43 columns, no search

    variant time
    xorq, current (44 queries, approximate distinct) 3,724 ms
    xorq, one query, per-column subplans cross-joined 3,052 ms
    xorq, one query, COUNT(*) FILTER per bin 3,703 ms
    xorq, one query, unpivot layout 3,807 ms
    xorq, 2 queries 3,996 ms
    polars, today (50k-row sample, distinct_count caps at 50,000) 318 ms
    polars, today with the sample disabled 12,791 ms
    polars, one select, exact distinct 2,819 ms
    polars, one select, exact distinct + top-10 value_counts for every column 3,830 ms

    The "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_counts for 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_count is 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 with XorqStatPipeline yet.

    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, while count() 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_rows here could be the shared mechanism. #814 covers reruns for a repeated search term.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions