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,496 @@
|
|
|
1
|
+
"""APOC Core's apoc.map.* functions: rows of the cypher-apoc table (`cypher_apoc.TABLE`) for maps.
|
|
2
|
+
|
|
3
|
+
A Cypher map is held as a struct of keys known at planning (rule cypher-maps), so every one of these functions
|
|
4
|
+
is worked out at planning into the map home's own calls: a map literal (`MAP`), a key's read (`FIELD`), a
|
|
5
|
+
list literal (`LIST`). Their values stay expressions, read and lowered as any map's or list's are; nothing is
|
|
6
|
+
evaluated row by row in Python. APOC 5.26's code decides each answer (apoc/map/Maps.java, apoc/util/Util.java),
|
|
7
|
+
checked against Neo4j 5 with APOC Core (tests/cypher/apoc/answers.json).
|
|
8
|
+
|
|
9
|
+
What makes an answer's keys depend on a row is refused by name (`ApocFunctionError`), never approximated:
|
|
10
|
+
keys or pairs not written in the query, a map that may be null in some rows (its keys would be the other
|
|
11
|
+
map's there), clean() of a value read at run time, groupBy() of a key's value read at run time, a nested map
|
|
12
|
+
that may be null where flatten() or removeKeys(recursive) look into it, and lists under removeKeys(recursive)
|
|
13
|
+
(an emptied list drops its key). Where Neo4j raises for every row (a null where APOC calls a method on it, a
|
|
14
|
+
missing key with fail, two list sizes), ProGraph refuses at planning, naming the error.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
from typing import Callable, Dict, List, Optional
|
|
20
|
+
|
|
21
|
+
from prograph.plan.ir import expr as E
|
|
22
|
+
from prograph.plan.ir.cypher_apoc import ApocFunctionError, _Function
|
|
23
|
+
from prograph.plan.ir.slot import LIST, MAP, Bound, Slot
|
|
24
|
+
|
|
25
|
+
SlotOf = Callable[[E.Expr], Slot]
|
|
26
|
+
_NAME = "apoc.map."
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _refuse(fn: str, why: str):
|
|
30
|
+
raise ApocFunctionError(_NAME + fn, why)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _null(x: E.Expr) -> bool:
|
|
34
|
+
return isinstance(x, E.Const) and x.value is None
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _items(fn: str, x: E.Expr, what: str) -> List[E.Expr]:
|
|
38
|
+
"""A list argument's elements, as written in the query."""
|
|
39
|
+
if isinstance(x, E.Call) and x.name == "LIST":
|
|
40
|
+
return list(x.args)
|
|
41
|
+
if isinstance(x, E.Const) and isinstance(x.value, (list, tuple)):
|
|
42
|
+
return [E.Const(v) for v in x.value]
|
|
43
|
+
_refuse(fn, f"{what} not written in the query as a list (the answer's keys would be known only at run time)")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _text(fn: str, value) -> Optional[str]:
|
|
47
|
+
"""A key's text, as Java's toString writes it; None for null."""
|
|
48
|
+
if value is None:
|
|
49
|
+
return None
|
|
50
|
+
if isinstance(value, bool):
|
|
51
|
+
return "true" if value else "false"
|
|
52
|
+
if isinstance(value, int):
|
|
53
|
+
return str(value)
|
|
54
|
+
if isinstance(value, float):
|
|
55
|
+
from prograph.plan.ir.cypher_functions import java_double
|
|
56
|
+
return java_double(value)
|
|
57
|
+
if isinstance(value, str):
|
|
58
|
+
return value
|
|
59
|
+
_refuse(fn, "a key that is not a string, a number or a boolean")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _key(fn: str, x: E.Expr, any_type: bool = False) -> Optional[str]:
|
|
63
|
+
"""A key written in the query: a string (APOC's STRING parameters; another type is Neo4j's type
|
|
64
|
+
mismatch), or with `any_type` any value's text (fromPairs' and fromValues' keys are objects)."""
|
|
65
|
+
if not isinstance(x, E.Const):
|
|
66
|
+
_refuse(fn, "a key not written in the query (the answer's keys would be known only at run time)")
|
|
67
|
+
if not any_type and x.value is not None and not isinstance(x.value, str):
|
|
68
|
+
_refuse(fn, "a key that is not a string (a type mismatch in Neo4j)")
|
|
69
|
+
return _text(fn, x.value)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _fields(fn: str, x: E.Expr, slot_of: SlotOf) -> Dict[str, E.Expr]:
|
|
73
|
+
"""A map argument's keys and each one's value, as expressions: a literal's own, else its key read."""
|
|
74
|
+
if isinstance(x, E.Call) and x.name == "MAP":
|
|
75
|
+
return {k.value: v for k, v in zip(x.args[0::2], x.args[1::2])}
|
|
76
|
+
s = slot_of(x)
|
|
77
|
+
if s.present is Bound.NEVER:
|
|
78
|
+
return {}
|
|
79
|
+
if s.kinds != {MAP}:
|
|
80
|
+
_refuse(fn, "an argument that is not a map")
|
|
81
|
+
if s.present is not Bound.ALWAYS:
|
|
82
|
+
_refuse(fn, "a map that may be null in some rows (the answer's keys would differ there)")
|
|
83
|
+
from prograph.plan.ir.cypher_maps import fields_of
|
|
84
|
+
return {k: E.Call("FIELD", (x, E.Const(k)), E.CYPHER) for k in fields_of(s)}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _map(fields: Dict[str, E.Expr]) -> E.Expr:
|
|
88
|
+
parts: List[E.Expr] = []
|
|
89
|
+
for k, v in fields.items():
|
|
90
|
+
parts += [E.Const(k), v]
|
|
91
|
+
return E.Call("MAP", tuple(parts), E.CYPHER)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _list(values: List[E.Expr]) -> E.Expr:
|
|
95
|
+
return E.Call("LIST", tuple(values), E.CYPHER)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _flag(fn: str, x: E.Expr, what: str) -> bool:
|
|
99
|
+
if not (isinstance(x, E.Const) and isinstance(x.value, bool)):
|
|
100
|
+
_refuse(fn, f"{what} that is not true or false written in the query")
|
|
101
|
+
return x.value
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _not_null(fn: str, x: E.Expr, what: str) -> None:
|
|
105
|
+
if _null(x):
|
|
106
|
+
_refuse(fn, f"a null {what} (an error in APOC)")
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
# --- building maps from lists -------------------------------------------------------------------------
|
|
110
|
+
|
|
111
|
+
def _from_pairs(fn: str, pairs: E.Expr) -> Dict[str, E.Expr]:
|
|
112
|
+
"""Util.mapFromPairs: each pair's first element the key (null or an empty pair skipped), its second the
|
|
113
|
+
value (null where the pair has one element); a later key's value wins."""
|
|
114
|
+
_not_null(fn, pairs, "list of pairs")
|
|
115
|
+
out: Dict[str, E.Expr] = {}
|
|
116
|
+
for pair in _items(fn, pairs, "pairs"):
|
|
117
|
+
inner = _items(fn, pair, "a pair")
|
|
118
|
+
if not inner:
|
|
119
|
+
continue
|
|
120
|
+
k = _key(fn, inner[0], any_type=True)
|
|
121
|
+
if k is not None:
|
|
122
|
+
out[k] = inner[1] if len(inner) > 1 else E.Const(None)
|
|
123
|
+
return out
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _from_lists(fn: str, keys: E.Expr, values: E.Expr) -> Dict[str, E.Expr]:
|
|
127
|
+
"""Util.mapFromLists: the keys and values position by position; two sizes or a null list raise."""
|
|
128
|
+
if _null(keys) or _null(values):
|
|
129
|
+
_refuse(fn, "a null list of keys or values (an error in APOC)")
|
|
130
|
+
ks, vs = _items(fn, keys, "keys"), _items(fn, values, "values")
|
|
131
|
+
if len(ks) != len(vs):
|
|
132
|
+
_refuse(fn, "lists of keys and values of two sizes (an error in APOC)")
|
|
133
|
+
out: Dict[str, E.Expr] = {}
|
|
134
|
+
for k, v in zip(ks, vs):
|
|
135
|
+
text = _key(fn, k)
|
|
136
|
+
if text is None:
|
|
137
|
+
_refuse(fn, "a null key")
|
|
138
|
+
out[text] = v
|
|
139
|
+
return out
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _from_values(fn: str, values: E.Expr) -> Dict[str, E.Expr]:
|
|
143
|
+
"""Util.map: keys and values alternating; a null key's pair skipped; an odd count raises."""
|
|
144
|
+
_not_null(fn, values, "list")
|
|
145
|
+
items = _items(fn, values, "values")
|
|
146
|
+
if len(items) % 2:
|
|
147
|
+
_refuse(fn, "an odd number of keys and values (an error in APOC)")
|
|
148
|
+
out: Dict[str, E.Expr] = {}
|
|
149
|
+
for k, v in zip(items[0::2], items[1::2]):
|
|
150
|
+
text = _key(fn, k, any_type=True)
|
|
151
|
+
if text is not None:
|
|
152
|
+
out[text] = v
|
|
153
|
+
return out
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _merged(*maps: Dict[str, E.Expr]) -> Dict[str, E.Expr]:
|
|
157
|
+
out: Dict[str, E.Expr] = {}
|
|
158
|
+
for m in maps:
|
|
159
|
+
out.update(m)
|
|
160
|
+
return out
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _of(fn: str, x: E.Expr, slot_of: SlotOf) -> Dict[str, E.Expr]:
|
|
164
|
+
"""A map argument that may be null (merge's, setKey's): null as no key."""
|
|
165
|
+
return {} if _null(x) else _fields(fn, x, slot_of)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def from_pairs(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
169
|
+
return _map(_from_pairs("fromPairs", a[0]))
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def from_lists(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
173
|
+
return _map(_from_lists("fromLists", a[0], a[1]))
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def from_values(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
177
|
+
return _map(_from_values("fromValues", a[0]))
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def merge(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
181
|
+
"""Util.merge: the second map's keys over the first's; a null map is no key."""
|
|
182
|
+
return _map(_merged(_of("merge", a[0], s), _of("merge", a[1], s)))
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def merge_list(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
186
|
+
_not_null("mergeList", a[0], "list of maps")
|
|
187
|
+
return _map(_merged(*(_fields("mergeList", m, s) for m in _items("mergeList", a[0], "maps"))))
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def set_key(fn: str):
|
|
191
|
+
def rewrite(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
192
|
+
"""Util.merge(map, Util.map(key, value)): a null key adds nothing."""
|
|
193
|
+
k = _key(fn, a[1])
|
|
194
|
+
return _map(_merged(_of(fn, a[0], s), {} if k is None else {k: a[2]}))
|
|
195
|
+
return rewrite
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def set_pairs(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
199
|
+
return _map(_merged(_of("setPairs", a[0], s), _from_pairs("setPairs", a[1])))
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def set_lists(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
203
|
+
return _map(_merged(_of("setLists", a[0], s), _from_lists("setLists", a[1], a[2])))
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def set_values(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
207
|
+
return _map(_merged(_of("setValues", a[0], s), _from_values("setValues", a[1])))
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
# --- removing keys --------------------------------------------------------------------------------------
|
|
211
|
+
|
|
212
|
+
def _recursive(fn: str, a: List[E.Expr]) -> bool:
|
|
213
|
+
if len(a) < 3 or _null(a[2]):
|
|
214
|
+
return False
|
|
215
|
+
config = a[2]
|
|
216
|
+
if not (isinstance(config, E.Call) and config.name == "MAP"):
|
|
217
|
+
_refuse(fn, "a config that is not a map written in the query")
|
|
218
|
+
for k, v in zip(config.args[0::2], config.args[1::2]):
|
|
219
|
+
if k.value == "recursive":
|
|
220
|
+
if not isinstance(v, E.Const):
|
|
221
|
+
_refuse(fn, "a recursive flag not written in the query")
|
|
222
|
+
value = v.value # Util.toBoolean: null, 0, "", "false", "no", "0" and false are false
|
|
223
|
+
return not (value is None or value is False or (isinstance(value, (int, float)) and not
|
|
224
|
+
isinstance(value, bool) and value == 0) or
|
|
225
|
+
(isinstance(value, str) and value.lower() in ("", "false", "no", "0")))
|
|
226
|
+
return False
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def _without(fn: str, fields: Dict[str, E.Expr], keys: List[str], recursive: bool, s: SlotOf) -> Dict[str, E.Expr]:
|
|
230
|
+
out = {k: v for k, v in fields.items() if k not in keys}
|
|
231
|
+
if not recursive:
|
|
232
|
+
return out
|
|
233
|
+
for k, v in list(out.items()):
|
|
234
|
+
kinds = s(v).kinds
|
|
235
|
+
if kinds == {MAP}:
|
|
236
|
+
if not (isinstance(v, E.Call) and v.name == "MAP"):
|
|
237
|
+
_refuse(fn, "a nested map that is not written in the query, recursively (it may be null)")
|
|
238
|
+
inner = _without(fn, _fields(fn, v, s), keys, True, s)
|
|
239
|
+
if inner:
|
|
240
|
+
out[k] = _map(inner)
|
|
241
|
+
else:
|
|
242
|
+
del out[k]
|
|
243
|
+
elif kinds == {LIST}:
|
|
244
|
+
_refuse(fn, "a list in the map, recursively (an emptied list drops its key in APOC)")
|
|
245
|
+
return out
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def remove_keys(fn: str, single: bool):
|
|
249
|
+
def rewrite(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
250
|
+
"""removeKey / removeKeys: the map without the keys (with recursive: in nested maps too, a map left
|
|
251
|
+
empty removed); a null map raises."""
|
|
252
|
+
_not_null(fn, a[0], "map")
|
|
253
|
+
fields = _fields(fn, a[0], s)
|
|
254
|
+
if single:
|
|
255
|
+
keys = [_key(fn, a[1])]
|
|
256
|
+
if keys[0] not in fields:
|
|
257
|
+
return _map(fields)
|
|
258
|
+
else:
|
|
259
|
+
_not_null(fn, a[1], "list of keys")
|
|
260
|
+
keys = [_key(fn, k) for k in _items(fn, a[1], "keys")]
|
|
261
|
+
return _map(_without(fn, fields, keys, _recursive(fn, a), s))
|
|
262
|
+
return rewrite
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def clean(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
266
|
+
"""clean(map, keys, values): without the given keys, the null values, and the values equal to a given
|
|
267
|
+
one (Java's equals: 1 is no 1.0) or whose text is one. A value read at run time decides its key only
|
|
268
|
+
where nothing could drop it (no values given and never null)."""
|
|
269
|
+
for x, what in ((a[0], "map"), (a[1], "list of keys"), (a[2], "list of values")):
|
|
270
|
+
_not_null("clean", x, what)
|
|
271
|
+
keys = {_key("clean", k) for k in _items("clean", a[1], "keys")}
|
|
272
|
+
drop = []
|
|
273
|
+
for v in _items("clean", a[2], "values"):
|
|
274
|
+
if not isinstance(v, E.Const) or isinstance(v.value, (list, tuple, dict)):
|
|
275
|
+
_refuse("clean", "values to remove that are not plain values written in the query")
|
|
276
|
+
drop.append(v.value)
|
|
277
|
+
texts = {x for x in drop if isinstance(x, str)}
|
|
278
|
+
out: Dict[str, E.Expr] = {}
|
|
279
|
+
for k, v in _fields("clean", a[0], s).items():
|
|
280
|
+
if k in keys:
|
|
281
|
+
continue
|
|
282
|
+
if isinstance(v, E.Const) and not isinstance(v.value, (list, tuple, dict)):
|
|
283
|
+
x = v.value
|
|
284
|
+
if x is None or any(type(x) is type(d) and x == d for d in drop) or _text("clean", x) in texts:
|
|
285
|
+
continue
|
|
286
|
+
out[k] = v
|
|
287
|
+
elif not drop and s(v).present is Bound.ALWAYS:
|
|
288
|
+
out[k] = v
|
|
289
|
+
else:
|
|
290
|
+
_refuse("clean", "a value read at run time (whether its key is kept would depend on the row)")
|
|
291
|
+
return _map(out)
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
# --- reading keys ----------------------------------------------------------------------------------------
|
|
295
|
+
|
|
296
|
+
def _get(fn: str, fields: Dict[str, E.Expr], key: Optional[str], default: E.Expr, fail: bool) -> E.Expr:
|
|
297
|
+
"""Maps.get: the key's value (null where it is null); a missing key's default; with fail, a missing key
|
|
298
|
+
and a null default raise."""
|
|
299
|
+
if key in fields:
|
|
300
|
+
return fields[key]
|
|
301
|
+
if fail and _null(default):
|
|
302
|
+
_refuse(fn, f"the key {key} missing from the map, with fail (an error in APOC)")
|
|
303
|
+
if fail and not isinstance(default, E.Const):
|
|
304
|
+
_refuse(fn, "a default read at run time for a missing key, with fail (null would raise)")
|
|
305
|
+
return default
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def get(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
309
|
+
_not_null("get", a[0], "map")
|
|
310
|
+
default = a[2] if len(a) > 2 else E.Const(None)
|
|
311
|
+
fail = _flag("get", a[3], "fail") if len(a) > 3 else True
|
|
312
|
+
return _get("get", _fields("get", a[0], s), _key("get", a[1]), default, fail)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def _defaults(fn: str, a: List[E.Expr]) -> List[E.Expr]:
|
|
316
|
+
if len(a) < 3 or _null(a[2]):
|
|
317
|
+
return []
|
|
318
|
+
return _items(fn, a[2], "default values")
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def mget(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
322
|
+
if _null(a[0]) or _null(a[1]):
|
|
323
|
+
return E.Const(None)
|
|
324
|
+
fields = _fields("mget", a[0], s)
|
|
325
|
+
defaults = _defaults("mget", a)
|
|
326
|
+
fail = _flag("mget", a[3], "fail") if len(a) > 3 else True
|
|
327
|
+
keys = [_key("mget", k) for k in _items("mget", a[1], "keys")]
|
|
328
|
+
return _list([_get("mget", fields, k, defaults[i] if i < len(defaults) else E.Const(None), fail)
|
|
329
|
+
for i, k in enumerate(keys)])
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def submap(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
333
|
+
if _null(a[0]) or _null(a[1]):
|
|
334
|
+
return E.Const(None)
|
|
335
|
+
fields = _fields("submap", a[0], s)
|
|
336
|
+
defaults = _defaults("submap", a)
|
|
337
|
+
fail = _flag("submap", a[3], "fail") if len(a) > 3 else True
|
|
338
|
+
out: Dict[str, E.Expr] = {}
|
|
339
|
+
for i, k in enumerate(_key("submap", k) for k in _items("submap", a[1], "keys")):
|
|
340
|
+
if k is None:
|
|
341
|
+
_refuse("submap", "a null key")
|
|
342
|
+
out[k] = _get("submap", fields, k, defaults[i] if i < len(defaults) else E.Const(None), fail)
|
|
343
|
+
return _map(out)
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def values(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
347
|
+
"""values(map, keys [], addNullsForMissing false): the keys' values, a missing key's null where
|
|
348
|
+
addNullsForMissing, else none; [] for no keys."""
|
|
349
|
+
if len(a) < 2 or _null(a[1]):
|
|
350
|
+
return _list([])
|
|
351
|
+
keys = [_key("values", k) for k in _items("values", a[1], "keys")]
|
|
352
|
+
if not keys:
|
|
353
|
+
return _list([])
|
|
354
|
+
_not_null("values", a[0], "map")
|
|
355
|
+
fields = _fields("values", a[0], s)
|
|
356
|
+
nulls = _flag("values", a[2], "addNullsForMissing") if len(a) > 2 else False
|
|
357
|
+
return _list([fields[k] if k in fields else E.Const(None) for k in keys if k in fields or nulls])
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def sorted_properties(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
361
|
+
"""sortedProperties(map, ignoreCase true): [key, value] pairs by key, String.CASE_INSENSITIVE_ORDER or
|
|
362
|
+
compareTo (UTF-16 units)."""
|
|
363
|
+
_not_null("sortedProperties", a[0], "map")
|
|
364
|
+
fields = _fields("sortedProperties", a[0], s)
|
|
365
|
+
ignore = _flag("sortedProperties", a[1], "ignoreCase") if len(a) > 1 else True
|
|
366
|
+
|
|
367
|
+
def folded(k: str):
|
|
368
|
+
return [ord(_one(_one(c, str.upper), str.lower)) for c in k]
|
|
369
|
+
order = sorted(fields, key=folded if ignore else (lambda k: k.encode("utf-16-be")))
|
|
370
|
+
return _list([_list([E.Const(k), fields[k]]) for k in order])
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
def _one(c: str, f) -> str:
|
|
374
|
+
"""Java's Character.toUpperCase / toLowerCase of one character: one character (a mapping to several
|
|
375
|
+
keeps the character)."""
|
|
376
|
+
m = f(c)
|
|
377
|
+
return m if len(m) == 1 else c
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
# --- nesting ------------------------------------------------------------------------------------------
|
|
381
|
+
|
|
382
|
+
def flatten(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
383
|
+
"""flatten(map, delimiter "."): each nested map's keys prefixed by its key and the delimiter (a null
|
|
384
|
+
delimiter is "."); a key written twice so refused (Neo4j's answer would depend on its map order)."""
|
|
385
|
+
_not_null("flatten", a[0], "map")
|
|
386
|
+
delim = "."
|
|
387
|
+
if len(a) > 1 and not _null(a[1]):
|
|
388
|
+
if not (isinstance(a[1], E.Const) and isinstance(a[1].value, str)):
|
|
389
|
+
_refuse("flatten", "a delimiter not written in the query")
|
|
390
|
+
delim = a[1].value
|
|
391
|
+
out: Dict[str, E.Expr] = {}
|
|
392
|
+
|
|
393
|
+
def walk(fields: Dict[str, E.Expr], prefix: str) -> None:
|
|
394
|
+
for k, v in fields.items():
|
|
395
|
+
if s(v).kinds == {MAP}:
|
|
396
|
+
if not (isinstance(v, E.Call) and v.name == "MAP"):
|
|
397
|
+
_refuse("flatten", "a nested map that is not written in the query (it may be null)")
|
|
398
|
+
walk(_fields("flatten", v, s), prefix + k + delim)
|
|
399
|
+
else:
|
|
400
|
+
if prefix + k in out:
|
|
401
|
+
_refuse("flatten", f"two entries flattened to the key {prefix + k}")
|
|
402
|
+
out[prefix + k] = v
|
|
403
|
+
walk(_fields("flatten", a[0], s), "")
|
|
404
|
+
return _map(out)
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
def unflatten(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
408
|
+
"""unflatten(map, delimiter "."): each key split at the delimiter's first occurrence into nested maps,
|
|
409
|
+
recursively (a blank delimiter is "."); a key both a value and a prefix refused (Neo4j's answer would
|
|
410
|
+
depend on its map order)."""
|
|
411
|
+
_not_null("unflatten", a[0], "map")
|
|
412
|
+
delim = "."
|
|
413
|
+
if len(a) > 1 and not _null(a[1]):
|
|
414
|
+
if not (isinstance(a[1], E.Const) and isinstance(a[1].value, str)):
|
|
415
|
+
_refuse("unflatten", "a delimiter not written in the query")
|
|
416
|
+
if a[1].value.strip():
|
|
417
|
+
delim = a[1].value
|
|
418
|
+
tree: Dict[str, object] = {}
|
|
419
|
+
for k, v in _fields("unflatten", a[0], s).items():
|
|
420
|
+
node, parts = tree, k.split(delim)
|
|
421
|
+
for part in parts[:-1]:
|
|
422
|
+
child = node.setdefault(part, {})
|
|
423
|
+
if not isinstance(child, dict):
|
|
424
|
+
_refuse("unflatten", f"the key {part} both a value and a prefix")
|
|
425
|
+
node = child
|
|
426
|
+
if parts[-1] in node:
|
|
427
|
+
_refuse("unflatten", f"the key {parts[-1]} both a value and a prefix")
|
|
428
|
+
node[parts[-1]] = v
|
|
429
|
+
|
|
430
|
+
def build(node: Dict[str, object]) -> E.Expr:
|
|
431
|
+
return _map({k: build(v) if isinstance(v, dict) else v for k, v in node.items()})
|
|
432
|
+
return build(tree)
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
def group_by(a: List[E.Expr], s: SlotOf) -> E.Expr:
|
|
436
|
+
"""groupBy(values, key): each map by its key's value's text (a later one wins); a map without the key,
|
|
437
|
+
or whose value is null, left out. The key's values must be written in the query (they are the answer's
|
|
438
|
+
keys)."""
|
|
439
|
+
_not_null("groupBy", a[0], "list")
|
|
440
|
+
key = _key("groupBy", a[1])
|
|
441
|
+
out: Dict[str, E.Expr] = {}
|
|
442
|
+
for item in _items("groupBy", a[0], "values"):
|
|
443
|
+
fields = _fields("groupBy", item, s)
|
|
444
|
+
if key not in fields:
|
|
445
|
+
continue
|
|
446
|
+
v = fields[key]
|
|
447
|
+
if not isinstance(v, E.Const):
|
|
448
|
+
_refuse("groupBy", "a key's value read at run time (it would be one of the answer's keys)")
|
|
449
|
+
text = _text("groupBy", v.value)
|
|
450
|
+
if text is not None:
|
|
451
|
+
out[text] = item
|
|
452
|
+
return _map(out)
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
def _nulls_written(a: List[E.Expr], s: SlotOf) -> List[E.Expr]:
|
|
456
|
+
"""The arguments, one that is null in every row (an unmapped property, `UNWIND [null] AS m`) as a
|
|
457
|
+
written null: APOC reads it as the null it is, never as an empty map or list."""
|
|
458
|
+
from prograph.plan.ir.facts import NotBuilt
|
|
459
|
+
out = []
|
|
460
|
+
for x in a:
|
|
461
|
+
try:
|
|
462
|
+
never = not isinstance(x, E.Const) and s(x).present is Bound.NEVER
|
|
463
|
+
except NotBuilt: # a list of several types, read only element by element here
|
|
464
|
+
never = False
|
|
465
|
+
out.append(E.Const(None) if never else x)
|
|
466
|
+
return out
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
def _f(name: str, arity, rewrite) -> _Function:
|
|
470
|
+
return _Function(_NAME + name, arity, None, None, rewrite=lambda a, s: rewrite(_nulls_written(a, s), s))
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
FUNCTIONS = [
|
|
474
|
+
_f("fromPairs", (1,), from_pairs),
|
|
475
|
+
_f("fromLists", (2,), from_lists),
|
|
476
|
+
_f("fromValues", (1,), from_values),
|
|
477
|
+
_f("merge", (2,), merge),
|
|
478
|
+
_f("mergeList", (1,), merge_list),
|
|
479
|
+
_f("setKey", (3,), set_key("setKey")),
|
|
480
|
+
_f("setEntry", (3,), set_key("setEntry")),
|
|
481
|
+
_f("setPairs", (2,), set_pairs),
|
|
482
|
+
_f("setLists", (3,), set_lists),
|
|
483
|
+
_f("setValues", (2,), set_values),
|
|
484
|
+
_f("removeKey", (2, 3), remove_keys("removeKey", single=True)),
|
|
485
|
+
_f("removeKeys", (2, 3), remove_keys("removeKeys", single=False)),
|
|
486
|
+
_f("clean", (3,), clean),
|
|
487
|
+
_f("get", (2, 3, 4), get),
|
|
488
|
+
_f("mget", (2, 3, 4), mget),
|
|
489
|
+
_f("submap", (2, 3, 4), submap),
|
|
490
|
+
_f("values", (1, 2, 3), values),
|
|
491
|
+
_f("flatten", (1, 2), flatten),
|
|
492
|
+
_f("unflatten", (1, 2), unflatten),
|
|
493
|
+
_f("groupBy", (2,), group_by),
|
|
494
|
+
_f("sortedProperties", (1, 2), sorted_properties),
|
|
495
|
+
]
|
|
496
|
+
"""The apoc.map.* rows of the cypher-apoc table: each a rewrite into the map and list homes' calls."""
|