You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Guarantee cache column alignment via required order_by
The lazy column cache caches each column independently and recombines them
positionally, so a source that does not emit a deterministic row order across
collects could silently misalign cached columns. Previously this was only a
warn-on-every-call plus a docstring caveat.
Require an order_by key (a unique row identity). Every cached block is collected
sorted by order_by, so independently cached columns share one canonical order and
recombine in alignment. Uniqueness is validated on the already-collected fill
(no extra source pass) and can be skipped with validate=False. order_by and
partition_cols are folded into the cache key so caches built with different
layouts never collide. The runtime ordering warning is removed.
Tests cover alignment under a shuffling source, uniqueness rejection,
validate=False, and cross-partition-layout cache reuse. Docs document the new
required argument.
Signed-off-by: Pascal Tomecek <pascal.tomecek@cubistsystematic.com>
cache: An optional implementation of a cache backend. Defaults to a global in-memory cache.
162
+
order_by: One or more columns that uniquely identify each row (a unique total order) —
163
+
typically the frame's natural row identity, such as a primary key or ``date`` + ``symbol``.
164
+
Every cached column is collected and sorted by these columns so that independently cached
165
+
columns share one row order and stay aligned when recombined. ``partition_cols`` is not a
166
+
substitute: a partition normally holds many rows, so ``order_by`` must be unique *within*
167
+
each partition. Uniqueness is verified (see ``validate``). Output rows are returned
168
+
sorted by ``order_by`` (within each partition when ``partition_cols`` is set).
144
169
partition_cols: An optional set of columns to partition the cache by. It is recommended that queries to the underlying frame for the partition cols are fast,
145
-
i.e. they correspond to the parquet partition columns. Ordering of the result is not guaranteed when using partition columns.
170
+
i.e. they correspond to the parquet partition columns.
146
171
cache_mode: The caching mode; use "cache" for regular caching, "rebuild" to overwrite existing elements of the cache (i.e. to force a refresh), or "ignore" to not use the cache at all.
172
+
validate: If True (default), verify that ``order_by`` uniquely identifies rows of each
173
+
collected block, raising at ``collect`` time otherwise (Polars surfaces this as a
174
+
``ComputeError`` wrapping the validation ``ValueError``). The check runs on data already
175
+
collected for the cache fill, so it adds no extra pass over the source. Set to False to
176
+
skip it on hot paths where uniqueness is already guaranteed.
147
177
log_explain: If True, logs the query plan when defining the function.
148
178
**kwargs: Arguments to pass to the collect() method of the input data frame (i.e. to use a different engine)
149
179
@@ -152,26 +182,19 @@ def cache(
152
182
It means that if a persistent cache implementation is provided, the cache can remain valid between sessions.
153
183
- The cache will be invalidated if the input LazyFrame is changed in any way (i.e. by adding a new column, or changing the underlying data source).
154
184
It also means that the act of generating the column_cache will change the key for downstream caches,
155
-
i.e. `df.piot.cache().select(expr_1).piot.cache()` will have a different cache from `df.select(expr_1).piot.cache()`,
185
+
i.e. `df.piot.cache(order_by="id").select(expr_1).piot.cache(order_by="id")` will have a different cache from `df.select(expr_1).piot.cache(order_by="id")`,
156
186
even though they return the same result.
157
187
- Turn on debug level logging for more info about the cache hits and misses.
158
188
159
-
.. warning::
160
-
161
-
**Ordering Requirement**: This function relies on the source LazyFrame producing
162
-
consistent row ordering across multiple collects. Since columns are cached independently,
163
-
if the source LazyFrame does not guarantee deterministic ordering, different columns
164
-
may be cached with different row orderings, leading to misaligned data when combined.
0 commit comments