#27454·polars

Unnecessary multiplexing causes OOM with in-memory sources.

Author: ramcasaCreated Apr 30, 2026Updated Sep 29, 2026
Labelsbugpythonneeds triage

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

Consider the following plan that computes the number of elements equal to the minimum value (and forget for a moment that this can be computed in streaming fashion with a specialized node, since the example applies to more general cases).

python
import polars as pl

lf = pl.DataFrame({"x": range(100)}).lazy()
plan = lf.select(pl.col("x").filter(pl.col("x") == pl.col("x").min()).count())
plan.show_graph(engine="streaming", plan_stage="physical")

Computationally, something like this requires two passes. One to compute the aggregation, and one that goes over the data once the aggregation is done. This is because it is impossible to know the value of the aggregation before all values are seen. Exceptions to this rule are aggregations such as first(), but it holds for most aggregations. The way polars plans this right now is by incorporating a multiplexer which is potentially memory intensive.

Image

Log output

bash

Issue description

I find there are two issues with this approach:

First, the information is slightly misleading and could be stated better: the node is not potentially memory intensive. It is definitely memory intensive. The multiplexer will always need to hold all the data read. So this should be shown in red in the graph even though it runs in the streaming engine.

Second, this should not run out of memory with in-memory sources. (Note that the fact that it is an in-memory-source does not mean that it fits in memory, since it can be a memory-mapped in-memory source.) Using multiplexers with in-memory sources are not a requirement because the data can be read multiple times. (In fact, multipass algorithms, which polars does not implement, are possible). Multiplexers in this case are only a potential optimization for improving cache access. In particular, when the data in one branch needs to see the full input to produce the result and the data in the other path cannot start computing before that branch is done, it is better to not have the multiplexer.

Expected behavior

There are two issues to solve. I think polars should properly display this plan with red nodes and I would like polars to optimize differently based on whether the data read can be read multiple times or not, such that this plan can run with constant memory for in-memory sources.

Installed versions

--------Version info---------
Polars:              1.40.1
Index type:          UInt32
Platform:            Linux-6.8.0-110-generic-x86_64-with-glibc2.35
Python:              3.12.12 (main, Feb 12 2026, 00:42:14) [Clang 21.1.4 ]
Runtime:             rt32

----Optional dependencies----
Azure CLI            <not installed>
adbc_driver_manager  <not installed>
altair               <not installed>
azure.identity       <not installed>
boto3                <not installed>
cloudpickle          <not installed>
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               2026.3.0
gevent               <not installed>
google.auth          <not installed>
great_tables         <not installed>
matplotlib           <not installed>
numpy                2.4.4
openpyxl             <not installed>
pandas               3.0.2
polars_cloud         <not installed>
pyarrow              23.0.1
pydantic             2.13.3
pyiceberg            0.11.1
sqlalchemy           <not installed>
torch                <not installed>
xlsx2csv             <not installed>
xlsxwriter           <not installed>