Skip to content

Commit 81f5b7f

Browse files
lisa: Remove uses of polars.StringCache()
FIX StringCache has been a no-op for quite some time now (since polars 1.32). It has now been marked as deprecated in polars 1.41, so we can remove it from LISA code.
1 parent 57c87e9 commit 81f5b7f

4 files changed

Lines changed: 33 additions & 46 deletions

File tree

lisa/analysis/base.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,28 +1199,23 @@ def df_method(cls, f, index=None):
11991199
# can be changed using the AnalysisProxy(params=dict(...)) when the
12001200
# AnalysisProxy is instanciated in lisa.trace
12011201
def wrapper(self, *args, df_fmt=None, **kwargs):
1202-
# Ease working with LazyFrames coming from various sources. When
1203-
# they are collect()'ed in f(), they will be created using a common
1204-
# StringCache so Categorical columns can be concatenated and such.
1205-
with pl.StringCache():
1206-
1207-
# We might get different types based on whether the content
1208-
# comes from the function directly (could be a pandas object)
1209-
# or from the cache (polars LazyFrame).
1210-
df = cached_f(self, *args, **kwargs)
1211-
assert isinstance(df, (pd.DataFrame, pl.DataFrame, pl.LazyFrame))
1212-
1213-
df_fmt = df_fmt or 'pandas'
1214-
df = _df_to(
1215-
df,
1216-
fmt=df_fmt,
1217-
index=(
1218-
('Time' if 'Time' in df.collect_schema().names() else None)
1219-
if index is None and isinstance(df, (pl.LazyFrame, pl.DataFrame)) else
1220-
index
1221-
),
1222-
)
1223-
return df
1202+
# We might get different types based on whether the content
1203+
# comes from the function directly (could be a pandas object)
1204+
# or from the cache (polars LazyFrame).
1205+
df = cached_f(self, *args, **kwargs)
1206+
assert isinstance(df, (pd.DataFrame, pl.DataFrame, pl.LazyFrame))
1207+
1208+
df_fmt = df_fmt or 'pandas'
1209+
df = _df_to(
1210+
df,
1211+
fmt=df_fmt,
1212+
index=(
1213+
('Time' if 'Time' in df.collect_schema().names() else None)
1214+
if index is None and isinstance(df, (pl.LazyFrame, pl.DataFrame)) else
1215+
index
1216+
),
1217+
)
1218+
return df
12241219

12251220
return wrapper
12261221

lisa/analysis/tasks.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,7 @@ def load(event, *args, **kwargs):
317317
maintain_order=True,
318318
)
319319
df = df.select('name', 'pid')
320-
321-
with pl.StringCache():
322-
df = _polars_fast_collect(df)
320+
df = _polars_fast_collect(df)
323321

324322
def finalize(df, key_col):
325323
assert len(df.columns) == 2

lisa/trace.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6367,20 +6367,19 @@ def _data_to_parquet(cls, data, path, compression=_DEFAULT_PARQUET_COMPRESSION,
63676367
elif isinstance(data, pl.DataFrame):
63686368
data.write_parquet(path, **kwargs)
63696369
elif isinstance(data, pl.LazyFrame):
6370-
with pl.StringCache():
6371-
try:
6372-
# TOOD: revisit when polars streaming engine is complete
6373-
# and it does not raise a DeprecationWarning anymore.
6374-
with warnings.catch_warnings():
6375-
warnings.simplefilter("ignore", category=DeprecationWarning)
6376-
data.sink_parquet(path, **kwargs)
6377-
# The streaming engine may have issues with some LazyFrames, so
6378-
# fall back on collecting.
6379-
except Exception as e:
6380-
cls.get_logger().debug(f'Could not use sink_parquet(): {e}')
6381-
path.unlink(missing_ok=True)
6382-
data = _polars_fast_collect(data)
6383-
data.write_parquet(path, **kwargs)
6370+
try:
6371+
# TOOD: revisit when polars streaming engine is complete
6372+
# and it does not raise a DeprecationWarning anymore.
6373+
with warnings.catch_warnings():
6374+
warnings.simplefilter("ignore", category=DeprecationWarning)
6375+
data.sink_parquet(path, **kwargs)
6376+
# The streaming engine may have issues with some LazyFrames, so
6377+
# fall back on collecting.
6378+
except Exception as e:
6379+
cls.get_logger().debug(f'Could not use sink_parquet(): {e}')
6380+
path.unlink(missing_ok=True)
6381+
data = _polars_fast_collect(data)
6382+
data.write_parquet(path, **kwargs)
63846383
else:
63856384
data.to_parquet(path, **kwargs)
63866385

@@ -7199,12 +7198,6 @@ def __init__(self,
71997198
at_exit=True,
72007199
)
72017200

7202-
# Make sure that we always operate with an active StringCache when
7203-
# manipulating a trace object. This prevents issues with LazyFrame
7204-
# built out of a DataFrame containing Categorical data, in places where
7205-
# the user does not control the creation of the DataFrame.
7206-
cm_stack.enter_context(pl.StringCache())
7207-
72087201
trace_path = str(trace_path) if trace_path else None
72097202
self.trace_path = trace_path
72107203

@@ -7849,7 +7842,7 @@ def _get_parser(
78497842

78507843
@contextlib.contextmanager
78517844
def cm():
7852-
with pl.StringCache(), self._cache._parser_temp_path() as temp_dir:
7845+
with self._cache._parser_temp_path() as temp_dir:
78537846
self._activity_log.log(
78547847
'spinup-parser',
78557848
{

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def _find_packages(toplevel):
143143
# Require:
144144
# polars >= 1.15.0: https://github.com/pola-rs/polars/issues/19994
145145
# polars >= 1.16.0: https://github.com/pola-rs/polars/issues/20000
146+
# polars >= 1.32.0: https://github.com/pola-rs/polars/pull/23016
146147
# polars >= 1.34.0: https://github.com/pola-rs/polars/pull/24531
147148
# polars != 1.35.1: https://github.com/pola-rs/polars/issues/25261
148149
# polars != 1.35.2: https://github.com/pola-rs/polars/issues/25261

0 commit comments

Comments
 (0)