|
| 1 | +"""A query must not mutate the ``Plottable`` it was run on (#1786). |
| 2 | +
|
| 3 | +``WITH`` re-entry seeds the follow-up ``MATCH`` from the carried nodes. That seed used to be |
| 4 | +assigned to the CALLER's graph and never cleared, so the next -- entirely unrelated -- query on |
| 5 | +the same object was answered against the stale seed: no error, just the previous query's count. |
| 6 | +Engine-independent (wrong on pandas too), so an engine A/B never surfaces it. |
| 7 | +
|
| 8 | +Everything here is written to FAIL if the leak comes back: the assertions compare a query run on |
| 9 | +a re-used graph against the SAME query run on a pristine one, and pin the absolute counts so a |
| 10 | +fix that merely makes both sides equally wrong is caught too. |
| 11 | +""" |
| 12 | +import pandas as pd |
| 13 | +import pytest |
| 14 | + |
| 15 | +import graphistry |
| 16 | + |
| 17 | + |
| 18 | +def _engines(): |
| 19 | + out = ["pandas"] |
| 20 | + try: |
| 21 | + import polars # noqa: F401 |
| 22 | + out.append("polars") |
| 23 | + except Exception: |
| 24 | + pass |
| 25 | + return out |
| 26 | + |
| 27 | + |
| 28 | +ENGINES = _engines() |
| 29 | + |
| 30 | +# The #1786 repro graph. `kind` alternates a/b/a/b/a/b/a, so `{kind:'a'}` selects 4 of 7 nodes. |
| 31 | +NODES = pd.DataFrame({"id": list(range(7)), "kind": ["a", "b"] * 3 + ["a"]}) |
| 32 | +EDGES = pd.DataFrame( |
| 33 | + [(0, 3), (1, 2), (4, 6), (3, 5), (5, 6), (0, 6), (3, 4)], columns=["s", "d"] |
| 34 | +) |
| 35 | + |
| 36 | +PLAIN = "MATCH (a)-[*]->(b) RETURN count(*) AS c" |
| 37 | +REENTRY = "MATCH (a {kind:'a'}) WITH a MATCH (a)-[*]->(b) RETURN count(*) AS c" |
| 38 | +NESTED = ( |
| 39 | + "MATCH (a {kind:'a'}) WITH a MATCH (a)-[]->(b) WITH b MATCH (b)-[]->(c) " |
| 40 | + "RETURN count(*) AS c" |
| 41 | +) |
| 42 | + |
| 43 | +# Independently established on this fixed graph (identical on both engines). |
| 44 | +PLAIN_COUNT = 13 |
| 45 | +REENTRY_COUNT = 7 |
| 46 | +NESTED_COUNT = 2 |
| 47 | + |
| 48 | + |
| 49 | +def _g(): |
| 50 | + """A PRISTINE graph. Every oracle needs one: re-using a graph is the thing under test.""" |
| 51 | + return graphistry.nodes(NODES, "id").edges(EDGES, "s", "d") |
| 52 | + |
| 53 | + |
| 54 | +def _count(g, query, engine): |
| 55 | + return int(g.gfql(query, engine=engine)._nodes["c"].to_list()[0]) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize("engine", ENGINES) |
| 59 | +def test_reentry_then_unrelated_query_is_unaffected(engine): |
| 60 | + """THE regression: two independent queries, one graph. The second must not see the first.""" |
| 61 | + g = _g() |
| 62 | + assert _count(g, REENTRY, engine) == REENTRY_COUNT |
| 63 | + assert _count(g, PLAIN, engine) == PLAIN_COUNT |
| 64 | + # ... and identical to the same query on a graph that never ran the re-entry query. |
| 65 | + assert _count(g, PLAIN, engine) == _count(_g(), PLAIN, engine) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize("engine", ENGINES) |
| 69 | +def test_reentry_leaves_no_execution_state_on_caller_graph(engine): |
| 70 | + """Directly: the per-execution fields on the caller's object are untouched.""" |
| 71 | + g = _g() |
| 72 | + g.gfql(REENTRY, engine=engine) |
| 73 | + assert g._gfql_start_nodes is None |
| 74 | + assert g._gfql_rows_base_graph is None |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize("engine", ENGINES) |
| 78 | +def test_reentry_still_works(engine): |
| 79 | + """Do not "fix" the leak by breaking WITH: the re-entry answer itself must survive.""" |
| 80 | + assert _count(_g(), REENTRY, engine) == REENTRY_COUNT |
| 81 | + assert REENTRY_COUNT != PLAIN_COUNT # else the test above proves nothing |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize("engine", ENGINES) |
| 85 | +def test_repeated_reentry_is_stable(engine): |
| 86 | + """Re-entry twice on one graph: the second run must not inherit the first's seed.""" |
| 87 | + g = _g() |
| 88 | + assert [_count(g, REENTRY, engine) for _ in range(3)] == [REENTRY_COUNT] * 3 |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize("engine", ENGINES) |
| 92 | +def test_nested_reentry(engine): |
| 93 | + """Two WITH boundaries in one query, then an unrelated query on the same graph.""" |
| 94 | + g = _g() |
| 95 | + assert _count(g, NESTED, engine) == NESTED_COUNT |
| 96 | + assert g._gfql_start_nodes is None |
| 97 | + assert _count(g, PLAIN, engine) == PLAIN_COUNT |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize("engine", ENGINES) |
| 101 | +def test_query_order_does_not_change_answers(engine): |
| 102 | + """Whole point of immutability: the answers do not depend on execution order.""" |
| 103 | + g1 = _g() |
| 104 | + plain_first = (_count(g1, PLAIN, engine), _count(g1, REENTRY, engine)) |
| 105 | + g2 = _g() |
| 106 | + reentry_first = (_count(g2, REENTRY, engine), _count(g2, PLAIN, engine)) |
| 107 | + assert plain_first == (PLAIN_COUNT, REENTRY_COUNT) |
| 108 | + assert reentry_first == (REENTRY_COUNT, PLAIN_COUNT) |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.parametrize("engine", ENGINES) |
| 112 | +def test_reentry_result_does_not_carry_execution_state(engine): |
| 113 | + """The same defect one hop out: a RESULT that carries the seed poisons queries on IT.""" |
| 114 | + out = _g().gfql("MATCH (a {kind:'a'}) WITH a MATCH (a)-[]->(b) RETURN a", engine=engine) |
| 115 | + assert out._gfql_start_nodes is None |
| 116 | + assert out._gfql_rows_base_graph is None |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize("engine", ENGINES) |
| 120 | +def test_pure_call_chain_result_does_not_carry_execution_state(engine): |
| 121 | + """The all-calls boundary run (``let()`` bodies) hands its graph straight through. |
| 122 | +
|
| 123 | + On polars that graph is the one the run started from, so the boundary's base-graph |
| 124 | + write lands on the returned object unless it is attached to a copy and cleared. |
| 125 | + """ |
| 126 | + from graphistry.compute.ast import call |
| 127 | + |
| 128 | + out = _g().gfql([call("rows", {"table": "nodes"})], engine=engine) |
| 129 | + assert out._gfql_rows_base_graph is None |
| 130 | + assert out._gfql_start_nodes is None |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.parametrize("engine", ENGINES) |
| 134 | +def test_shortest_path_backend_is_not_written_onto_caller_graph(engine): |
| 135 | + """A per-CALL argument is not a property of the graph; it must not persist on it.""" |
| 136 | + g = _g() |
| 137 | + g.gfql(PLAIN, engine=engine, shortest_path_backend="bfs") |
| 138 | + assert g._gfql_shortest_path_backend == "auto" |
0 commit comments