|
| 1 | +import polars as pl |
| 2 | +import pytest |
| 3 | +from opentelemetry import trace |
| 4 | +from opentelemetry.sdk.trace import TracerProvider |
| 5 | +from opentelemetry.sdk.trace.export import SimpleSpanProcessor |
| 6 | +from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter |
| 7 | +from polars.testing import assert_frame_equal |
| 8 | + |
| 9 | +import polars_io_tools # noqa: F401 -- registers the `.piot` namespace |
| 10 | +from polars_io_tools.io_sources.util import register_io_source_with_is_pure |
| 11 | + |
| 12 | +_EXPORTER = InMemorySpanExporter() |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(scope="module", autouse=True) |
| 16 | +def _otel_provider(): |
| 17 | + provider = trace.get_tracer_provider() |
| 18 | + if not hasattr(provider, "add_span_processor"): |
| 19 | + trace.set_tracer_provider(TracerProvider()) |
| 20 | + provider = trace.get_tracer_provider() |
| 21 | + if hasattr(provider, "add_span_processor"): |
| 22 | + provider.add_span_processor(SimpleSpanProcessor(_EXPORTER)) |
| 23 | + yield |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(autouse=True) |
| 27 | +def _reset(): |
| 28 | + _EXPORTER.clear() |
| 29 | + yield |
| 30 | + |
| 31 | + |
| 32 | +def _probe_spans(): |
| 33 | + return [s for s in _EXPORTER.get_finished_spans() if s.name.startswith("io_source.execute[probe]")] |
| 34 | + |
| 35 | + |
| 36 | +def test_probe_is_result_preserving_passthrough(): |
| 37 | + df = pl.DataFrame({"id": [1, 2, 3], "v": [10, 20, 30]}) |
| 38 | + out = df.lazy().piot.probe().collect(engine="streaming") |
| 39 | + assert_frame_equal(out, df) |
| 40 | + |
| 41 | + |
| 42 | +def test_probe_forwards_n_rows_pushdown(): |
| 43 | + out = pl.LazyFrame({"id": list(range(100))}).piot.probe().head(5).collect(engine="streaming") |
| 44 | + assert out["id"].to_list() == [0, 1, 2, 3, 4] |
| 45 | + assert _probe_spans()[-1].attributes["polars_io_tools.total_rows"] == 5 |
| 46 | + |
| 47 | + |
| 48 | +def test_probe_does_not_dedupe_impure_input(): |
| 49 | + # A pass-through cannot claim purity: reusing the same probe over an impure input must not collapse executions. |
| 50 | + state = {"n": 0} |
| 51 | + |
| 52 | + def impure(with_columns, predicate, n_rows, batch_size): |
| 53 | + state["n"] += 1 |
| 54 | + yield pl.DataFrame({"run": [state["n"]]}) |
| 55 | + |
| 56 | + probe = register_io_source_with_is_pure(impure, schema=pl.Schema({"run": pl.Int64}), is_pure=False).piot.probe() |
| 57 | + out = pl.concat([probe, probe]).collect(engine="streaming") |
| 58 | + assert sorted(out["run"].to_list()) == [1, 2] |
| 59 | + |
| 60 | + |
| 61 | +def test_probe_emits_one_span_with_description(): |
| 62 | + df = pl.DataFrame({"id": [1, 2, 3]}) |
| 63 | + df.lazy().piot.probe(description="stage1").collect(engine="streaming") |
| 64 | + |
| 65 | + spans = _probe_spans() |
| 66 | + assert len(spans) == 1 |
| 67 | + span = spans[0] |
| 68 | + assert span.attributes["polars_io_tools.explain_name"] == "probe" |
| 69 | + assert span.attributes["polars_io_tools.explain_detail"] == "stage1" |
| 70 | + assert span.attributes["polars_io_tools.total_rows"] == 3 |
| 71 | + |
| 72 | + |
| 73 | +def test_probe_forwards_predicate_pushdown(): |
| 74 | + # A pushed predicate reaches the probe, so only matching rows flow through it (and are counted). |
| 75 | + df = pl.DataFrame({"id": [1, 2, 3, 4], "grp": [0, 1, 0, 1]}) |
| 76 | + out = df.lazy().piot.probe().filter(pl.col("grp") == 1).collect(engine="streaming") |
| 77 | + |
| 78 | + assert out.sort("id")["id"].to_list() == [2, 4] |
| 79 | + assert _probe_spans()[-1].attributes["polars_io_tools.total_rows"] == 2 |
| 80 | + |
| 81 | + |
| 82 | +def test_probe_forwards_is_pure(monkeypatch): |
| 83 | + # is_pure defaults to False (safe for an input of unknown purity) and can be opted into for a known-pure input. |
| 84 | + captured = {} |
| 85 | + |
| 86 | + def fake_register(io_source, *, schema, **kwargs): |
| 87 | + captured.update(kwargs) |
| 88 | + return object() |
| 89 | + |
| 90 | + monkeypatch.setattr("polars.io.plugins.register_io_source", fake_register) |
| 91 | + |
| 92 | + pl.LazyFrame({"a": [1]}).piot.probe() |
| 93 | + assert captured["is_pure"] is False |
| 94 | + |
| 95 | + captured.clear() |
| 96 | + pl.LazyFrame({"a": [1]}).piot.probe(is_pure=True) |
| 97 | + assert captured["is_pure"] is True |
0 commit comments