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
prograph/plan/ir/expr.py
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"""Expressions of the one plan.
|
|
2
|
+
|
|
3
|
+
An expression is evaluated over one row of its operator's input and gives
|
|
4
|
+
a value, nothing (an unbound term or a null value: the row has no value) or
|
|
5
|
+
an error (SPARQL 17.2). How an operator treats an error is the operator's:
|
|
6
|
+
`Filter` and a join's condition take it as false, `Extend` leaves the
|
|
7
|
+
column with no value, `Coalesce` and `If` catch it (SPARQL 17.2, 17.4.1).
|
|
8
|
+
|
|
9
|
+
Where SPARQL and Cypher give one construct different meanings, the
|
|
10
|
+
expression says which it means by its `semantics` (`SPARQL`, `CYPHER`, or
|
|
11
|
+
`TERM` for RDF term identity), as a property path's closure says its mode:
|
|
12
|
+
the plan holds no language flag, and a rewrite reads an expression's own
|
|
13
|
+
semantics.
|
|
14
|
+
|
|
15
|
+
Terms are built and taken apart by expressions, never inside a leaf:
|
|
16
|
+
`MakeTerm` builds a term from a source map's term map over plan
|
|
17
|
+
expressions (an IRI template, a column of IRIs, a literal with its
|
|
18
|
+
datatype or language, a blank node, a constant), and `Call` holds the deconstructors (STR,
|
|
19
|
+
LANG, DATATYPE, isIRI, ...).
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
from dataclasses import dataclass
|
|
25
|
+
from typing import Any, Dict, FrozenSet, Optional, Tuple
|
|
26
|
+
|
|
27
|
+
from prograph.schema.source_map import TermMap
|
|
28
|
+
from prograph.rules import module_home
|
|
29
|
+
|
|
30
|
+
module_home("expression-walkers", "Walking an expression tree",
|
|
31
|
+
terms=("walker", "expression tree", "visit", "transform", "expression"))
|
|
32
|
+
|
|
33
|
+
SPARQL = "sparql"
|
|
34
|
+
CYPHER = "cypher"
|
|
35
|
+
TERM = "term"
|
|
36
|
+
SEMANTICS = (SPARQL, CYPHER)
|
|
37
|
+
COMPARE_SEMANTICS = (SPARQL, CYPHER, TERM)
|
|
38
|
+
COMPARISONS = ("=", "!=", "<", "<=", ">", ">=")
|
|
39
|
+
|
|
40
|
+
class Expr:
|
|
41
|
+
"""An expression of the plan."""
|
|
42
|
+
|
|
43
|
+
def children(self) -> Tuple["Expr", ...]:
|
|
44
|
+
return ()
|
|
45
|
+
|
|
46
|
+
def columns(self) -> FrozenSet[str]:
|
|
47
|
+
"""Every column the expression reads."""
|
|
48
|
+
out: FrozenSet[str] = frozenset()
|
|
49
|
+
for child in self.children():
|
|
50
|
+
out = out | child.columns()
|
|
51
|
+
return out
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass(frozen=True)
|
|
55
|
+
class Col(Expr):
|
|
56
|
+
"""A column of the row. In the right side of a `LateralJoin`, a name
|
|
57
|
+
the right side does not output is the left row's column."""
|
|
58
|
+
name: str
|
|
59
|
+
|
|
60
|
+
def columns(self) -> FrozenSet[str]:
|
|
61
|
+
return frozenset({self.name})
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass(frozen=True)
|
|
65
|
+
class Const(Expr):
|
|
66
|
+
"""A constant as the query wrote it: an RDF term (`terms.IRITerm`,
|
|
67
|
+
`BlankTerm`, `LiteralTerm`) or a Cypher value (None is null)."""
|
|
68
|
+
value: Any
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass(frozen=True)
|
|
72
|
+
class MakeTerm(Expr):
|
|
73
|
+
"""The term `map` gives for the values `args` hold: (the map's column,
|
|
74
|
+
the expression giving its value) for each column it reads, its
|
|
75
|
+
language column included. A missing value gives no term (R2RML 11.2:
|
|
76
|
+
a NULL gives no triple)."""
|
|
77
|
+
map: TermMap
|
|
78
|
+
args: Tuple[Tuple[str, Expr], ...]
|
|
79
|
+
|
|
80
|
+
def __post_init__(self) -> None:
|
|
81
|
+
needed = set(self.map.columns())
|
|
82
|
+
if self.map.language_column:
|
|
83
|
+
needed.add(self.map.language_column)
|
|
84
|
+
given = [name for name, _ in self.args]
|
|
85
|
+
if len(set(given)) != len(given) or set(given) != needed:
|
|
86
|
+
raise ValueError(f"a term map reading {sorted(needed)} is given {given}")
|
|
87
|
+
|
|
88
|
+
def children(self) -> Tuple[Expr, ...]:
|
|
89
|
+
return tuple(e for _, e in self.args)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@dataclass(frozen=True)
|
|
93
|
+
class Compare(Expr):
|
|
94
|
+
"""`left op right`. `TERM`: RDF term identity, `=` and `!=` only
|
|
95
|
+
(sameTerm); `SPARQL`: SPARQL's operator mapping (17.3: RDFterm-equal,
|
|
96
|
+
numeric, string, boolean and date-time comparison, an error otherwise);
|
|
97
|
+
`CYPHER`: Cypher's equality and comparability (null where values are
|
|
98
|
+
incomparable or one is null)."""
|
|
99
|
+
op: str
|
|
100
|
+
left: Expr
|
|
101
|
+
right: Expr
|
|
102
|
+
semantics: str = SPARQL
|
|
103
|
+
|
|
104
|
+
def __post_init__(self) -> None:
|
|
105
|
+
if self.op not in COMPARISONS:
|
|
106
|
+
raise ValueError(f"no comparison {self.op!r}")
|
|
107
|
+
if self.semantics not in COMPARE_SEMANTICS:
|
|
108
|
+
raise ValueError(f"no comparison semantics {self.semantics!r}")
|
|
109
|
+
if self.semantics == TERM and self.op not in ("=", "!="):
|
|
110
|
+
raise ValueError("term identity is = or !=")
|
|
111
|
+
|
|
112
|
+
def children(self) -> Tuple[Expr, ...]:
|
|
113
|
+
return (self.left, self.right)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@dataclass(frozen=True)
|
|
117
|
+
class IsBound(Expr):
|
|
118
|
+
"""Whether the row has a value: SPARQL's BOUND, Cypher's IS NOT NULL.
|
|
119
|
+
Never an error."""
|
|
120
|
+
arg: Expr
|
|
121
|
+
|
|
122
|
+
def children(self) -> Tuple[Expr, ...]:
|
|
123
|
+
return (self.arg,)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@dataclass(frozen=True)
|
|
127
|
+
class And(Expr):
|
|
128
|
+
"""Conjunction over booleans, nothing and error taken as unknown
|
|
129
|
+
(SPARQL 17.2 and Cypher's three-valued logic agree): false if any is
|
|
130
|
+
false, else unknown if any is unknown, else true. An operand that is
|
|
131
|
+
no boolean is an error; SPARQL's effective boolean value is an explicit
|
|
132
|
+
`Call('EBV')`."""
|
|
133
|
+
args: Tuple[Expr, ...]
|
|
134
|
+
|
|
135
|
+
def children(self) -> Tuple[Expr, ...]:
|
|
136
|
+
return self.args
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
@dataclass(frozen=True)
|
|
140
|
+
class Or(Expr):
|
|
141
|
+
"""Disjunction, as `And`: true if any is true, else unknown if any is
|
|
142
|
+
unknown, else false."""
|
|
143
|
+
args: Tuple[Expr, ...]
|
|
144
|
+
|
|
145
|
+
def children(self) -> Tuple[Expr, ...]:
|
|
146
|
+
return self.args
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
@dataclass(frozen=True)
|
|
150
|
+
class Not(Expr):
|
|
151
|
+
"""Negation; unknown stays unknown."""
|
|
152
|
+
arg: Expr
|
|
153
|
+
|
|
154
|
+
def children(self) -> Tuple[Expr, ...]:
|
|
155
|
+
return (self.arg,)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
@dataclass(frozen=True)
|
|
159
|
+
class If(Expr):
|
|
160
|
+
"""SPARQL's IF and Cypher's CASE: `then` where `cond` is true, `else_`
|
|
161
|
+
where it is false; an error where `cond` is unknown in `SPARQL`
|
|
162
|
+
semantics, `else_` in `CYPHER`'s."""
|
|
163
|
+
cond: Expr
|
|
164
|
+
then: Expr
|
|
165
|
+
else_: Expr
|
|
166
|
+
semantics: str = SPARQL
|
|
167
|
+
|
|
168
|
+
def children(self) -> Tuple[Expr, ...]:
|
|
169
|
+
return (self.cond, self.then, self.else_)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
@dataclass(frozen=True)
|
|
173
|
+
class Coalesce(Expr):
|
|
174
|
+
"""The first argument that has a value, errors passed over (SPARQL
|
|
175
|
+
17.4.1.4, Cypher coalesce)."""
|
|
176
|
+
args: Tuple[Expr, ...]
|
|
177
|
+
|
|
178
|
+
def children(self) -> Tuple[Expr, ...]:
|
|
179
|
+
return self.args
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
@dataclass(frozen=True)
|
|
183
|
+
class Call(Expr):
|
|
184
|
+
"""A function of the named semantics: SPARQL's over RDF terms (`STR`,
|
|
185
|
+
`LANG`, `DATATYPE`, `isIRI`, `+`, `EBV`, ...) or Cypher's over values
|
|
186
|
+
(`toUpper`, `+`, ...)."""
|
|
187
|
+
name: str
|
|
188
|
+
args: Tuple[Expr, ...]
|
|
189
|
+
semantics: str = SPARQL
|
|
190
|
+
|
|
191
|
+
def __post_init__(self) -> None:
|
|
192
|
+
if self.semantics not in SEMANTICS:
|
|
193
|
+
raise ValueError(f"no function semantics {self.semantics!r}")
|
|
194
|
+
|
|
195
|
+
def children(self) -> Tuple[Expr, ...]:
|
|
196
|
+
return self.args
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
AGGREGATES = ("COUNT", "SUM", "AVG", "MIN", "MAX", "SAMPLE", "GROUP_CONCAT", "COLLECT", "ONE", "SUM_COUNTS",
|
|
200
|
+
"PERCENTILE_DISC", "PERCENTILE_CONT")
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
@dataclass(frozen=True)
|
|
204
|
+
class Agg:
|
|
205
|
+
"""An aggregate of an `Aggregate` operator. `arg` None is COUNT(*).
|
|
206
|
+
`SPARQL` semantics: over terms, DISTINCT by term, an error in any value
|
|
207
|
+
the aggregate's error (18.5.1), MIN and MAX by ORDER BY's order;
|
|
208
|
+
`CYPHER`: over values, nulls skipped; a COLLECT's `order` the (key,
|
|
209
|
+
descending) its list follows (the WITH ... ORDER BY just before it)."""
|
|
210
|
+
fn: str
|
|
211
|
+
arg: Optional[Expr] = None
|
|
212
|
+
distinct: bool = False
|
|
213
|
+
separator: str = " "
|
|
214
|
+
semantics: str = SPARQL
|
|
215
|
+
order: Tuple[Tuple[Expr, bool], ...] = ()
|
|
216
|
+
# A Cypher ONE's node label and property (its two-values error names them, rule cypher-node-scan).
|
|
217
|
+
what: Tuple[str, ...] = ()
|
|
218
|
+
# A Cypher percentile's fraction, in [0, 1] (checked where the query is translated).
|
|
219
|
+
fraction: Optional[float] = None
|
|
220
|
+
|
|
221
|
+
def __post_init__(self) -> None:
|
|
222
|
+
if self.fn not in AGGREGATES:
|
|
223
|
+
raise ValueError(f"no aggregate {self.fn!r}")
|
|
224
|
+
if self.arg is None and self.fn != "COUNT":
|
|
225
|
+
raise ValueError("only COUNT takes no argument")
|
|
226
|
+
if self.fn.startswith("PERCENTILE") and self.fraction is None:
|
|
227
|
+
raise ValueError("a percentile takes a fraction")
|
|
228
|
+
if self.semantics not in SEMANTICS:
|
|
229
|
+
raise ValueError(f"no aggregate semantics {self.semantics!r}")
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def replaced(expr: Expr, columns: Dict[str, "Expr"]) -> Expr:
|
|
233
|
+
"""`expr` with each read of a column of `columns` replaced by its expression."""
|
|
234
|
+
import dataclasses
|
|
235
|
+
if isinstance(expr, Col):
|
|
236
|
+
return columns.get(expr.name, expr)
|
|
237
|
+
if not dataclasses.is_dataclass(expr):
|
|
238
|
+
return expr
|
|
239
|
+
changes = {}
|
|
240
|
+
for f in dataclasses.fields(expr):
|
|
241
|
+
v = getattr(expr, f.name)
|
|
242
|
+
if isinstance(v, Expr):
|
|
243
|
+
changes[f.name] = replaced(v, columns)
|
|
244
|
+
elif isinstance(v, tuple) and any(isinstance(x, Expr) for x in v):
|
|
245
|
+
changes[f.name] = tuple(replaced(x, columns) if isinstance(x, Expr) else x for x in v)
|
|
246
|
+
return dataclasses.replace(expr, **changes) if changes else expr
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def renamed(expr: Expr, names: Dict[str, str]) -> Expr:
|
|
250
|
+
"""`expr` with each column of `names` read under its new name."""
|
|
251
|
+
return replaced(expr, {old: Col(new) for old, new in names.items()})
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def conjuncts(expr: Optional[Expr]) -> Tuple[Expr, ...]:
|
|
255
|
+
"""The conjuncts of a condition, nested `And`s flattened; () for none."""
|
|
256
|
+
if expr is None:
|
|
257
|
+
return ()
|
|
258
|
+
if isinstance(expr, And):
|
|
259
|
+
return tuple(c for a in expr.args for c in conjuncts(a))
|
|
260
|
+
return (expr,)
|