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.
- prograph/__init__.py +36 -0
- prograph/__main__.py +5 -0
- prograph/_version.py +375 -0
- prograph/cli/__init__.py +1 -0
- prograph/cli/_iri_utils.py +124 -0
- prograph/cli/emit_genie_space.py +107 -0
- prograph/cli/generate_views.py +140 -0
- prograph/cli/main.py +420 -0
- prograph/cli/schema_inferrer.py +746 -0
- prograph/cli/ttl_loader.py +289 -0
- prograph/cli/upload_genie_space.py +84 -0
- prograph/config/__init__.py +27 -0
- prograph/config/yaml_config.py +848 -0
- prograph/demos/__init__.py +14 -0
- prograph/demos/turbofan/README.md +74 -0
- prograph/demos/turbofan/SCENARIO.md +209 -0
- prograph/demos/turbofan/__init__.py +75 -0
- prograph/demos/turbofan/create_tables.sql +206 -0
- prograph/demos/turbofan/data/engine-instances.ttl +1123 -0
- prograph/demos/turbofan/data/sensor-timeseries.csv +90 -0
- prograph/demos/turbofan/demo.py +162 -0
- prograph/demos/turbofan/genie_qa_notebook.py +434 -0
- prograph/demos/turbofan/graph.yaml +1098 -0
- prograph/demos/turbofan/instance_graph_demo.py +32 -0
- prograph/demos/turbofan/load.py +223 -0
- prograph/demos/turbofan/mapping.r2rml.ttl +991 -0
- prograph/demos/turbofan/ontology/engine-maintenance.ttl +606 -0
- prograph/demos/turbofan/payload-shapes.ttl +33 -0
- prograph/demos/turbofan/queries/example-queries.sparql +215 -0
- prograph/demos/turbofan/shapes.ttl +75 -0
- prograph/demos/turbofan/tables.py +106 -0
- prograph/demos/turbofan/turbofan_schema.py +393 -0
- prograph/demos/turbofan/validation_demo.py +153 -0
- prograph/describe.py +231 -0
- prograph/endpoint/__init__.py +12 -0
- prograph/endpoint/app.py +722 -0
- prograph/endpoint/cli.py +280 -0
- prograph/endpoint/credentials.py +240 -0
- prograph/endpoint/engines.py +418 -0
- prograph/endpoint/negotiate.py +101 -0
- prograph/endpoint/results.py +181 -0
- prograph/endpoint/service.py +31 -0
- prograph/engine.py +2718 -0
- prograph/execution.py +455 -0
- prograph/executors/__init__.py +6 -0
- prograph/executors/base.py +11 -0
- prograph/executors/databricks_sql_executor.py +51 -0
- prograph/executors/duckdb_executor.py +24 -0
- prograph/executors/mysql_executor.py +20 -0
- prograph/executors/oracle_executor.py +24 -0
- prograph/executors/pandas_executor.py +72 -0
- prograph/executors/postgresql_executor.py +35 -0
- prograph/executors/spark_executor.py +88 -0
- prograph/executors/sqlserver_executor.py +35 -0
- prograph/extras/__init__.py +1 -0
- prograph/extras/graphql/__init__.py +33 -0
- prograph/extras/graphql/execute.py +25 -0
- prograph/extras/graphql/schema.py +314 -0
- prograph/extras/instance_graph.py +559 -0
- prograph/genie/__init__.py +83 -0
- prograph/genie/databricks_uploader.py +306 -0
- prograph/genie/deploy.py +393 -0
- prograph/genie/formats.py +414 -0
- prograph/genie/instruction_generator.py +375 -0
- prograph/genie/sample_query_generator.py +256 -0
- prograph/genie/shacl_views.py +318 -0
- prograph/genie/space_emitter.py +329 -0
- prograph/genie/view_generator.py +628 -0
- prograph/genie/view_writer.py +172 -0
- prograph/graph/__init__.py +1 -0
- prograph/graph/ansi_session.py +348 -0
- prograph/graph/databricks_sql_graph.py +300 -0
- prograph/graph/drivers.py +143 -0
- prograph/graph/duckdb_graph.py +68 -0
- prograph/graph/mysql_graph.py +38 -0
- prograph/graph/oracle_graph.py +285 -0
- prograph/graph/pandas_graph.py +343 -0
- prograph/graph/postgresql_graph.py +644 -0
- prograph/graph/spark_graph.py +556 -0
- prograph/graph/sql_graph.py +83 -0
- prograph/graph/sqlserver_graph.py +72 -0
- prograph/hints.py +95 -0
- prograph/logging.py +550 -0
- prograph/mcp/__init__.py +29 -0
- prograph/mcp/__main__.py +180 -0
- prograph/mcp/bootstrap.py +98 -0
- prograph/mcp/server.py +601 -0
- prograph/mcp/sparql.py +397 -0
- prograph/mcp/tools.py +504 -0
- prograph/nlp/__init__.py +79 -0
- prograph/nlp/client.py +305 -0
- prograph/nlp/databricks_tools.py +369 -0
- prograph/nlp/graph_context.py +313 -0
- prograph/nlp/interface.py +652 -0
- prograph/nlp/prompts/__init__.py +19 -0
- prograph/nlp/prompts/examples.py +258 -0
- prograph/nlp/prompts/system.py +239 -0
- prograph/nlp/providers/__init__.py +14 -0
- prograph/nlp/providers/anthropic.py +274 -0
- prograph/nlp/providers/databricks.py +312 -0
- prograph/nlp/providers/openai.py +203 -0
- prograph/nlp/schema_context.py +78 -0
- prograph/ontology/__init__.py +53 -0
- prograph/ontology/alignment.py +542 -0
- prograph/ontology/hierarchy.py +310 -0
- prograph/ontology/imports.py +587 -0
- prograph/ontology/layer.py +991 -0
- prograph/ontology/skos.py +61 -0
- prograph/owl/__init__.py +27 -0
- prograph/owl/defined_classes.py +323 -0
- prograph/parameters.py +242 -0
- prograph/parser/__init__.py +1 -0
- prograph/parser/cypher/CypherLexer.py +836 -0
- prograph/parser/cypher/CypherListener.py +966 -0
- prograph/parser/cypher/CypherParser.py +10854 -0
- prograph/parser/cypher/CypherVisitor.py +543 -0
- prograph/parser/cypher/__init__.py +1 -0
- prograph/parser/sparql/SparqlLexer.py +801 -0
- prograph/parser/sparql/SparqlParser.py +10146 -0
- prograph/parser/sparql/SparqlParserListener.py +1056 -0
- prograph/parser/sparql/SparqlParserVisitor.py +593 -0
- prograph/parser/sparql/__init__.py +8 -0
- prograph/plan/__init__.py +3 -0
- prograph/plan/ir/__init__.py +1 -0
- prograph/plan/ir/casts.py +338 -0
- prograph/plan/ir/consistency.py +397 -0
- prograph/plan/ir/construct.py +90 -0
- prograph/plan/ir/cost.py +234 -0
- prograph/plan/ir/cypher_answer.py +1081 -0
- prograph/plan/ir/cypher_apoc.py +717 -0
- prograph/plan/ir/cypher_apoc_convert.py +505 -0
- prograph/plan/ir/cypher_apoc_date.py +845 -0
- prograph/plan/ir/cypher_apoc_map.py +496 -0
- prograph/plan/ir/cypher_apoc_meta.py +384 -0
- prograph/plan/ir/cypher_apoc_path.py +597 -0
- prograph/plan/ir/cypher_apoc_procedures.py +163 -0
- prograph/plan/ir/cypher_apoc_text.py +785 -0
- prograph/plan/ir/cypher_front.py +5474 -0
- prograph/plan/ir/cypher_functions.py +565 -0
- prograph/plan/ir/cypher_lists.py +935 -0
- prograph/plan/ir/cypher_maps.py +370 -0
- prograph/plan/ir/cypher_mixed.py +604 -0
- prograph/plan/ir/cypher_ontology.py +226 -0
- prograph/plan/ir/cypher_procedures.py +191 -0
- prograph/plan/ir/cypher_runtime_errors.py +107 -0
- prograph/plan/ir/cypher_temporal.py +1357 -0
- prograph/plan/ir/cypher_temporal_arith.py +339 -0
- prograph/plan/ir/cypher_values.py +546 -0
- prograph/plan/ir/cypher_write.py +612 -0
- prograph/plan/ir/dataset.py +643 -0
- prograph/plan/ir/dates.py +102 -0
- prograph/plan/ir/entry.py +992 -0
- prograph/plan/ir/expr.py +260 -0
- prograph/plan/ir/facts.py +1171 -0
- prograph/plan/ir/fresh.py +291 -0
- prograph/plan/ir/freshness.py +625 -0
- prograph/plan/ir/identity.py +797 -0
- prograph/plan/ir/java_datetime.py +265 -0
- prograph/plan/ir/java_regex.py +391 -0
- prograph/plan/ir/join_order.py +39 -0
- prograph/plan/ir/key_checks.py +433 -0
- prograph/plan/ir/keys.py +454 -0
- prograph/plan/ir/lower.py +5323 -0
- prograph/plan/ir/ops.py +619 -0
- prograph/plan/ir/owl_rl.py +1119 -0
- prograph/plan/ir/owl_rl_rules.py +725 -0
- prograph/plan/ir/profiles.py +694 -0
- prograph/plan/ir/ql.py +987 -0
- prograph/plan/ir/rdfs.py +342 -0
- prograph/plan/ir/rewrite.py +1782 -0
- prograph/plan/ir/saturate.py +339 -0
- prograph/plan/ir/semantics.py +103 -0
- prograph/plan/ir/slot.py +509 -0
- prograph/plan/ir/sparql_front.py +3418 -0
- prograph/plan/ir/strings.py +534 -0
- prograph/plan/ir/term_functions.py +274 -0
- prograph/plan/ir/terms.py +84 -0
- prograph/plan/ir/xpath_regex.py +264 -0
- prograph/plan/run_record.py +65 -0
- prograph/plan/sql/__init__.py +21 -0
- prograph/plan/sql/dialect.py +3726 -0
- prograph/plan/sql/mysql_serializer.py +103 -0
- prograph/plan/sql/names.py +65 -0
- prograph/plan/sql/oracle_serializer.py +306 -0
- prograph/plan/sql/pg_serializer.py +501 -0
- prograph/plan/sql/rel_algebra.py +781 -0
- prograph/plan/sql/sql_serializer.py +1419 -0
- prograph/plan/sql/sqlserver_serializer.py +603 -0
- prograph/provenance.py +222 -0
- prograph/r2rml/__init__.py +48 -0
- prograph/r2rml/catalog.py +259 -0
- prograph/r2rml/compile.py +372 -0
- prograph/r2rml/direct.py +140 -0
- prograph/r2rml/export.py +495 -0
- prograph/r2rml/model.py +180 -0
- prograph/r2rml/parser.py +546 -0
- prograph/r2rml/schema.py +120 -0
- prograph/read_only.py +122 -0
- prograph/results.py +157 -0
- prograph/rules/__init__.py +168 -0
- prograph/rules/__main__.py +32 -0
- prograph/schema/__init__.py +60 -0
- prograph/schema/change_set.py +50 -0
- prograph/schema/collations.py +198 -0
- prograph/schema/compile_schema.py +388 -0
- prograph/schema/context.py +845 -0
- prograph/schema/examples.py +234 -0
- prograph/schema/iri.py +817 -0
- prograph/schema/literals.py +94 -0
- prograph/schema/lpg_view.py +617 -0
- prograph/schema/mapping.py +1803 -0
- prograph/schema/maps_schema.py +40 -0
- prograph/schema/naming.py +77 -0
- prograph/schema/query_sources.py +59 -0
- prograph/schema/rows.py +215 -0
- prograph/schema/source_map.py +1103 -0
- prograph/schema/store_view.py +459 -0
- prograph/schema/types.py +569 -0
- prograph/schema/xsd.py +569 -0
- prograph/shacl/__init__.py +30 -0
- prograph/shacl/dqv.py +58 -0
- prograph/shacl/generate.py +255 -0
- prograph/shacl/report.py +138 -0
- prograph/shacl/validator.py +164 -0
- prograph/store.py +1737 -0
- prograph/utils/__init__.py +6 -0
- prograph/utils/exceptions.py +308 -0
- prograph/utils/helpers.py +126 -0
- prograph/warnings.py +46 -0
- prograph/writes.py +369 -0
- prograph-1.3.3.dist-info/METADATA +281 -0
- prograph-1.3.3.dist-info/RECORD +237 -0
- prograph-1.3.3.dist-info/WHEEL +5 -0
- prograph-1.3.3.dist-info/entry_points.txt +3 -0
- prograph-1.3.3.dist-info/licenses/LICENSE +201 -0
- prograph-1.3.3.dist-info/licenses/NOTICE +10 -0
- prograph-1.3.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
"""apoc.meta.* over the property-graph view (rule cypher-apoc-procedures; design 261): schema, graph, stats,
|
|
2
|
+
nodeTypeProperties and relTypeProperties, with APOC 5.26's output names and shapes.
|
|
3
|
+
|
|
4
|
+
What labels, relationship types and properties exist is read from the view at planning. A count is a COUNT
|
|
5
|
+
over the maps' own scans (`cypher_front._Translator.scan`, `scan_rel`), joined into the one row, never
|
|
6
|
+
guessed. Departures from APOC, which samples the data:
|
|
7
|
+
- a property's type is the mapping's declared type (a column's type, or the term map's datatype), exact
|
|
8
|
+
and the same on every run; a property two maps declare in two types lists both in `propertyTypes`;
|
|
9
|
+
- every label is a node type of its own (APOC groups by the whole label set), every relationship type one
|
|
10
|
+
type whose source and target labels are those its maps declare (APOC groups by the pair);
|
|
11
|
+
- a label or relationship type the mapping declares is listed whether or not any row has it (its count 0).
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from contextlib import contextmanager
|
|
17
|
+
from typing import Any, Dict, Iterator, List, Optional, Sequence, Tuple
|
|
18
|
+
|
|
19
|
+
from prograph.plan.ir import expr as E
|
|
20
|
+
from prograph.plan.ir import ops as O
|
|
21
|
+
from prograph.plan.ir.cypher_apoc_procedures import ApocProcedureError, Procedure, literal, register, values
|
|
22
|
+
from prograph.schema import xsd
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _type_text(name: str) -> str:
|
|
26
|
+
"""APOC's `nodeType` and `relType` text for a label or type (`:`Person``): the name in backticks."""
|
|
27
|
+
return ":" + "`" + name + "`"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _map(**parts: E.Expr) -> E.Expr:
|
|
31
|
+
return _map_of(list(parts.items()))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _map_of(parts: Sequence[Tuple[str, E.Expr]]) -> E.Expr:
|
|
35
|
+
out: List[E.Expr] = []
|
|
36
|
+
for k, v in parts:
|
|
37
|
+
out += [E.Const(k), v]
|
|
38
|
+
return E.Call("MAP", tuple(out), E.CYPHER)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _list(items: Sequence[E.Expr]) -> E.Expr:
|
|
42
|
+
return E.Call("LIST", tuple(items), E.CYPHER)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _strings(values: Sequence[str]) -> E.Expr:
|
|
46
|
+
return _list([E.Const(v) for v in values])
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# -- the view, as the mapping declares it ---------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
def _type_name(t: Any, m: Any, column: Optional[str], datatype: Optional[str]) -> str:
|
|
52
|
+
"""The APOC type name of a property read from `column` of map `m`: the term map's datatype where the
|
|
53
|
+
mapping declares one, else the column's declared type."""
|
|
54
|
+
from prograph.plan.ir import cypher_temporal as T
|
|
55
|
+
from prograph.plan.ir.cypher_lists import LIST_OF, column_list_type
|
|
56
|
+
if column is not None and datatype is None:
|
|
57
|
+
try:
|
|
58
|
+
target = t.source_target(m, column)
|
|
59
|
+
except Exception: # noqa: BLE001 - a column of a type no temporal value is (a time with a zone ...)
|
|
60
|
+
target = None
|
|
61
|
+
named = {T.CYPHER_DATE: "Date", T.CYPHER_LOCALTIME: "LocalTime", T.CYPHER_LOCALDATETIME: "LocalDateTime",
|
|
62
|
+
T.CYPHER_DATETIME: "DateTime"}
|
|
63
|
+
if target in named:
|
|
64
|
+
return named[target]
|
|
65
|
+
held = t.held(m.source.table, column)[0]
|
|
66
|
+
if held is None:
|
|
67
|
+
listed = column_list_type(getattr(t.held, "kind_of", lambda *_: None)(m.source.table, column))
|
|
68
|
+
if listed is not None:
|
|
69
|
+
element = listed[len(LIST_OF):]
|
|
70
|
+
return _scalar(element) + "Array"
|
|
71
|
+
return "String"
|
|
72
|
+
datatype = held
|
|
73
|
+
return _scalar(datatype)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _scalar(datatype: Optional[str]) -> str:
|
|
77
|
+
if datatype is None:
|
|
78
|
+
return "String"
|
|
79
|
+
if datatype == xsd.INTEGER or datatype in xsd.INTEGER_DATATYPES:
|
|
80
|
+
return "Long"
|
|
81
|
+
if datatype in (xsd.DOUBLE, xsd.FLOAT, xsd.DECIMAL):
|
|
82
|
+
return "Double"
|
|
83
|
+
if datatype == xsd.BOOLEAN:
|
|
84
|
+
return "Boolean"
|
|
85
|
+
if datatype == xsd.DATE:
|
|
86
|
+
return "Date"
|
|
87
|
+
if datatype == xsd.DATETIME:
|
|
88
|
+
return "DateTime"
|
|
89
|
+
return "String"
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _node_properties(t: Any, label: str) -> Dict[str, List[str]]:
|
|
93
|
+
"""The property keys of the nodes of `label` (the union over its maps) and the types they are declared as."""
|
|
94
|
+
out: Dict[str, List[str]] = {}
|
|
95
|
+
for m in t.view.node_maps(label):
|
|
96
|
+
for key, pos in t.view.properties.get(m.id, {}).items():
|
|
97
|
+
for po in pos:
|
|
98
|
+
name = _type_name(t, m, po.object.column, po.object.datatype)
|
|
99
|
+
if name not in out.setdefault(key, []):
|
|
100
|
+
out[key].append(name)
|
|
101
|
+
return {k: sorted(v) for k, v in sorted(out.items())}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _end_labels(t: Any, end: Any) -> List[str]:
|
|
105
|
+
if end.label is not None:
|
|
106
|
+
return [end.label]
|
|
107
|
+
if end.map_id is not None:
|
|
108
|
+
return list(t.view.node_labels.get(end.map_id, ()))
|
|
109
|
+
return []
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _rel_types(t: Any) -> Dict[str, Dict[str, Any]]:
|
|
113
|
+
"""Each relationship type: its source and target labels and its property keys with their types, over its maps."""
|
|
114
|
+
out: Dict[str, Dict[str, Any]] = {}
|
|
115
|
+
for name in sorted(t.view.rel_types):
|
|
116
|
+
info: Dict[str, Any] = {"source": [], "target": [], "properties": {}, "pairs": []}
|
|
117
|
+
for e in t.view.rel_types[name]:
|
|
118
|
+
starts, ends = _end_labels(t, e.start), _end_labels(t, e.end)
|
|
119
|
+
for s in starts:
|
|
120
|
+
if s not in info["source"]:
|
|
121
|
+
info["source"].append(s)
|
|
122
|
+
for x in ends:
|
|
123
|
+
if x not in info["target"]:
|
|
124
|
+
info["target"].append(x)
|
|
125
|
+
for s in starts:
|
|
126
|
+
for x in ends:
|
|
127
|
+
if (s, x) not in info["pairs"]:
|
|
128
|
+
info["pairs"].append((s, x))
|
|
129
|
+
for key, column in e.property_columns:
|
|
130
|
+
kind = _type_name(t, e, column, None)
|
|
131
|
+
if kind not in info["properties"].setdefault(key, []):
|
|
132
|
+
info["properties"][key].append(kind)
|
|
133
|
+
info["source"].sort(), info["target"].sort(), info["pairs"].sort()
|
|
134
|
+
info["properties"] = {k: sorted(v) for k, v in sorted(info["properties"].items())}
|
|
135
|
+
out[name] = info
|
|
136
|
+
return out
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
# -- counts: COUNT over the maps' scans -----------------------------------------------------------------
|
|
140
|
+
|
|
141
|
+
@contextmanager
|
|
142
|
+
def _reading(t: Any, keys: Sequence[str]) -> Iterator[None]:
|
|
143
|
+
"""The translator reading only `keys` (a scan reads what the query reads): a count scans nothing else."""
|
|
144
|
+
names = ("keys_read", "labels_read", "node_cols", "rel_cols", "whole", "wrote", "node_keys", "rel_keys")
|
|
145
|
+
saved = {n: getattr(t, n, None) for n in names}
|
|
146
|
+
t.keys_read, t.labels_read, t.whole, t.wrote = list(keys), [], False, False
|
|
147
|
+
t.node_cols, t.rel_cols = list(keys), ["__type"] + list(keys)
|
|
148
|
+
t.node_keys, t.rel_keys = list(keys), list(keys)
|
|
149
|
+
try:
|
|
150
|
+
yield
|
|
151
|
+
finally:
|
|
152
|
+
for n, v in saved.items():
|
|
153
|
+
setattr(t, n, v)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
class _Row:
|
|
157
|
+
"""One result row: COUNT plans cross-joined (each a single row of one named count), the outputs
|
|
158
|
+
expressions over them."""
|
|
159
|
+
|
|
160
|
+
def __init__(self, t: Any) -> None:
|
|
161
|
+
self.t = t
|
|
162
|
+
self.parts: List[O.Op] = []
|
|
163
|
+
|
|
164
|
+
def count(self, plan: O.Op, of: Optional[str] = None) -> str:
|
|
165
|
+
name = self.t.fresh("k")
|
|
166
|
+
agg = E.Agg("COUNT", E.Col(of) if of else None, semantics=E.CYPHER)
|
|
167
|
+
self.parts.append(O.Aggregate(plan, (), ((name, agg),)))
|
|
168
|
+
return name
|
|
169
|
+
|
|
170
|
+
def nodes(self, label: Optional[str]) -> str:
|
|
171
|
+
from prograph.plan.ir.cypher_front import _Node
|
|
172
|
+
with _reading(self.t, ()):
|
|
173
|
+
return self.count(self.t.scan(_Node(self.t.fresh("m"), (label,) if label else (), {})))
|
|
174
|
+
|
|
175
|
+
def with_property(self, label: str, key: str) -> str:
|
|
176
|
+
from prograph.plan.ir.cypher_front import _Node
|
|
177
|
+
var = self.t.fresh("m")
|
|
178
|
+
with _reading(self.t, (key,)):
|
|
179
|
+
return self.count(self.t.scan(_Node(var, (label,), {})), f"{var}.{key}")
|
|
180
|
+
|
|
181
|
+
def rels(self, types: Sequence[str], ends: Sequence[Tuple[str, str]] = ()) -> str:
|
|
182
|
+
"""Relationships of `types` (all for none); `ends` as (side, label): only those whose end node of that
|
|
183
|
+
side ('left' or 'right') has the label."""
|
|
184
|
+
from prograph.plan.ir.cypher_front import _Node, _Rel
|
|
185
|
+
var = self.t.fresh("r")
|
|
186
|
+
with _reading(self.t, ()):
|
|
187
|
+
plan = self.t.scan_rel(_Rel(var, tuple(types), {}, "", "", "->"), identity=True)
|
|
188
|
+
for side, label in ends:
|
|
189
|
+
node = self.t.fresh("m")
|
|
190
|
+
scanned = self.t.scan(_Node(node, (label,), {}))
|
|
191
|
+
plan = O.SemiJoin(plan, scanned, E.Compare("=", E.Col(f"{var}.__{side}"), E.Col(node), E.TERM))
|
|
192
|
+
return self.count(plan, f"{var}.__id")
|
|
193
|
+
|
|
194
|
+
def with_rel_property(self, types: Sequence[str], key: str) -> str:
|
|
195
|
+
from prograph.plan.ir.cypher_front import _Rel
|
|
196
|
+
var = self.t.fresh("r")
|
|
197
|
+
with _reading(self.t, (key,)):
|
|
198
|
+
plan = self.t.scan_rel(_Rel(var, tuple(types), {}, "", "", "->"), identity=True)
|
|
199
|
+
return self.count(plan, f"{var}.{key}")
|
|
200
|
+
|
|
201
|
+
def plan(self) -> O.Op:
|
|
202
|
+
out: O.Op = O.Unit()
|
|
203
|
+
for part in self.parts:
|
|
204
|
+
out = part if isinstance(out, O.Unit) else O.Join(out, part)
|
|
205
|
+
return out
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _rows(t: Any, rows: List[Tuple[_Row, Dict[str, E.Expr]]], names: Sequence[str]) -> Tuple[O.Op, Dict[str, str]]:
|
|
209
|
+
"""The union of one-row plans, each its counts and its outputs `names` (a column each, fresh)."""
|
|
210
|
+
columns = {n: t.fresh("o") for n in names}
|
|
211
|
+
branches: List[O.Op] = []
|
|
212
|
+
for row, outputs in rows:
|
|
213
|
+
plan = row.plan()
|
|
214
|
+
for n in names:
|
|
215
|
+
plan = O.Extend(plan, columns[n], outputs[n])
|
|
216
|
+
branches.append(O.Project(plan, tuple(columns[n] for n in names)))
|
|
217
|
+
if not branches:
|
|
218
|
+
return O.Values(tuple(columns[n] for n in names), ()), columns
|
|
219
|
+
return (branches[0] if len(branches) == 1 else O.Union(tuple(branches))), columns
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def _no_config(name: str, args: List[E.Expr]) -> None:
|
|
223
|
+
if args:
|
|
224
|
+
config = literal(args[0], name, "the config map")
|
|
225
|
+
if config:
|
|
226
|
+
raise ApocProcedureError(name, f"config key(s) {', '.join(sorted(config))} are not built (the "
|
|
227
|
+
"mapping's whole graph is described, none sampled)")
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _labels(t: Any) -> List[str]:
|
|
231
|
+
return sorted(t.view.labels)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
# -- apoc.meta.stats ------------------------------------------------------------------------------------
|
|
235
|
+
|
|
236
|
+
def _stats(t: Any, args: List[E.Expr]) -> Tuple[O.Op, Dict[str, str]]:
|
|
237
|
+
row = _Row(t)
|
|
238
|
+
labels, types = _labels(t), _rel_types(t)
|
|
239
|
+
node_count, rel_count = row.nodes(None), row.rels(())
|
|
240
|
+
label_counts = {label: row.nodes(label) for label in labels}
|
|
241
|
+
type_counts = {name: row.rels((name,)) for name in types}
|
|
242
|
+
rel_patterns: List[Tuple[str, E.Expr]] = []
|
|
243
|
+
for name, info in types.items():
|
|
244
|
+
rel_patterns.append((f"()-[:{name}]->()", E.Col(type_counts[name])))
|
|
245
|
+
for s in info["source"]:
|
|
246
|
+
rel_patterns.append((f"(:{s})-[:{name}]->()", E.Col(row.rels((name,), [("left", s)]))))
|
|
247
|
+
for x in info["target"]:
|
|
248
|
+
rel_patterns.append((f"()-[:{name}]->(:{x})", E.Col(row.rels((name,), [("right", x)]))))
|
|
249
|
+
keys = len(t.view.all_node_keys() | t.view.all_rel_keys())
|
|
250
|
+
label_map = _map_of([(label, E.Col(label_counts[label])) for label in labels])
|
|
251
|
+
rel_map = _map_of(rel_patterns)
|
|
252
|
+
simple = {"labelCount": E.Const(len(labels)), "relTypeCount": E.Const(len(types)),
|
|
253
|
+
"propertyKeyCount": E.Const(keys), "nodeCount": E.Col(node_count), "relCount": E.Col(rel_count)}
|
|
254
|
+
types_count = _map_of([(name, E.Col(c)) for name, c in type_counts.items()])
|
|
255
|
+
outputs = {**simple, "labels": label_map, "relTypes": rel_map, "relTypesCount": types_count,
|
|
256
|
+
"stats": _map_of(list(simple.items()) + [("labels", label_map), ("relTypes", rel_map)])}
|
|
257
|
+
names = [n for n, _ in _STATS_OUT]
|
|
258
|
+
return _rows(t, [(row, outputs)], names)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
_STATS_OUT = (("labelCount", "INTEGER"), ("relTypeCount", "INTEGER"), ("propertyKeyCount", "INTEGER"),
|
|
262
|
+
("nodeCount", "INTEGER"), ("relCount", "INTEGER"), ("labels", "ANY"), ("relTypes", "ANY"),
|
|
263
|
+
("relTypesCount", "ANY"), ("stats", "ANY"))
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
# -- apoc.meta.nodeTypeProperties, relTypeProperties -----------------------------------------------------
|
|
267
|
+
|
|
268
|
+
_PROPS_COMMON = (("propertyName", "STRING"), ("propertyTypes", "ANY"), ("mandatory", "BOOLEAN"),
|
|
269
|
+
("propertyObservations", "INTEGER"), ("totalObservations", "INTEGER"))
|
|
270
|
+
_NODE_PROPS_OUT = (("nodeType", "STRING"), ("nodeLabels", "ANY")) + _PROPS_COMMON
|
|
271
|
+
_REL_PROPS_OUT = (("relType", "STRING"), ("sourceNodeLabels", "ANY"), ("targetNodeLabels", "ANY")) + _PROPS_COMMON
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def _property_row(row: _Row, head: Dict[str, E.Expr], key: Optional[str], kinds: List[str],
|
|
275
|
+
observed: Optional[str], total: str) -> Dict[str, E.Expr]:
|
|
276
|
+
out = dict(head)
|
|
277
|
+
out["propertyName"] = E.Const(key)
|
|
278
|
+
out["propertyTypes"] = _strings(kinds) if key is not None else E.Const(None)
|
|
279
|
+
out["mandatory"] = E.Const(False) # APOC: an existence constraint, which a mapping does not declare
|
|
280
|
+
out["propertyObservations"] = E.Col(observed) if observed else E.Const(0)
|
|
281
|
+
out["totalObservations"] = E.Col(total)
|
|
282
|
+
return out
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def _node_type_properties(t: Any, args: List[E.Expr]) -> Tuple[O.Op, Dict[str, str]]:
|
|
286
|
+
_no_config("apoc.meta.nodeTypeProperties", args)
|
|
287
|
+
rows: List[Tuple[_Row, Dict[str, E.Expr]]] = []
|
|
288
|
+
for label in _labels(t):
|
|
289
|
+
head = {"nodeType": E.Const(_type_text(label)), "nodeLabels": _strings([label])}
|
|
290
|
+
props = _node_properties(t, label)
|
|
291
|
+
for key, kinds in props.items() or {None: []}.items():
|
|
292
|
+
row = _Row(t)
|
|
293
|
+
total = row.nodes(label)
|
|
294
|
+
observed = row.with_property(label, key) if key is not None else None
|
|
295
|
+
rows.append((row, _property_row(row, head, key, kinds, observed, total)))
|
|
296
|
+
return _rows(t, rows, [n for n, _ in _NODE_PROPS_OUT])
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def _rel_type_properties(t: Any, args: List[E.Expr]) -> Tuple[O.Op, Dict[str, str]]:
|
|
300
|
+
_no_config("apoc.meta.relTypeProperties", args)
|
|
301
|
+
rows: List[Tuple[_Row, Dict[str, E.Expr]]] = []
|
|
302
|
+
for name, info in _rel_types(t).items():
|
|
303
|
+
head = {"relType": E.Const(_type_text(name)), "sourceNodeLabels": _strings(info["source"]),
|
|
304
|
+
"targetNodeLabels": _strings(info["target"])}
|
|
305
|
+
for key, kinds in info["properties"].items() or {None: []}.items():
|
|
306
|
+
row = _Row(t)
|
|
307
|
+
total = row.rels((name,))
|
|
308
|
+
observed = row.with_rel_property((name,), key) if key is not None else None
|
|
309
|
+
rows.append((row, _property_row(row, head, key, kinds, observed, total)))
|
|
310
|
+
return _rows(t, rows, [n for n, _ in _REL_PROPS_OUT])
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
# -- apoc.meta.schema, apoc.meta.graph -------------------------------------------------------------------
|
|
314
|
+
|
|
315
|
+
_SCHEMA_TYPES = {"Long": "INTEGER", "Double": "FLOAT", "String": "STRING", "Boolean": "BOOLEAN", "Date": "DATE",
|
|
316
|
+
"DateTime": "DATE_TIME", "LocalDateTime": "LOCAL_DATE_TIME", "LocalTime": "LOCAL_TIME"}
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def _schema_type(kind: str) -> str:
|
|
320
|
+
"""`apoc.meta.schema`'s type name (Neo4j's value types in capitals), where nodeTypeProperties writes `Long`."""
|
|
321
|
+
return "LIST" if kind.endswith("Array") else _SCHEMA_TYPES.get(kind, "STRING")
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def _property_map(kinds: Dict[str, List[str]], node: bool) -> E.Expr:
|
|
325
|
+
"""A node's properties carry `unique`, a relationship's `array`, as APOC 5.26 gives each."""
|
|
326
|
+
entries = []
|
|
327
|
+
for key, v in kinds.items():
|
|
328
|
+
parts = {"type": E.Const(_schema_type(v[0])), "indexed": E.Const(False), "existence": E.Const(False)}
|
|
329
|
+
parts["unique" if node else "array"] = E.Const(False if node else v[0].endswith("Array"))
|
|
330
|
+
entries.append((key, _map(**parts)))
|
|
331
|
+
return _map_of(entries)
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def _schema(t: Any, args: List[E.Expr]) -> Tuple[O.Op, Dict[str, str]]:
|
|
335
|
+
_no_config("apoc.meta.schema", args)
|
|
336
|
+
row = _Row(t)
|
|
337
|
+
types = _rel_types(t)
|
|
338
|
+
entries: List[Tuple[str, E.Expr]] = []
|
|
339
|
+
for label in _labels(t):
|
|
340
|
+
relationships: List[Tuple[str, E.Expr]] = []
|
|
341
|
+
for name, info in types.items():
|
|
342
|
+
for direction, side, others in (("out", "left", info["target"]), ("in", "right", info["source"])):
|
|
343
|
+
mine = info["source"] if side == "left" else info["target"]
|
|
344
|
+
if label in mine:
|
|
345
|
+
relationships.append((name if direction == "out" else name, _map(
|
|
346
|
+
count=E.Col(row.rels((name,), [(side, label)])), direction=E.Const(direction),
|
|
347
|
+
labels=_strings(others), properties=_property_map(info["properties"], False))))
|
|
348
|
+
entries.append((label, _map(type=E.Const("node"), count=E.Col(row.nodes(label)),
|
|
349
|
+
labels=_strings([]), properties=_property_map(_node_properties(t, label), True),
|
|
350
|
+
relationships=_map_of(_unique(relationships)))))
|
|
351
|
+
for name, info in types.items():
|
|
352
|
+
entries.append((name, _map(type=E.Const("relationship"), count=E.Col(row.rels((name,))),
|
|
353
|
+
properties=_property_map(info["properties"], False))))
|
|
354
|
+
return _rows(t, [(row, {"value": _map_of(entries)})], ["value"])
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def _unique(pairs: List[Tuple[str, E.Expr]]) -> List[Tuple[str, E.Expr]]:
|
|
358
|
+
"""A node's relationships keyed by type: a type it both starts and ends keeps its first direction."""
|
|
359
|
+
seen: Dict[str, E.Expr] = {}
|
|
360
|
+
for k, v in pairs:
|
|
361
|
+
seen.setdefault(k, v)
|
|
362
|
+
return list(seen.items())
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def _graph(t: Any, args: List[E.Expr]) -> Tuple[O.Op, Dict[str, str]]:
|
|
366
|
+
_no_config("apoc.meta.graph", args)
|
|
367
|
+
row = _Row(t)
|
|
368
|
+
nodes = [E.Call("NODE", (E.Const(f"label:{label}"), E.Const("name"), E.Const(label), E.Const("count"),
|
|
369
|
+
E.Col(row.nodes(label)), E.Const(f"label:{label}"), E.Const(True)), E.CYPHER)
|
|
370
|
+
for label in _labels(t)]
|
|
371
|
+
rels: List[E.Expr] = []
|
|
372
|
+
for name, info in _rel_types(t).items():
|
|
373
|
+
for s, x in info["pairs"]:
|
|
374
|
+
rels.append(E.Call("REL", (E.Const(f"rel:{s}:{name}:{x}"), E.Const(name), E.Const(f"label:{s}"),
|
|
375
|
+
E.Const(f"label:{x}"), E.Const("count"),
|
|
376
|
+
E.Col(row.rels((name,), [("left", s), ("right", x)]))), E.CYPHER))
|
|
377
|
+
return _rows(t, [(row, {"nodes": _list(nodes), "relationships": _list(rels)})], ["nodes", "relationships"])
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
register(Procedure("apoc.meta.stats", _STATS_OUT, (0,), values(_stats)))
|
|
381
|
+
register(Procedure("apoc.meta.nodeTypeProperties", _NODE_PROPS_OUT, (0, 1), values(_node_type_properties)))
|
|
382
|
+
register(Procedure("apoc.meta.relTypeProperties", _REL_PROPS_OUT, (0, 1), values(_rel_type_properties)))
|
|
383
|
+
register(Procedure("apoc.meta.schema", (("value", "ANY"),), (0, 1), values(_schema)))
|
|
384
|
+
register(Procedure("apoc.meta.graph", (("nodes", "ANY"), ("relationships", "ANY")), (0, 1), values(_graph)))
|