Releases24 Nov 269 mins

Announcing Polars 2.2

Polars 2.2 brings out-of-core joins, SQL that changes tables, Iceberg table writes and maintenance from SQL, scan_database and over 100 performance improvements.

Polars team
Release Polars 2.2 on a magenta and yellow aurora

Polars 2.2 is out. This post gives an overview of the highlights grouped by area. Everything else is in the release notes.

Upgrade with:

pip install -U polars

Before you upgrade

Six changes may need your attention:

  • #31026 – Return character offsets in str.find and str.find_many. pl.Series(["héllo"]).str.find("l") used to return the byte offset 3 and now returns 2. For byte offsets, use the new bin.find and bin.find_many
  • #30914 – Require scalar function arguments at plan time. Arguments such as the n of head, the k of top_k or the quantile of quantile must now be a scalar when the query is planned. pl.col("a").head(pl.col("n").head(1)) used to run when it happened to return one row and now raises a ShapeError; use .first() or .item() instead
  • #30869 – Make SQL DELETE change the registered table in a SQLContext. Later queries on the table no longer see the deleted rows, and DELETE FROM (subquery) is an error
  • #30942 – Make median, mean, quantile and sum consistent across dtypes and contexts. Series.mean() and Series.median() on String, Categorical, List, Struct and similar dtypes used to return None and now raise, as the expressions already did. sum of a Date, Datetime, Time, Categorical or Enum column now raises in every context, also in a group_by where it used to return nulls
  • #30493 – Return Int64 from SQL string length, position and date part functions. They used to return UInt32 or Int32
  • #30243 – Raise on extra keys in from_dicts. Keys that didn’t appear in the first infer_schema_length dicts used to be dropped silently. Increase infer_schema_length or pass the full schema

Out-of-core joins

In 2.1 the group-by learned to spill to disk. In this release joins follow. When the build side of a streaming equi-join doesn’t fit in the memory budget, the join splits it into partitions by key hash, builds the hash tables of the partitions that fit in memory and spills the rest to disk. This release Polars is also better at the bookkeeping required to spill.

  • #30679 – Run equi-joins out of core when the build side is larger than the memory budget
  • #30558 – Make the build side of the streaming equi-join fully spillable
  • #30524 – Add a memory grant system, so operators that need hash tables share the memory budget instead of each taking a fixed share
  • #30496 – Store rows once in unordered streaming windows under memory pressure
  • #30749 – Use a purge-before-grow dirty page strategy in jemalloc while running out of core
  • #30838 – Lower the default out-of-core dirty page limit to 0.8 of total memory

SQL

SQL is again the largest area of this release. Tables registered in a SQLContext can now be changed with INSERT, UPDATE, DELETE and COPY, and many statements and functions from other databases now work in Polars.

  • #30958 – Support INSERT and UPDATE on registered tables
  • #30975 – Support COPY <table> FROM '<file>' on registered tables
  • #30834 – Support COPY ... TO '<file>'
  • #30778 – Support CREATE VIEW, DROP VIEW and DESCRIBE
  • #30840 – Support query parameters, passed with SQLContext.execute(..., params=...)
  • #30852 – Support SQL macros
  • #30799 – Support PIVOT, UNPIVOT and POSITIONAL JOIN
  • #30467 – Support derived tables that read the relations joined before them (LATERAL)
  • #30453 – Support correlated subqueries with GROUP BY
  • #30763 – Add BOOL_AND, BOOL_OR, ANY_VALUE, COUNT_IF, MAX_BY, MIN_BY, APPROX_COUNT_DISTINCT and REGEXP_EXTRACT
  • #30809 – Add TO_CHAR, TO_DATE, TO_TIMESTAMP, TYPEOF, REGEXP_MATCHES and JSON text access
  • #30442 – Support PERCENTILE_CONT and PERCENTILE_DISC with WITHIN GROUP
  • #30849 – Support offsets in RANGE window frames
  • #31017 – Support UNNEST ... WITH ORDINALITY
  • #30819 – Support SELECT alias reuse and information_schema.columns
  • #30825 – Add SQLContext(allow_file_reads=False) to turn off reading files from SQL

Iceberg

In 2.1 you could attach an Iceberg catalog to a SQLContext and create and drop tables in it. Now you can also write to those tables, change them and maintain them from SQL.

  • #30463 – Support INSERT INTO and INSERT OVERWRITE on attached catalog tables
  • #30603 – Support UPDATE on attached catalog tables
  • #30530 – Support DELETE on attached catalog tables
  • #30633 – Support MERGE INTO on attached catalog tables
  • #30267 – Support CREATE TABLE ... AS SELECT in attached catalogs
  • #30436 – Support ALTER TABLE on attached catalog tables
  • #30756 – Support partition evolution on attached catalog tables
  • #30751 – Support time travel on attached catalog tables
  • #30888 – Support tags and branches on attached catalog tables
  • #30697 – Read Iceberg metadata tables in SQL (lake.ns.t$snapshots)
  • #30856 – Support CALL rewrite_data_files to compact catalog tables
  • #30974 – Support CALL add_files on attached catalog tables
  • #30690 – Answer min, max and counts over an Iceberg scan from manifest statistics
  • #30620 – Accept a catalog name in scan_iceberg, resolved where the plan runs

