Polars Engine

Run your Polars queries on Ibex

Keep writing Polars. IbexEngine is a Polars execution engine: you build a lazy query with the ordinary Polars API, choose the engine when you collect, and get a real Polars DataFrame back. Polars builds and optimizes the plan; Ibex executes all of it.

Experimental, built from source

The engine runs every query of the PDS-H benchmark unchanged, with results that match Polars in full. It is still experimental, and the adapter is pinned to Polars 2.1.0, because it reads Polars' internal plan format, which can change between releases.

Install the wheel

Prebuilt wheels cover Python 3.11–3.14 on Linux x86_64 (glibc 2.28+) and Windows x86_64; no compiler is needed. They bundle the native bridge and its Arrow libraries, and pull in polars==2.1.0, pyarrow and numpy. Install the current main:

pip install --pre ibex-polars --find-links https://github.com/bobjansen/Ibex/releases/expanded_assets/wheels-latest

CI rebuilds the wheels-latest prerelease on every push to main. Its wheels carry development versions (such as 0.4.1.dev539), and pip skips those unless given --pre. Tagged releases after v0.4.0 attach wheels too: to pin one, replace wheels-latest with the release tag and drop --pre.

To build the same wheel from a checkout, run pip wheel --no-deps ./python (it compiles Arrow, so the first build is slow).

Or build the native bridge

For development, you need Clang 20, CMake 3.31+, Ninja, and a Python with development headers, pyarrow and polars==2.1.0 installed. Build Ibex with the Python bridge and Parquet support, pointing CMake at that Python:

git clone https://github.com/bobjansen/Ibex.git && cd Ibex
python3 -m venv .venv && .venv/bin/pip install polars==2.1.0 pyarrow
cmake -S . -B build-release -G Ninja \
  -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release \
  -DIBEX_BUILD_PYTHON_BRIDGE=ON -DIBEX_BUILD_PARQUET=ON \
  -DIBEX_PYTHON_EXECUTABLE="$PWD/.venv/bin/python"
cmake --build build-release --target ibex_pyarrow --parallel 4

The build produces the ibex_pyarrow extension in build-release/python; the ibex_polars package lives in python/. Python has to find both, from whatever directory your code runs in. The simplest way is to register them with the venv once; run this from the repository root:

.venv/bin/python -c 'import pathlib, site; root = pathlib.Path.cwd(); pathlib.Path(site.getsitepackages()[0], "ibex.pth").write_text(f"{root}/build-release/python\n{root}/python\n")'

From then on, the venv's Python imports the engine from any directory:

cd /anywhere && /path/to/Ibex/.venv/bin/python -c "from ibex_polars import IbexEngine; print('ok')"

If you would rather not touch the venv, set PYTHONPATH instead. It has to hold absolute paths, so set it from the repository root, and set it again in every new shell:

export PYTHONPATH="$PWD/build-release/python:$PWD/python"

If CMake picked up headers from a different interpreter, also set Python3_INCLUDE_DIR to the include directory of the Python you selected.

Choose the engine at collect

Nothing about the query changes. Pass an IbexEngine to collect:

import polars as pl
from ibex_polars import IbexEngine

trades = pl.DataFrame({
    "symbol": ["A", "A", "B", "B"],
    "day": [1, 1, 1, 2],
    "price": [10.0, 11.0, 20.0, 21.0],
    "qty": [5, 7, 2, 3],
})

query = (
    trades.lazy()
    .filter(pl.col("qty") > 2)
    .group_by("symbol", "day")
    .agg(avg_price=pl.mean("price"), volume=pl.sum("qty"))
    .sort("symbol", "day")
)

engine = IbexEngine()
result = query.collect(engine=engine)   # a polars.DataFrame
shape: (2, 4)
┌────────┬─────┬───────────┬────────┐
│ symbol ┆ day ┆ avg_price ┆ volume │
│ ---    ┆ --- ┆ ---       ┆ ---    │
│ str    ┆ i64 ┆ f64       ┆ i64    │
╞════════╪═════╪═══════════╪════════╡
│ A      ┆ 1   ┆ 10.5      ┆ 12     │
│ B      ┆ 2   ┆ 21.0      ┆ 3      │
└────────┴─────┴───────────┴────────┘

Sources can be resident DataFrames, as above, or a local Parquet file through pl.scan_parquet(path), which Ibex reads with its own lazy Parquet reader. To make Ibex the default for every lazy collect, set the engine affinity:

pl.Config.set_engine_affinity(engine)
result = query.collect()          # runs on Ibex
pl.Config.set_engine_affinity("auto")  # back to Polars

