|
| 1 | +"""Single-alias predicate pushdown keeps working after an earlier pushdown narrowed the frame (#2020).""" |
| 2 | +import pandas as pd |
| 3 | +import pytest |
| 4 | + |
| 5 | +import graphistry |
| 6 | + |
| 7 | +Q = ("MATCH (a)-[e]->(t) WHERE a.type IN ['person','company'] AND e.e_type IN ['sent','transfer'] " |
| 8 | + "AND t.type IN ['transaction','account'] RETURN t.id AS id") |
| 9 | + |
| 10 | + |
| 11 | +def _graph(engine): |
| 12 | + nodes = pd.DataFrame({"id": ["a", "b", "c", "tx1", "tx2"], |
| 13 | + "type": ["person", "person", "company", "transaction", "transaction"]}) |
| 14 | + edges = pd.DataFrame({"src": ["a", "b", "a", "tx1", "tx2"], "dst": ["b", "c", "tx1", "tx2", "c"], |
| 15 | + "e_type": ["knows", "works_at", "sent", "transfer", "received"]}) |
| 16 | + if engine == "polars": |
| 17 | + pl = pytest.importorskip("polars") |
| 18 | + nodes, edges = pl.from_pandas(nodes), pl.from_pandas(edges) |
| 19 | + elif engine == "cudf": |
| 20 | + cudf = pytest.importorskip("cudf") |
| 21 | + nodes, edges = cudf.from_pandas(nodes), cudf.from_pandas(edges) |
| 22 | + return graphistry.edges(edges, "src", "dst").nodes(nodes, "id") |
| 23 | + |
| 24 | + |
| 25 | +def _ids(res): |
| 26 | + nodes = res._nodes |
| 27 | + df = nodes.to_pandas() if hasattr(nodes, "to_pandas") else pd.DataFrame(nodes) |
| 28 | + return sorted(df["id"].tolist()) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("engine", ["pandas", "polars", "cudf"]) |
| 32 | +def test_three_in_predicates_across_a_hop_agree_on_every_engine(engine): |
| 33 | + assert _ids(_graph(engine).gfql(Q, engine=engine)) == ["tx1"] |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize("engine", ["pandas", "cudf"]) |
| 37 | +def test_pushdown_on_a_frame_with_filtered_labels_matches_the_scalar_form(engine): |
| 38 | + g = _graph(engine) |
| 39 | + scalar = Q.replace("t.type IN ['transaction','account']", "t.type = 'transaction'") |
| 40 | + assert _ids(g.gfql(Q, engine=engine)) == _ids(g.gfql(scalar, engine=engine)) == ["tx1"] |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize("engine", ["pandas", "cudf"]) |
| 44 | +def test_pushdown_on_a_non_range_indexed_node_frame(engine): |
| 45 | + g = _graph("pandas") |
| 46 | + nodes = g._nodes.iloc[[4, 2, 0, 3, 1]] # labels out of order, no RangeIndex |
| 47 | + if engine == "cudf": |
| 48 | + cudf = pytest.importorskip("cudf") |
| 49 | + nodes = cudf.from_pandas(nodes) |
| 50 | + g = _graph("cudf") |
| 51 | + g = g.nodes(nodes) |
| 52 | + assert _ids(g.gfql(Q, engine=engine)) == ["tx1"] |
0 commit comments