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,505 @@
|
|
|
1
|
+
"""APOC Core's apoc.convert.* functions: rows of the cypher-apoc table (`cypher_apoc.TABLE`).
|
|
2
|
+
|
|
3
|
+
Each follows APOC Core 5.26's code (apoc/convert/Convert.java, Json.java, apoc/util/JsonUtil.java: Jackson),
|
|
4
|
+
checked against Neo4j 5 with APOC Core (tests/cypher/apoc/answers.json), and builds on the map, list and
|
|
5
|
+
conversion homes.
|
|
6
|
+
|
|
7
|
+
- toJson writes Jackson's text in SQL from the value's type, known at planning: strings escaped as Jackson
|
|
8
|
+
escapes them, numbers as Java writes them, lists in order, a map's keys in a java.util.HashMap's order
|
|
9
|
+
(its buckets by the keys' hashes; keys in one bucket in the order the map was written). A float read from
|
|
10
|
+
a column (Java's text of a double), a node, and a map read at run time whose keys share a bucket are refused.
|
|
11
|
+
- fromJsonMap and fromJsonList read a JSON text written in the query at planning (Jackson's lenient forms:
|
|
12
|
+
single quotes, unquoted names, comments) into a map or list literal; a JSON read at run time, whose shape
|
|
13
|
+
ProGraph's maps cannot hold, and a JSON path, are refused.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import math
|
|
19
|
+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple
|
|
20
|
+
|
|
21
|
+
from prograph.plan.ir import expr as E
|
|
22
|
+
from prograph.plan.ir.cypher_apoc import ApocFunctionError, _Function, _raising
|
|
23
|
+
from prograph.plan.ir.facts import NotBuilt, expr_slot
|
|
24
|
+
from prograph.plan.ir.slot import LIST, MAP, NOTHING, VALUE, Bound, Slot, list_slot, value_slot
|
|
25
|
+
from prograph.plan.sql import rel_algebra as R
|
|
26
|
+
from prograph.schema import xsd
|
|
27
|
+
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
from prograph.plan.ir import lower as L
|
|
30
|
+
|
|
31
|
+
_INTEGERS = frozenset({xsd.INTEGER}) | xsd.INTEGER_DATATYPES
|
|
32
|
+
_FLOATS = frozenset({xsd.DOUBLE, xsd.FLOAT})
|
|
33
|
+
_S_STRING = ("scalar", xsd.STRING)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _refuse(fn: str, why: str):
|
|
37
|
+
raise ApocFunctionError(fn, why)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
# --- Jackson's text --------------------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
_ESCAPES = {"\\": "\\\\", '"': '\\"', "\n": "\\n", "\r": "\\r", "\t": "\\t", "\b": "\\b", "\f": "\\f"}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _json_string(s: str) -> str:
|
|
46
|
+
out = []
|
|
47
|
+
for c in s:
|
|
48
|
+
if c in _ESCAPES:
|
|
49
|
+
out.append(_ESCAPES[c])
|
|
50
|
+
elif ord(c) < 0x20:
|
|
51
|
+
out.append(f"\\u{ord(c):04X}")
|
|
52
|
+
else:
|
|
53
|
+
out.append(c)
|
|
54
|
+
return '"' + "".join(out) + '"'
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _java_hash(s: str) -> int:
|
|
58
|
+
"""java.lang.String.hashCode, over UTF-16 units."""
|
|
59
|
+
h = 0
|
|
60
|
+
data = s.encode("utf-16-be")
|
|
61
|
+
for i in range(0, len(data), 2):
|
|
62
|
+
h = (31 * h + int.from_bytes(data[i:i + 2], "big")) & 0xFFFFFFFF
|
|
63
|
+
return h
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def hashmap_order(keys: List[str]) -> Tuple[List[str], bool]:
|
|
67
|
+
"""The keys in a java.util.HashMap's iteration order, put in this order into `new HashMap<>()`: by bucket
|
|
68
|
+
(the hash spread, modulo the table's size, 16 doubled while more than three quarters full), keys in one
|
|
69
|
+
bucket in the order they were put. Also whether two keys share a bucket."""
|
|
70
|
+
size = 16
|
|
71
|
+
while len(keys) > size * 3 // 4:
|
|
72
|
+
size *= 2
|
|
73
|
+
|
|
74
|
+
def bucket(k: str) -> int:
|
|
75
|
+
h = _java_hash(k)
|
|
76
|
+
return (h ^ (h >> 16)) & (size - 1)
|
|
77
|
+
buckets = [bucket(k) for k in keys]
|
|
78
|
+
order = sorted(range(len(keys)), key=lambda i: (buckets[i], i))
|
|
79
|
+
return [keys[i] for i in order], len(set(buckets)) < len(keys)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _const_json(fn: str, v: Any) -> str:
|
|
83
|
+
"""A value written in the query, as Jackson writes it."""
|
|
84
|
+
from prograph.plan.ir.cypher_functions import java_double
|
|
85
|
+
if v is None:
|
|
86
|
+
return "null"
|
|
87
|
+
if isinstance(v, bool):
|
|
88
|
+
return "true" if v else "false"
|
|
89
|
+
if isinstance(v, int):
|
|
90
|
+
return str(v)
|
|
91
|
+
if isinstance(v, float):
|
|
92
|
+
if not math.isfinite(v):
|
|
93
|
+
return '"NaN"' if repr(v) == "nan" else ('"Infinity"' if v > 0 else '"-Infinity"')
|
|
94
|
+
return java_double(v)
|
|
95
|
+
if isinstance(v, str):
|
|
96
|
+
return _json_string(v)
|
|
97
|
+
if isinstance(v, (list, tuple)):
|
|
98
|
+
return "[" + ",".join(_const_json(fn, x) for x in v) + "]"
|
|
99
|
+
_refuse(fn, "a value that is not a string, a number, a boolean, a list or a map")
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _escaped(x: R.Expr, L) -> R.Expr:
|
|
103
|
+
"""A string's text inside Jackson's quotes: \\ " and the usual controls escaped; another control
|
|
104
|
+
character (written \\u00XX) raises (not built)."""
|
|
105
|
+
from prograph.plan.ir.cypher_apoc_text import _matches
|
|
106
|
+
out = x
|
|
107
|
+
for c, e in _ESCAPES.items():
|
|
108
|
+
out = L.fn("REPLACE", out, L.text(c), L.text(e))
|
|
109
|
+
bad = _matches(x, "[\\x00-\\x07\\x0B\\x0E-\\x1F]")
|
|
110
|
+
return _raising(L.fn("COALESCE", bad, L.FALSE), "apoc.convert.toJson() of a string holding a control "
|
|
111
|
+
"character (written \\u00XX; not built)", out, _S_STRING)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _json_cell(fn: str, cell: "L.Cell", L, written: Optional[List[str]] = None) -> R.Expr:
|
|
115
|
+
"""Jackson's text of a value read at run time, by its slot."""
|
|
116
|
+
from prograph.plan.ir.cypher_lists import element_slot
|
|
117
|
+
from prograph.plan.ir.cypher_maps import fields_of, _read
|
|
118
|
+
from prograph.plan.ir.cypher_temporal import TEMPORAL, text as temporal_text
|
|
119
|
+
s = cell.slot
|
|
120
|
+
if s.present is Bound.NEVER:
|
|
121
|
+
return L.text("null")
|
|
122
|
+
v = cell.value
|
|
123
|
+
if s.kinds == {LIST}:
|
|
124
|
+
if not s.datatypes:
|
|
125
|
+
body = L.text("[]")
|
|
126
|
+
else:
|
|
127
|
+
from prograph.plan.ir.cypher_apoc import _var
|
|
128
|
+
x = _var()
|
|
129
|
+
items = L.fn("LIST_TRANSFORM", v, x, _json_cell(fn, L.Cell(element_slot(s), x), L))
|
|
130
|
+
body = L.fn("STR_CONCAT", L.fn("STR_CONCAT", L.text("["), L.fn("CYPHER_STR_JOIN", items, L.text(","))),
|
|
131
|
+
L.text("]"))
|
|
132
|
+
elif s.kinds == {MAP}:
|
|
133
|
+
from prograph.plan.ir.cypher_lists import typed_slot
|
|
134
|
+
fields = fields_of(s)
|
|
135
|
+
keys, shared = hashmap_order(written or sorted(fields))
|
|
136
|
+
if shared and written is None:
|
|
137
|
+
_refuse(fn, "a map read at run time whose keys share a bucket of Java's HashMap (their order is "
|
|
138
|
+
"the map's, unknown here)")
|
|
139
|
+
parts: List[R.Expr] = [L.text("{")]
|
|
140
|
+
for i, k in enumerate(keys):
|
|
141
|
+
t = fields[k]
|
|
142
|
+
value = L.text("null") if t is None else _json_cell(fn, _read(cell, k, typed_slot(t, Bound.MAYBE)), L)
|
|
143
|
+
parts += [L.text(("," if i else "") + _json_string(k) + ":"), value]
|
|
144
|
+
parts.append(L.text("}"))
|
|
145
|
+
body = parts[0]
|
|
146
|
+
for p in parts[1:]:
|
|
147
|
+
body = L.fn("STR_CONCAT", body, p)
|
|
148
|
+
elif s.kinds == {VALUE} and len(s.datatypes) == 1:
|
|
149
|
+
t = s.datatype
|
|
150
|
+
if t == xsd.STRING:
|
|
151
|
+
body = L.fn("STR_CONCAT", L.fn("STR_CONCAT", L.text('"'), _escaped(v, L)), L.text('"'))
|
|
152
|
+
elif t in _INTEGERS:
|
|
153
|
+
body = L.fn("CAST_STRING", v)
|
|
154
|
+
elif t == xsd.BOOLEAN:
|
|
155
|
+
body = L.case(v, L.text("true"), L.text("false"))
|
|
156
|
+
elif t in TEMPORAL:
|
|
157
|
+
body = L.fn("STR_CONCAT", L.fn("STR_CONCAT", L.text('"'), temporal_text(t, v, L)), L.text('"'))
|
|
158
|
+
elif t in _FLOATS:
|
|
159
|
+
_refuse(fn, "a float read at run time (Java's text of a double)")
|
|
160
|
+
else:
|
|
161
|
+
_refuse(fn, f"a value of type {t}")
|
|
162
|
+
else:
|
|
163
|
+
_refuse(fn, "a node, a relationship or a value of no one type")
|
|
164
|
+
return L.case(L.is_null(v), L.text("null"), body) if s.present is not Bound.ALWAYS else body
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _json_expr(fn: str, x: E.Expr, cells, L) -> R.Expr:
|
|
168
|
+
"""Jackson's text of an expression: a literal list or map element by element (any mix of types), a
|
|
169
|
+
constant at planning, anything else from its cell."""
|
|
170
|
+
if isinstance(x, E.Const) and not hasattr(x.value, "days") and not hasattr(x.value, "nanos") \
|
|
171
|
+
and not hasattr(x.value, "instant") and not hasattr(x.value, "months"):
|
|
172
|
+
return L.text(_const_json(fn, x.value))
|
|
173
|
+
if isinstance(x, E.Call) and x.name == "LIST":
|
|
174
|
+
parts = [L.text("[")]
|
|
175
|
+
for i, item in enumerate(x.args):
|
|
176
|
+
if i:
|
|
177
|
+
parts.append(L.text(","))
|
|
178
|
+
parts.append(_json_expr(fn, item, cells, L))
|
|
179
|
+
parts.append(L.text("]"))
|
|
180
|
+
return _concat(parts, L)
|
|
181
|
+
if isinstance(x, E.Call) and x.name == "MAP":
|
|
182
|
+
written = [k.value for k in x.args[0::2]]
|
|
183
|
+
values = dict(zip(written, x.args[1::2]))
|
|
184
|
+
keys, _ = hashmap_order(written)
|
|
185
|
+
parts = [L.text("{")]
|
|
186
|
+
for i, k in enumerate(keys):
|
|
187
|
+
parts += [L.text(("," if i else "") + _json_string(k) + ":"), _json_expr(fn, values[k], cells, L)]
|
|
188
|
+
parts.append(L.text("}"))
|
|
189
|
+
return _concat(parts, L)
|
|
190
|
+
return _json_cell(fn, L.expr_cell(x, cells), L)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _concat(parts, L) -> R.Expr:
|
|
194
|
+
out = parts[0]
|
|
195
|
+
for p in parts[1:]:
|
|
196
|
+
out = L.fn("STR_CONCAT", out, p)
|
|
197
|
+
return out
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def to_json(e_args, cells, L) -> "L.Cell":
|
|
201
|
+
return L.Cell(value_slot(xsd.STRING, Bound.NEVER), _json_expr("apoc.convert.toJson", e_args[0], cells, L))
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def to_sorted_json_map(e_args, cells, L) -> "L.Cell":
|
|
205
|
+
"""toSortedJsonMap(value, ignoreCase true): a map's keys sorted (String.CASE_INSENSITIVE_ORDER or
|
|
206
|
+
compareTo), each value as toJson writes it; not a map raises."""
|
|
207
|
+
from prograph.plan.ir.cypher_apoc_map import _one
|
|
208
|
+
from prograph.plan.ir.cypher_maps import fields_of
|
|
209
|
+
fn = "apoc.convert.toSortedJsonMap"
|
|
210
|
+
x = e_args[0]
|
|
211
|
+
ignore = True
|
|
212
|
+
if len(e_args) > 1:
|
|
213
|
+
if not (isinstance(e_args[1], E.Const) and isinstance(e_args[1].value, bool)):
|
|
214
|
+
_refuse(fn, "an ignoreCase that is not true or false written in the query")
|
|
215
|
+
ignore = e_args[1].value
|
|
216
|
+
if isinstance(x, E.Call) and x.name == "MAP":
|
|
217
|
+
values = dict(zip((k.value for k in x.args[0::2]), x.args[1::2]))
|
|
218
|
+
cell = None
|
|
219
|
+
else:
|
|
220
|
+
cell = L.expr_cell(x, cells)
|
|
221
|
+
if cell.slot.kinds != {MAP} or cell.slot.present is not Bound.ALWAYS:
|
|
222
|
+
_refuse(fn, "a value that is not a map, or a map that may be null (an error in APOC)")
|
|
223
|
+
values = {k: E.Call("FIELD", (x, E.Const(k)), E.CYPHER) for k in fields_of(cell.slot)}
|
|
224
|
+
|
|
225
|
+
def folded(k: str):
|
|
226
|
+
return [ord(_one(_one(c, str.upper), str.lower)) for c in k]
|
|
227
|
+
keys = sorted(values, key=folded if ignore else (lambda k: k.encode("utf-16-be")))
|
|
228
|
+
parts = [L.text("{")]
|
|
229
|
+
for i, k in enumerate(keys):
|
|
230
|
+
parts += [L.text(("," if i else "") + _json_string(k) + ":"), _json_expr(fn, values[k], cells, L)]
|
|
231
|
+
parts.append(L.text("}"))
|
|
232
|
+
return L.Cell(value_slot(xsd.STRING, Bound.NEVER), _concat(parts, L))
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
# --- reading JSON written in the query ----------------------------------------------------------------
|
|
236
|
+
|
|
237
|
+
class _JsonError(ValueError):
|
|
238
|
+
pass
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
class _Reader:
|
|
242
|
+
"""Jackson's reading with APOC's features: single quotes, unquoted field names, comments, backslash
|
|
243
|
+
before any character, NaN and Infinity; integers as longs."""
|
|
244
|
+
|
|
245
|
+
def __init__(self, text: str) -> None:
|
|
246
|
+
self.s, self.i = text, 0
|
|
247
|
+
|
|
248
|
+
def ws(self) -> None:
|
|
249
|
+
s = self.s
|
|
250
|
+
while self.i < len(s):
|
|
251
|
+
if s[self.i] in " \t\r\n":
|
|
252
|
+
self.i += 1
|
|
253
|
+
elif s.startswith("//", self.i):
|
|
254
|
+
j = s.find("\n", self.i)
|
|
255
|
+
self.i = len(s) if j < 0 else j + 1
|
|
256
|
+
elif s.startswith("/*", self.i):
|
|
257
|
+
j = s.find("*/", self.i + 2)
|
|
258
|
+
if j < 0:
|
|
259
|
+
raise _JsonError("an unclosed comment")
|
|
260
|
+
self.i = j + 2
|
|
261
|
+
else:
|
|
262
|
+
break
|
|
263
|
+
|
|
264
|
+
def value(self) -> Any:
|
|
265
|
+
self.ws()
|
|
266
|
+
c = self.s[self.i:self.i + 1]
|
|
267
|
+
if c == "{":
|
|
268
|
+
return self.obj()
|
|
269
|
+
if c == "[":
|
|
270
|
+
return self.arr()
|
|
271
|
+
if c in ("\"", "'"):
|
|
272
|
+
return self.string()
|
|
273
|
+
for word, v in (("true", True), ("false", False), ("null", None), ("NaN", float("nan")),
|
|
274
|
+
("Infinity", float("inf")), ("-Infinity", float("-inf"))):
|
|
275
|
+
if self.s.startswith(word, self.i):
|
|
276
|
+
self.i += len(word)
|
|
277
|
+
return v
|
|
278
|
+
return self.number()
|
|
279
|
+
|
|
280
|
+
def obj(self) -> Dict[str, Any]:
|
|
281
|
+
self.i += 1
|
|
282
|
+
out: Dict[str, Any] = {}
|
|
283
|
+
self.ws()
|
|
284
|
+
if self.s[self.i:self.i + 1] == "}":
|
|
285
|
+
self.i += 1
|
|
286
|
+
return out
|
|
287
|
+
while True:
|
|
288
|
+
self.ws()
|
|
289
|
+
c = self.s[self.i:self.i + 1]
|
|
290
|
+
if c in ("\"", "'"):
|
|
291
|
+
key = self.string()
|
|
292
|
+
else:
|
|
293
|
+
j = self.i
|
|
294
|
+
while j < len(self.s) and (self.s[j].isalnum() or self.s[j] in "_$"):
|
|
295
|
+
j += 1
|
|
296
|
+
if j == self.i:
|
|
297
|
+
raise _JsonError("a field name")
|
|
298
|
+
key, self.i = self.s[self.i:j], j
|
|
299
|
+
self.ws()
|
|
300
|
+
if self.s[self.i:self.i + 1] != ":":
|
|
301
|
+
raise _JsonError("a ':'")
|
|
302
|
+
self.i += 1
|
|
303
|
+
out.pop(key, None)
|
|
304
|
+
out[key] = self.value()
|
|
305
|
+
self.ws()
|
|
306
|
+
c = self.s[self.i:self.i + 1]
|
|
307
|
+
self.i += 1
|
|
308
|
+
if c == "}":
|
|
309
|
+
return out
|
|
310
|
+
if c != ",":
|
|
311
|
+
raise _JsonError("a ',' or '}'")
|
|
312
|
+
|
|
313
|
+
def arr(self) -> List[Any]:
|
|
314
|
+
self.i += 1
|
|
315
|
+
out: List[Any] = []
|
|
316
|
+
self.ws()
|
|
317
|
+
if self.s[self.i:self.i + 1] == "]":
|
|
318
|
+
self.i += 1
|
|
319
|
+
return out
|
|
320
|
+
while True:
|
|
321
|
+
out.append(self.value())
|
|
322
|
+
self.ws()
|
|
323
|
+
c = self.s[self.i:self.i + 1]
|
|
324
|
+
self.i += 1
|
|
325
|
+
if c == "]":
|
|
326
|
+
return out
|
|
327
|
+
if c != ",":
|
|
328
|
+
raise _JsonError("a ',' or ']'")
|
|
329
|
+
|
|
330
|
+
def string(self) -> str:
|
|
331
|
+
quote = self.s[self.i]
|
|
332
|
+
self.i += 1
|
|
333
|
+
out = []
|
|
334
|
+
while True:
|
|
335
|
+
if self.i >= len(self.s):
|
|
336
|
+
raise _JsonError("an unclosed string")
|
|
337
|
+
c = self.s[self.i]
|
|
338
|
+
if c == quote:
|
|
339
|
+
self.i += 1
|
|
340
|
+
return "".join(out)
|
|
341
|
+
if c == "\\":
|
|
342
|
+
n = self.s[self.i + 1:self.i + 2]
|
|
343
|
+
if n == "u":
|
|
344
|
+
out.append(chr(int(self.s[self.i + 2:self.i + 6], 16)))
|
|
345
|
+
self.i += 6
|
|
346
|
+
continue
|
|
347
|
+
out.append({"n": "\n", "t": "\t", "r": "\r", "b": "\b", "f": "\f"}.get(n, n))
|
|
348
|
+
self.i += 2
|
|
349
|
+
continue
|
|
350
|
+
out.append(c)
|
|
351
|
+
self.i += 1
|
|
352
|
+
|
|
353
|
+
def number(self) -> Any:
|
|
354
|
+
import re
|
|
355
|
+
m = re.compile(r"-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?").match(self.s, self.i)
|
|
356
|
+
if not m or m.end() == self.i:
|
|
357
|
+
raise _JsonError("a value")
|
|
358
|
+
self.i = m.end()
|
|
359
|
+
text = m.group(0)
|
|
360
|
+
if m.group(2) or m.group(3):
|
|
361
|
+
return float(text)
|
|
362
|
+
n = int(text)
|
|
363
|
+
if not -2 ** 63 <= n < 2 ** 63:
|
|
364
|
+
raise _JsonError("an integer past 64 bits (a BigInteger in Jackson)")
|
|
365
|
+
return n
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def _expr_of(v: Any) -> E.Expr:
|
|
369
|
+
"""A parsed JSON value as a Cypher literal: a map literal, a list literal, a constant."""
|
|
370
|
+
if isinstance(v, dict):
|
|
371
|
+
parts: List[E.Expr] = []
|
|
372
|
+
for k, x in v.items():
|
|
373
|
+
parts += [E.Const(k), _expr_of(x)]
|
|
374
|
+
return E.Call("MAP", tuple(parts), E.CYPHER)
|
|
375
|
+
if isinstance(v, list):
|
|
376
|
+
return E.Call("LIST", tuple(_expr_of(x) for x in v), E.CYPHER)
|
|
377
|
+
return E.Const(v)
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def _from_json(fn: str, kind: type, a: List[E.Expr]) -> E.Expr:
|
|
381
|
+
text = a[0]
|
|
382
|
+
if len(a) > 1 and not (isinstance(a[1], E.Const) and a[1].value in (None, "")):
|
|
383
|
+
_refuse(fn, "a JSON path (not built)")
|
|
384
|
+
if len(a) > 2 and not (isinstance(a[2], E.Const) and a[2].value is None):
|
|
385
|
+
_refuse(fn, "path options (not built)")
|
|
386
|
+
if not isinstance(text, E.Const):
|
|
387
|
+
_refuse(fn, "a JSON text not written in the query (its shape, the answer's keys and types, would be "
|
|
388
|
+
"known only at run time)")
|
|
389
|
+
if text.value is None or text.value == "":
|
|
390
|
+
return E.Const(None)
|
|
391
|
+
if not isinstance(text.value, str):
|
|
392
|
+
_refuse(fn, "a value that is not a string")
|
|
393
|
+
r = _Reader(text.value)
|
|
394
|
+
try:
|
|
395
|
+
v = r.value()
|
|
396
|
+
r.ws()
|
|
397
|
+
if r.i != len(r.s):
|
|
398
|
+
raise _JsonError("text after the value")
|
|
399
|
+
except (_JsonError, IndexError, ValueError) as why:
|
|
400
|
+
_refuse(fn, f"text that is no JSON ({why}; an error in APOC)")
|
|
401
|
+
if not isinstance(v, kind):
|
|
402
|
+
_refuse(fn, f"JSON that is not a {'map' if kind is dict else 'list'} (an error in APOC)")
|
|
403
|
+
return _expr_of(v)
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
def from_json_map(a: List[E.Expr], s) -> E.Expr:
|
|
407
|
+
return _from_json("apoc.convert.fromJsonMap", dict, a)
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def from_json_list(a: List[E.Expr], s) -> E.Expr:
|
|
411
|
+
return _from_json("apoc.convert.fromJsonList", list, a)
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def to_map(a: List[E.Expr], s) -> E.Expr:
|
|
415
|
+
"""toMap(value): a map as it is; any other value null (a node's properties are not built)."""
|
|
416
|
+
x = a[0]
|
|
417
|
+
if isinstance(x, E.Const) and x.value is None:
|
|
418
|
+
return x
|
|
419
|
+
slot = s(x)
|
|
420
|
+
if slot.kinds == {MAP}:
|
|
421
|
+
return x
|
|
422
|
+
if slot.kinds == {VALUE} or slot.kinds == {LIST} or slot.present is Bound.NEVER:
|
|
423
|
+
return E.Const(None)
|
|
424
|
+
_refuse("apoc.convert.toMap", "a node or relationship (its properties as a map are not built)")
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def to_node(fn: str):
|
|
428
|
+
def rewrite(a: List[E.Expr], s) -> E.Expr:
|
|
429
|
+
slot = s(a[0])
|
|
430
|
+
if slot.present is Bound.NEVER or slot.kinds <= {VALUE, LIST, MAP}:
|
|
431
|
+
return E.Const(None)
|
|
432
|
+
_refuse(fn, "a node or relationship argument (not built)")
|
|
433
|
+
return rewrite
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
def to_node_list(fn: str):
|
|
437
|
+
def rewrite(a: List[E.Expr], s) -> E.Expr:
|
|
438
|
+
if isinstance(a[0], E.Const) and a[0].value is None:
|
|
439
|
+
return a[0]
|
|
440
|
+
_refuse(fn, "a list of nodes or relationships (not built)")
|
|
441
|
+
return rewrite
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
# --- toList and toSet -----------------------------------------------------------------------------------
|
|
445
|
+
|
|
446
|
+
def _listed_slot(slots: List[Slot], exprs=()) -> Slot:
|
|
447
|
+
from prograph.plan.ir.cypher_lists import value_type
|
|
448
|
+
x = slots[0]
|
|
449
|
+
if x.present is Bound.NEVER:
|
|
450
|
+
return NOTHING
|
|
451
|
+
if x.kinds == {LIST}:
|
|
452
|
+
return x
|
|
453
|
+
t = value_type(x)
|
|
454
|
+
if t is None:
|
|
455
|
+
raise ApocFunctionError("apoc.convert.toList", "a value of no one type")
|
|
456
|
+
return list_slot(t, Bound.NEVER if x.present is Bound.ALWAYS else Bound.MAYBE)
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
def to_list(args, L, exprs) -> R.Expr:
|
|
460
|
+
"""toList(value): a list as it is; null for null; any other value in a list of one."""
|
|
461
|
+
(x,) = args
|
|
462
|
+
if x.slot.kinds == {LIST}:
|
|
463
|
+
return x.value
|
|
464
|
+
out = L.fn("ARRAY", x.value)
|
|
465
|
+
return L.case(L.is_null(x.value), L.NULL, out) if x.slot.present is not Bound.ALWAYS else out
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
def to_set(args, L, exprs) -> R.Expr:
|
|
469
|
+
"""toSet(value): toList's list, each element once, first appearance first (LinkedHashSet)."""
|
|
470
|
+
from prograph.plan.ir.cypher_apoc import _distinct
|
|
471
|
+
(x,) = args
|
|
472
|
+
if x.slot.kinds == {LIST}:
|
|
473
|
+
if not x.slot.datatypes:
|
|
474
|
+
return x.value
|
|
475
|
+
return L.case(L.is_null(x.value), L.NULL, _distinct(x))
|
|
476
|
+
return to_list(args, L, exprs)
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
FUNCTIONS = [
|
|
480
|
+
_Function("apoc.convert.toJson", (1,), lambda slots, exprs=(): value_slot(xsd.STRING, Bound.NEVER),
|
|
481
|
+
None, exprs=True),
|
|
482
|
+
_Function("apoc.convert.toSortedJsonMap", (1, 2), lambda slots, exprs=(): value_slot(xsd.STRING, Bound.NEVER),
|
|
483
|
+
None, exprs=True),
|
|
484
|
+
_Function("apoc.convert.fromJsonMap", (1, 2, 3), None, None, rewrite=from_json_map),
|
|
485
|
+
_Function("apoc.convert.fromJsonList", (1, 2, 3), None, None, rewrite=from_json_list),
|
|
486
|
+
_Function("apoc.convert.toList", (1,), _listed_slot, to_list, exprs=True),
|
|
487
|
+
_Function("apoc.convert.toSet", (1,), _listed_slot, to_set, exprs=True),
|
|
488
|
+
_Function("apoc.convert.toMap", (1,), None, None, rewrite=to_map),
|
|
489
|
+
_Function("apoc.convert.toNode", (1,), None, None, rewrite=to_node("apoc.convert.toNode")),
|
|
490
|
+
_Function("apoc.convert.toRelationship", (1,), None, None, rewrite=to_node("apoc.convert.toRelationship")),
|
|
491
|
+
_Function("apoc.convert.toNodeList", (1,), None, None, rewrite=to_node_list("apoc.convert.toNodeList")),
|
|
492
|
+
_Function("apoc.convert.toRelationshipList", (1,), None, None,
|
|
493
|
+
rewrite=to_node_list("apoc.convert.toRelationshipList")),
|
|
494
|
+
]
|
|
495
|
+
"""The apoc.convert.* rows of the cypher-apoc table. toJson and toSortedJsonMap are lowered from their
|
|
496
|
+
expressions and the row's cells (`CELLS`)."""
|
|
497
|
+
|
|
498
|
+
CELLS = {"apoc.convert.tojson": to_json, "apoc.convert.tosortedjsonmap": to_sorted_json_map}
|
|
499
|
+
|
|
500
|
+
GONE = {
|
|
501
|
+
"apoc.convert.getjsonproperty": "a node's JSON property read at run time (its shape would be known only at "
|
|
502
|
+
"run time; not built)",
|
|
503
|
+
"apoc.convert.getjsonpropertymap": "a node's JSON property read at run time (its shape would be known only "
|
|
504
|
+
"at run time; not built)",
|
|
505
|
+
}
|