core_lens.utils.polars_utils#
Polars scan helpers with predicate pushdown for entity materialisation.
Functions#
|
Return a glob-safe path for |
|
Wrap pl.read_parquet_schema with caching. |
|
Collect a |
|
Return a |
Module Contents#
- core_lens.utils.polars_utils.parquet_scan_path(path: str) str#
Return a glob-safe path for
pl.scan_parquet.When path is a directory polars will reject it if the directory contains files with mixed extensions (e.g. a
data_dictionary.csvsitting alongside Hive-partitioned.parquetfiles). This helper coerces a bare directory to<path>/**/*.parquetso only Parquet files are matched.- Parameters:
path (str) – Filesystem path or cloud URI to a Parquet file or directory.
- Returns:
The original path if it already points to a file, otherwise
<path>/**/*.parquet.- Return type:
str
- core_lens.utils.polars_utils.cached_read_schema(path: str, storage_options: dict[str, Any] | None = None) Any#
Wrap pl.read_parquet_schema with caching.
Avoids repeated Parquet footer reads for schema resolution in hot paths.
- core_lens.utils.polars_utils.collect_lf(lf: polars.LazyFrame) polars.DataFrame#
Collect a
LazyFrameusing the best available backend.GPU present — executes via the RAPIDS
cudf_polarsstreaming engine (GPUEngine(executor="streaming")). Handles datasets larger than VRAM through data partitioning.No GPU — falls back to Polars’ built-in CPU streaming executor (
collect(streaming=True)), which keeps memory usage low for large Parquet scans.
Use this function for all data scans (materialisation, geometry joins, similarity fetches). Tiny index scans that feed into subsequent in-process joins should stay as bare
.collect()calls — the streaming path can occasionally change row ordering in ways that break those joins.- Parameters:
lf (pl.LazyFrame) – The lazy frame to collect.
- Returns:
A materialised
pl.DataFrame.- Return type:
pl.DataFrame
- core_lens.utils.polars_utils.scan_with_key_filter(path: str, key_cols: list[str], key_values: polars.DataFrame | polars.LazyFrame, time_expr: polars.Expr | None = None, storage_options: dict[str, Any] | None = None) polars.LazyFrame#
Return a
pl.LazyFramefiltered to the given keys and optional time range.Uses
pl.scan_parquetwith two predicate-pushdown layers:Key filter — restricts to entity instances whose key column(s) are in
key_values. For a single-column key this is anis_inpredicate pushed down to the Parquet reader. For composite keys each column is filtered independently (over-selects slightly, then pruned by the join at collect time).Time filter — an optional Polars expression appended with
&, also pushed down if the Parquet file carries column statistics.
- Parameters:
path (str) – Absolute path or cloud URI to a Parquet file.
key_cols (list[str]) – Column name(s) that form the entity’s unique key.
key_values (pl.DataFrame | pl.LazyFrame) – A narrow frame containing only the key column(s) with the exact values to retain.
time_expr (pl.Expr | None, optional) – An optional Polars filter expression for the time column, as produced by
resolve_time_filter().storage_options (dict[str, Any] | None, optional) – Cloud credential / configuration options forwarded to
pl.scan_parquet.Noneuses ambient credentials.
- Returns:
A
pl.LazyFrameready to be.collect()-ed.- Return type:
pl.LazyFrame