prograph 1.3.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. prograph/__init__.py +36 -0
  2. prograph/__main__.py +5 -0
  3. prograph/_version.py +375 -0
  4. prograph/cli/__init__.py +1 -0
  5. prograph/cli/_iri_utils.py +124 -0
  6. prograph/cli/emit_genie_space.py +107 -0
  7. prograph/cli/generate_views.py +140 -0
  8. prograph/cli/main.py +420 -0
  9. prograph/cli/schema_inferrer.py +746 -0
  10. prograph/cli/ttl_loader.py +289 -0
  11. prograph/cli/upload_genie_space.py +84 -0
  12. prograph/config/__init__.py +27 -0
  13. prograph/config/yaml_config.py +848 -0
  14. prograph/demos/__init__.py +14 -0
  15. prograph/demos/turbofan/README.md +74 -0
  16. prograph/demos/turbofan/SCENARIO.md +209 -0
  17. prograph/demos/turbofan/__init__.py +75 -0
  18. prograph/demos/turbofan/create_tables.sql +206 -0
  19. prograph/demos/turbofan/data/engine-instances.ttl +1123 -0
  20. prograph/demos/turbofan/data/sensor-timeseries.csv +90 -0
  21. prograph/demos/turbofan/demo.py +162 -0
  22. prograph/demos/turbofan/genie_qa_notebook.py +434 -0
  23. prograph/demos/turbofan/graph.yaml +1098 -0
  24. prograph/demos/turbofan/instance_graph_demo.py +32 -0
  25. prograph/demos/turbofan/load.py +223 -0
  26. prograph/demos/turbofan/mapping.r2rml.ttl +991 -0
  27. prograph/demos/turbofan/ontology/engine-maintenance.ttl +606 -0
  28. prograph/demos/turbofan/payload-shapes.ttl +33 -0
  29. prograph/demos/turbofan/queries/example-queries.sparql +215 -0
  30. prograph/demos/turbofan/shapes.ttl +75 -0
  31. prograph/demos/turbofan/tables.py +106 -0
  32. prograph/demos/turbofan/turbofan_schema.py +393 -0
  33. prograph/demos/turbofan/validation_demo.py +153 -0
  34. prograph/describe.py +231 -0
  35. prograph/endpoint/__init__.py +12 -0
  36. prograph/endpoint/app.py +722 -0
  37. prograph/endpoint/cli.py +280 -0
  38. prograph/endpoint/credentials.py +240 -0
  39. prograph/endpoint/engines.py +418 -0
  40. prograph/endpoint/negotiate.py +101 -0
  41. prograph/endpoint/results.py +181 -0
  42. prograph/endpoint/service.py +31 -0
  43. prograph/engine.py +2718 -0
  44. prograph/execution.py +455 -0
  45. prograph/executors/__init__.py +6 -0
  46. prograph/executors/base.py +11 -0
  47. prograph/executors/databricks_sql_executor.py +51 -0
  48. prograph/executors/duckdb_executor.py +24 -0
  49. prograph/executors/mysql_executor.py +20 -0
  50. prograph/executors/oracle_executor.py +24 -0
  51. prograph/executors/pandas_executor.py +72 -0
  52. prograph/executors/postgresql_executor.py +35 -0
  53. prograph/executors/spark_executor.py +88 -0
  54. prograph/executors/sqlserver_executor.py +35 -0
  55. prograph/extras/__init__.py +1 -0
  56. prograph/extras/graphql/__init__.py +33 -0
  57. prograph/extras/graphql/execute.py +25 -0
  58. prograph/extras/graphql/schema.py +314 -0
  59. prograph/extras/instance_graph.py +559 -0
  60. prograph/genie/__init__.py +83 -0
  61. prograph/genie/databricks_uploader.py +306 -0
  62. prograph/genie/deploy.py +393 -0
  63. prograph/genie/formats.py +414 -0
  64. prograph/genie/instruction_generator.py +375 -0
  65. prograph/genie/sample_query_generator.py +256 -0
  66. prograph/genie/shacl_views.py +318 -0
  67. prograph/genie/space_emitter.py +329 -0
  68. prograph/genie/view_generator.py +628 -0
  69. prograph/genie/view_writer.py +172 -0
  70. prograph/graph/__init__.py +1 -0
  71. prograph/graph/ansi_session.py +348 -0
  72. prograph/graph/databricks_sql_graph.py +300 -0
  73. prograph/graph/drivers.py +143 -0
  74. prograph/graph/duckdb_graph.py +68 -0
  75. prograph/graph/mysql_graph.py +38 -0
  76. prograph/graph/oracle_graph.py +285 -0
  77. prograph/graph/pandas_graph.py +343 -0
  78. prograph/graph/postgresql_graph.py +644 -0
  79. prograph/graph/spark_graph.py +556 -0
  80. prograph/graph/sql_graph.py +83 -0
  81. prograph/graph/sqlserver_graph.py +72 -0
  82. prograph/hints.py +95 -0
  83. prograph/logging.py +550 -0
  84. prograph/mcp/__init__.py +29 -0
  85. prograph/mcp/__main__.py +180 -0
  86. prograph/mcp/bootstrap.py +98 -0
  87. prograph/mcp/server.py +601 -0
  88. prograph/mcp/sparql.py +397 -0
  89. prograph/mcp/tools.py +504 -0
  90. prograph/nlp/__init__.py +79 -0
  91. prograph/nlp/client.py +305 -0
  92. prograph/nlp/databricks_tools.py +369 -0
  93. prograph/nlp/graph_context.py +313 -0
  94. prograph/nlp/interface.py +652 -0
  95. prograph/nlp/prompts/__init__.py +19 -0
  96. prograph/nlp/prompts/examples.py +258 -0
  97. prograph/nlp/prompts/system.py +239 -0
  98. prograph/nlp/providers/__init__.py +14 -0
  99. prograph/nlp/providers/anthropic.py +274 -0
  100. prograph/nlp/providers/databricks.py +312 -0
  101. prograph/nlp/providers/openai.py +203 -0
  102. prograph/nlp/schema_context.py +78 -0
  103. prograph/ontology/__init__.py +53 -0
  104. prograph/ontology/alignment.py +542 -0
  105. prograph/ontology/hierarchy.py +310 -0
  106. prograph/ontology/imports.py +587 -0
  107. prograph/ontology/layer.py +991 -0
  108. prograph/ontology/skos.py +61 -0
  109. prograph/owl/__init__.py +27 -0
  110. prograph/owl/defined_classes.py +323 -0
  111. prograph/parameters.py +242 -0
  112. prograph/parser/__init__.py +1 -0
  113. prograph/parser/cypher/CypherLexer.py +836 -0
  114. prograph/parser/cypher/CypherListener.py +966 -0
  115. prograph/parser/cypher/CypherParser.py +10854 -0
  116. prograph/parser/cypher/CypherVisitor.py +543 -0
  117. prograph/parser/cypher/__init__.py +1 -0
  118. prograph/parser/sparql/SparqlLexer.py +801 -0
  119. prograph/parser/sparql/SparqlParser.py +10146 -0
  120. prograph/parser/sparql/SparqlParserListener.py +1056 -0
  121. prograph/parser/sparql/SparqlParserVisitor.py +593 -0
  122. prograph/parser/sparql/__init__.py +8 -0
  123. prograph/plan/__init__.py +3 -0
  124. prograph/plan/ir/__init__.py +1 -0
  125. prograph/plan/ir/casts.py +338 -0
  126. prograph/plan/ir/consistency.py +397 -0
  127. prograph/plan/ir/construct.py +90 -0
  128. prograph/plan/ir/cost.py +234 -0
  129. prograph/plan/ir/cypher_answer.py +1081 -0
  130. prograph/plan/ir/cypher_apoc.py +717 -0
  131. prograph/plan/ir/cypher_apoc_convert.py +505 -0
  132. prograph/plan/ir/cypher_apoc_date.py +845 -0
  133. prograph/plan/ir/cypher_apoc_map.py +496 -0
  134. prograph/plan/ir/cypher_apoc_meta.py +384 -0
  135. prograph/plan/ir/cypher_apoc_path.py +597 -0
  136. prograph/plan/ir/cypher_apoc_procedures.py +163 -0
  137. prograph/plan/ir/cypher_apoc_text.py +785 -0
  138. prograph/plan/ir/cypher_front.py +5474 -0
  139. prograph/plan/ir/cypher_functions.py +565 -0
  140. prograph/plan/ir/cypher_lists.py +935 -0
  141. prograph/plan/ir/cypher_maps.py +370 -0
  142. prograph/plan/ir/cypher_mixed.py +604 -0
  143. prograph/plan/ir/cypher_ontology.py +226 -0
  144. prograph/plan/ir/cypher_procedures.py +191 -0
  145. prograph/plan/ir/cypher_runtime_errors.py +107 -0
  146. prograph/plan/ir/cypher_temporal.py +1357 -0
  147. prograph/plan/ir/cypher_temporal_arith.py +339 -0
  148. prograph/plan/ir/cypher_values.py +546 -0
  149. prograph/plan/ir/cypher_write.py +612 -0
  150. prograph/plan/ir/dataset.py +643 -0
  151. prograph/plan/ir/dates.py +102 -0
  152. prograph/plan/ir/entry.py +992 -0
  153. prograph/plan/ir/expr.py +260 -0
  154. prograph/plan/ir/facts.py +1171 -0
  155. prograph/plan/ir/fresh.py +291 -0
  156. prograph/plan/ir/freshness.py +625 -0
  157. prograph/plan/ir/identity.py +797 -0
  158. prograph/plan/ir/java_datetime.py +265 -0
  159. prograph/plan/ir/java_regex.py +391 -0
  160. prograph/plan/ir/join_order.py +39 -0
  161. prograph/plan/ir/key_checks.py +433 -0
  162. prograph/plan/ir/keys.py +454 -0
  163. prograph/plan/ir/lower.py +5323 -0
  164. prograph/plan/ir/ops.py +619 -0
  165. prograph/plan/ir/owl_rl.py +1119 -0
  166. prograph/plan/ir/owl_rl_rules.py +725 -0
  167. prograph/plan/ir/profiles.py +694 -0
  168. prograph/plan/ir/ql.py +987 -0
  169. prograph/plan/ir/rdfs.py +342 -0
  170. prograph/plan/ir/rewrite.py +1782 -0
  171. prograph/plan/ir/saturate.py +339 -0
  172. prograph/plan/ir/semantics.py +103 -0
  173. prograph/plan/ir/slot.py +509 -0
  174. prograph/plan/ir/sparql_front.py +3418 -0
  175. prograph/plan/ir/strings.py +534 -0
  176. prograph/plan/ir/term_functions.py +274 -0
  177. prograph/plan/ir/terms.py +84 -0
  178. prograph/plan/ir/xpath_regex.py +264 -0
  179. prograph/plan/run_record.py +65 -0
  180. prograph/plan/sql/__init__.py +21 -0
  181. prograph/plan/sql/dialect.py +3726 -0
  182. prograph/plan/sql/mysql_serializer.py +103 -0
  183. prograph/plan/sql/names.py +65 -0
  184. prograph/plan/sql/oracle_serializer.py +306 -0
  185. prograph/plan/sql/pg_serializer.py +501 -0
  186. prograph/plan/sql/rel_algebra.py +781 -0
  187. prograph/plan/sql/sql_serializer.py +1419 -0
  188. prograph/plan/sql/sqlserver_serializer.py +603 -0
  189. prograph/provenance.py +222 -0
  190. prograph/r2rml/__init__.py +48 -0
  191. prograph/r2rml/catalog.py +259 -0
  192. prograph/r2rml/compile.py +372 -0
  193. prograph/r2rml/direct.py +140 -0
  194. prograph/r2rml/export.py +495 -0
  195. prograph/r2rml/model.py +180 -0
  196. prograph/r2rml/parser.py +546 -0
  197. prograph/r2rml/schema.py +120 -0
  198. prograph/read_only.py +122 -0
  199. prograph/results.py +157 -0
  200. prograph/rules/__init__.py +168 -0
  201. prograph/rules/__main__.py +32 -0
  202. prograph/schema/__init__.py +60 -0
  203. prograph/schema/change_set.py +50 -0
  204. prograph/schema/collations.py +198 -0
  205. prograph/schema/compile_schema.py +388 -0
  206. prograph/schema/context.py +845 -0
  207. prograph/schema/examples.py +234 -0
  208. prograph/schema/iri.py +817 -0
  209. prograph/schema/literals.py +94 -0
  210. prograph/schema/lpg_view.py +617 -0
  211. prograph/schema/mapping.py +1803 -0
  212. prograph/schema/maps_schema.py +40 -0
  213. prograph/schema/naming.py +77 -0
  214. prograph/schema/query_sources.py +59 -0
  215. prograph/schema/rows.py +215 -0
  216. prograph/schema/source_map.py +1103 -0
  217. prograph/schema/store_view.py +459 -0
  218. prograph/schema/types.py +569 -0
  219. prograph/schema/xsd.py +569 -0
  220. prograph/shacl/__init__.py +30 -0
  221. prograph/shacl/dqv.py +58 -0
  222. prograph/shacl/generate.py +255 -0
  223. prograph/shacl/report.py +138 -0
  224. prograph/shacl/validator.py +164 -0
  225. prograph/store.py +1737 -0
  226. prograph/utils/__init__.py +6 -0
  227. prograph/utils/exceptions.py +308 -0
  228. prograph/utils/helpers.py +126 -0
  229. prograph/warnings.py +46 -0
  230. prograph/writes.py +369 -0
  231. prograph-1.3.3.dist-info/METADATA +281 -0
  232. prograph-1.3.3.dist-info/RECORD +237 -0
  233. prograph-1.3.3.dist-info/WHEEL +5 -0
  234. prograph-1.3.3.dist-info/entry_points.txt +3 -0
  235. prograph-1.3.3.dist-info/licenses/LICENSE +201 -0
  236. prograph-1.3.3.dist-info/licenses/NOTICE +10 -0
  237. prograph-1.3.3.dist-info/top_level.txt +1 -0