To write the result to a file instead, sink it through the engine. The query runs in Ibex and Polars' own Parquet writer writes the result batch by batch as Ibex produces it, so the file is the one Polars would write and every sink_parquet option works unchanged:

query.sink_parquet("out.parquet", engine=engine)

Translate once, collect many times

engine.prepare(query)

Translates the plan and binds its inputs without running it. The first collect does this implicitly. Preparing a query over resident frames converts those frames once, which can take noticeably longer than a collect for large inputs; prepare first if you want to keep that out of a timed path. The engine keeps up to 32 prepared plans (IbexEngine(cache_size=N), 0 to disable). Plans are cached, results never are: each collect reads its sources again.

engine.explain(query)

Returns the Ibex program the query becomes, so you can see exactly what runs. Literals cross as typed bindings (^_ibex_scalar_N), never as text pasted into the source.

engine.prepare(query)
print(engine.explain(query))
_ibex_input[filter ((`qty` > ^_ibex_scalar_0))]
  [select { `symbol`, `day`, `avg_price` = mean(`price`),
            `volume` = coalesce(sum(`qty`), ^_ibex_scalar_1) }, by { `symbol`, `day` }]
  [update { `_ibex_sort_null_2_0` = is_null(`symbol`), `_ibex_sort_null_2_1` = is_null(`day`) }]
  [order { `_ibex_sort_null_2_0` desc, `symbol` asc, `_ibex_sort_null_2_1` desc, `day` asc }];

The output above is wrapped for the page. The coalesce and the null-sort columns are how Polars semantics are kept: a Polars sum of no values is zero where Ibex's is null, and Polars' sort puts nulls first.

Setting the thread budget

Ibex sizes its worker pool from the machine. Set IBEX_CORES before the first collect to cap it, the counterpart of Polars' POLARS_MAX_THREADS; set both to the same value when comparing the two engines.

IBEX_CORES=8 POLARS_MAX_THREADS=8 python my_pipeline.py

What runs on Ibex

Supported

  • Resident DataFrames and single local Parquet files.
  • filter, select, with_columns, arithmetic, comparisons, Boolean logic, is_between, is_in, when/then/otherwise, null checks and casts.
  • Column-key group_by with sum, mean, min, max, count, len, n_unique, first/last, median, std (ddof 1), Pearson corr and its square, including the null-on-empty sum pl.when(x.count() > 0).then(x.sum()).
  • Equality joins (inner, left, right, full, semi, anti), cross joins, composite keys and suffixes.
  • Per-group rows: group_by(...).head(n), and agg(pl.col(x).top_k(k)).explode(...) on a numeric column.
  • Sorting, unique, slice/head/tail, rounding, string slicing and starts_with/ends_with/contains, Date years.
  • Integer, float, Boolean, String, Categorical, Date, Datetime and Duration columns.

Not yet

  • Quantiles, var, std with another ddof, Spearman corr, and powers other than a float squared; list-valued results (agg(pl.col(x).head(n)) without explode), and explode other than of a per-group head or top_k.
  • Window expressions (over), computed group or sort keys, Python UDFs.
  • As-of and general non-equi joins, nondefault join validation.
  • General regular expressions; multiple or remote files; struct and list columns.
  • Background and async execution, lazy sinks (lazy=True), and sinks other than sink_parquet.

There is no silent fallback. A plan the engine cannot translate raises NotImplementedError before anything runs, naming the construct. If you want Polars to take over in that case, say so explicitly:

try:
    result = query.collect(engine=engine)
except NotImplementedError as exc:   # e.g. "unsupported function ('quantile', 'nearest')"
    result = query.collect(engine="streaming")

On the two public benchmark suites: all 22 PDS-H queries run, and of the h2o db-benchmark, all ten groupby questions and all five joins.

Measure it on your own machine

python/bench_polars_engines.sh runs the same Polars code on Polars' streaming engine and on IbexEngine, over PDS-H (scale 10, Parquet) and the h2o db-benchmark (10M rows, resident), at 8 and 1 threads, and checks every answer. It reports each query's prepare() time beside the timed collects: for resident frames, preparing imports them once, which Polars never does. Pin it to one CPU per physical core:

taskset -c 0,2,4,6,8,10,12,14 python/bench_polars_engines.sh \
    --pdsh-root ../polars-benchmark
python/summarize_polars_engines.py python/polars-engines-results/<stamp>

The benchmarks page compares Ibex's own language against Polars and DuckDB on the same suites; the PDS-H engine summary has per-query measurements of the Polars engine.