Databases, Delta and Parquet

  • #30331 – Add scan_database to read a database table or query lazily, with projections, filters and limits pushed into the SQL
  • #30721 – Write Delta tables with the native Parquet sink, in parallel
  • #30359 – Allow using a KMS for Parquet encryption
  • #30816 – Make Parquet encryption and decryption properties serializable
  • #30737 – Support per-row bounding box (bbox covering) columns in experimental GeoParquet

Smaller additions

  • #30950 – Support a pattern per row in str.replace and str.replace_all
  • #30835 – Add format specifications to pl.format and SQL PRINTF and FORMAT
  • #30875 – Allow reading other columns in list.eval, arr.eval and cumulative_eval
  • #30826 – Add str.from_code_point and SQL CHR
  • #30318 – Support more dtypes in approx_quantile
  • #30861 – Release free-threaded Python wheels using the stable ABI

Performance

This release has 111 performance PRs. These are the ones you are most likely to notice:

Joins

  • #30708 – Pack multi-column join and group-by keys into fixed-width integers
  • #30681 – Use estimated rows to choose the build side of streaming equi-joins
  • #30662 – Use an exact bitset as runtime filter for integer join keys in a small range
  • #30651 – Use a hash join for outer joins on an equality and a condition on one input
  • #30692 – Skip the probe side of joins without non-null build keys

Group-by and aggregations

  • #30813 – Reduce runs of consecutive rows of one group as slices in the streaming group-by
  • #30808 – Reduce runs of equal groups locally in sum, mean, min and max
  • #30569 – Compute quantiles and quantile and rank bins by selection instead of sorting
  • #31062 – Run streaming cumulative aggregations in parallel
  • #29445 – Speed up rolling_min and rolling_max with a van Herk block scan
  • #30636 – Use the streaming group-by for value_counts with sort or normalize

Optimizer

  • #30694 – Move inner, semi and anti joins below left joins in more cases
  • #30680 – Use column equalities between joined inputs as join keys when reordering joins
  • #30598 – Prune unused struct fields in projection pushdown
  • #30802 – Skip files by predicates on the include_file_paths column
  • #30773 – Rewrite get(arg_max()) to max_by

Parquet, CSV and cloud

  • #30522 – Keep dictionary-encoded Parquet String columns as dictionaries in memory
  • #30601 – Speed up decoding of Parquet Struct, Map, Array and List rows
  • #30982 – Avoid redundant dictionary scans and byte-array copies in Parquet writes
  • #30639 – Parse CSV a column at a time over a mask-based field index
  • #30102 – Let the cloud read rate limiter follow demand
  • #30966 – Abandon stalled cloud reads after a stall timeout

Kernels

  • #30704 – Vectorize Decimal mean and sum
  • #30777 – Compute date parts with integer arithmetic
  • #30604 – Add a dedicated kernel for concat_list
  • #30261 – Trim leading and trailing .* from str.contains regexes

Bug fixes

Some of the bug fixes in this release:

Wrong results

  • #30682 – Don’t create spurious nulls in when/then/otherwise
  • #30315 – Keep row order for when/then/otherwise with literal branches
  • #30310 – Keep null rows for is_null() & ~is_in(..., nulls_equal=True) filters
  • #30330 – Use the exact join column mapping in projection and predicate pushdown
  • #30817 – Keep deleted rows deleted in delete_iceberg and sink_iceberg overwrites
  • #30412 – Fix a slice returning too many rows when an Iceberg position delete is past the end of the file
  • #30454 – Apply Delta deletion vectors to files in escaped partition paths
  • #30313 – Don’t drop rows after an empty chunk in write_json
  • #30743 – Return null instead of silently overflowing in the temporal total_* functions
  • #31048 – Fix a streaming window over a literal returning 0 rows

Hangs, panics and crashes

  • #31021 – Fix a race condition in the spill token waiters
  • #30361 – Prevent a to_pandas() deadlock with map_elements on another thread
  • #30665 – Avoid a segfault on environment changes after a cloud scan
  • #30458 – Fix segfaults and leaked references with Object values in group contexts
  • #30886 – Turn panics in Arrow C stream callbacks into stream errors
  • #30940 – Don’t panic on extreme counts in string, binary and list slicing

Other fixes

  • #30897 – Lower the cloud connect timeout from 300 to 10 seconds and add POLARS_HTTP_READ_TIMEOUT_SECONDS and POLARS_HTTP_REQUEST_TIMEOUT_SECONDS
  • #30279 – Don’t finish a sink’s output file when its query fails
  • #29450 – Report the failing object path in object-store errors
  • #30052 – Load Delta partition values from the log in scan_delta

Last words

Thanks to everyone who contributed code or filed an issue for this release. If something breaks after upgrading, please let us know.