Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions examples/lazy_check.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions tests/test_lazy_check_example.py
Original file line number Diff line number Diff line change
@@ -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
Loading