-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsweep_direct_cypher.py
More file actions
356 lines (302 loc) · 12.4 KB
/
Copy pathsweep_direct_cypher.py
File metadata and controls
356 lines (302 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from __future__ import annotations
import argparse
import contextlib
import io
from pathlib import Path
from typing import List, Sequence, Tuple
from graphistry.gfql.ref.enumerator import OracleCaps, enumerate_chain
from tests.cypher_tck.direct_cypher_xfail_contract import (
DIRECT_CYPHER_NONVALIDATION_XFAIL_OUTCOME_BY_KEY,
DirectCypherXfailOutcome,
)
from tests.cypher_tck.models import Scenario
from tests.cypher_tck.scenarios import SCENARIOS
from tests.cypher_tck.sweep_promotions import (
_expects_error_scenario as _expects_translated_error_scenario,
)
from tests.cypher_tck.test_tck_runner import (
_assert_expected_rows,
_build_graph,
_ids_from_df,
_rows_from_result,
)
_SUPPORT_FILE = Path(__file__).resolve().parent / "direct_cypher_support.py"
def _expects_direct_cypher_error_scenario(scenario: object) -> bool:
status = getattr(scenario, "status", None)
if status == "skip":
return False
expected = getattr(scenario, "expected", None)
if expected is None or getattr(expected, "rows", None) is not None:
return False
tags = set(getattr(scenario, "tags", ()))
if "cypher-string-error" in tags:
return True
return _expects_translated_error_scenario(scenario)
def _ids_from_entity_projection_meta(
result: object,
*,
table: str,
alias_hint: str | None = None,
) -> set:
meta = getattr(result, "_cypher_entity_projection_meta", None)
if not isinstance(meta, dict):
return set()
candidates: List[object] = []
if alias_hint is not None:
candidate = meta.get(alias_hint)
if candidate is not None:
candidates = [candidate]
else:
candidates = [candidate for candidate in meta.values() if isinstance(candidate, dict) and candidate.get("table") == table]
if len(candidates) != 1:
return set()
candidate = candidates[0]
if not isinstance(candidate, dict) or candidate.get("table") != table:
return set()
ids = candidate.get("ids")
if ids is None:
return set()
values = ids.tolist() if hasattr(ids, "tolist") else list(ids)
normalized = set()
for value in values:
if hasattr(value, "item") and not isinstance(value, (str, bytes, list, tuple, dict)):
try:
value = value.item()
except Exception:
pass
if value is not None:
normalized.add(value)
return normalized
def _compare_graph_result(scenario: Scenario, result: object) -> None:
actual_nodes = _ids_from_df(getattr(result, "_nodes", None), scenario.graph.node_id)
actual_edges = _ids_from_df(getattr(result, "_edges", None), scenario.graph.edge_id)
if scenario.expected.node_ids is not None and not actual_nodes:
actual_nodes = _ids_from_entity_projection_meta(result, table="nodes", alias_hint=scenario.return_alias)
if scenario.expected.edge_ids is not None and not actual_edges:
actual_edges = _ids_from_entity_projection_meta(result, table="edges", alias_hint=scenario.return_alias)
if scenario.expected.node_ids is not None:
assert set(scenario.expected.node_ids) == actual_nodes, (
f"node id mismatch for {scenario.key}: expected={sorted(set(scenario.expected.node_ids))}, "
f"actual={sorted(actual_nodes)}"
)
if scenario.expected.edge_ids is not None:
assert set(scenario.expected.edge_ids) == actual_edges, (
f"edge id mismatch for {scenario.key}: expected={sorted(set(scenario.expected.edge_ids))}, "
f"actual={sorted(actual_edges)}"
)
if scenario.expected.node_ids is not None or scenario.expected.edge_ids is not None:
return
if scenario.gfql is None:
raise AssertionError(
f"graph comparison for {scenario.key} requires translated GFQL oracle or explicit expected ids"
)
oracle = enumerate_chain(_build_graph(scenario.graph), scenario.gfql, caps=OracleCaps(max_nodes=100, max_edges=100))
oracle_nodes = _ids_from_df(oracle._nodes, scenario.graph.node_id)
oracle_edges = _ids_from_df(oracle._edges, scenario.graph.edge_id)
assert oracle_nodes == actual_nodes, (
f"node id mismatch vs translated oracle for {scenario.key}: "
f"oracle={sorted(oracle_nodes)}, actual={sorted(actual_nodes)}"
)
assert oracle_edges == actual_edges, (
f"edge id mismatch vs translated oracle for {scenario.key}: "
f"oracle={sorted(oracle_edges)}, actual={sorted(actual_edges)}"
)
def _run_direct_cypher_scenario(scenario: Scenario) -> Tuple[bool, str]:
graph = _build_graph(scenario.graph)
expects_error = _expects_direct_cypher_error_scenario(scenario)
try:
result = graph.gfql(scenario.cypher, params=scenario.params)
except Exception as exc: # noqa: BLE001 - report exact blocker class/message
if expects_error:
return True, ""
return False, f"{type(exc).__name__}: {exc}"
if expects_error:
return False, "expected error but direct Cypher executed successfully"
try:
if scenario.expected.rows is not None:
_assert_expected_rows(scenario, _rows_from_result(result))
else:
_compare_graph_result(scenario, result)
except Exception as exc: # noqa: BLE001 - report exact blocker class/message
return False, f"{type(exc).__name__}: {exc}"
return True, ""
def _compute_direct_cypher_sets(
scenarios: Sequence[Scenario] = SCENARIOS,
) -> Tuple[List[str], List[str], List[str], List[Tuple[str, str]], List[Tuple[str, str]]]:
overlap_keys: List[str] = []
promotion_row_keys: List[str] = []
promotion_error_keys: List[str] = []
overlap_failures: List[Tuple[str, str]] = []
promotion_failures: List[Tuple[str, str]] = []
for scenario in scenarios:
if scenario.status == "skip":
continue
passed, detail = _run_direct_cypher_scenario(scenario)
is_direct_supported = scenario.status == "supported" and "cypher-string" in scenario.tags
is_translated_supported = (
scenario.status == "supported"
and scenario.gfql is not None
and not is_direct_supported
)
if is_translated_supported:
if passed:
overlap_keys.append(scenario.key)
else:
overlap_failures.append((scenario.key, detail))
continue
if passed:
if _expects_direct_cypher_error_scenario(scenario):
promotion_error_keys.append(scenario.key)
else:
promotion_row_keys.append(scenario.key)
else:
promotion_failures.append((scenario.key, detail))
overlap_keys.sort()
promotion_row_keys.sort()
promotion_error_keys.sort()
return (
overlap_keys,
promotion_row_keys,
promotion_error_keys,
overlap_failures,
promotion_failures,
)
def _compute_direct_cypher_nonvalidation_details(
scenarios: Sequence[Scenario] = SCENARIOS,
) -> List[Tuple[str, DirectCypherXfailOutcome, bool, str]]:
scenarios_by_key = {scenario.key: scenario for scenario in scenarios}
details: List[Tuple[str, DirectCypherXfailOutcome, bool, str]] = []
for key, expected_outcome in sorted(
DIRECT_CYPHER_NONVALIDATION_XFAIL_OUTCOME_BY_KEY.items()
):
scenario = scenarios_by_key.get(key)
if scenario is None:
details.append((key, expected_outcome, False, "missing scenario"))
continue
if scenario.status != "xfail":
details.append(
(
key,
expected_outcome,
False,
f"scenario status is {scenario.status!r}, expected 'xfail'",
)
)
continue
with contextlib.redirect_stdout(io.StringIO()):
passed, detail = _run_direct_cypher_scenario(scenario)
details.append((key, expected_outcome, passed, detail))
return details
def _render_direct_cypher_nonvalidation_details(
details: Sequence[Tuple[str, DirectCypherXfailOutcome, bool, str]],
*,
limit: int = 0,
) -> List[str]:
visible_details = list(details if limit <= 0 else details[:limit])
lines = [
"Direct-Cypher non-validation debt details:",
f"- shown: {len(visible_details)} / {len(details)}",
]
for key, expected_outcome, passed, detail in visible_details:
current = "passes expected oracle" if passed else detail
lines.append(f"- {key}: expected={expected_outcome}; current={current}")
return lines
def _render_direct_cypher_support(
overlap_keys: Sequence[str],
promotion_row_keys: Sequence[str],
promotion_error_keys: Sequence[str],
) -> str:
lines = [
'"""Generated direct-Cypher support snapshots for deterministic reporting."""',
"",
"from __future__ import annotations",
"",
"DIRECT_CYPHER_OVERLAP_KEYS = {",
]
lines.extend([f' "{key}",' for key in overlap_keys])
lines.append("}")
lines.append("")
lines.append("DIRECT_CYPHER_PROMOTION_ROW_KEYS = {")
lines.extend([f' "{key}",' for key in promotion_row_keys])
lines.append("}")
lines.append("")
lines.append("DIRECT_CYPHER_PROMOTION_ERROR_KEYS = {")
lines.extend([f' "{key}",' for key in promotion_error_keys])
lines.append("}")
lines.append("")
lines.append("DIRECT_CYPHER_PROMOTION_KEYS = DIRECT_CYPHER_PROMOTION_ROW_KEYS | DIRECT_CYPHER_PROMOTION_ERROR_KEYS")
lines.append("DIRECT_CYPHER_ADDITIONAL_PASS_KEYS = DIRECT_CYPHER_PROMOTION_KEYS")
lines.append("")
return "\n".join(lines)
def main() -> None:
parser = argparse.ArgumentParser(description="Sweep direct local Cypher support and refresh direct support snapshots.")
parser.add_argument("--write", action="store_true", help="Write refreshed direct support keys")
parser.add_argument("--show-failures", type=int, default=0, help="Show first N overlap/additional failures")
parser.add_argument(
"--show-nonvalidation-debt",
action="store_true",
help="Show current details for tracked non-validation xfail debt",
)
parser.add_argument(
"--nonvalidation-limit",
type=int,
default=0,
help="Limit non-validation debt details; 0 means all",
)
args = parser.parse_args()
if args.show_nonvalidation_debt and not args.write and args.show_failures == 0:
details = _compute_direct_cypher_nonvalidation_details()
print(
"\n".join(
_render_direct_cypher_nonvalidation_details(
details,
limit=args.nonvalidation_limit,
)
)
)
return
(
overlap_keys,
promotion_row_keys,
promotion_error_keys,
overlap_failures,
promotion_failures,
) = _compute_direct_cypher_sets()
supported_total = sum(
1
for scenario in SCENARIOS
if scenario.status == "supported"
and scenario.gfql is not None
and "cypher-string" not in scenario.tags
)
print(f"direct-Cypher overlap keys: {len(overlap_keys)} / {supported_total}")
print(f"direct-Cypher promotion row keys: {len(promotion_row_keys)}")
print(f"direct-Cypher promotion error keys: {len(promotion_error_keys)}")
print(f"direct-Cypher promotion total: {len(promotion_row_keys) + len(promotion_error_keys)}")
print(f"direct-Cypher overlap failures: {len(overlap_failures)}")
print(f"direct-Cypher promotion failures: {len(promotion_failures)}")
if args.show_failures > 0:
print("overlap failures:")
for key, reason in overlap_failures[: args.show_failures]:
print(f"- {key}: {reason}")
print("promotion failures:")
for key, reason in promotion_failures[: args.show_failures]:
print(f"- {key}: {reason}")
if args.show_nonvalidation_debt:
details = _compute_direct_cypher_nonvalidation_details()
print(
"\n".join(
_render_direct_cypher_nonvalidation_details(
details,
limit=args.nonvalidation_limit,
)
)
)
if args.write:
_SUPPORT_FILE.write_text(
_render_direct_cypher_support(overlap_keys, promotion_row_keys, promotion_error_keys),
encoding="utf-8",
)
print(f"wrote: {_SUPPORT_FILE}")
if __name__ == "__main__":
main()