From 238b9da6bbad1fcb938627692a52bcf539e6c4ae Mon Sep 17 00:00:00 2001 From: Johnny Wilson Dougherty Date: Sat, 28 Nov 2026 22:18:29 +0530 Subject: [PATCH] docs: add examples/lazy_check.py, a 5-minute check that a LazyFrame stays lazy The script writes a Parquet file (5M rows by default, or the row count in argv), plans and applies a cleaning pass on a scan of it, and times suggest_plan, apply_plan and collect. It prints the Polars query plan. tests/test_lazy_check_example.py runs it on 1,000 rows, so the file the launch posts link to cannot break without a red test. The README links to it after the quickstart. --- README.md | 4 +++ examples/lazy_check.py | 51 ++++++++++++++++++++++++++++++++ tests/test_lazy_check_example.py | 18 +++++++++++ 3 files changed, 73 insertions(+) create mode 100644 examples/lazy_check.py create mode 100644 tests/test_lazy_check_example.py diff --git a/README.md b/README.md index a7a51e4..f79ee84 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,10 @@ assert "PYTHON_UDF" not in cleaned_lf.explain() df = cleaned_lf.collect(engine="streaming") ``` +To check that the LazyFrame stays lazy on 5 million rows, run +[`examples/lazy_check.py`](https://github.com/FreshCode-Org/freshdata/blob/main/examples/lazy_check.py). +It times `suggest_plan`, `apply_plan` and `collect`, and prints the query plan. + --- ## Diffing Repair Plans Before Execution diff --git a/examples/lazy_check.py b/examples/lazy_check.py new file mode 100644 index 0000000..9008da0 --- /dev/null +++ b/examples/lazy_check.py @@ -0,0 +1,51 @@ +"""5-minute check: does freshdata keep a Polars LazyFrame lazy? + + pip install "freshdata-cleaner[polars]" # 3.0.0 or later + python lazy_check.py # 5,000,000 rows + python lazy_check.py 50000000 # needs about 3 GB of RAM. apply_plan stays under 1 ms + +It writes a 5M-row Parquet file, plans and applies a cleaning pass on a +scan of it, and times each step. If apply_plan reads the data, its time +grows with the file. If it stays lazy, it takes milliseconds and the work +happens in your own .collect(). +""" + +import pathlib +import sys +import tempfile +import time + +import polars as pl + +import freshdata as fd + +N = int(sys.argv[1]) if len(sys.argv) > 1 else 5_000_000 +N -= N % 5 # the sample columns repeat a 5-row pattern +path = pathlib.Path(tempfile.mkdtemp()) / "events.parquet" +pl.DataFrame( + { + "Customer ID": pl.int_range(N, eager=True), + "Signup Age": [34.0, None, 29.0, 120.0, 41.0] * (N // 5), + "Segment": [" retail", "b2b ", "retail", None, "b2b"] * (N // 5), + } +).write_parquet(path) + +lf = pl.scan_parquet(path) + +t0 = time.perf_counter() +plan = fd.suggest_plan(lf) # one 1-row aggregate query for medians and IQR fences +t1 = time.perf_counter() +cleaned = fd.apply_plan(lf, plan) # builds expressions only +t2 = time.perf_counter() +df = cleaned.collect() # the only full read of the data +t3 = time.perf_counter() + +print(f"freshdata {fd.__version__}, polars {pl.__version__}, {N:,} rows") +for action in plan.actions: + print(f" plan: [{action.stage}] {action.column}: {action.action} {action.parameters}") +print(f"suggest_plan: {(t1 - t0) * 1000:8.1f} ms") +print(f"apply_plan: {(t2 - t1) * 1000:8.1f} ms -> returns {type(cleaned).__name__}") +print(f"collect: {(t3 - t2) * 1000:8.1f} ms -> {df.shape}") +print("\nThe query that apply_plan built (one Polars plan, still unexecuted at apply time):") +print(cleaned.explain()) +assert isinstance(cleaned, pl.LazyFrame) diff --git a/tests/test_lazy_check_example.py b/tests/test_lazy_check_example.py new file mode 100644 index 0000000..1f654bc --- /dev/null +++ b/tests/test_lazy_check_example.py @@ -0,0 +1,18 @@ +"""The launch posts link examples/lazy_check.py. It must run on the current package.""" + +import pathlib +import subprocess +import sys + +import pytest + +SCRIPT = pathlib.Path(__file__).resolve().parents[1] / "examples" / "lazy_check.py" + + +def test_lazy_check_example_runs(tmp_path): + pytest.importorskip("polars") + run = subprocess.run([sys.executable, str(SCRIPT), "1000"], cwd=tmp_path, + capture_output=True, text=True, check=False) + assert run.returncode == 0, run.stderr + assert "1,000 rows" in run.stdout + assert "-> returns LazyFrame" in run.stdout