@@ -0,0 +1,597 @@
1
+ """apoc.path.* over the closure and trail homes (rule cypher-apoc-procedures; design 261): subgraphNodes,
2
+ subgraphAll, expand, expandConfig and spanningTree, from a node variable an earlier clause bound.
3
+
4
+ Each is the plan of one traversal over a step relation: the union, over the relationship filter's types
5
+ and directions, of the maps' relationship scans (`scan_rel`, `rule cypher-var-length`'s step), each step
6
+ kept only where its end node passes the label filter. What differs is the uniqueness APOC gives it:
7
+ - subgraphNodes, subgraphAll (NODE_GLOBAL): the nodes at first-reached depth minLevel..maxLevel. Bounded
8
+ by maxLevel, the nodes within k steps by k joins of the step (walks reach what shortest paths do);
9
+ with none, the SPARQL closure (`Closure`, rule closure) seeded with the start nodes. minLevel m
10
+ leaves out the nodes within m-1 steps.
11
+ - expand, expandConfig (RELATIONSHIP_PATH): Cypher's trails of minLevel..maxLevel steps, as
12
+ `_Translator.trails` writes the bounded form (rule cypher-var-length), each a path value.
13
+ - spanningTree (NODE_GLOBAL, BFS): one path per node reached, the breadth-first frontier expanded a level
14
+ at a time, a node kept by the first path to it.
15
+
16
+ Departures from APOC (docs/reference/apoc-functions.md):
17
+ - Where breadth-first search finds a node by several shortest paths APOC takes the first in storage order,
18
+ which no query can reproduce; ProGraph takes, per node, the parent of least identity text (a shorter text
19
+ first), then the relationship of least identity. Compare to APOC on node sets, or where the path is unique.
20
+ - A maxLevel of -1 or none runs unbounded in subgraphNodes and subgraphAll (the closure's guard), and in
21
+ expand and expandConfig as the Cypher trails closure carrying the path's values (`_unbounded_trails`,
22
+ followed to path_depth, a longer trail raises); spanningTree as `_unbounded_tree` (depths from the shortest
23
+ closure, one parent each, the paths from the trails closure over the parent edges).
24
+ - limit keeps the first N nodes (or paths) by depth, then node identity; APOC keeps the first it finds.
25
+ - A config key outside relationshipFilter, labelFilter, minLevel, maxLevel, limit (and uniqueness
26
+ RELATIONSHIP_PATH, bfs true) is refused by name; the filters' `/` and `>` label forms likewise.
27
+ """
28
+
29
+ from __future__ import annotations
30
+
31
+ from typing import Any, Dict, List, Optional, Sequence, Tuple
32
+
33
+ from prograph.plan.ir import expr as E
34
+ from prograph.plan.ir import ops as O
35
+ from prograph.plan.ir.cypher_apoc_meta import _list, _reading
36
+ from prograph.plan.ir.cypher_apoc_procedures import ApocProcedureError, Procedure, literal, register
37
+
38
+ LINEAR_LEVELS = 32
39
+ """The most levels of a bounded subgraphNodes, subgraphAll or spanningTree: each is one more join."""
40
+ DEPTH_LEVELS = 8
41
+ """The depths an unbounded walk's `limit` tells apart (a node deeper comes after them, by identity)."""
42
+ TRAIL_LEVELS = 12
43
+ """The most levels of expand's trails: a level of k steps is k joins, and each level is a branch."""
44
+
45
+ _CONFIG = {"relationshipFilter", "labelFilter", "minLevel", "maxLevel", "limit", "uniqueness", "bfs"}
46
+ _REFUSED_KEYS = {
47
+ "sequence": "a relationship and label sequence",
48
+ "whitelistNodes": "a node whitelist", "blacklistNodes": "a node blacklist", "endNodes": "end nodes",
49
+ "terminatorNodes": "terminator nodes", "filterStartNode": "filtering the start node",
50
+ "beginSequenceAtStart": "a sequence from the start node", "optional": "an optional expansion"}
51
+
52
+
53
+ # -- the configuration ----------------------------------------------------------------------------------
54
+
55
+ class _Config:
56
+ """A call's configuration: its filters worked out, every level an integer, uniqueness the procedure's own."""
57
+
58
+ def __init__(self, name: str, config: Dict[str, Any], unique: str) -> None:
59
+ self.name = name
60
+ for key in config:
61
+ if key in _REFUSED_KEYS:
62
+ raise ApocProcedureError(name, f"the config key {key} ({_REFUSED_KEYS[key]}) is not built")
63
+ if key not in _CONFIG:
64
+ raise ApocProcedureError(name, f"the config key {key} is not built")
65
+ if config.get("bfs") not in (None, True):
66
+ raise ApocProcedureError(name, "bfs: false (a depth-first expansion) is not built")
67
+ given = config.get("uniqueness")
68
+ if given is not None and given != unique:
69
+ raise ApocProcedureError(name, f"uniqueness {given} is not built (this procedure's is {unique})")
70
+ self.entries = _relationship_filter(name, config.get("relationshipFilter"))
71
+ self.white, self.black = _label_filter(name, config.get("labelFilter"))
72
+ self.min = _level(name, "minLevel", config.get("minLevel"), 0)
73
+ if unique == "NODE_GLOBAL" and self.min not in (0, 1):
74
+ raise ApocProcedureError(name, f"minLevel can only be 0 or 1 in {name.split('.')[-1]}() (as APOC raises)")
75
+ self.max = _level(name, "maxLevel", config.get("maxLevel"), -1)
76
+ self.max = None if self.max < 0 else self.max
77
+ self.limit = _level(name, "limit", config.get("limit"), -1)
78
+ self.limit = None if self.limit < 0 else self.limit
79
+ if self.max is not None and self.max < self.min:
80
+ self.max = self.min - 1 # no path of such a length
81
+
82
+
83
+ def _level(name: str, key: str, value: Any, default: int) -> int:
84
+ if value is None:
85
+ return default
86
+ if isinstance(value, bool) or not isinstance(value, int):
87
+ raise ApocProcedureError(name, f"{key} must be an integer, given {value!r}")
88
+ return value
89
+
90
+
91
+ def _relationship_filter(name: str, text: Any) -> List[Tuple[Optional[str], str]]:
92
+ """(type or None for any, direction) per entry of a relationshipFilter such as `KNOWS>|<LIKES|FRIEND`: a
93
+ trailing `>` is outgoing, a leading `<` incoming, none either way. None or empty: every relationship, either way."""
94
+ if text is None or text == "":
95
+ return [(None, "-")]
96
+ if not isinstance(text, str):
97
+ raise ApocProcedureError(name, f"relationshipFilter must be a string, given {text!r}")
98
+ ways: Dict[Optional[str], set] = {}
99
+ for token in text.split("|"):
100
+ token = token.strip()
101
+ out, into = token.endswith(">"), token.startswith("<")
102
+ word = token[1 if into else 0:len(token) - (1 if out else 0)].strip().strip("`")
103
+ ways.setdefault(word or None, set()).add("-" if out == into else "->" if out else "<-")
104
+ if None in ways and len(ways) > 1:
105
+ raise ApocProcedureError(name, "a relationshipFilter of a direction with no type beside typed entries "
106
+ "is not built")
107
+ return [(word, next(iter(d)) if len(d) == 1 else "-") for word, d in ways.items()]
108
+
109
+
110
+ def _label_filter(name: str, text: Any) -> Tuple[List[str], List[str]]:
111
+ """(whitelist, blacklist) of a labelFilter such as `+Person|Movie|-Admin`: a node passes if it has one of
112
+ the whitelist (when there is one) and none of the blacklist. `/` (end node) and `>` (termination) are refused."""
113
+ white: List[str] = []
114
+ black: List[str] = []
115
+ if text is None or text == "":
116
+ return white, black
117
+ if not isinstance(text, str):
118
+ raise ApocProcedureError(name, f"labelFilter must be a string, given {text!r}")
119
+ for token in text.split("|"):
120
+ token = token.strip()
121
+ if token[:1] in ("/", ">"):
122
+ raise ApocProcedureError(name, f"the labelFilter form {token[:1]}Label "
123
+ f"({'an end node' if token[0] == '/' else 'a termination node'}) is not built")
124
+ if token[:1] == "-":
125
+ black.append(token[1:].strip().strip("`"))
126
+ else:
127
+ white.append(token.lstrip("+").strip().strip("`"))
128
+ return [w for w in white if w], [b for b in black if b]
129
+
130
+
131
+ # -- the step relation ----------------------------------------------------------------------------------
132
+
133
+ def _nodes_of(t: Any, labels: Sequence[str], var: str) -> O.Op:
134
+ from prograph.plan.ir.cypher_front import _Node
135
+ scans = [O.Project(t.scan(_Node(var, (label,), {})), (var,)) for label in labels]
136
+ return O.Distinct(scans[0] if len(scans) == 1 else O.Union(tuple(scans)))
137
+
138
+
139
+ def _step(t: Any, hv: str, config: _Config, reading: bool = True) -> O.Op:
140
+ """The steps of the traversal as `<hv>.__left`, `<hv>.__right` (in the direction walked) and
141
+ `<hv>.__id`, each whose end passes the label filter; the relationship's value columns too where the
142
+ translator reads whole elements. `reading`: only what a count needs is read."""
143
+ from prograph.plan.ir.cypher_front import _Rel
144
+ from contextlib import nullcontext
145
+ with (_reading(t, ()) if reading else nullcontext()):
146
+ branches = []
147
+ for word, direction in config.entries:
148
+ scanned = t.scan_rel(_Rel(hv, (word,) if word else (), {}, "", "", direction), identity=True)
149
+ if isinstance(scanned, O.Values) and not scanned.rows:
150
+ continue
151
+ branches.append(scanned)
152
+ if not branches:
153
+ return O.Values((f"{hv}.__left", f"{hv}.__right", f"{hv}.__id"), ())
154
+ step: O.Op = branches[0] if len(branches) == 1 else O.Union(tuple(branches))
155
+ right = f"{hv}.__right"
156
+ if config.white:
157
+ node = t.fresh("w")
158
+ step = O.SemiJoin(step, _nodes_of(t, config.white, node), E.Compare("=", E.Col(right), E.Col(node), E.TERM))
159
+ if config.black:
160
+ node = t.fresh("w")
161
+ step = O.AntiJoin(step, _nodes_of(t, config.black, node), E.Compare("=", E.Col(right), E.Col(node), E.TERM))
162
+ return step
163
+
164
+
165
+ def _none(step: O.Op) -> bool:
166
+ """Whether the step relation has no rows by construction (no map gives the relationship types asked)."""
167
+ return isinstance(step, O.Values) and not step.rows
168
+
169
+
170
+ # -- what a call needs of the translator ----------------------------------------------------------------
171
+
172
+ def _start(t: Any, name: str, args: List[E.Expr], plan: O.Op) -> str:
173
+ """The node variable a path starts from: bound by an earlier clause."""
174
+ e = args[0]
175
+ if isinstance(e, E.Col) and e.name in t.nodes and not isinstance(plan, O.Unit):
176
+ return e.name
177
+ raise ApocProcedureError(name, "the start must be a node variable an earlier clause binds (a MATCH or "
178
+ "WITH before the CALL); a list of nodes, an id or a value is not built")
179
+
180
+
181
+ def _config_of(name: str, args: List[E.Expr], unique: str) -> _Config:
182
+ given = literal(args[1], name, "the config map") if len(args) > 1 else {}
183
+ if given is None:
184
+ given = {}
185
+ if not isinstance(given, dict):
186
+ raise ApocProcedureError(name, "the config must be a map written in the query")
187
+ return _Config(name, given, unique)
188
+
189
+
190
+ def _seeds(plan: O.Op, start: str) -> O.Op:
191
+ return O.Distinct(O.Project(O.Extend(plan, "#a", E.Col(start)), ("#a",)))
192
+
193
+
194
+ def _attach(t: Any, plan: O.Op, rows: O.Op, start: str, wanted: Dict[str, str], config: _Config,
195
+ order: Sequence[str]) -> O.Op:
196
+ """`rows` (each a start in `#a` and what a call yields, in columns) joined to `plan` where the start is
197
+ that node, at most `limit` of a start's rows (`order` decides which), each yielded variable (`wanted`:
198
+ column -> variable) a column of the result."""
199
+ if config.limit is not None:
200
+ rows = O.Ordinal(rows, "#rank", ("#a",), tuple(order))
201
+ rows = O.Project(O.Filter(rows, E.Compare("<=", E.Col("#rank"), E.Const(config.limit))),
202
+ tuple(c for c in rows.columns if c != "#rank"))
203
+ joined: O.Op = O.Join(plan, rows, E.Compare("=", E.Col(start), E.Col("#a"), E.TERM))
204
+ for column, var in wanted.items():
205
+ if column != var:
206
+ joined = O.Extend(joined, var, E.Col(column))
207
+ return O.Project(joined, tuple(dict.fromkeys(plan.columns + tuple(wanted.values()))))
208
+
209
+
210
+ def _node_vars(t: Any, plan: O.Op, var_of: Dict[str, str], output: str, column: str) -> O.Op:
211
+ """The yielded node variable `var_of[output]`: a node of the mapping, scanned and met on its identity."""
212
+ from prograph.plan.ir.cypher_front import _Node
213
+ var = var_of[output]
214
+ t.nodes[var] = _Node(var, (), {})
215
+ return O.Join(O.Extend(plan, var, E.Col(column)), t.scan(t.nodes[var]))
216
+
217
+
218
+ # -- reach: the nodes within k steps ---------------------------------------------------------------------
219
+
220
+ def _reach(t: Any, seeds: O.Op, config: _Config, levels: Optional[int], leveled: bool = False) -> O.Op:
221
+ """(`#a`, `#n`): each seed with the nodes within `levels` steps of it (its own included); with no bound,
222
+ every node the closure reaches. `levels` below 0 is none."""
223
+ if levels is not None and levels < 0:
224
+ return O.Values(("#a", "#n"), ())
225
+ zero = O.Project(O.Extend(seeds, "#n", E.Col("#a")), ("#a", "#n"))
226
+ if levels == 0:
227
+ return O.Extend(zero, "#l", E.Const(0)) if leveled else zero
228
+ if levels is None:
229
+ hv = t.fresh("s")
230
+ step = _step(t, hv, config)
231
+ if _none(step):
232
+ return zero
233
+ left, right = f"{hv}.__left", f"{hv}.__right"
234
+ walk = O.Closure(O.Project(step, (left, right)), start=left, end=right, mode=O.SPARQL, min=1, max=None,
235
+ seed=("start", seeds, "#a"))
236
+ reached = O.Project(O.Join(seeds, walk, E.Compare("=", E.Col("#a"), E.Col(left), E.TERM)), ("#a", right))
237
+ return O.Distinct(O.Union((zero, O.Project(O.Extend(reached, "#n", E.Col(right)), ("#a", "#n")))))
238
+ pairs, current = [O.Extend(zero, "#l", E.Const(0)) if leveled else zero], zero
239
+ last = "#n"
240
+ for j in range(1, levels + 1):
241
+ hv = t.fresh("s")
242
+ step = _step(t, hv, config)
243
+ if _none(step):
244
+ break
245
+ right = f"{hv}.__right"
246
+ current = O.Distinct(O.Project(O.Join(current, step, E.Compare("=", E.Col(last), E.Col(f"{hv}.__left"), E.TERM)),
247
+ ("#a", right)))
248
+ level = O.Extend(current, "#n", E.Col(right))
249
+ pairs.append(O.Project(O.Extend(level, "#l", E.Const(j)), ("#a", "#n", "#l")) if leveled
250
+ else O.Project(level, ("#a", "#n")))
251
+ last = right
252
+ return O.Union(tuple(pairs)) if leveled else O.Distinct(O.Union(tuple(pairs)))
253
+
254
+
255
+ def _subgraph_reached(t: Any, name: str, seeds: O.Op, config: _Config) -> O.Op:
256
+ """The nodes first reached at depth minLevel..maxLevel (NODE_GLOBAL): within max steps, but not within min-1."""
257
+ if config.max is not None and config.max > LINEAR_LEVELS:
258
+ raise ApocProcedureError(name, f"a maxLevel above {LINEAR_LEVELS} is not built (each level is a join); "
259
+ "omit maxLevel to walk the whole subgraph")
260
+ reached = _reach(t, seeds, config, config.max)
261
+ if config.min >= 1:
262
+ reached = O.AntiJoin(reached, _reach(t, seeds, config, min(config.min - 1, LINEAR_LEVELS)))
263
+ if config.limit is not None:
264
+ # `#l`, each node's depth, for the limit's order (past DEPTH_LEVELS an unbounded walk's depth is unknown).
265
+ top = DEPTH_LEVELS if config.max is None else config.max
266
+ depth = O.Aggregate(_reach(t, seeds, config, top, leveled=True), ("#a", "#n"), (("#l", E.Agg("MIN", E.Col("#l"), semantics=E.CYPHER)),))
267
+ reached = (O.LeftJoin if config.max is None else O.Join)(reached, depth)
268
+ return reached
269
+
270
+
271
+ def _subgraph_nodes(t: Any, args: List[E.Expr], plan: O.Op, var_of: Dict[str, str]) -> O.Op:
272
+ from prograph.plan.ir.cypher_front import _Node
273
+ name = "apoc.path.subgraphNodes"
274
+ start, config = _start(t, name, args, plan), _config_of(name, args, "NODE_GLOBAL")
275
+ rows = _subgraph_reached(t, name, _seeds(plan, start), config)
276
+ var = var_of["node"]
277
+ out = _attach(t, plan, rows, start, {"#n": var}, config, ("#l", "#n"))
278
+ t.nodes[var] = _Node(var, (), {}) # a node of the mapping: met on its identity (a ghost end is no node)
279
+ return O.Join(out, t.scan(t.nodes[var]))
280
+
281
+
282
+ def _subgraph_all(t: Any, args: List[E.Expr], plan: O.Op, var_of: Dict[str, str]) -> O.Op:
283
+ from prograph.plan.ir.cypher_front import _Node, _Rel, _WholeNode
284
+ name = "apoc.path.subgraphAll"
285
+ start, config = _start(t, name, args, plan), _config_of(name, args, "NODE_GLOBAL")
286
+ if not t.whole:
287
+ raise _WholeNode() # a list of whole nodes and relationships
288
+ seeds = _seeds(plan, start)
289
+ reached = _subgraph_reached(t, name, seeds, config)
290
+ if config.limit is not None:
291
+ reached = O.Ordinal(reached, "#rank", ("#a",), ("#l", "#n"))
292
+ reached = O.Project(O.Filter(reached, E.Compare("<=", E.Col("#rank"), E.Const(config.limit))),
293
+ ("#a", "#n"))
294
+ # The nodes: each reached node as its value, collected for its start.
295
+ nv = t.fresh("n")
296
+ node_rows = O.Join(O.Extend(reached, nv, E.Col("#n")), t.scan(_Node(nv, (), {})))
297
+ node_rows = O.Extend(node_rows, "#nv", t.node_value(nv))
298
+ node_sentinel = O.Project(O.Extend(seeds, "#nv", E.Const(None)), ("#a", "#nv"))
299
+ nodes = O.Aggregate(O.Union((O.Project(node_rows, ("#a", "#nv")), node_sentinel)), ("#a",),
300
+ (("#nodes", E.Agg("COLLECT", E.Col("#nv"), semantics=E.CYPHER)),))
301
+ # The relationships among them: every relationship, of any type, with both ends reached for the same
302
+ # start (APOC 5.26 does not filter them by the relationshipFilter).
303
+ parts: List[O.Op] = []
304
+ for word in [None]:
305
+ rv = t.fresh("r")
306
+ rels = t.scan_rel(_Rel(rv, (word,) if word else (), {}, "", "", "->"), identity=True)
307
+ if isinstance(rels, O.Values) and not rels.rows:
308
+ continue
309
+ r1, r2 = t.fresh("e"), t.fresh("e")
310
+ x = O.Join(rels, O.Project(O.Extend(reached, r1, E.Col("#n")), ("#a", r1)),
311
+ E.Compare("=", E.Col(f"{rv}.__left"), E.Col(r1), E.TERM))
312
+ far = O.Project(O.Extend(reached, r2, E.Col("#n")), ("#a", r2))
313
+ x = O.SemiJoin(x, far, E.Compare("=", E.Col(f"{rv}.__right"), E.Col(r2), E.TERM))
314
+ parts.append(O.Project(O.Extend(x, "#rv", t.rel_value(rv)), ("#a", "#rv")))
315
+ # A start with none still has a row: a null row per start, which COLLECT leaves out (an empty list).
316
+ sentinel = O.Project(O.Extend(seeds, "#rv", E.Const(None)), ("#a", "#rv"))
317
+ rel_rows = O.Union(tuple(parts) + (sentinel,))
318
+ relationships = O.Aggregate(rel_rows, ("#a",), (("#rels", E.Agg("COLLECT", E.Col("#rv"), semantics=E.CYPHER)),))
319
+ rows = O.Extend(O.Join(nodes, relationships), "#relationships", E.Col("#rels"))
320
+ wanted = {"#nodes": var_of["nodes"]} if "nodes" in var_of else {}
321
+ if "relationships" in var_of:
322
+ wanted["#relationships"] = var_of["relationships"]
323
+ t.values |= set(wanted.values())
324
+ return _attach(t, plan, rows, start, wanted, _Config(name, {}, "NODE_GLOBAL"), ())
325
+
326
+
327
+ # -- expand, expandConfig: trails -----------------------------------------------------------------------
328
+
329
+ def _path_var(t: Any, var_of: Dict[str, str], start: str, seg: str) -> str:
330
+ var = var_of["path"]
331
+ t.paths[var] = ([start, f"{seg}#end"], [("segment", seg)])
332
+ t.segments.add(seg)
333
+ return var
334
+
335
+
336
+ def _trails(t: Any, name: str, args: List[E.Expr], plan: O.Op, var_of: Dict[str, str], config: _Config) -> O.Op:
337
+ from prograph.plan.ir.cypher_front import _Node
338
+ start = _start(t, name, args, plan)
339
+ if config.max is None:
340
+ return _unbounded_trails(t, name, start, plan, var_of, config)
341
+ if config.max > TRAIL_LEVELS:
342
+ raise ApocProcedureError(name, f"a maxLevel above {TRAIL_LEVELS} is not built (a trail of k steps is "
343
+ "k joins); give a smaller maxLevel")
344
+ seg = t.fresh("p")
345
+ _path_var(t, var_of, start, seg)
346
+ extra = ("#lv", "#end") if config.limit is not None else ()
347
+ branches: List[O.Op] = []
348
+ for k in range(max(config.min, 0), config.max + 1):
349
+ x: O.Op = plan
350
+ steps: List[str] = []
351
+ nodes: List[E.Expr] = []
352
+ for i in range(1, k + 1):
353
+ hv = f"{seg}#{i}"
354
+ scanned = _step(t, hv, config, reading=False)
355
+ if _none(scanned):
356
+ x = None
357
+ break
358
+ prev = start if i == 1 else f"{steps[-1]}.__right"
359
+ conds: List[E.Expr] = [E.Compare("=", E.Col(prev), E.Col(f"{hv}.__left"), E.TERM)]
360
+ conds += [E.Not(E.Compare("=", E.Col(f"{s}.__id"), E.Col(f"{hv}.__id"), E.TERM)) for s in steps]
361
+ x = O.Join(x, scanned, E.And(tuple(conds)) if len(conds) > 1 else conds[0])
362
+ steps.append(hv)
363
+ if t.whole:
364
+ node = _Node(f"{seg}#n{i}", (), {})
365
+ x = O.Join(x, t.scan(node), E.Compare("=", E.Col(node.var), E.Col(f"{hv}.__right"), E.TERM))
366
+ nodes.append(t.node_value(node.var))
367
+ if x is None:
368
+ continue
369
+ columns = plan.columns
370
+ if t.whole:
371
+ # The zero-step trail's lists are typed empty element lists (a bare `[]` has no element type,
372
+ # which a branch alone, with no longer trail beside it, leaves unknown).
373
+ x = O.Extend(x, f"{seg}#seg_nodes", _list(nodes) if steps
374
+ else E.Call("EMPTY_ELEMENTS", (E.Const("nodes"),), E.CYPHER))
375
+ x = O.Extend(x, f"{seg}#seg_rels", _list([t.rel_value(s) for s in steps]) if steps
376
+ else E.Call("EMPTY_ELEMENTS", (E.Const("relationships"),), E.CYPHER))
377
+ columns += (f"{seg}#seg_nodes", f"{seg}#seg_rels")
378
+ if config.limit is not None:
379
+ x = O.Extend(x, "#lv", E.Const(k))
380
+ x = O.Extend(x, "#end", E.Col(f"{steps[-1]}.__right") if steps else E.Col(start))
381
+ columns += extra
382
+ branches.append(O.Project(x, columns))
383
+ if not branches:
384
+ return O.Filter(plan, E.Const(False))
385
+ out: O.Op = branches[0] if len(branches) == 1 else O.Union(tuple(branches))
386
+ if config.limit is not None:
387
+ keep = (start, "#lv", "#end")
388
+ out = O.Ordinal(out, "#rank", (start,), keep[1:])
389
+ out = O.Project(O.Filter(out, E.Compare("<=", E.Col("#rank"), E.Const(config.limit))),
390
+ tuple(c for c in out.columns if c not in ("#rank",) + extra))
391
+ return out
392
+
393
+
394
+ def _unbounded_trails(t: Any, name: str, start: str, plan: O.Op, var_of: Dict[str, str], config: _Config) -> O.Op:
395
+ """Trails of minLevel or more steps with no upper bound: the Cypher closure (rule cypher-trails) over the
396
+ step relation, each trail carrying its relationships' values and its end nodes' (the path's lists, built as
397
+ a bounded path's are), joined to the rows on the start. Seeded at the rows' starts; followed to `path_depth`,
398
+ a longer trail from one of them raises."""
399
+ from prograph.plan.ir.cypher_front import _Node
400
+ seg = t.fresh("p")
401
+ _path_var(t, var_of, start, seg)
402
+ seg_nodes, seg_rels = f"{seg}#seg_nodes", f"{seg}#seg_rels"
403
+ values = t.whole
404
+ columns = plan.columns + ((seg_nodes, seg_rels) if values else ())
405
+ extra = ("#lv", "#end") if config.limit is not None else ()
406
+ branches: List[O.Op] = []
407
+ if config.min <= 0: # the start alone: no relationship, no node after it
408
+ zero: O.Op = plan
409
+ if values:
410
+ zero = O.Extend(O.Extend(zero, seg_nodes, E.Call("EMPTY_ELEMENTS", (E.Const("nodes"),), E.CYPHER)),
411
+ seg_rels, E.Call("EMPTY_ELEMENTS", (E.Const("relationships"),), E.CYPHER))
412
+ if config.limit is not None:
413
+ zero = O.Extend(O.Extend(zero, "#lv", E.Const(0)), "#end", E.Col(start))
414
+ branches.append(O.Project(zero, columns + extra))
415
+ hv = f"{seg}#s"
416
+ step = _step(t, hv, config, reading=False)
417
+ if not _none(step):
418
+ left, right, ident = f"{hv}.__left", f"{hv}.__right", f"{hv}.__id"
419
+ carried: Tuple[str, ...] = ()
420
+ if values:
421
+ node = _Node(f"{hv}#n", (), {})
422
+ step = O.Join(step, t.scan(node), E.Compare("=", E.Col(node.var), E.Col(right), E.TERM))
423
+ step = O.Extend(O.Extend(step, seg_rels, t.rel_value(hv)), seg_nodes, t.node_value(node.var))
424
+ carried = (seg_rels, seg_nodes)
425
+ ids = f"{seg}#ids"
426
+ walks = O.Closure(O.Project(step, (left, right, ident) + carried), start=left, end=right, mode=O.CYPHER,
427
+ min=max(config.min, 1), max=None, edge=ident, path=ids, carry=carried,
428
+ seed=t._trail_seed(plan, start))
429
+ rows: O.Op = O.Join(plan, walks, E.Compare("=", E.Col(start), E.Col(left), E.TERM))
430
+ if config.limit is not None:
431
+ rows = O.Extend(O.Extend(rows, "#lv", E.Call("SIZE", (E.Col(ids),), E.CYPHER)), "#end", E.Col(right))
432
+ branches.append(O.Project(rows, columns + extra))
433
+ if not branches:
434
+ return O.Filter(plan, E.Const(False))
435
+ out: O.Op = branches[0] if len(branches) == 1 else O.Union(tuple(branches))
436
+ if config.limit is not None: # by depth, then end node (as the bounded form's)
437
+ out = O.Ordinal(out, "#rank", (start,), extra)
438
+ out = O.Project(O.Filter(out, E.Compare("<=", E.Col("#rank"), E.Const(config.limit))),
439
+ tuple(c for c in out.columns if c not in ("#rank",) + extra))
440
+ return out
441
+
442
+
443
+ def _expand_config(t: Any, args: List[E.Expr], plan: O.Op, var_of: Dict[str, str]) -> O.Op:
444
+ name = "apoc.path.expandConfig"
445
+ return _trails(t, name, args, plan, var_of, _config_of(name, args, "RELATIONSHIP_PATH"))
446
+
447
+
448
+ def _expand(t: Any, args: List[E.Expr], plan: O.Op, var_of: Dict[str, str]) -> O.Op:
449
+ name = "apoc.path.expand"
450
+ config = {"relationshipFilter": literal(args[1], name, "the relationshipFilter"),
451
+ "labelFilter": literal(args[2], name, "the labelFilter"),
452
+ "minLevel": literal(args[3], name, "minLevel"), "maxLevel": literal(args[4], name, "maxLevel")}
453
+ return _trails(t, name, args, plan, var_of, _Config(name, {k: v for k, v in config.items() if v is not None},
454
+ "RELATIONSHIP_PATH"))
455
+
456
+
457
+ # -- spanningTree: one shortest path per node ------------------------------------------------------------
458
+
459
+ def _spanning_tree(t: Any, args: List[E.Expr], plan: O.Op, var_of: Dict[str, str]) -> O.Op:
460
+ """The breadth-first frontier a level at a time: (`#a` start, `#c` node, its path's nodes and relationships,
461
+ `#rk` its rank among the start's frontier, by identity); a level's candidates are the steps from the frontier to
462
+ a node no earlier level reached, one kept per node (the least by parent's identity, then relationship
463
+ identity). With no maxLevel: `_unbounded_tree`."""
464
+ from prograph.plan.ir.cypher_front import _Node
465
+ name = "apoc.path.spanningTree"
466
+ start, config = _start(t, name, args, plan), _config_of(name, args, "NODE_GLOBAL")
467
+ if config.max is None:
468
+ return _unbounded_tree(t, start, plan, var_of, config)
469
+ if config.max > LINEAR_LEVELS:
470
+ raise ApocProcedureError(name, f"a maxLevel above {LINEAR_LEVELS} is not built (each level is a join)")
471
+ seg = t.fresh("p")
472
+ _path_var(t, var_of, start, seg)
473
+ whole = t.whole
474
+ cols = ("#a", "#c", "#rk") + (("#nl", "#rl") if whole else ())
475
+ frontier: O.Op = O.Ordinal(O.Extend(_seeds(plan, start), "#c", E.Col("#a")), "#rk", ("#a",), ("#a",))
476
+ if whole:
477
+ frontier = O.Extend(O.Extend(frontier, "#nl", E.Call("EMPTY_ELEMENTS", (E.Const("nodes"),), E.CYPHER)),
478
+ "#rl", E.Call("EMPTY_ELEMENTS", (E.Const("relationships"),), E.CYPHER))
479
+ levels: List[Tuple[int, O.Op]] = [(0, frontier)]
480
+ seen: List[O.Op] = [O.Project(O.Extend(frontier, "#b", E.Col("#c")), ("#a", "#b"))]
481
+ for k in range(1, config.max + 1):
482
+ hv = f"{seg}#{k}"
483
+ step = _step(t, hv, config, reading=False)
484
+ if _none(step):
485
+ break
486
+ right = f"{hv}.__right"
487
+ cand = O.Join(frontier, step, E.Compare("=", E.Col("#c"), E.Col(f"{hv}.__left"), E.TERM))
488
+ cand = O.Extend(cand, "#b", E.Col(right))
489
+ cand = O.AntiJoin(cand, O.Distinct(seen[0] if len(seen) == 1 else O.Union(tuple(seen))))
490
+ cand = O.Ordinal(cand, "#pick", ("#a", "#b"), ("#c", f"{hv}.__id"))
491
+ cand = O.Filter(cand, E.Compare("=", E.Col("#pick"), E.Const(1)))
492
+ nxt = O.Extend(cand, "#c2", E.Col("#b"))
493
+ if whole:
494
+ node = _Node(f"{seg}#n{k}", (), {})
495
+ nxt = O.Join(nxt, t.scan(node), E.Compare("=", E.Col(node.var), E.Col("#b"), E.TERM))
496
+ here, rel = _list([t.node_value(node.var)]), _list([t.rel_value(hv)])
497
+ # The first level's lists are one element; later ones extend the parent's (a level-0 list is empty).
498
+ nxt = O.Extend(nxt, "#nl2", here if k == 1 else E.Call("+", (E.Col("#nl"), here), E.CYPHER))
499
+ nxt = O.Extend(nxt, "#rl2", rel if k == 1 else E.Call("+", (E.Col("#rl"), rel), E.CYPHER))
500
+ nxt = O.Ordinal(nxt, "#rk2", ("#a",), ("#b",))
501
+ nxt = O.Project(nxt, ("#a", "#c2", "#rk2") + (("#nl2", "#rl2") if whole else ()))
502
+ renamed = O.Extend(O.Extend(nxt, "#c", E.Col("#c2")), "#rk", E.Col("#rk2"))
503
+ if whole:
504
+ renamed = O.Extend(O.Extend(renamed, "#nl", E.Col("#nl2")), "#rl", E.Col("#rl2"))
505
+ frontier = O.Project(renamed, cols)
506
+ levels.append((k, frontier))
507
+ seen.append(O.Project(O.Extend(frontier, "#b", E.Col("#c")), ("#a", "#b")))
508
+ branches = []
509
+ for k, f in levels:
510
+ if k < config.min:
511
+ continue
512
+ branches.append(O.Project(O.Extend(f, "#lv", E.Const(k)), cols + ("#lv",)))
513
+ if not branches:
514
+ return O.Filter(plan, E.Const(False))
515
+ rows: O.Op = branches[0] if len(branches) == 1 else O.Union(tuple(branches))
516
+ wanted: Dict[str, str] = {}
517
+ if whole:
518
+ rows = O.Extend(O.Extend(rows, f"{seg}#seg_nodes", E.Col("#nl")), f"{seg}#seg_rels", E.Col("#rl"))
519
+ wanted = {f"{seg}#seg_nodes": f"{seg}#seg_nodes", f"{seg}#seg_rels": f"{seg}#seg_rels"}
520
+ return _attach(t, plan, rows, start, wanted, config, ("#lv", "#rk"))
521
+
522
+
523
+ def _unbounded_tree(t: Any, start: str, plan: O.Op, var_of: Dict[str, str], config: _Config) -> O.Op:
524
+ """A spanning tree with no maxLevel (APOC's -1), without a level a join and without enumerating trails.
525
+ Each reached node's breadth-first depth: the shortest-path closure (`any`, rule cypher-shortest-paths)
526
+ from the starts over the step relation, followed to `path_depth` (a deeper search raises). Each node
527
+ at depth d > 0 takes one parent: the neighbour at depth d-1 by a step, the least by identity, then the
528
+ step's relationship identity (the bounded form's tie rule). The parent edges are a tree, so the trails
529
+ closure over them (partitioned by start, seeded at the starts) has one trail per node: its path, with the
530
+ nodes' and relationships' values as `carry`. A start with no step has the zero-length path."""
531
+ from prograph.plan.ir.cypher_front import _Node
532
+ seg = t.fresh("p")
533
+ _path_var(t, var_of, start, seg)
534
+ seg_nodes, seg_rels = f"{seg}#seg_nodes", f"{seg}#seg_rels"
535
+ values = t.whole
536
+ lists = (seg_nodes, seg_rels) if values else ()
537
+ cols = ("#a", "#lv", "#n") + lists
538
+ seeds = _seeds(plan, start)
539
+ branches: List[O.Op] = []
540
+ if config.min <= 0: # the start alone: no relationship, no node after it
541
+ zero: O.Op = O.Extend(O.Extend(seeds, "#lv", E.Const(0)), "#n", E.Col("#a"))
542
+ if values:
543
+ zero = O.Extend(O.Extend(zero, seg_nodes, E.Call("EMPTY_ELEMENTS", (E.Const("nodes"),), E.CYPHER)),
544
+ seg_rels, E.Call("EMPTY_ELEMENTS", (E.Const("relationships"),), E.CYPHER))
545
+ branches.append(O.Project(zero, cols))
546
+ hv = f"{seg}#s"
547
+ step = _step(t, hv, config, reading=False)
548
+ if not _none(step):
549
+ left, right, ident = f"{hv}.__left", f"{hv}.__right", f"{hv}.__id"
550
+ asked = O.Project(O.Extend(seeds, left, E.Col("#a")), (left,))
551
+ dist = O.Closure(O.Project(step, (left, right, ident)), start=left, end=right, mode=O.CYPHER, min=1,
552
+ max=None, edge=ident, shortest="any", starts=asked, depth="#d", closed=False,
553
+ bound_by="maxLevel")
554
+ reached = O.Project(O.Join(seeds, dist, E.Compare("=", E.Col("#a"), E.Col(left), E.TERM)),
555
+ ("#a", right, "#d"))
556
+ depths = O.Union((O.Project(O.Extend(O.Extend(seeds, "#n", E.Col("#a")), "#d", E.Const(0)),
557
+ ("#a", "#n", "#d")),
558
+ O.Project(O.Extend(reached, "#n", E.Col(right)), ("#a", "#n", "#d"))))
559
+ parents = O.Project(O.Extend(O.Extend(depths, "#p", E.Col("#n")), "#dn",
560
+ E.Call("+", (E.Col("#d"), E.Const(1)), E.CYPHER)), ("#a", "#p", "#dn"))
561
+ children = O.Project(O.Extend(O.Extend(O.Extend(depths, "#a2", E.Col("#a")), "#b", E.Col("#n")), "#db",
562
+ E.Col("#d")), ("#a2", "#b", "#db"))
563
+ hv2 = f"{seg}#t"
564
+ edges = _step(t, hv2, config, reading=False)
565
+ left2, right2, ident2 = f"{hv2}.__left", f"{hv2}.__right", f"{hv2}.__id"
566
+ carried: Tuple[str, ...] = ()
567
+ if values:
568
+ node = _Node(f"{hv2}#n", (), {})
569
+ edges = O.Join(edges, t.scan(node), E.Compare("=", E.Col(node.var), E.Col(right2), E.TERM))
570
+ edges = O.Extend(O.Extend(edges, seg_rels, t.rel_value(hv2)), seg_nodes, t.node_value(node.var))
571
+ carried = (seg_rels, seg_nodes)
572
+ x = O.Join(parents, edges, E.Compare("=", E.Col("#p"), E.Col(left2), E.TERM))
573
+ x = O.Join(x, children, E.And((E.Compare("=", E.Col("#a"), E.Col("#a2"), E.TERM),
574
+ E.Compare("=", E.Col(right2), E.Col("#b"), E.TERM),
575
+ E.Compare("=", E.Col("#db"), E.Col("#dn"), E.CYPHER))))
576
+ x = O.Ordinal(x, "#pick", ("#a", "#b"), ("#p", ident2))
577
+ x = O.Filter(x, E.Compare("=", E.Col("#pick"), E.Const(1)))
578
+ tree = O.Project(O.Extend(O.Extend(O.Extend(O.Extend(x, "#ta", E.Col("#a")), "#tl", E.Col("#p")),
579
+ "#tr", E.Col("#b")), "#ti", E.Col(ident2)),
580
+ ("#ta", "#tl", "#tr", "#ti") + carried)
581
+ root = O.Project(O.Extend(seeds, "#tl", E.Col("#a")), ("#tl",))
582
+ walks = O.Closure(tree, start="#tl", end="#tr", mode=O.CYPHER, min=1, max=None, edge="#ti", path="#ids",
583
+ carry=carried, partition=("#ta",), seed=("start", root, "#tl"))
584
+ rows: O.Op = O.Filter(walks, E.Compare("=", E.Col("#tl"), E.Col("#ta"), E.TERM))
585
+ rows = O.Extend(O.Extend(O.Extend(rows, "#a", E.Col("#ta")), "#lv", E.Call("SIZE", (E.Col("#ids"),), E.CYPHER)),
586
+ "#n", E.Col("#tr"))
587
+ branches.append(O.Project(rows, cols))
588
+ out: O.Op = branches[0] if len(branches) == 1 else O.Union(tuple(branches))
589
+ wanted = {c: c for c in lists}
590
+ return _attach(t, plan, out, start, wanted, config, ("#lv", "#n"))
591
+
592
+
593
+ register(Procedure("apoc.path.subgraphNodes", (("node", "ANY"),), (2,), _subgraph_nodes))
594
+ register(Procedure("apoc.path.subgraphAll", (("nodes", "ANY"), ("relationships", "ANY")), (2,), _subgraph_all))
595
+ register(Procedure("apoc.path.expand", (("path", "ANY"),), (5,), _expand))
596
+ register(Procedure("apoc.path.expandConfig", (("path", "ANY"),), (2,), _expand_config))
597
+ register(Procedure("apoc.path.spanningTree", (("path", "ANY"),), (2,), _spanning_tree))