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,1171 @@
|
|
|
1
|
+
"""The facts of a plan node (one-plan.md 1.5, 4), worked out once per node.
|
|
2
|
+
|
|
3
|
+
Each node's facts are its slots (what each output column holds,
|
|
4
|
+
`prograph.plan.ir.slot`), its unique keys, whether it is distinct, and its
|
|
5
|
+
row bounds (one-plan.md 4.1, 4.3). A rule proves a fact or leaves it out:
|
|
6
|
+
unknown is no. Functional dependencies, equal pairs and constants come with
|
|
7
|
+
the rewrites that read them.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from dataclasses import dataclass, replace
|
|
13
|
+
from types import MappingProxyType
|
|
14
|
+
from typing import Any, Dict, FrozenSet, Iterable, Mapping, Optional, Tuple
|
|
15
|
+
|
|
16
|
+
from prograph.plan.ir import expr as E
|
|
17
|
+
from prograph.plan.ir import ops as O
|
|
18
|
+
from prograph.plan.ir.slot import (
|
|
19
|
+
BOOLEAN, LIST, MAP, MIXED, NOTHING, ROW, UNKNOWN, Bound, Slot, both, held_as_values, injective_over, literal_slot, optional,
|
|
20
|
+
VALUE, union, value_slot,
|
|
21
|
+
)
|
|
22
|
+
from prograph.plan.ir.terms import BlankTerm, IRITerm, LiteralTerm
|
|
23
|
+
from prograph.rules import home
|
|
24
|
+
from prograph.schema import xsd
|
|
25
|
+
from prograph.schema.source_map import BLANK, DATABASE_DECIMAL_TEXT, IRI, LITERAL, UUID_READERS, TermMap, constant_term
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class NotBuilt(NotImplementedError):
|
|
29
|
+
"""A construct the new path does not handle yet: reported as not built,
|
|
30
|
+
never as a wrong answer (`entry.run`)."""
|
|
31
|
+
|
|
32
|
+
def __init__(self, what: str) -> None:
|
|
33
|
+
super().__init__(f"one-plan: {what}")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True)
|
|
37
|
+
class Facts:
|
|
38
|
+
"""What is known of a node's rows (one-plan.md 4.1).
|
|
39
|
+
|
|
40
|
+
Attributes:
|
|
41
|
+
keys: Unique keys: no two rows have every cell of a key present and
|
|
42
|
+
equal (SQL's UNIQUE; a row with an empty key cell is exempt).
|
|
43
|
+
The empty key means at most one row. Minimal, at most 32.
|
|
44
|
+
distinct: No two rows are equal (an empty cell equal to an empty
|
|
45
|
+
cell).
|
|
46
|
+
rows: (lo, hi) bounds on the number of rows; hi None for no bound.
|
|
47
|
+
"""
|
|
48
|
+
columns: tuple
|
|
49
|
+
slots: Mapping[str, Slot]
|
|
50
|
+
keys: frozenset = frozenset()
|
|
51
|
+
distinct: bool = False
|
|
52
|
+
rows: Tuple[int, Optional[int]] = (0, None)
|
|
53
|
+
|
|
54
|
+
def unique(self, cols) -> bool:
|
|
55
|
+
"""Some key lies within `cols`."""
|
|
56
|
+
cols = frozenset(cols)
|
|
57
|
+
return any(k <= cols for k in self.keys)
|
|
58
|
+
|
|
59
|
+
def facts(op: O.Op) -> Facts:
|
|
60
|
+
"""`op`'s facts, cached on the node (a node is never changed)."""
|
|
61
|
+
cached = op.__dict__.get("_facts")
|
|
62
|
+
if cached is None:
|
|
63
|
+
slots = MappingProxyType({c: s for c, s in _RULES[type(op)](op).items()})
|
|
64
|
+
slots = MappingProxyType({c: slots.get(c, NOTHING) for c in op.columns})
|
|
65
|
+
keys, distinct, rows = _SHAPE.get(type(op), _unknown_shape)(op)
|
|
66
|
+
keys, distinct = _normalized(op.columns, slots, keys, distinct, rows)
|
|
67
|
+
cached = Facts(columns=op.columns, slots=slots, keys=keys, distinct=distinct,
|
|
68
|
+
rows=rows)
|
|
69
|
+
object.__setattr__(op, "_facts", cached)
|
|
70
|
+
return cached
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _slots(op: O.Op) -> Mapping[str, Slot]:
|
|
74
|
+
return facts(op).slots
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
# -- leaves -------------------------------------------------------------------------
|
|
78
|
+
|
|
79
|
+
def _map_scan(op: O.MapScan) -> Dict[str, Slot]:
|
|
80
|
+
# `required` names output columns, `not_null` source columns (ops.MapScan): each compared with
|
|
81
|
+
# its own kind of name (M2 review R10; nothing sets not_null yet).
|
|
82
|
+
out = {}
|
|
83
|
+
for r in op.reads:
|
|
84
|
+
firm = r.name in op.required or (not r.parent and r.column in op.not_null)
|
|
85
|
+
null = Bound.NEVER if firm else Bound.MAYBE
|
|
86
|
+
from prograph.plan.ir.cypher_lists import nested, typed_slot
|
|
87
|
+
if nested(r.held): # a list column (`cypher_lists.column_list_type`)
|
|
88
|
+
out[r.name] = typed_slot(r.held, null)
|
|
89
|
+
continue
|
|
90
|
+
out[r.name] = replace(value_slot(r.held, null,
|
|
91
|
+
f"{_read_table(op, r)}.{r.column}: no recorded type",
|
|
92
|
+
digits=r.digits, bytewise=r.bytewise,
|
|
93
|
+
text_of=xsd.DECIMAL if r.reader == DATABASE_DECIMAL_TEXT else None,
|
|
94
|
+
uuid=r.reader if r.uuid and r.reader in UUID_READERS else None),
|
|
95
|
+
zone=r.zone)
|
|
96
|
+
if op.graph is not None:
|
|
97
|
+
out[op.graph.name] = union(term_map_slot(m, Bound.MAYBE, None) for m in op.graph.maps)
|
|
98
|
+
return out
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _read_table(op: O.MapScan, r: O.ScanColumn) -> str:
|
|
102
|
+
"""The table a scan's column is read from: a joint query's parent's for a parent column."""
|
|
103
|
+
return op.joint.parent.table if r.parent and op.joint is not None else op.map.source.table
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _values(op: O.Values) -> Dict[str, Slot]:
|
|
107
|
+
out = {}
|
|
108
|
+
for i, name in enumerate(op.names):
|
|
109
|
+
cells = [row[i] for row in op.rows]
|
|
110
|
+
found = [const_slot(c) for c in cells if c is not None]
|
|
111
|
+
slot = union(found) if found else NOTHING
|
|
112
|
+
if found and len(found) < len(cells):
|
|
113
|
+
slot = optional(slot)
|
|
114
|
+
out[name] = _mixed_values_digits(slot, [c for c in cells if c is not None])
|
|
115
|
+
return out
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _mixed_values_digits(slot: Slot, cells) -> Slot:
|
|
119
|
+
"""A VALUES column of integers and decimals, each written validly: the decimal that holds every one
|
|
120
|
+
(its largest whole part and its longest fraction), so arithmetic, comparisons and aggregates read it
|
|
121
|
+
as a mixed exact column (`mixed_exact`), an integer or a decimal row by row. Past 38 digits, or an
|
|
122
|
+
integer past 18, as was."""
|
|
123
|
+
if not (slot.kinds == {LITERAL} and slot.datatypes == {xsd.INTEGER, xsd.DECIMAL}
|
|
124
|
+
and slot.native is None and slot.digits is None):
|
|
125
|
+
return slot
|
|
126
|
+
whole = places = 0
|
|
127
|
+
for c in cells:
|
|
128
|
+
try:
|
|
129
|
+
xsd.parse(c.lexical, c.datatype)
|
|
130
|
+
except (ValueError, TypeError, ArithmeticError):
|
|
131
|
+
return slot
|
|
132
|
+
p, s = operand_digits(const_slot(c))
|
|
133
|
+
if c.datatype == xsd.INTEGER and p > 18:
|
|
134
|
+
return slot # an integer result is written through 64 bits (`lower.arithmetic`): past them, as was
|
|
135
|
+
whole, places = max(whole, p - s), max(places, s)
|
|
136
|
+
if whole + places > 38:
|
|
137
|
+
return slot
|
|
138
|
+
return replace(slot, digits=(whole + places, places))
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
# -- joins and set operators --------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
def _join(op: O.Join) -> Dict[str, Slot]:
|
|
144
|
+
left, right = _slots(op.left), _slots(op.right)
|
|
145
|
+
out = dict(left)
|
|
146
|
+
for c, s in right.items():
|
|
147
|
+
if c not in left:
|
|
148
|
+
out[c] = s
|
|
149
|
+
continue
|
|
150
|
+
l = left[c]
|
|
151
|
+
if l.present is Bound.ALWAYS and s.present is Bound.ALWAYS:
|
|
152
|
+
out[c] = both(l, s)
|
|
153
|
+
elif l.present is Bound.ALWAYS:
|
|
154
|
+
out[c] = l # where the right side binds it, it is the left's term
|
|
155
|
+
elif s.present is Bound.ALWAYS:
|
|
156
|
+
out[c] = s
|
|
157
|
+
else:
|
|
158
|
+
out[c] = union([l, s])
|
|
159
|
+
return out
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _left_join(op: O.LeftJoin) -> Dict[str, Slot]:
|
|
163
|
+
left, right = _slots(op.left), _slots(op.right)
|
|
164
|
+
out = dict(left)
|
|
165
|
+
for c, s in right.items():
|
|
166
|
+
if c not in left:
|
|
167
|
+
out[c] = optional(s)
|
|
168
|
+
elif left[c].present is not Bound.ALWAYS:
|
|
169
|
+
out[c] = union([left[c], optional(s)])
|
|
170
|
+
return out
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def _union(op: O.Union) -> Dict[str, Slot]:
|
|
174
|
+
inputs = [_slots(i) for i in op.inputs]
|
|
175
|
+
# A column of integers and decimals: the SQL decimal that holds either (`_with_exact_digits`), so it
|
|
176
|
+
# is compared, ordered and summed by value exactly to 38 digits, not as DECIMAL(38, 18) (D7).
|
|
177
|
+
return {c: _with_exact_digits(union(s.get(c, NOTHING) for s in inputs),
|
|
178
|
+
[s[c] for s in inputs if c in s]) for c in op.columns}
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _left(op: Any) -> Mapping[str, Slot]:
|
|
182
|
+
return _slots(op.left)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _input(op: Any) -> Mapping[str, Slot]:
|
|
186
|
+
return _slots(op.input)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
@home("filter-bound-narrows", "A filter's IsBound conjuncts make those columns bound in its rows",
|
|
190
|
+
terms=("IsBound narrows", "filter bound", "BOUND", "slot narrowing", "facts of a filter"))
|
|
191
|
+
def _filter(op: O.Filter) -> Mapping[str, Slot]:
|
|
192
|
+
"""The input's slots, a term column the condition requires bound (a conjunct `IsBound(?c)`)
|
|
193
|
+
bound in every row kept: an unbound one makes the conjunct false (R10 states the
|
|
194
|
+
union's bound columns so)."""
|
|
195
|
+
slots = _slots(op.input)
|
|
196
|
+
required = [c.arg.name for c in E.conjuncts(op.condition)
|
|
197
|
+
if isinstance(c, E.IsBound) and isinstance(c.arg, E.Col) and c.arg.name in slots]
|
|
198
|
+
if not required:
|
|
199
|
+
return slots
|
|
200
|
+
out = dict(slots)
|
|
201
|
+
for name in required:
|
|
202
|
+
s = out[name]
|
|
203
|
+
if s.terms and s.bound is not Bound.ALWAYS:
|
|
204
|
+
out[name] = replace(s, bound=Bound.ALWAYS)
|
|
205
|
+
return out
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _extend(op: O.Extend) -> Dict[str, Slot]:
|
|
209
|
+
out = dict(_slots(op.input))
|
|
210
|
+
out[op.name] = expr_slot(op.expr, out)
|
|
211
|
+
return out
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _project(op: O.Project) -> Dict[str, Slot]:
|
|
215
|
+
inner = _slots(op.input)
|
|
216
|
+
return {c: inner.get(c, NOTHING) for c in op.names}
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def _aggregate(op: O.Aggregate) -> Dict[str, Slot]:
|
|
220
|
+
inner = _slots(op.input)
|
|
221
|
+
out = {g: inner[g] for g in op.group}
|
|
222
|
+
for name, agg in op.aggregates:
|
|
223
|
+
out[name] = agg_slot(agg, inner, grouped=bool(op.group))
|
|
224
|
+
return out
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
# An average's exact quotient is a DECIMAL(38, 18): the dialects' `EXACT_QUOTIENT` (xsd.QUOTIENT_SCALE).
|
|
228
|
+
AVERAGE_DIGITS = (xsd.QUOTIENT_PRECISION, xsd.QUOTIENT_SCALE)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def mixed_exact(slot: Slot) -> bool:
|
|
232
|
+
"""Whether a column holds integers and decimals in different rows, as the SQL decimal `digits` that
|
|
233
|
+
holds either (not a native cell: the rows' datatype is a part)."""
|
|
234
|
+
return (slot.kinds == {LITERAL} and slot.datatypes == {xsd.INTEGER, xsd.DECIMAL}
|
|
235
|
+
and slot.native is None and slot.digits is not None)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _sum_type(dt: str) -> Optional[str]:
|
|
239
|
+
"""SUM's datatype: its values added (18.5.1.3), by the operator
|
|
240
|
+
mapping's home (`xsd.numeric_datatype`); None for no number."""
|
|
241
|
+
return xsd.numeric_datatype("+", dt, dt)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def cypher_agg_slot(agg: E.Agg, inner: Mapping[str, Slot]) -> Slot:
|
|
245
|
+
"""A Cypher aggregate's slot (rule cypher-aggregates): count an integer, never null; sum of
|
|
246
|
+
integers an integer and of floats a float, of decimals an exact decimal, 0 over no value; avg a float
|
|
247
|
+
(of decimals the exact decimal quotient) and min and max the argument's type, null over no value. The
|
|
248
|
+
argument a value of one type."""
|
|
249
|
+
if agg.fn == "COUNT":
|
|
250
|
+
return value_slot(xsd.INTEGER, Bound.NEVER)
|
|
251
|
+
if agg.fn == "SAMPLE": # a value the group's rows all hold (a node's property grouped by its identity): as ONE
|
|
252
|
+
agg = replace(agg, fn="ONE")
|
|
253
|
+
arg = expr_slot(agg.arg, inner)
|
|
254
|
+
if agg.fn == "COLLECT":
|
|
255
|
+
from prograph.plan.ir.cypher_lists import collect_slot
|
|
256
|
+
return collect_slot(arg)
|
|
257
|
+
if agg.fn == "ONE" and arg.kinds and arg.kinds <= {VALUE, LIST, MAP} and len(arg.datatypes) <= 1:
|
|
258
|
+
# A node's property merged over its rows: any value of one type (agreement checked on its text where
|
|
259
|
+
# the engine cannot compare it natively; `lower.cypher_aggregate`). A column of no recorded type (a
|
|
260
|
+
# list column of a table) is one SQL type too, so its rows agree where their text does.
|
|
261
|
+
return replace(arg, null=Bound.MAYBE, provenance=None)
|
|
262
|
+
if arg.kinds == {MIXED} and agg.fn in ("MIN", "MAX", "COUNT", "ONE"):
|
|
263
|
+
return replace(arg, null=Bound.MAYBE) # the least or greatest value by orderability, null over none
|
|
264
|
+
if arg.kinds == {LIST} and agg.fn in ("MIN", "MAX"):
|
|
265
|
+
from prograph.plan.ir.cypher_mixed import needs_key
|
|
266
|
+
if needs_key(arg):
|
|
267
|
+
# Lists of scalars (one type or mixed): the least or greatest list by orderability, element by
|
|
268
|
+
# element (`cypher_mixed.extreme`), null over none.
|
|
269
|
+
return replace(arg, null=Bound.MAYBE, provenance=None)
|
|
270
|
+
if not arg.datatypes:
|
|
271
|
+
# Only empty lists and lists of nulls (`[[], [null]]`): no element type to order the lists by.
|
|
272
|
+
raise NotBuilt(f"{agg.fn.lower()}() of lists of no element type (empty lists or lists of nulls only)")
|
|
273
|
+
raise NotBuilt(f"{agg.fn.lower()}() of lists of lists, maps, temporal values or nodes")
|
|
274
|
+
if arg.kinds - {VALUE}:
|
|
275
|
+
raise NotBuilt(f"{agg.fn.lower()}() of a node, relationship or term")
|
|
276
|
+
if arg.present is Bound.NEVER:
|
|
277
|
+
if agg.fn == "SUM":
|
|
278
|
+
return value_slot(xsd.INTEGER, Bound.NEVER)
|
|
279
|
+
return Slot(kinds=frozenset({VALUE}), bound=Bound.NEVER, null=Bound.ALWAYS)
|
|
280
|
+
if len(arg.datatypes) != 1 or arg.datatype == UNKNOWN:
|
|
281
|
+
raise NotBuilt(f"{agg.fn.lower()}() of a value of no one type")
|
|
282
|
+
dt = arg.datatype
|
|
283
|
+
number = dt in xsd.INTEGER_DATATYPES or dt == xsd.INTEGER or dt in (xsd.DOUBLE, xsd.FLOAT, xsd.DECIMAL)
|
|
284
|
+
if agg.fn == "SUM":
|
|
285
|
+
if not number:
|
|
286
|
+
raise NotBuilt("sum() of a value that is not a number")
|
|
287
|
+
if dt == xsd.DECIMAL:
|
|
288
|
+
# Exact, a SUM's DECIMAL(38, s) (rule decimal-arithmetic-width); of no known digits the
|
|
289
|
+
# database's own.
|
|
290
|
+
return value_slot(dt, Bound.NEVER, digits=None if arg.digits is None else (38, arg.digits[1]))
|
|
291
|
+
return value_slot(xsd.DOUBLE if dt in (xsd.DOUBLE, xsd.FLOAT) else xsd.INTEGER, Bound.NEVER)
|
|
292
|
+
if agg.fn == "AVG":
|
|
293
|
+
if not number:
|
|
294
|
+
raise NotBuilt("avg() of a value that is not a number")
|
|
295
|
+
if dt == xsd.DECIMAL: # the exact quotient, DECIMAL(38, 18)
|
|
296
|
+
return value_slot(dt, Bound.MAYBE, digits=AVERAGE_DIGITS)
|
|
297
|
+
return value_slot(xsd.DOUBLE, Bound.MAYBE)
|
|
298
|
+
if agg.fn in ("PERCENTILE_DISC", "PERCENTILE_CONT"):
|
|
299
|
+
if not number:
|
|
300
|
+
raise NotBuilt(f"{agg.fn.lower().replace('_', '')}() of a value that is not a number")
|
|
301
|
+
# Disc is an element of the input (its type); cont interpolates, a float.
|
|
302
|
+
if agg.fn == "PERCENTILE_CONT":
|
|
303
|
+
return value_slot(xsd.DOUBLE, Bound.MAYBE)
|
|
304
|
+
if dt == xsd.DECIMAL:
|
|
305
|
+
return value_slot(dt, Bound.MAYBE, digits=arg.digits)
|
|
306
|
+
return value_slot(xsd.DOUBLE if dt in (xsd.DOUBLE, xsd.FLOAT) else xsd.INTEGER, Bound.MAYBE)
|
|
307
|
+
if agg.fn == "ONE":
|
|
308
|
+
if not number and dt not in (xsd.STRING, xsd.BOOLEAN, xsd.DATE, xsd.DATETIME):
|
|
309
|
+
raise NotBuilt(f"a node's {dt} property merged across maps")
|
|
310
|
+
return replace(arg, null=Bound.MAYBE, provenance=None)
|
|
311
|
+
if agg.fn in ("MIN", "MAX"):
|
|
312
|
+
from prograph.plan.ir.cypher_temporal import CYPHER_DURATION, TEMPORAL
|
|
313
|
+
# A Cypher temporal by its order (`cypher_temporal.struct_keys`); durations are not ordered.
|
|
314
|
+
ordered = dt in TEMPORAL and dt != CYPHER_DURATION
|
|
315
|
+
if not number and not ordered and dt not in (xsd.STRING, xsd.BOOLEAN, xsd.DATE, xsd.DATETIME, xsd.DECIMAL):
|
|
316
|
+
raise NotBuilt(f"{agg.fn.lower()}() of a {dt} value")
|
|
317
|
+
return replace(arg, null=Bound.MAYBE, provenance=None)
|
|
318
|
+
raise NotBuilt(f"the Cypher aggregate {agg.fn.lower()}()")
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def agg_slot(agg: E.Agg, inner: Mapping[str, Slot], grouped: bool) -> Slot:
|
|
322
|
+
"""An aggregate's slot (one-plan.md 4.3, Aggregate): an error in a
|
|
323
|
+
value, or an empty group where the aggregate has no value, leaves it
|
|
324
|
+
unbound."""
|
|
325
|
+
if agg.semantics == E.CYPHER:
|
|
326
|
+
return cypher_agg_slot(agg, inner)
|
|
327
|
+
if agg.semantics != E.SPARQL:
|
|
328
|
+
raise NotBuilt(f"{agg.semantics} aggregates")
|
|
329
|
+
if agg.fn in ("COUNT", "SUM_COUNTS"):
|
|
330
|
+
# SUM_COUNTS (R11): the sum of products of counts, an integer never unbound, 0 over no row.
|
|
331
|
+
return literal_slot(xsd.INTEGER, Bound.ALWAYS, native=True)
|
|
332
|
+
arg = expr_slot(agg.arg, inner)
|
|
333
|
+
if arg.present is Bound.NEVER:
|
|
334
|
+
# A value no row binds (a predicate no map has): over a group of no rows SUM and AVG are
|
|
335
|
+
# 0 and GROUP_CONCAT "" (18.5.1.3, 18.5.1.4, 18.5.1.7); over rows, each value is an error, so
|
|
336
|
+
# the aggregate is unbound (18.5.1); MIN, MAX and SAMPLE are unbound either way.
|
|
337
|
+
if agg.fn in ("SUM", "AVG"):
|
|
338
|
+
return literal_slot(xsd.INTEGER, Bound.MAYBE, native=True)
|
|
339
|
+
if agg.fn == "GROUP_CONCAT":
|
|
340
|
+
return literal_slot(xsd.STRING, Bound.MAYBE, native=True)
|
|
341
|
+
if agg.fn in ("MIN", "MAX", "SAMPLE"):
|
|
342
|
+
return arg
|
|
343
|
+
raise NotBuilt(f"{agg.fn} of nothing")
|
|
344
|
+
one = arg.kinds == {LITERAL} and len(arg.datatypes) == 1 and arg.datatype != UNKNOWN
|
|
345
|
+
if agg.fn in ("SUM", "AVG"):
|
|
346
|
+
if mixed_exact(arg) and agg.fn == "SUM":
|
|
347
|
+
# Integers and decimals in one column (an IF's two branches, TPC-H Q14): the sum is a decimal
|
|
348
|
+
# where the group has a decimal, else an integer (18.5.1.3), so its datatype is a part rows
|
|
349
|
+
# decide, as a decimal sum over the whole input is (below). AVG is a decimal: `mixed_sum`.
|
|
350
|
+
return Slot(kinds=frozenset({LITERAL}), datatypes=frozenset({xsd.DECIMAL, xsd.INTEGER}),
|
|
351
|
+
bound=Bound.MAYBE, digits=(38, arg.digits[1]))
|
|
352
|
+
dt = _sum_type(arg.datatype) if one else None
|
|
353
|
+
if dt is None or arg.native is None:
|
|
354
|
+
if arg.terms and UNKNOWN not in arg.datatypes and not agg.distinct:
|
|
355
|
+
# Rows of several datatypes, or numbers held as text: the group's datatype by
|
|
356
|
+
# promotion (17.3), an error where a row is no valid number (18.5.1); "0" over
|
|
357
|
+
# no row (`lower.mixed_sum`).
|
|
358
|
+
numbers = {xsd.numeric_datatype("+", d, d) for d in arg.datatypes} - {None}
|
|
359
|
+
kinds = {d for d in numbers if d in (xsd.INTEGER, xsd.DECIMAL, xsd.DOUBLE, xsd.FLOAT)}
|
|
360
|
+
if agg.fn == "AVG":
|
|
361
|
+
kinds = {xsd.numeric_datatype("/", d, xsd.INTEGER) for d in kinds}
|
|
362
|
+
return Slot(kinds=frozenset({LITERAL}),
|
|
363
|
+
datatypes=frozenset(kinds | {xsd.INTEGER}), bound=Bound.MAYBE)
|
|
364
|
+
raise NotBuilt(f"{agg.fn} of a column that is not one numeric datatype")
|
|
365
|
+
if agg.fn == "AVG":
|
|
366
|
+
# Sum / Count (18.5.1.4) by the operator mapping's division: integers and decimals give an
|
|
367
|
+
# exact decimal (m2-functions.md 6, `AVERAGE_DIGITS`), doubles a double.
|
|
368
|
+
quotient = xsd.numeric_datatype("/", dt, xsd.INTEGER)
|
|
369
|
+
if quotient == xsd.DECIMAL:
|
|
370
|
+
if not grouped:
|
|
371
|
+
# Over no row the average is "0"^^xsd:integer (18.5.1.4).
|
|
372
|
+
return Slot(kinds=frozenset({LITERAL}),
|
|
373
|
+
datatypes=frozenset({xsd.DECIMAL, xsd.INTEGER}),
|
|
374
|
+
bound=Bound.MAYBE, digits=AVERAGE_DIGITS)
|
|
375
|
+
return replace(literal_slot(xsd.DECIMAL, Bound.MAYBE, native=True),
|
|
376
|
+
digits=AVERAGE_DIGITS)
|
|
377
|
+
dt = quotient
|
|
378
|
+
if not grouped and dt != xsd.INTEGER:
|
|
379
|
+
# Over an empty input the answer is "0"^^xsd:integer (18.5.1.3):
|
|
380
|
+
# a decimal sum's answer is a decimal or that integer, so its
|
|
381
|
+
# cell is the term's text and its datatype (`lower.aggregate`).
|
|
382
|
+
if agg.fn == "SUM" and dt == xsd.DECIMAL:
|
|
383
|
+
# digits: the sum's SQL type, DECIMAL(38, s) on every engine.
|
|
384
|
+
scale = arg.digits[1] if arg.digits else None
|
|
385
|
+
return Slot(kinds=frozenset({LITERAL}),
|
|
386
|
+
datatypes=frozenset({xsd.DECIMAL, xsd.INTEGER}), bound=Bound.MAYBE,
|
|
387
|
+
digits=(38, scale) if scale is not None else None)
|
|
388
|
+
if dt in (xsd.DOUBLE, xsd.FLOAT):
|
|
389
|
+
# A double, or "0"^^xsd:integer over no row (18.5.1.3, 18.5.1.4).
|
|
390
|
+
return Slot(kinds=frozenset({LITERAL}), datatypes=frozenset({dt, xsd.INTEGER}),
|
|
391
|
+
bound=Bound.MAYBE)
|
|
392
|
+
raise NotBuilt(f"{agg.fn} of {dt} over the whole input")
|
|
393
|
+
if dt == xsd.DECIMAL and arg.digits is not None:
|
|
394
|
+
# The sum's SQL type, DECIMAL(38, s) on every engine.
|
|
395
|
+
return replace(literal_slot(dt, Bound.MAYBE, native=True), digits=(38, arg.digits[1]))
|
|
396
|
+
if dt == xsd.INTEGER and agg.fn == "SUM":
|
|
397
|
+
# Exact to 38 digits, held as DECIMAL(38, 0) (D7, 2026-11-09): a sum passes 64 bits
|
|
398
|
+
# where its rows do not.
|
|
399
|
+
return replace(literal_slot(dt, Bound.MAYBE, native=True), digits=WIDE_INTEGER)
|
|
400
|
+
return literal_slot(dt, Bound.MAYBE, native=True)
|
|
401
|
+
if agg.fn in ("MIN", "MAX", "SAMPLE"):
|
|
402
|
+
if arg.kind_varies or arg.datatype_varies or arg.language_varies:
|
|
403
|
+
# The least or greatest term by D8's total order (`lower.order_key`), its parts taken
|
|
404
|
+
# from that row; SAMPLE the least.
|
|
405
|
+
if arg.held_as is not None or UNKNOWN in arg.datatypes or not arg.terms:
|
|
406
|
+
raise NotBuilt(f"{agg.fn} of a mixed column of values of no recorded type")
|
|
407
|
+
return replace(arg, bound=Bound.MAYBE, provenance=None)
|
|
408
|
+
return replace(arg, bound=Bound.MAYBE, provenance=None, held_as=None)
|
|
409
|
+
if agg.fn == "GROUP_CONCAT":
|
|
410
|
+
if not (one and arg.datatype == xsd.STRING):
|
|
411
|
+
raise NotBuilt("GROUP_CONCAT of a column that is not simple literals")
|
|
412
|
+
return literal_slot(xsd.STRING, Bound.MAYBE, native=True)
|
|
413
|
+
raise NotBuilt(f"the aggregate {agg.fn}")
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
def _closure(op: O.Closure) -> Dict[str, Slot]:
|
|
417
|
+
"""A SPARQL closure's ends (one-plan.md 4, Closure): either end of a
|
|
418
|
+
step, or a term of `terms`, always present."""
|
|
419
|
+
step = _slots(op.step)
|
|
420
|
+
if op.mode != O.SPARQL: # Cypher's trails: its ends, and its relationships' identities (texts)
|
|
421
|
+
from prograph.plan.ir.slot import list_slot
|
|
422
|
+
if op.terms is not None or (op.partition and op.shortest):
|
|
423
|
+
raise NotBuilt("a Cypher closure of zero length or a shortest one in a graph")
|
|
424
|
+
# Both ends one slot: a trail's end is the next step's start (as the SPARQL closure's ends).
|
|
425
|
+
ends = replace(union([step[op.start], step[op.end]]), bound=Bound.ALWAYS)
|
|
426
|
+
# A partition column (spanningTree's start) is the step's.
|
|
427
|
+
out = {c: replace(step[c], bound=Bound.ALWAYS) for c in op.partition}
|
|
428
|
+
out |= {op.start: ends, op.end: ends}
|
|
429
|
+
if op.path:
|
|
430
|
+
out[op.path] = list_slot(xsd.STRING, Bound.NEVER)
|
|
431
|
+
for c in op.carry: # a list of the step column's values (a node's or relationship's value)
|
|
432
|
+
out[c] = list_slot(step[c].datatype, Bound.NEVER)
|
|
433
|
+
if op.depth:
|
|
434
|
+
out[op.depth] = value_slot(xsd.INTEGER, Bound.NEVER)
|
|
435
|
+
return out
|
|
436
|
+
terms = _slots(op.terms) if op.terms is not None else None
|
|
437
|
+
sides = [step[op.start], step[op.end]]
|
|
438
|
+
if terms is not None:
|
|
439
|
+
sides.append(terms[op.terms_column])
|
|
440
|
+
slot = replace(union(sides), bound=Bound.ALWAYS)
|
|
441
|
+
# A partition column (the graph of GRAPH ?g) is the step's, or the terms'.
|
|
442
|
+
parts = {c: replace(union([step[c]] + ([terms[c]] if terms is not None else [])), bound=Bound.ALWAYS)
|
|
443
|
+
for c in op.partition}
|
|
444
|
+
return {**parts, op.start: slot, op.end: slot}
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def _unwind(op: O.Unwind) -> Dict[str, Slot]:
|
|
448
|
+
inner = _slots(op.input)
|
|
449
|
+
listed = expr_slot(op.list, inner)
|
|
450
|
+
if listed.present is not Bound.NEVER and listed.kinds != {LIST}:
|
|
451
|
+
raise NotBuilt("UNWIND of a value that is not a list")
|
|
452
|
+
from prograph.plan.ir.cypher_lists import element_slot
|
|
453
|
+
return {**inner, op.name: element_slot(listed) if listed.present is not Bound.NEVER else NOTHING}
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
def _ordinal(op: O.Ordinal) -> Dict[str, Slot]:
|
|
457
|
+
return {**_slots(op.input), op.name: literal_slot(xsd.INTEGER, Bound.ALWAYS, native=True)}
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
def _not_built(op: O.Op) -> Dict[str, Slot]:
|
|
461
|
+
raise NotBuilt(f"facts of {type(op).__name__}")
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
_RULES = {
|
|
465
|
+
O.MapScan: _map_scan, O.Values: _values, O.Unit: lambda op: {},
|
|
466
|
+
O.Join: _join, O.LeftJoin: _left_join, O.Union: _union,
|
|
467
|
+
O.Minus: _left, O.SemiJoin: _left, O.AntiJoin: _left,
|
|
468
|
+
O.Filter: _filter, O.Distinct: _input, O.Reduced: _input, O.Order: _input,
|
|
469
|
+
O.Slice: _input, O.Extend: _extend, O.Project: _project,
|
|
470
|
+
O.LateralJoin: _not_built, O.Aggregate: _aggregate, O.Closure: _closure, O.Unwind: _unwind,
|
|
471
|
+
O.Ordinal: _ordinal,
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
# -- expressions --------------------------------------------------------------------
|
|
476
|
+
|
|
477
|
+
# Datatypes a literal constant is held as its SQL value where written in
|
|
478
|
+
# canonical form: its text and its value then say the same.
|
|
479
|
+
NATIVE_CONSTANTS = (xsd.INTEGER, xsd.BOOLEAN, xsd.STRING)
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
def const_slot(value: Any) -> Slot:
|
|
483
|
+
"""The slot of a constant written in the query."""
|
|
484
|
+
if isinstance(value, IRITerm):
|
|
485
|
+
return Slot(kinds=frozenset({IRI}), provenance=frozenset({constant_term(IRI, value.text)}))
|
|
486
|
+
if isinstance(value, BlankTerm):
|
|
487
|
+
return Slot(kinds=frozenset({BLANK}),
|
|
488
|
+
provenance=frozenset({constant_term(BLANK, value.label)}))
|
|
489
|
+
if isinstance(value, LiteralTerm):
|
|
490
|
+
native = value.datatype in NATIVE_CONSTANTS and _canonical(value)
|
|
491
|
+
return literal_slot(value.datatype, Bound.ALWAYS, native=native,
|
|
492
|
+
language=value.language, provenance=frozenset({constant_term(
|
|
493
|
+
LITERAL, value.lexical,
|
|
494
|
+
None if value.language else value.datatype, value.language)}))
|
|
495
|
+
# A Cypher value written in the query (one-plan 1.1): a database value of its type; null is a null.
|
|
496
|
+
if value is None:
|
|
497
|
+
# Null: a value of no type, always missing; a union with a typed column keeps its type.
|
|
498
|
+
return Slot(kinds=frozenset({VALUE}), bound=Bound.NEVER, null=Bound.ALWAYS)
|
|
499
|
+
if isinstance(value, bool):
|
|
500
|
+
return value_slot(xsd.BOOLEAN, Bound.NEVER)
|
|
501
|
+
if isinstance(value, int):
|
|
502
|
+
return value_slot(xsd.INTEGER, Bound.NEVER)
|
|
503
|
+
if isinstance(value, float):
|
|
504
|
+
return value_slot(xsd.DOUBLE, Bound.NEVER)
|
|
505
|
+
if isinstance(value, str):
|
|
506
|
+
return value_slot(xsd.STRING, Bound.NEVER)
|
|
507
|
+
from prograph.plan.ir.cypher_temporal import CYPHER_DATE, CYPHER_LOCALTIME, CypherDate, CypherLocalTime
|
|
508
|
+
if isinstance(value, CypherDate):
|
|
509
|
+
return value_slot(CYPHER_DATE, Bound.NEVER)
|
|
510
|
+
if isinstance(value, CypherLocalTime):
|
|
511
|
+
return value_slot(CYPHER_LOCALTIME, Bound.NEVER)
|
|
512
|
+
from prograph.plan.ir.cypher_temporal import (CYPHER_DATETIME, CYPHER_LOCALDATETIME, CYPHER_TIME,
|
|
513
|
+
CypherDateTime, CypherLocalDateTime, CypherTime)
|
|
514
|
+
if isinstance(value, CypherLocalDateTime):
|
|
515
|
+
return value_slot(CYPHER_LOCALDATETIME, Bound.NEVER)
|
|
516
|
+
if isinstance(value, CypherTime):
|
|
517
|
+
return value_slot(CYPHER_TIME, Bound.NEVER)
|
|
518
|
+
if isinstance(value, CypherDateTime):
|
|
519
|
+
return value_slot(CYPHER_DATETIME, Bound.NEVER)
|
|
520
|
+
from prograph.plan.ir.cypher_temporal import CYPHER_DURATION, CypherDuration
|
|
521
|
+
if isinstance(value, CypherDuration):
|
|
522
|
+
return value_slot(CYPHER_DURATION, Bound.NEVER)
|
|
523
|
+
raise NotBuilt(f"a constant {type(value).__name__}")
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
def _canonical(term: LiteralTerm) -> bool:
|
|
527
|
+
try:
|
|
528
|
+
return xsd.canonical_lexical(xsd.parse(term.lexical, term.datatype),
|
|
529
|
+
term.datatype) == term.lexical
|
|
530
|
+
except (ValueError, TypeError, ArithmeticError):
|
|
531
|
+
return False
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
def term_map_slot(m: TermMap, bound: Bound, held: Any) -> Slot:
|
|
535
|
+
"""The slot of the terms `m` gives, `bound` as its columns are present,
|
|
536
|
+
`held` the slot of the column a literal column map reads."""
|
|
537
|
+
if m.constant is not None:
|
|
538
|
+
if m.term_type == LITERAL:
|
|
539
|
+
dt = xsd.RDF_LANGSTRING if m.language else (m.datatype or xsd.STRING)
|
|
540
|
+
return const_slot(LiteralTerm(m.constant, None if m.language else dt, m.language))
|
|
541
|
+
return const_slot(IRITerm(m.constant) if m.term_type == IRI else BlankTerm(m.constant))
|
|
542
|
+
prov = frozenset({m})
|
|
543
|
+
if m.term_type != LITERAL:
|
|
544
|
+
return Slot(kinds=frozenset({m.term_type}), bound=bound, provenance=prov)
|
|
545
|
+
if m.language_column is not None:
|
|
546
|
+
plain = m.datatype or xsd.STRING
|
|
547
|
+
return Slot(kinds=frozenset({LITERAL}), datatypes=frozenset({plain, xsd.RDF_LANGSTRING}),
|
|
548
|
+
language=ROW, bound=bound, provenance=prov)
|
|
549
|
+
if m.language is not None:
|
|
550
|
+
return literal_slot(xsd.RDF_LANGSTRING, bound, language=m.language.lower(),
|
|
551
|
+
provenance=prov)
|
|
552
|
+
if m.template is not None:
|
|
553
|
+
return literal_slot(m.datatype or xsd.STRING, bound, native=m.datatype in (None, xsd.STRING),
|
|
554
|
+
provenance=prov)
|
|
555
|
+
native = held.native if held is not None else None
|
|
556
|
+
zone = held.zone if held is not None else None
|
|
557
|
+
digits = held.digits if held is not None else None
|
|
558
|
+
if held is not None and held.text_of is not None and m.datatype in (None, held.text_of):
|
|
559
|
+
# A DECIMAL column read as the database's text (`decimal_text: database`): its natural
|
|
560
|
+
# literal is an xsd:decimal held as that text, as a text column declared xsd:decimal is, with
|
|
561
|
+
# the column's declared digits: its value is read back as that DECIMAL(p, s) (`lower.decimal_of_text`).
|
|
562
|
+
return replace(literal_slot(held.text_of, bound, native=False, provenance=prov), digits=digits)
|
|
563
|
+
if m.datatype is not None:
|
|
564
|
+
out = literal_slot(m.datatype, bound, native=native == m.datatype, provenance=prov)
|
|
565
|
+
return replace(out, zone=zone, digits=digits) if native == m.datatype else out
|
|
566
|
+
if native is None:
|
|
567
|
+
reason = held.reason if held is not None and held.reason else "no recorded type"
|
|
568
|
+
return Slot(kinds=frozenset({LITERAL}), datatypes=frozenset({UNKNOWN}), bound=bound,
|
|
569
|
+
provenance=prov, reason=reason)
|
|
570
|
+
return replace(literal_slot(native, bound, native=True, provenance=prov), zone=zone,
|
|
571
|
+
digits=digits)
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
# SPARQL's arithmetic (17.3): the binary operators, unary minus and plus.
|
|
575
|
+
ARITHMETIC = frozenset({"+", "-", "*", "/", "NEG", "POS"})
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
def arithmetic_slot(op: str, args) -> Slot:
|
|
579
|
+
"""The slot of an arithmetic operation over numbers of one datatype
|
|
580
|
+
each: its datatype by the operator mapping's home
|
|
581
|
+
(`xsd.numeric_datatype`); unbound where an operand is (an error)."""
|
|
582
|
+
dts = []
|
|
583
|
+
for a in args:
|
|
584
|
+
if a.present is Bound.NEVER:
|
|
585
|
+
return NOTHING
|
|
586
|
+
if mixed_exact(a) and op not in ("NEG", "POS"):
|
|
587
|
+
# An integer or a decimal row by row (m2-functions.md 9, rule 3).
|
|
588
|
+
dts.append(MIXED_NUMBER)
|
|
589
|
+
continue
|
|
590
|
+
if a.terms and not (a.kinds & {LITERAL} and a.datatypes & (_NUMBERS | {UNKNOWN})):
|
|
591
|
+
# No row's operand is a number (an IRI, a blank node, a string, a date): 17.3 has no
|
|
592
|
+
# operator for it, a type error in every row, so the result is unbound (P7).
|
|
593
|
+
return NOTHING
|
|
594
|
+
if a.kinds != {LITERAL} or len(a.datatypes) != 1 or a.datatype not in _NUMBERS:
|
|
595
|
+
raise NotBuilt(f"arithmetic over a {a.datatype or 'mixed'} operand")
|
|
596
|
+
dts.append(a.datatype)
|
|
597
|
+
bound = Bound.ALWAYS if all(a.present is Bound.ALWAYS for a in args) else Bound.MAYBE
|
|
598
|
+
if MIXED_NUMBER in dts:
|
|
599
|
+
if any(d in (xsd.FLOAT, xsd.DOUBLE) for d in dts):
|
|
600
|
+
raise NotBuilt("arithmetic over integers and decimals with a double")
|
|
601
|
+
if op == "/" or xsd.DECIMAL in dts:
|
|
602
|
+
dt = xsd.DECIMAL
|
|
603
|
+
else:
|
|
604
|
+
# Integers and mixed operands: a decimal where a row's operand is one, else an integer.
|
|
605
|
+
operands = [operand_digits(a) for a in args]
|
|
606
|
+
width, places = decimal_width(op, operands)
|
|
607
|
+
if width > 38 or places > RESULT_SCALE:
|
|
608
|
+
bound = Bound.MAYBE # past 38 digits a value is unbound (D7)
|
|
609
|
+
return Slot(kinds=frozenset({LITERAL}), datatypes=frozenset({xsd.DECIMAL, xsd.INTEGER}),
|
|
610
|
+
bound=bound, digits=decimal_digits(op, operands, stand_in_digits(args)))
|
|
611
|
+
else:
|
|
612
|
+
dt = xsd.numeric_datatype(op if op in "+-*/" else "+", dts[0], dts[-1])
|
|
613
|
+
digits = None
|
|
614
|
+
if op in ("NEG", "POS") and dt == xsd.DECIMAL:
|
|
615
|
+
digits = operand_digits(args[0]) # a sign changes no digit
|
|
616
|
+
if op == "/":
|
|
617
|
+
if dt != xsd.DECIMAL:
|
|
618
|
+
raise NotBuilt("division of doubles or floats (IEEE's INF and NaN per engine)")
|
|
619
|
+
folded = folded_quotient(args)
|
|
620
|
+
if folded is not None:
|
|
621
|
+
return const_slot(folded) if folded else NOTHING
|
|
622
|
+
import warnings
|
|
623
|
+
warnings.warn("A decimal / is exact to 18 places after the point, rounded half away from zero, "
|
|
624
|
+
"so (1/3)*3 is 0.999999999999999999; a quotient whose whole part passes 20 digits "
|
|
625
|
+
"is unbound (sparql-choices.md D7).", QuotientScaleNote, stacklevel=2)
|
|
626
|
+
# The exact quotient rounded at 18 places (17.3, op:numeric-divide; m2-functions.md 9);
|
|
627
|
+
# a zero divisor is an error, unbound.
|
|
628
|
+
return replace(literal_slot(dt, Bound.MAYBE, native=True), digits=AVERAGE_DIGITS)
|
|
629
|
+
if dt == xsd.INTEGER and op in ("NEG", "POS") and wide_integer(args[0]):
|
|
630
|
+
digits = args[0].digits
|
|
631
|
+
if dt == xsd.INTEGER and op in ("+", "-", "*"):
|
|
632
|
+
bound = Bound.MAYBE # past 64 bits a value is unbound (D7)
|
|
633
|
+
if any(wide_integer(a) for a in args):
|
|
634
|
+
# A 38-digit operand (a SUM) keeps its width: exact to 38 digits, unbound past it (D7).
|
|
635
|
+
digits = decimal_digits(op, [operand_digits(a) for a in args], stand_in_digits(args))
|
|
636
|
+
if dt == xsd.DECIMAL and op in ("+", "-", "*") and any(operand_digits(a) is None for a in args):
|
|
637
|
+
# An operand of no known precision (an unconstrained NUMERIC, `postgresql-column-digits`): the
|
|
638
|
+
# database's own exact arithmetic, a result of no known digits, read back as such a column is. The
|
|
639
|
+
# source's warning said so once (`UnknownNumericDigitsWarning`); here a note for `explain`.
|
|
640
|
+
import warnings
|
|
641
|
+
warnings.warn(f"A decimal {op} over an operand of no known precision (an unconstrained NUMERIC) is the "
|
|
642
|
+
f"database's exact arithmetic. On PostgreSQL it is read back as DECIMAL(38, 18): rounded past 18 "
|
|
643
|
+
f"places, a whole part past 20 digits an error naming the column; on other engines the "
|
|
644
|
+
f"result is the engine's own decimal, and an overflow is the engine's error.",
|
|
645
|
+
DecimalRangeNote, stacklevel=2)
|
|
646
|
+
return literal_slot(dt, bound, native=True)
|
|
647
|
+
if dt == xsd.DECIMAL and op in ("+", "-", "*"):
|
|
648
|
+
operands = [operand_digits(a) for a in args]
|
|
649
|
+
digits = decimal_digits(op, operands, stand_in_digits(args))
|
|
650
|
+
width, places = decimal_width(op, operands)
|
|
651
|
+
if width > 38 or places > RESULT_SCALE:
|
|
652
|
+
bound = Bound.MAYBE # past 38 digits a value is unbound (D7)
|
|
653
|
+
return replace(literal_slot(dt, bound, native=True), digits=digits)
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
class QuotientScaleNote(UserWarning):
|
|
657
|
+
"""The scale of a decimal quotient, for `explain`'s notes (D7); not shown otherwise."""
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
import warnings as _warnings
|
|
661
|
+
_warnings.filterwarnings("ignore", category=QuotientScaleNote)
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
def constant_number(slot: Slot):
|
|
665
|
+
"""The value of an integer or decimal constant written in the query, None otherwise (or ill-typed)."""
|
|
666
|
+
if slot.kinds != {LITERAL} or len(slot.datatypes) != 1 or not slot.provenance or len(slot.provenance) != 1:
|
|
667
|
+
return None
|
|
668
|
+
m = next(iter(slot.provenance))
|
|
669
|
+
if m.constant is None or slot.datatype not in xsd.INTEGER_DATATYPES | {xsd.INTEGER, xsd.DECIMAL}:
|
|
670
|
+
return None
|
|
671
|
+
try:
|
|
672
|
+
return xsd.parse(m.constant, slot.datatype)
|
|
673
|
+
except (ValueError, TypeError, ArithmeticError):
|
|
674
|
+
return None
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
@home("folded-quotient", "A division of constants worked out at planning",
|
|
678
|
+
terms=("constant folding", "quotient of constants", "10^22 / 2", "D7"))
|
|
679
|
+
def folded_quotient(args):
|
|
680
|
+
"""A division of two integer or decimal constants, worked out at planning (D7, 2026-11-09): its
|
|
681
|
+
exact quotient rounded at 18 places as SQL's would be (`xsd.quotient`), as a decimal constant, so
|
|
682
|
+
`10000000000000000000000 / 2` is exact where SQL's DECIMAL(38, 18) holds 20 whole digits; False for
|
|
683
|
+
an error (a zero divisor, or a quotient past 38 digits); None where an operand is no constant."""
|
|
684
|
+
if len(args) != 2:
|
|
685
|
+
return None
|
|
686
|
+
a, b = (constant_number(x) for x in args)
|
|
687
|
+
if a is None or b is None:
|
|
688
|
+
return None
|
|
689
|
+
q = xsd.quotient(a, b)
|
|
690
|
+
if q is None:
|
|
691
|
+
return False
|
|
692
|
+
text = xsd.canonical_lexical(q, xsd.DECIMAL)
|
|
693
|
+
if len(text.lstrip("-").replace(".", "")) > 38:
|
|
694
|
+
return False
|
|
695
|
+
return LiteralTerm(text, xsd.DECIMAL)
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
# An arithmetic operand that is an integer or a decimal row by row.
|
|
699
|
+
MIXED_NUMBER = "mixed"
|
|
700
|
+
|
|
701
|
+
# An integer held wider than 64 bits: a SUM of integers, DECIMAL(38, 0) (D7, 2026-11-09).
|
|
702
|
+
WIDE_INTEGER = (38, 0)
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
def wide_integer(slot: Slot) -> bool:
|
|
706
|
+
"""A native integer held as a DECIMAL wider than 64 bits (a SUM's, D7), not as a BIGINT."""
|
|
707
|
+
return (slot.native in xsd.INTEGER_DATATYPES | {xsd.INTEGER} and slot.digits is not None
|
|
708
|
+
and slot.digits[0] > 19)
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
@home("decimal-arithmetic-width", "A decimal result's SQL precision and scale",
|
|
712
|
+
terms=("DECIMAL", "precision", "scale", "38 digits", "D7", "product of decimals"))
|
|
713
|
+
def decimal_digits(op: str, operands, stand_in=None, warn: bool = True) -> Tuple[int, int]:
|
|
714
|
+
"""The SQL type DECIMAL(p, s) an exact result is held in (D7: exact to
|
|
715
|
+
38 digits on every engine): `decimal_width`'s, held to 38 digits with
|
|
716
|
+
its scale kept, so a value whose whole part needs more than 38 - s
|
|
717
|
+
digits is an overflow error, never rounded (m2-functions.md 9). A scale
|
|
718
|
+
past 38 is not built. A width past 38 warns (`DecimalRangeWarning`),
|
|
719
|
+
unless it passes 38 only through the plan's own conventional widths (a
|
|
720
|
+
SUM's 38 digits, a quotient's 38 with 18 places), which say nothing of
|
|
721
|
+
the values: with each such operand read as a 64-bit integer's width
|
|
722
|
+
(`stand_in`, `stand_in_digits`) it fits, so it is a note for `explain`
|
|
723
|
+
(`DecimalRangeNote`, D7). With `warn` false nothing is said (Cypher's
|
|
724
|
+
arithmetic raises past 38 digits, `cypher_values.cypher_cell`)."""
|
|
725
|
+
p, sc = decimal_width(op, operands)
|
|
726
|
+
if warn and (p > 38 or sc > RESULT_SCALE):
|
|
727
|
+
import warnings
|
|
728
|
+
warnings.warn(f"A decimal {op} is declared {p} digits wide with {sc} places: past "
|
|
729
|
+
f"{RESULT_SCALE} places it is rounded, and a value whose whole part does not fit "
|
|
730
|
+
f"38 digits is unbound, as an expression error is (sparql-choices.md D7).",
|
|
731
|
+
DecimalRangeNote if stand_in is not None and _fits(op, stand_in) else DecimalRangeWarning,
|
|
732
|
+
stacklevel=2)
|
|
733
|
+
held = min(sc, RESULT_SCALE)
|
|
734
|
+
return min(p - sc + held, 38), held
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
# The places after the point a decimal result keeps (D7, revised 2026-11-09):
|
|
738
|
+
# as the quotient's (xsd.QUOTIENT_SCALE); past them it is rounded.
|
|
739
|
+
RESULT_SCALE = xsd.QUOTIENT_SCALE
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
class DecimalRangeWarning(UserWarning):
|
|
743
|
+
"""A decimal operation whose declared width passes 38 digits: a value
|
|
744
|
+
past it is unbound (D7, m2-functions.md 9)."""
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
class DecimalRangeNote(UserWarning):
|
|
748
|
+
"""`DecimalRangeWarning`'s text where the width passes 38 digits only through the plan's own
|
|
749
|
+
conventional widths (an aggregate's): for `explain`'s notes (D7); not shown otherwise."""
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
_warnings.filterwarnings("ignore", category=DecimalRangeNote)
|
|
753
|
+
|
|
754
|
+
# A conventional width's stand-in: the width of the values it holds in ordinary use, a 64-bit integer's
|
|
755
|
+
# whole digits (a SUM of integers; a quotient's whole part, with its 18 places).
|
|
756
|
+
_STAND_IN = {WIDE_INTEGER: (19, 0), AVERAGE_DIGITS: (19 + AVERAGE_DIGITS[1], AVERAGE_DIGITS[1])}
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
def stand_in_digits(args):
|
|
760
|
+
"""The operands' digits with each conventional width (a SUM's, a quotient's; not a mapped column's
|
|
761
|
+
declared digits) read as its stand-in (`_STAND_IN`), or None where no operand has one: what
|
|
762
|
+
`decimal_digits` checks before it calls a width past 38 a note rather than a warning."""
|
|
763
|
+
out, conventional = [], False
|
|
764
|
+
for a in args:
|
|
765
|
+
digits = operand_digits(a)
|
|
766
|
+
if a.digits in _STAND_IN and not any(getattr(m, "column", None) is not None for m in (a.provenance or ())):
|
|
767
|
+
digits, conventional = _STAND_IN[a.digits], True
|
|
768
|
+
out.append(digits)
|
|
769
|
+
return out if conventional else None
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
def _fits(op: str, operands) -> bool:
|
|
773
|
+
if any(d is None for d in operands):
|
|
774
|
+
return False
|
|
775
|
+
p, sc = decimal_width(op, operands)
|
|
776
|
+
return p <= 38 and sc <= RESULT_SCALE
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
def decimal_width(op: str, operands) -> Tuple[int, int]:
|
|
780
|
+
"""The DECIMAL(p, s) every exact result of `op` over operands of these
|
|
781
|
+
digits fits in: a product DECIMAL(p1 + p2, s1 + s2); a sum or difference
|
|
782
|
+
one integer digit more than the wider operand's and the larger scale; a
|
|
783
|
+
remainder (`%`, Cypher's) the narrower operand's whole digits and the
|
|
784
|
+
larger scale."""
|
|
785
|
+
if any(d is None for d in operands):
|
|
786
|
+
raise NotBuilt("decimal arithmetic over an operand of unknown precision")
|
|
787
|
+
(p1, s1), (p2, s2) = operands
|
|
788
|
+
if op == "*":
|
|
789
|
+
return p1 + p2, s1 + s2
|
|
790
|
+
sc = max(s1, s2)
|
|
791
|
+
if op == "%": # a remainder is smaller than the divisor and no larger than the dividend
|
|
792
|
+
return min(p1 - s1, p2 - s2) + sc, sc
|
|
793
|
+
return max(p1 - s1, p2 - s2) + sc + 1, sc
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
def rounded_digits(digits: Optional[Tuple[int, int]], places: int = 0) -> Optional[Tuple[int, int]]:
|
|
797
|
+
"""The DECIMAL(p, s) of a decimal of `digits` rounded (or its ceiling or floor taken) to `places`
|
|
798
|
+
after the point: one whole digit more (9.99 rounds to 10), the scale `places`; the digits themselves
|
|
799
|
+
where `places` keeps every place."""
|
|
800
|
+
if digits is None:
|
|
801
|
+
return None
|
|
802
|
+
p, s = digits
|
|
803
|
+
if places >= s:
|
|
804
|
+
return digits
|
|
805
|
+
return min(p - s + 1 + places, 38), places
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
def _text_of_no_width(slot: Slot) -> bool:
|
|
809
|
+
"""Whether `slot` holds numbers as text of no recorded width: not native, no digits, and not
|
|
810
|
+
only constants written in the query (whose digits their lexical forms give)."""
|
|
811
|
+
if slot.native is not None or slot.digits is not None:
|
|
812
|
+
return False
|
|
813
|
+
return not (slot.provenance and all(m.constant is not None for m in slot.provenance))
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
def _with_exact_digits(slot: Slot, branches) -> Slot:
|
|
817
|
+
"""A union of integers and decimals (an IF's branches, m2-functions.md 6) with the SQL decimal that
|
|
818
|
+
holds any of its values, where every branch's digits are known and fit 38."""
|
|
819
|
+
if slot.kinds != {LITERAL} or slot.datatypes != {xsd.INTEGER, xsd.DECIMAL} or slot.native is not None:
|
|
820
|
+
return slot
|
|
821
|
+
present = [b for b in branches if b.present is not Bound.NEVER]
|
|
822
|
+
if any(_text_of_no_width(b) for b in present):
|
|
823
|
+
# A number a column holds as text (a VARCHAR mapped xsd:integer or xsd:decimal) has no width
|
|
824
|
+
# the plan knows (`operand_digits`' (19, 0) or (38, 18) is how arithmetic reads it, not a bound
|
|
825
|
+
# on its text): read as a recorded DECIMAL(p, s) a wider row would be dropped or overflow.
|
|
826
|
+
return slot
|
|
827
|
+
found = [operand_digits(b) for b in present]
|
|
828
|
+
if any(d is None for d in found):
|
|
829
|
+
return slot
|
|
830
|
+
scale = max(s for _, s in found)
|
|
831
|
+
whole = max(p - s for p, s in found)
|
|
832
|
+
if whole + scale > 38:
|
|
833
|
+
return slot
|
|
834
|
+
return replace(slot, digits=(whole + scale, scale))
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
def operand_digits(slot: Slot) -> Optional[Tuple[int, int]]:
|
|
838
|
+
"""An operand's DECIMAL(p, s): a column's or a result's recorded digits,
|
|
839
|
+
an integer's 64 bits (19, 0) as the lowering casts it, a constant's from
|
|
840
|
+
its lexical form."""
|
|
841
|
+
if slot.digits is not None:
|
|
842
|
+
return slot.digits
|
|
843
|
+
integer = slot.datatype in xsd.INTEGER_DATATYPES or slot.datatype == xsd.INTEGER
|
|
844
|
+
if (integer or slot.datatype == xsd.DECIMAL) and slot.provenance and len(slot.provenance) == 1:
|
|
845
|
+
m = next(iter(slot.provenance))
|
|
846
|
+
if m.constant is not None:
|
|
847
|
+
text = m.constant.lstrip("+-")
|
|
848
|
+
whole, _, frac = text.partition(".")
|
|
849
|
+
return (max(len(whole.lstrip("0")), 1) + len(frac), len(frac))
|
|
850
|
+
if integer:
|
|
851
|
+
return (19, 0)
|
|
852
|
+
if slot.datatype == xsd.DECIMAL and slot.native is None and slot.kinds == {LITERAL} \
|
|
853
|
+
and len(slot.datatypes) == 1:
|
|
854
|
+
return (38, 18) # a decimal a column holds as text, read as DECIMAL(38, 18) (`lower.held_number`)
|
|
855
|
+
return None
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
_NUMBERS = frozenset(xsd.INTEGER_DATATYPES | {xsd.INTEGER, xsd.DECIMAL, xsd.FLOAT, xsd.DOUBLE})
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
_BOOLEAN_CALLS = frozenset({"isIRI", "isURI", "isBLANK", "isLITERAL", "isNUMERIC", "sameTerm", "sameValue",
|
|
862
|
+
"LANGMATCHES", "EBV"})
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
def expr_slot(e: E.Expr, slots: Mapping[str, Slot]) -> Slot:
|
|
866
|
+
"""The slot of `e`'s value over rows of `slots`; an error is unbound."""
|
|
867
|
+
if isinstance(e, E.Col):
|
|
868
|
+
return slots.get(e.name, NOTHING)
|
|
869
|
+
if isinstance(e, E.Const):
|
|
870
|
+
return NOTHING if e.value is None else const_slot(e.value)
|
|
871
|
+
if isinstance(e, E.MakeTerm):
|
|
872
|
+
args = {c: expr_slot(x, slots) for c, x in e.args}
|
|
873
|
+
firm = all(s.present is Bound.ALWAYS for s in args.values())
|
|
874
|
+
held = args.get(e.map.column) if e.map.column is not None else None
|
|
875
|
+
slot = term_map_slot(e.map, Bound.ALWAYS if firm else Bound.MAYBE, held)
|
|
876
|
+
held_as = held_as_values(e.map, args)
|
|
877
|
+
return replace(slot, held_as=held_as) if held_as is not None else slot
|
|
878
|
+
if isinstance(e, E.IsBound):
|
|
879
|
+
return replace(BOOLEAN, bound=Bound.ALWAYS)
|
|
880
|
+
if isinstance(e, (E.Compare, E.And, E.Or, E.Not)):
|
|
881
|
+
return BOOLEAN
|
|
882
|
+
if isinstance(e, E.Call) and e.semantics == E.CYPHER:
|
|
883
|
+
from prograph.plan.ir.cypher_values import cypher_slot
|
|
884
|
+
return cypher_slot(e, slots)
|
|
885
|
+
if isinstance(e, E.Call) and e.semantics == E.SPARQL:
|
|
886
|
+
if e.name in _BOOLEAN_CALLS:
|
|
887
|
+
return BOOLEAN
|
|
888
|
+
if e.name == "valueKey": # one text per value (`lower.value_key_text`): a database value
|
|
889
|
+
from prograph.plan.ir.slot import value_slot
|
|
890
|
+
return value_slot(xsd.STRING, Bound.NEVER)
|
|
891
|
+
from prograph.plan.ir.strings import STRING_CALLS, string_slot
|
|
892
|
+
if e.name in STRING_CALLS:
|
|
893
|
+
return string_slot(e, slots)
|
|
894
|
+
from prograph.plan.ir.dates import DATE_PARTS, date_slot
|
|
895
|
+
if e.name in DATE_PARTS:
|
|
896
|
+
return date_slot(e, slots)
|
|
897
|
+
if e.name == "CAST":
|
|
898
|
+
from prograph.plan.ir.casts import cast_slot
|
|
899
|
+
return cast_slot(e, slots)
|
|
900
|
+
from prograph.plan.ir.fresh import FRESH, fresh_slot
|
|
901
|
+
if e.name in FRESH:
|
|
902
|
+
return fresh_slot(e, slots)
|
|
903
|
+
from prograph.plan.ir.term_functions import NUMERIC_CALLS, TERM_CALLS, numeric_slot, term_slot
|
|
904
|
+
if e.name in TERM_CALLS:
|
|
905
|
+
return term_slot(e, slots)
|
|
906
|
+
if e.name in NUMERIC_CALLS:
|
|
907
|
+
return numeric_slot(e, slots)
|
|
908
|
+
if e.name in ARITHMETIC:
|
|
909
|
+
return arithmetic_slot(e.name, [expr_slot(a, slots) for a in e.args])
|
|
910
|
+
if isinstance(e, (E.If, E.Coalesce)):
|
|
911
|
+
branches = (e.then, e.else_) if isinstance(e, E.If) else e.args
|
|
912
|
+
slots_of = [expr_slot(b, slots) for b in branches]
|
|
913
|
+
if isinstance(e, E.If) and _never_unknown(e.cond, slots):
|
|
914
|
+
# A condition that is never an error (sameTerm of bound columns and constants): the
|
|
915
|
+
# IF is one of its branches on every row, as bound as they are (R10).
|
|
916
|
+
return _with_exact_digits(union(slots_of), slots_of)
|
|
917
|
+
return _with_exact_digits(optional(union(slots_of)), slots_of)
|
|
918
|
+
raise NotBuilt(f"the slot of {type(e).__name__}"
|
|
919
|
+
+ (f" {e.name}" if isinstance(e, E.Call) else ""))
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
def _never_unknown(cond: E.Expr, slots) -> bool:
|
|
923
|
+
"""`cond` is true or false on every row, never an error: sameTerm (`E.TERM`) of columns bound
|
|
924
|
+
in every row and constants."""
|
|
925
|
+
if not (isinstance(cond, E.Compare) and cond.semantics == E.TERM and cond.op in ("=", "!=")):
|
|
926
|
+
return False
|
|
927
|
+
for side in (cond.left, cond.right):
|
|
928
|
+
if isinstance(side, E.Const):
|
|
929
|
+
if side.value is None:
|
|
930
|
+
return False
|
|
931
|
+
elif isinstance(side, E.Col):
|
|
932
|
+
s = slots.get(side.name)
|
|
933
|
+
if s is None or s.present is not Bound.ALWAYS:
|
|
934
|
+
return False
|
|
935
|
+
else:
|
|
936
|
+
return False
|
|
937
|
+
return True
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
# -- keys, distinctness and row bounds (one-plan.md 4.3) ----------------------------
|
|
941
|
+
|
|
942
|
+
Shape = Tuple[FrozenSet[FrozenSet[str]], bool, Tuple[int, Optional[int]]]
|
|
943
|
+
_ANY = (0, None)
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
def _unknown_shape(op: O.Op) -> Shape:
|
|
947
|
+
return frozenset(), False, _ANY
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
def _normalized(columns, slots, keys, distinct, rows):
|
|
951
|
+
"""One-plan.md 4.2: keys restricted to the output, an always-present
|
|
952
|
+
constant left out of every key, minimal and at most 32; at most one row
|
|
953
|
+
makes the empty set a key; distinct where a key's every column is
|
|
954
|
+
always present, or at most one row."""
|
|
955
|
+
out = set()
|
|
956
|
+
cols = set(columns)
|
|
957
|
+
for k in keys:
|
|
958
|
+
if not k <= cols:
|
|
959
|
+
continue
|
|
960
|
+
out.add(frozenset(k))
|
|
961
|
+
if rows[1] is not None and rows[1] <= 1:
|
|
962
|
+
out.add(frozenset())
|
|
963
|
+
minimal = {k for k in out if not any(o < k for o in out)}
|
|
964
|
+
minimal = frozenset(sorted(minimal, key=lambda k: (len(k), sorted(k)))[:32])
|
|
965
|
+
if any(all(slots[c].present is Bound.ALWAYS for c in k) for k in minimal):
|
|
966
|
+
distinct = True
|
|
967
|
+
return minimal, distinct
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
def _fs(cols: Iterable[str]) -> FrozenSet[str]:
|
|
971
|
+
return frozenset(cols)
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
def _shape_map_scan(op: O.MapScan) -> Shape:
|
|
975
|
+
if op.joint is not None:
|
|
976
|
+
return frozenset(), False, _ANY
|
|
977
|
+
# A query's keys are the table's under a plain select (`keys.query_keys`); any other query has none.
|
|
978
|
+
by_column = {}
|
|
979
|
+
for r in op.reads:
|
|
980
|
+
if not r.parent:
|
|
981
|
+
by_column.setdefault(r.column, r.name)
|
|
982
|
+
# A quad scan reads a row once per graph its triple is in (`GraphColumn`): a source key is a
|
|
983
|
+
# key of the scan only with the graph column beside it (M2 review R10, before GRAPH is built).
|
|
984
|
+
graph = (op.graph.name,) if op.graph is not None else ()
|
|
985
|
+
keys = set()
|
|
986
|
+
for k in op.unique_keys:
|
|
987
|
+
if all(c in by_column for c in k):
|
|
988
|
+
keys.add(_fs([by_column[c] for c in k] + list(graph)))
|
|
989
|
+
return frozenset(keys), False, _ANY
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
def _shape_values(op: O.Values) -> Shape:
|
|
993
|
+
n = len(op.rows)
|
|
994
|
+
rows = (n, n)
|
|
995
|
+
distinct = len(set(op.rows)) == n
|
|
996
|
+
return frozenset(), distinct, rows
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
def _shape_input(op) -> Shape:
|
|
1000
|
+
f = facts(op.input)
|
|
1001
|
+
return f.keys, f.distinct, f.rows
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
def _shape_ordinal(op) -> Shape:
|
|
1005
|
+
"""Its input's keys and rows; with the ordinal, the `by` columns are a key."""
|
|
1006
|
+
f = facts(op.input)
|
|
1007
|
+
if op.constant: # every row 1: no key of its own
|
|
1008
|
+
return f.keys, f.distinct, f.rows
|
|
1009
|
+
return f.keys | {frozenset(op.by + (op.name,))}, f.distinct, f.rows
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
def _shape_filter(op) -> Shape:
|
|
1013
|
+
f = facts(op.input)
|
|
1014
|
+
return f.keys, f.distinct, (0, f.rows[1])
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
def _shape_extend(op: O.Extend) -> Shape:
|
|
1018
|
+
"""A key through a term constructor: `x := MakeTerm(map, args)` over
|
|
1019
|
+
columns, the map injective (`one_term_per_values`): a key K holding the
|
|
1020
|
+
args' columns A gives the key K - A + {x} (one-plan.md 4.1)."""
|
|
1021
|
+
f = facts(op.input)
|
|
1022
|
+
keys = set(f.keys)
|
|
1023
|
+
e = op.expr
|
|
1024
|
+
if isinstance(e, E.MakeTerm) and e.args and all(isinstance(a, E.Col) for _, a in e.args) and \
|
|
1025
|
+
injective_over(e.map, {c: f.slots.get(a.name) for c, a in e.args}):
|
|
1026
|
+
args = _fs(a.name for _, a in e.args)
|
|
1027
|
+
for k in f.keys:
|
|
1028
|
+
if args <= k:
|
|
1029
|
+
keys.add((k - args) | {op.name})
|
|
1030
|
+
return frozenset(keys), f.distinct, f.rows
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
def _shape_project(op: O.Project) -> Shape:
|
|
1034
|
+
f = facts(op.input)
|
|
1035
|
+
names = set(op.names)
|
|
1036
|
+
keep = frozenset(k for k in f.keys if k <= names)
|
|
1037
|
+
# Dropping no column keeps the rows as they are.
|
|
1038
|
+
distinct = f.distinct and set(op.input.columns) <= names
|
|
1039
|
+
return keep, distinct, f.rows
|
|
1040
|
+
|
|
1041
|
+
|
|
1042
|
+
def _shape_distinct(op: O.Distinct) -> Shape:
|
|
1043
|
+
"""The input's keys, plus all columns (one-plan.md 4.3)."""
|
|
1044
|
+
f = facts(op.input)
|
|
1045
|
+
lo, hi = f.rows
|
|
1046
|
+
return f.keys | {_fs(op.columns)}, True, (min(lo, 1), hi)
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
def _shape_join(op: O.Join) -> Shape:
|
|
1050
|
+
"""Compatible join: a key of each side together is a key. Where one
|
|
1051
|
+
side is unique on the shared columns, every row of the other matches at
|
|
1052
|
+
most one of its rows, so the other side's keys stay keys. Two inputs
|
|
1053
|
+
without equal rows give none: two different pairs never merge into one
|
|
1054
|
+
row (a merged row determines both its rows' cells)."""
|
|
1055
|
+
l, r = facts(op.left), facts(op.right)
|
|
1056
|
+
shared = _fs(op.shared)
|
|
1057
|
+
firm = all(op.left.slots[c].present is Bound.ALWAYS and
|
|
1058
|
+
op.right.slots[c].present is Bound.ALWAYS for c in shared)
|
|
1059
|
+
keys = set()
|
|
1060
|
+
if firm:
|
|
1061
|
+
# A key of each side together, where every shared column is bound
|
|
1062
|
+
# on both sides: a row with a shared column unbound is compatible
|
|
1063
|
+
# with many rows of the other side, and two pairs may merge into
|
|
1064
|
+
# one row (fresh review, 2026-11-09).
|
|
1065
|
+
for a in l.keys:
|
|
1066
|
+
for b in r.keys:
|
|
1067
|
+
keys.add(a | b)
|
|
1068
|
+
bounds = []
|
|
1069
|
+
if l.rows[1] is not None and r.rows[1] is not None:
|
|
1070
|
+
bounds.append(l.rows[1] * r.rows[1])
|
|
1071
|
+
if l.rows[1] == 0 or r.rows[1] == 0:
|
|
1072
|
+
bounds.append(0)
|
|
1073
|
+
if firm and any(not both(op.left.slots[c], op.right.slots[c]).kinds for c in shared):
|
|
1074
|
+
# A shared column no term of the left can share with the right
|
|
1075
|
+
# (no kind, or no map that may give one term): no row joins. Said
|
|
1076
|
+
# here, in the rows, since a slot cannot tell a column no row has
|
|
1077
|
+
# from one each row leaves unbound.
|
|
1078
|
+
bounds.append(0)
|
|
1079
|
+
if firm and shared:
|
|
1080
|
+
if r.unique(shared):
|
|
1081
|
+
keys |= set(l.keys)
|
|
1082
|
+
if l.rows[1] is not None:
|
|
1083
|
+
bounds.append(l.rows[1])
|
|
1084
|
+
if l.unique(shared):
|
|
1085
|
+
keys |= set(r.keys)
|
|
1086
|
+
if r.rows[1] is not None:
|
|
1087
|
+
bounds.append(r.rows[1])
|
|
1088
|
+
hi = min(bounds) if bounds else None
|
|
1089
|
+
distinct = l.distinct and r.distinct and firm
|
|
1090
|
+
return frozenset(keys), distinct, (0, hi)
|
|
1091
|
+
|
|
1092
|
+
|
|
1093
|
+
def _shape_semi(op) -> Shape:
|
|
1094
|
+
f = facts(op.left)
|
|
1095
|
+
return f.keys, f.distinct, (0, f.rows[1])
|
|
1096
|
+
|
|
1097
|
+
|
|
1098
|
+
def _shape_union(op: O.Union) -> Shape:
|
|
1099
|
+
"""Distinct where every branch is and no two branches can give one row:
|
|
1100
|
+
some column every branch binds in every row holds terms of maps that
|
|
1101
|
+
never meet (`TermMap.may_equal`), branch against branch. A key of the
|
|
1102
|
+
union: a key of some branch that lies within every branch's columns, holds
|
|
1103
|
+
a key of every branch, and has, for any two branches, a column that keeps
|
|
1104
|
+
them apart (post and comment IRIs under one variable: the variable is a key
|
|
1105
|
+
where each branch is keyed on it)."""
|
|
1106
|
+
fs = [facts(b) for b in op.inputs]
|
|
1107
|
+
his = [f.rows[1] for f in fs]
|
|
1108
|
+
hi = None if any(h is None for h in his) else sum(his)
|
|
1109
|
+
lo = sum(f.rows[0] for f in fs)
|
|
1110
|
+
distinct = all(f.distinct for f in fs) and _branches_apart(op.inputs)
|
|
1111
|
+
return _union_keys(op.inputs, fs), distinct, (lo, hi)
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
def _union_keys(branches, fs) -> FrozenSet[FrozenSet[str]]:
|
|
1115
|
+
keys = set()
|
|
1116
|
+
for k in {k for f in fs for k in f.keys if k}:
|
|
1117
|
+
if all(k <= set(b.columns) and f.unique(k) for b, f in zip(branches, fs)) and all(
|
|
1118
|
+
any(_column_apart(a.slots.get(c), b.slots.get(c)) for c in k)
|
|
1119
|
+
for i, a in enumerate(branches) for b in branches[i + 1:]):
|
|
1120
|
+
keys.add(k)
|
|
1121
|
+
return frozenset(keys)
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
def _branches_apart(branches) -> bool:
|
|
1125
|
+
for i, a in enumerate(branches):
|
|
1126
|
+
for b in branches[i + 1:]:
|
|
1127
|
+
if not any(_column_apart(a.slots.get(c), b.slots.get(c))
|
|
1128
|
+
for c in set(a.columns) & set(b.columns)):
|
|
1129
|
+
return False
|
|
1130
|
+
return True
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
def _column_apart(x: Optional[Slot], y: Optional[Slot]) -> bool:
|
|
1134
|
+
if x is None or y is None or x.present is not Bound.ALWAYS or y.present is not Bound.ALWAYS:
|
|
1135
|
+
return False
|
|
1136
|
+
if not x.kinds & y.kinds:
|
|
1137
|
+
return True
|
|
1138
|
+
if x.provenance is None or y.provenance is None:
|
|
1139
|
+
return False
|
|
1140
|
+
return not any(p.may_equal(q) for p in x.provenance for q in y.provenance)
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
def _shape_aggregate(op: O.Aggregate) -> Shape:
|
|
1144
|
+
f = facts(op.input)
|
|
1145
|
+
if not op.group:
|
|
1146
|
+
return frozenset({frozenset()}), True, (1, 1)
|
|
1147
|
+
return frozenset({_fs(op.group)}), True, (min(f.rows[0], 1), f.rows[1])
|
|
1148
|
+
|
|
1149
|
+
|
|
1150
|
+
def _shape_slice(op: O.Slice) -> Shape:
|
|
1151
|
+
f = facts(op.input)
|
|
1152
|
+
lo, hi = f.rows
|
|
1153
|
+
lo = max(0, lo - op.offset)
|
|
1154
|
+
hi = None if hi is None else max(0, hi - op.offset)
|
|
1155
|
+
if op.limit is not None:
|
|
1156
|
+
lo = min(lo, op.limit)
|
|
1157
|
+
hi = op.limit if hi is None else min(hi, op.limit)
|
|
1158
|
+
return f.keys, f.distinct, (lo, hi)
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
_SHAPE = {
|
|
1162
|
+
O.MapScan: _shape_map_scan, O.Values: _shape_values,
|
|
1163
|
+
O.Unit: lambda op: (frozenset({frozenset()}), True, (1, 1)),
|
|
1164
|
+
O.Join: _shape_join, O.Union: _shape_union,
|
|
1165
|
+
O.SemiJoin: _shape_semi, O.AntiJoin: _shape_semi, O.Minus: _shape_semi,
|
|
1166
|
+
O.Filter: _shape_filter, O.Extend: _shape_extend, O.Project: _shape_project,
|
|
1167
|
+
O.Distinct: _shape_distinct, O.Reduced: _shape_input, O.Order: _shape_input,
|
|
1168
|
+
O.Slice: _shape_slice, O.Aggregate: _shape_aggregate, O.Ordinal: _shape_ordinal,
|
|
1169
|
+
# A SPARQL closure is a set of pairs (18.4, ALP).
|
|
1170
|
+
O.Closure: lambda op: (frozenset({frozenset(op.columns)}), True, (0, None)),
|
|
1171
|
+
}
|