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.
Files changed (237) hide show
  1. prograph/__init__.py +36 -0
  2. prograph/__main__.py +5 -0
  3. prograph/_version.py +375 -0
  4. prograph/cli/__init__.py +1 -0
  5. prograph/cli/_iri_utils.py +124 -0
  6. prograph/cli/emit_genie_space.py +107 -0
  7. prograph/cli/generate_views.py +140 -0
  8. prograph/cli/main.py +420 -0
  9. prograph/cli/schema_inferrer.py +746 -0
  10. prograph/cli/ttl_loader.py +289 -0
  11. prograph/cli/upload_genie_space.py +84 -0
  12. prograph/config/__init__.py +27 -0
  13. prograph/config/yaml_config.py +848 -0
  14. prograph/demos/__init__.py +14 -0
  15. prograph/demos/turbofan/README.md +74 -0
  16. prograph/demos/turbofan/SCENARIO.md +209 -0
  17. prograph/demos/turbofan/__init__.py +75 -0
  18. prograph/demos/turbofan/create_tables.sql +206 -0
  19. prograph/demos/turbofan/data/engine-instances.ttl +1123 -0
  20. prograph/demos/turbofan/data/sensor-timeseries.csv +90 -0
  21. prograph/demos/turbofan/demo.py +162 -0
  22. prograph/demos/turbofan/genie_qa_notebook.py +434 -0
  23. prograph/demos/turbofan/graph.yaml +1098 -0
  24. prograph/demos/turbofan/instance_graph_demo.py +32 -0
  25. prograph/demos/turbofan/load.py +223 -0
  26. prograph/demos/turbofan/mapping.r2rml.ttl +991 -0
  27. prograph/demos/turbofan/ontology/engine-maintenance.ttl +606 -0
  28. prograph/demos/turbofan/payload-shapes.ttl +33 -0
  29. prograph/demos/turbofan/queries/example-queries.sparql +215 -0
  30. prograph/demos/turbofan/shapes.ttl +75 -0
  31. prograph/demos/turbofan/tables.py +106 -0
  32. prograph/demos/turbofan/turbofan_schema.py +393 -0
  33. prograph/demos/turbofan/validation_demo.py +153 -0
  34. prograph/describe.py +231 -0
  35. prograph/endpoint/__init__.py +12 -0
  36. prograph/endpoint/app.py +722 -0
  37. prograph/endpoint/cli.py +280 -0
  38. prograph/endpoint/credentials.py +240 -0
  39. prograph/endpoint/engines.py +418 -0
  40. prograph/endpoint/negotiate.py +101 -0
  41. prograph/endpoint/results.py +181 -0
  42. prograph/endpoint/service.py +31 -0
  43. prograph/engine.py +2718 -0
  44. prograph/execution.py +455 -0
  45. prograph/executors/__init__.py +6 -0
  46. prograph/executors/base.py +11 -0
  47. prograph/executors/databricks_sql_executor.py +51 -0
  48. prograph/executors/duckdb_executor.py +24 -0
  49. prograph/executors/mysql_executor.py +20 -0
  50. prograph/executors/oracle_executor.py +24 -0
  51. prograph/executors/pandas_executor.py +72 -0
  52. prograph/executors/postgresql_executor.py +35 -0
  53. prograph/executors/spark_executor.py +88 -0
  54. prograph/executors/sqlserver_executor.py +35 -0
  55. prograph/extras/__init__.py +1 -0
  56. prograph/extras/graphql/__init__.py +33 -0
  57. prograph/extras/graphql/execute.py +25 -0
  58. prograph/extras/graphql/schema.py +314 -0
  59. prograph/extras/instance_graph.py +559 -0
  60. prograph/genie/__init__.py +83 -0
  61. prograph/genie/databricks_uploader.py +306 -0
  62. prograph/genie/deploy.py +393 -0
  63. prograph/genie/formats.py +414 -0
  64. prograph/genie/instruction_generator.py +375 -0
  65. prograph/genie/sample_query_generator.py +256 -0
  66. prograph/genie/shacl_views.py +318 -0
  67. prograph/genie/space_emitter.py +329 -0
  68. prograph/genie/view_generator.py +628 -0
  69. prograph/genie/view_writer.py +172 -0
  70. prograph/graph/__init__.py +1 -0
  71. prograph/graph/ansi_session.py +348 -0
  72. prograph/graph/databricks_sql_graph.py +300 -0
  73. prograph/graph/drivers.py +143 -0
  74. prograph/graph/duckdb_graph.py +68 -0
  75. prograph/graph/mysql_graph.py +38 -0
  76. prograph/graph/oracle_graph.py +285 -0
  77. prograph/graph/pandas_graph.py +343 -0
  78. prograph/graph/postgresql_graph.py +644 -0
  79. prograph/graph/spark_graph.py +556 -0
  80. prograph/graph/sql_graph.py +83 -0
  81. prograph/graph/sqlserver_graph.py +72 -0
  82. prograph/hints.py +95 -0
  83. prograph/logging.py +550 -0
  84. prograph/mcp/__init__.py +29 -0
  85. prograph/mcp/__main__.py +180 -0
  86. prograph/mcp/bootstrap.py +98 -0
  87. prograph/mcp/server.py +601 -0
  88. prograph/mcp/sparql.py +397 -0
  89. prograph/mcp/tools.py +504 -0
  90. prograph/nlp/__init__.py +79 -0
  91. prograph/nlp/client.py +305 -0
  92. prograph/nlp/databricks_tools.py +369 -0
  93. prograph/nlp/graph_context.py +313 -0
  94. prograph/nlp/interface.py +652 -0
  95. prograph/nlp/prompts/__init__.py +19 -0
  96. prograph/nlp/prompts/examples.py +258 -0
  97. prograph/nlp/prompts/system.py +239 -0
  98. prograph/nlp/providers/__init__.py +14 -0
  99. prograph/nlp/providers/anthropic.py +274 -0
  100. prograph/nlp/providers/databricks.py +312 -0
  101. prograph/nlp/providers/openai.py +203 -0
  102. prograph/nlp/schema_context.py +78 -0
  103. prograph/ontology/__init__.py +53 -0
  104. prograph/ontology/alignment.py +542 -0
  105. prograph/ontology/hierarchy.py +310 -0
  106. prograph/ontology/imports.py +587 -0
  107. prograph/ontology/layer.py +991 -0
  108. prograph/ontology/skos.py +61 -0
  109. prograph/owl/__init__.py +27 -0
  110. prograph/owl/defined_classes.py +323 -0
  111. prograph/parameters.py +242 -0
  112. prograph/parser/__init__.py +1 -0
  113. prograph/parser/cypher/CypherLexer.py +836 -0
  114. prograph/parser/cypher/CypherListener.py +966 -0
  115. prograph/parser/cypher/CypherParser.py +10854 -0
  116. prograph/parser/cypher/CypherVisitor.py +543 -0
  117. prograph/parser/cypher/__init__.py +1 -0
  118. prograph/parser/sparql/SparqlLexer.py +801 -0
  119. prograph/parser/sparql/SparqlParser.py +10146 -0
  120. prograph/parser/sparql/SparqlParserListener.py +1056 -0
  121. prograph/parser/sparql/SparqlParserVisitor.py +593 -0
  122. prograph/parser/sparql/__init__.py +8 -0
  123. prograph/plan/__init__.py +3 -0
  124. prograph/plan/ir/__init__.py +1 -0
  125. prograph/plan/ir/casts.py +338 -0
  126. prograph/plan/ir/consistency.py +397 -0
  127. prograph/plan/ir/construct.py +90 -0
  128. prograph/plan/ir/cost.py +234 -0
  129. prograph/plan/ir/cypher_answer.py +1081 -0
  130. prograph/plan/ir/cypher_apoc.py +717 -0
  131. prograph/plan/ir/cypher_apoc_convert.py +505 -0
  132. prograph/plan/ir/cypher_apoc_date.py +845 -0
  133. prograph/plan/ir/cypher_apoc_map.py +496 -0
  134. prograph/plan/ir/cypher_apoc_meta.py +384 -0
  135. prograph/plan/ir/cypher_apoc_path.py +597 -0
  136. prograph/plan/ir/cypher_apoc_procedures.py +163 -0
  137. prograph/plan/ir/cypher_apoc_text.py +785 -0
  138. prograph/plan/ir/cypher_front.py +5474 -0
  139. prograph/plan/ir/cypher_functions.py +565 -0
  140. prograph/plan/ir/cypher_lists.py +935 -0
  141. prograph/plan/ir/cypher_maps.py +370 -0
  142. prograph/plan/ir/cypher_mixed.py +604 -0
  143. prograph/plan/ir/cypher_ontology.py +226 -0
  144. prograph/plan/ir/cypher_procedures.py +191 -0
  145. prograph/plan/ir/cypher_runtime_errors.py +107 -0
  146. prograph/plan/ir/cypher_temporal.py +1357 -0
  147. prograph/plan/ir/cypher_temporal_arith.py +339 -0
  148. prograph/plan/ir/cypher_values.py +546 -0
  149. prograph/plan/ir/cypher_write.py +612 -0
  150. prograph/plan/ir/dataset.py +643 -0
  151. prograph/plan/ir/dates.py +102 -0
  152. prograph/plan/ir/entry.py +992 -0
  153. prograph/plan/ir/expr.py +260 -0
  154. prograph/plan/ir/facts.py +1171 -0
  155. prograph/plan/ir/fresh.py +291 -0
  156. prograph/plan/ir/freshness.py +625 -0
  157. prograph/plan/ir/identity.py +797 -0
  158. prograph/plan/ir/java_datetime.py +265 -0
  159. prograph/plan/ir/java_regex.py +391 -0
  160. prograph/plan/ir/join_order.py +39 -0
  161. prograph/plan/ir/key_checks.py +433 -0
  162. prograph/plan/ir/keys.py +454 -0
  163. prograph/plan/ir/lower.py +5323 -0
  164. prograph/plan/ir/ops.py +619 -0
  165. prograph/plan/ir/owl_rl.py +1119 -0
  166. prograph/plan/ir/owl_rl_rules.py +725 -0
  167. prograph/plan/ir/profiles.py +694 -0
  168. prograph/plan/ir/ql.py +987 -0
  169. prograph/plan/ir/rdfs.py +342 -0
  170. prograph/plan/ir/rewrite.py +1782 -0
  171. prograph/plan/ir/saturate.py +339 -0
  172. prograph/plan/ir/semantics.py +103 -0
  173. prograph/plan/ir/slot.py +509 -0
  174. prograph/plan/ir/sparql_front.py +3418 -0
  175. prograph/plan/ir/strings.py +534 -0
  176. prograph/plan/ir/term_functions.py +274 -0
  177. prograph/plan/ir/terms.py +84 -0
  178. prograph/plan/ir/xpath_regex.py +264 -0
  179. prograph/plan/run_record.py +65 -0
  180. prograph/plan/sql/__init__.py +21 -0
  181. prograph/plan/sql/dialect.py +3726 -0
  182. prograph/plan/sql/mysql_serializer.py +103 -0
  183. prograph/plan/sql/names.py +65 -0
  184. prograph/plan/sql/oracle_serializer.py +306 -0
  185. prograph/plan/sql/pg_serializer.py +501 -0
  186. prograph/plan/sql/rel_algebra.py +781 -0
  187. prograph/plan/sql/sql_serializer.py +1419 -0
  188. prograph/plan/sql/sqlserver_serializer.py +603 -0
  189. prograph/provenance.py +222 -0
  190. prograph/r2rml/__init__.py +48 -0
  191. prograph/r2rml/catalog.py +259 -0
  192. prograph/r2rml/compile.py +372 -0
  193. prograph/r2rml/direct.py +140 -0
  194. prograph/r2rml/export.py +495 -0
  195. prograph/r2rml/model.py +180 -0
  196. prograph/r2rml/parser.py +546 -0
  197. prograph/r2rml/schema.py +120 -0
  198. prograph/read_only.py +122 -0
  199. prograph/results.py +157 -0
  200. prograph/rules/__init__.py +168 -0
  201. prograph/rules/__main__.py +32 -0
  202. prograph/schema/__init__.py +60 -0
  203. prograph/schema/change_set.py +50 -0
  204. prograph/schema/collations.py +198 -0
  205. prograph/schema/compile_schema.py +388 -0
  206. prograph/schema/context.py +845 -0
  207. prograph/schema/examples.py +234 -0
  208. prograph/schema/iri.py +817 -0
  209. prograph/schema/literals.py +94 -0
  210. prograph/schema/lpg_view.py +617 -0
  211. prograph/schema/mapping.py +1803 -0
  212. prograph/schema/maps_schema.py +40 -0
  213. prograph/schema/naming.py +77 -0
  214. prograph/schema/query_sources.py +59 -0
  215. prograph/schema/rows.py +215 -0
  216. prograph/schema/source_map.py +1103 -0
  217. prograph/schema/store_view.py +459 -0
  218. prograph/schema/types.py +569 -0
  219. prograph/schema/xsd.py +569 -0
  220. prograph/shacl/__init__.py +30 -0
  221. prograph/shacl/dqv.py +58 -0
  222. prograph/shacl/generate.py +255 -0
  223. prograph/shacl/report.py +138 -0
  224. prograph/shacl/validator.py +164 -0
  225. prograph/store.py +1737 -0
  226. prograph/utils/__init__.py +6 -0
  227. prograph/utils/exceptions.py +308 -0
  228. prograph/utils/helpers.py +126 -0
  229. prograph/warnings.py +46 -0
  230. prograph/writes.py +369 -0
  231. prograph-1.3.3.dist-info/METADATA +281 -0
  232. prograph-1.3.3.dist-info/RECORD +237 -0
  233. prograph-1.3.3.dist-info/WHEEL +5 -0
  234. prograph-1.3.3.dist-info/entry_points.txt +3 -0
  235. prograph-1.3.3.dist-info/licenses/LICENSE +201 -0
  236. prograph-1.3.3.dist-info/licenses/NOTICE +10 -0
  237. prograph-1.3.3.dist-info/top_level.txt +1 -0
@@ -0,0 +1,339 @@
1
+ """Cypher's temporal arithmetic (rule cypher-temporal-arithmetic): a temporal plus or minus a duration,
2
+ durations added and subtracted, a duration multiplied or divided by a number. Literal operands are worked
3
+ out at planning (`fold`); a value read from a column in SQL (`cell`), by integer arithmetic only.
4
+
5
+ openCypher's semantics (the temporal CIP, the TCK's Temporal8, Neo4j as the reference): a duration is
6
+ added to a temporal one unit at a time, months, then days, then seconds, then nanoseconds, each where the
7
+ temporal has it: a month added to a date clamps the day to the month's end (Jan 31 + P1M is Feb 28 or 29),
8
+ a date takes the whole days of the seconds (truncated toward zero) and no nanoseconds, a time of day takes
9
+ only the seconds and nanoseconds (around the clock), a date-time with a named zone adds months and days
10
+ on its local date-time (resolved in the zone at its offset where that offset is still valid) and seconds
11
+ on its instant. Subtraction subtracts each unit in the same order. Durations add part by part. A duration
12
+ times or divided by a number scales each part exactly, a fraction carried down in exact decimals (Neo4j uses doubles, so a float factor can differ from it in the last nanosecond), as Neo4j's DurationValue carries it
13
+ (a month's fraction as days at the average month of 2,629,746 seconds, then a day's as seconds,
14
+ then a second's as nanoseconds, each part truncated toward zero); a float literal is read as the decimal it
15
+ is written as.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ from fractions import Fraction as F
21
+ from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple
22
+
23
+ from prograph.plan.ir.facts import NotBuilt
24
+ from prograph.plan.ir.cypher_temporal import (
25
+ CYPHER_DATE, CYPHER_DATETIME, CYPHER_DURATION, CYPHER_LOCALDATETIME, CYPHER_LOCALTIME, CYPHER_TIME,
26
+ CypherDate, CypherDateTime, CypherDuration, CypherLocalDateTime, CypherLocalTime, CypherTime, TEMPORAL,
27
+ _DAY, _MONTH_SECONDS, _add_months, _offset_at, _offset_of_instant)
28
+ from prograph.rules import home
29
+
30
+ if TYPE_CHECKING:
31
+ from prograph.plan.ir import lower as L
32
+ from prograph.plan.sql import rel_algebra as R
33
+
34
+ _NS = 1_000_000_000
35
+ _FIRST_DAY, _LAST_DAY = -719_162, 2_932_896 # 0001-01-01 and 9999-12-31, days since 1970-01-01
36
+ INSTANTS = (CypherDate, CypherLocalTime, CypherTime, CypherLocalDateTime, CypherDateTime)
37
+
38
+
39
+ def _tdiv(a: int, b: int) -> int:
40
+ """a / b truncated toward zero (Java's long division), exactly."""
41
+ q = abs(a) // abs(b)
42
+ return q if (a >= 0) == (b > 0) else -q
43
+
44
+
45
+ def _normal(months: int, days: int, seconds: int, nanos: int) -> CypherDuration:
46
+ s, n = divmod(seconds * _NS + nanos, _NS)
47
+ return CypherDuration(months, days, s, n)
48
+
49
+
50
+ def scale(d: CypherDuration, p: int, q: int) -> CypherDuration:
51
+ """d times p/q (q not zero), each part exactly, a fraction carried down in exact decimals (Neo4j uses doubles, so a float factor can differ from it in the last nanosecond), as Neo4j's DurationValue carries
52
+ it: a month's to days (the average month), a day's to seconds, a second's to nanoseconds, each part
53
+ truncated toward zero."""
54
+ mp = d.months * p
55
+ months = _tdiv(mp, q)
56
+ days_num = d.days * p * 86400 + (mp - months * q) * _MONTH_SECONDS # over q * 86400
57
+ days = _tdiv(days_num, q * 86400)
58
+ sec_num = d.seconds * p + (days_num - days * q * 86400) # over q
59
+ seconds = _tdiv(sec_num, q)
60
+ nano_num = d.nanos * p + (sec_num - seconds * q) * _NS # over q
61
+ return _normal(months, days, seconds, _tdiv(nano_num, q))
62
+
63
+
64
+ def _ratio(v: Any) -> Tuple[int, int]:
65
+ """A number literal as p/q: an integer itself, a float the decimal it is written as."""
66
+ r = F(v) if isinstance(v, int) else F(repr(v))
67
+ return r.numerator, r.denominator
68
+
69
+
70
+ def _days_check(days: int) -> int:
71
+ if not _FIRST_DAY <= days <= _LAST_DAY:
72
+ raise NotBuilt("temporal arithmetic whose date falls outside years 1 to 9999")
73
+ return days
74
+
75
+
76
+ def _resolve(local: int, zone: str, preferred: int) -> Tuple[int, int]:
77
+ """(instant, offset) of a local date-time in a named zone, as Java's ZonedDateTime.ofLocal: the
78
+ preferred offset where it is one of the local time's, else the zone's (an overlap's earlier offset; a
79
+ gap's time moved later by the gap)."""
80
+ if _offset_of_instant(zone, local - preferred * _NS) == preferred:
81
+ return local - preferred * _NS, preferred
82
+ before = _offset_at(zone, local)
83
+ instant = local - before * _NS
84
+ return instant, _offset_of_instant(zone, instant)
85
+
86
+
87
+ def _plus(t: Any, d: CypherDuration, sign: int) -> Any:
88
+ m, days, s, n = sign * d.months, sign * d.days, sign * d.seconds, sign * d.nanos
89
+ if isinstance(t, CypherDate):
90
+ moved = _add_months(t.days * _DAY, m) // _DAY + days + _tdiv(s, 86400)
91
+ return CypherDate(_days_check(moved))
92
+ if isinstance(t, CypherLocalTime):
93
+ return CypherLocalTime((t.nanos + s * _NS + n) % _DAY)
94
+ if isinstance(t, CypherTime):
95
+ return CypherTime((t.nanos + s * _NS + n) % _DAY, t.offset)
96
+ if isinstance(t, CypherLocalDateTime):
97
+ local = _add_months(t.nanos, m) + days * _DAY + s * _NS + n
98
+ _days_check(local // _DAY)
99
+ return CypherLocalDateTime(local)
100
+ if t.zone is None: # an offset: local and instant arithmetic agree
101
+ local = _add_months(t.instant + t.offset * _NS, m) + days * _DAY + s * _NS + n
102
+ _days_check(local // _DAY)
103
+ return CypherDateTime(local - t.offset * _NS, t.offset)
104
+ instant, offset = t.instant, t.offset
105
+ if m:
106
+ instant, offset = _resolve(_add_months(instant + offset * _NS, m), t.zone, offset)
107
+ if days:
108
+ instant, offset = _resolve(instant + offset * _NS + days * _DAY, t.zone, offset)
109
+ instant += s * _NS + n
110
+ offset = _offset_of_instant(t.zone, instant)
111
+ _days_check((instant + offset * _NS) // _DAY)
112
+ return CypherDateTime(instant, offset, t.zone)
113
+
114
+
115
+ def _number(v: Any) -> bool:
116
+ return isinstance(v, (int, float)) and not isinstance(v, bool)
117
+
118
+
119
+ @home("cypher-temporal-arithmetic", "Cypher's temporal arithmetic: a temporal plus or minus a duration, "
120
+ "durations added, a duration times or divided by a number",
121
+ terms=("temporal arithmetic", "date + duration", "duration + duration", "duration * number",
122
+ "duration / number", "end-of-month clamping", "add months"))
123
+ def fold(op: str, a: Any, b: Any) -> Any:
124
+ """`a op b` of literal operands where one is a duration (openCypher, see the module's docstring);
125
+ NotBuilt for a pair openCypher has no such operator for (an error)."""
126
+ if op == "+" and isinstance(a, CypherDuration) and isinstance(b, CypherDuration):
127
+ return _normal(a.months + b.months, a.days + b.days, a.seconds + b.seconds, a.nanos + b.nanos)
128
+ if op == "-" and isinstance(a, CypherDuration) and isinstance(b, CypherDuration):
129
+ return _normal(a.months - b.months, a.days - b.days, a.seconds - b.seconds, a.nanos - b.nanos)
130
+ if op in ("+", "-") and isinstance(a, INSTANTS) and isinstance(b, CypherDuration):
131
+ return _plus(a, b, 1 if op == "+" else -1)
132
+ if op == "+" and isinstance(a, CypherDuration) and isinstance(b, INSTANTS):
133
+ return _plus(b, a, 1)
134
+ if op == "*" and isinstance(a, CypherDuration) and _number(b) or \
135
+ op == "*" and _number(a) and isinstance(b, CypherDuration):
136
+ d, x = (a, b) if isinstance(a, CypherDuration) else (b, a)
137
+ return scale(d, *_ratio(x))
138
+ if op == "/" and isinstance(a, CypherDuration) and _number(b):
139
+ p, q = _ratio(b)
140
+ if p == 0:
141
+ raise NotBuilt("a duration divided by zero (an error)")
142
+ return scale(a, q if p > 0 else -q, abs(p))
143
+ raise NotBuilt(f"{op} of a {type(a).__name__} and a {type(b).__name__} (no such operator in openCypher)")
144
+
145
+
146
+ # -- in SQL ------------------------------------------------------------------------------------------------
147
+
148
+ def is_temporal(slot: Any) -> bool:
149
+ return len(slot.datatypes) == 1 and slot.datatype in TEMPORAL
150
+
151
+
152
+ def result_type(op: str, a: Optional[str], b: Optional[str]) -> str:
153
+ """The Cypher type of `a op b` for operand types (a temporal type, or None for a number); NotBuilt
154
+ for a pair with no such operator."""
155
+ instants = (CYPHER_DATE, CYPHER_LOCALTIME, CYPHER_TIME, CYPHER_LOCALDATETIME, CYPHER_DATETIME)
156
+ if op in ("+", "-") and a == CYPHER_DURATION and b == CYPHER_DURATION:
157
+ return CYPHER_DURATION
158
+ if op in ("+", "-") and a in instants and b == CYPHER_DURATION:
159
+ return a
160
+ if op == "+" and a == CYPHER_DURATION and b in instants:
161
+ return b
162
+ if op in ("*", "/") and a == CYPHER_DURATION and b is None or op == "*" and a is None and b == CYPHER_DURATION:
163
+ return CYPHER_DURATION
164
+ raise NotBuilt(f"{op} of these temporal types (no such operator in openCypher, or not built)")
165
+
166
+
167
+ def _long(L, n: int) -> "R.Expr":
168
+ return L.fn("CAST_LONG", L.integer(n))
169
+
170
+
171
+ def _x(L, v: "R.Expr") -> "R.Expr":
172
+ """A 64-bit integer as an exact wide one (DECIMAL(38,0); NUMERIC on PostgreSQL)."""
173
+ return L.fn("CYPHER_XINT", v)
174
+
175
+
176
+ def _xn(L, n: int) -> "R.Expr":
177
+ return L.fn("CYPHER_XINT", L.integer(n))
178
+
179
+
180
+ def _neg(L, v: "R.Expr", sign: int) -> "R.Expr":
181
+ return v if sign > 0 else L.fn("-", L.fn("CAST_LONG", L.integer(0)), v)
182
+
183
+
184
+ _OUT_OF_RANGE = "ProGraph: temporal arithmetic gave a date outside years 1 to 9999"
185
+
186
+
187
+ def _raise(L, message: str, cast: str) -> "R.Expr":
188
+ """An error raised where a row reaches it, typed as `cast` makes it (a CASE arm beside a value)."""
189
+ return L.fn(cast, L.fn("RAISE_ERROR", L.text(message)))
190
+
191
+
192
+ def _guard(L, days: "R.Expr", value: "R.Expr", cast: str) -> "R.Expr":
193
+ """`value`, or an error where its date `days` falls outside years 1 to 9999 (the engines' calendars)."""
194
+ from prograph.plan.sql import rel_algebra as R
195
+ inside = L.all_of([R.CompareExpr(left=days, op=">=", right=_long(L, _FIRST_DAY)),
196
+ R.CompareExpr(left=days, op="<=", right=_long(L, _LAST_DAY))])
197
+ return L.case(inside, value, _raise(L, _OUT_OF_RANGE, cast))
198
+
199
+
200
+ def _parts(L, value: "R.Expr") -> Tuple["R.Expr", "R.Expr", "R.Expr", "R.Expr"]:
201
+ """A duration's months, days, seconds and nanoseconds: the struct's own arguments where the value is
202
+ one written here (a literal, a result, a source column's), so a chain of operators does not repeat
203
+ whole structs; else its fields. A part of a null duration is null either way."""
204
+ fields = _struct_fields(L, value)
205
+ if set(fields) == set("mdsn"):
206
+ return tuple(fields[k] for k in "mdsn")
207
+ return tuple(L.fn("CYPHER_FIELD_LONG", value, L.text(k)) for k in "mdsn")
208
+
209
+
210
+ def _struct_fields(L, value: "R.Expr") -> Dict[Any, "R.Expr"]:
211
+ """The fields of a struct written here (a literal, a result, a source column's), by name; {} where
212
+ the value is not one (a column of a derived table, a CASE)."""
213
+ inner = value
214
+ if getattr(inner, "name", None) == "CASE" and len(inner.args) == 3 and inner.args[1] is L.NULL:
215
+ inner = inner.args[2]
216
+ if getattr(inner, "name", None) != "NAMED_STRUCT":
217
+ return {}
218
+ return {getattr(k, "value", None): v for k, v in zip(inner.args[0::2], inner.args[1::2])}
219
+
220
+
221
+ def duration_struct(L, m: "R.Expr", d: "R.Expr", s: "R.Expr", n: "R.Expr") -> "R.Expr":
222
+ """A duration's struct of its 64-bit parts (n in [0, 10^9))."""
223
+ return L.fn("NAMED_STRUCT", *(x for k, v in zip("mdsn", (m, d, s, n)) for x in (L.text(k), L.fn("CAST_LONG", v))))
224
+
225
+
226
+ def _from_total(L, months, days, total) -> "R.Expr":
227
+ """A duration of months, days and its seconds and nanoseconds as one exact total of nanoseconds."""
228
+ ns = _xn(L, _NS)
229
+ nanos = L.fn("CYPHER_XMOD", L.fn("+", L.fn("CYPHER_XMOD", total, ns), ns), ns)
230
+ seconds = L.fn("CYPHER_XTDIV", L.fn("-", total, nanos), ns)
231
+ return duration_struct(L, months, days, seconds, nanos)
232
+
233
+
234
+ def _scale_sql(L, value: "R.Expr", p: "R.Expr", q: int) -> "R.Expr":
235
+ """`scale` in SQL: a duration's parts times p / q (q a positive constant), exactly; each quotient and
236
+ remainder truncated toward zero, as `scale` takes them."""
237
+ m, d, s, n = (_x(L, v) for v in _parts(L, value))
238
+ xq, q_day = _xn(L, q), _xn(L, q * 86400)
239
+
240
+ def div(a, b):
241
+ return L.fn("CYPHER_XTDIV", a, b)
242
+
243
+ def rem(a, b):
244
+ return L.fn("CYPHER_XMOD", a, b)
245
+ mp = L.fn("*", m, p)
246
+ days_num = L.fn("+", L.fn("*", L.fn("*", d, p), _xn(L, 86400)), L.fn("*", rem(mp, xq), _xn(L, _MONTH_SECONDS)))
247
+ sec_num = L.fn("+", L.fn("*", s, p), rem(days_num, q_day))
248
+ nano_num = L.fn("+", L.fn("*", n, p), L.fn("*", rem(sec_num, xq), _xn(L, _NS)))
249
+ total = L.fn("+", L.fn("*", div(sec_num, xq), _xn(L, _NS)), div(nano_num, xq))
250
+ return _from_total(L, div(mp, xq), div(days_num, q_day), total)
251
+
252
+
253
+ def _local_plus(L, local: "R.Expr", m, d, s, n, sign: int) -> "R.Expr":
254
+ """A local date-time's nanoseconds (DECIMAL) plus (sign 1) or minus (-1) a duration's parts: months on
255
+ its date (the day clamped), then days, then the seconds and nanoseconds."""
256
+ days = L.fn("+", L.fn("CYPHER_DAYS_PLUS_MONTHS", L.fn("CYPHER_NS_DAYS", local), _neg(L, m, sign)), _neg(L, d, sign))
257
+ rest = L.fn("+", L.fn("*", _x(L, s), _xn(L, _NS)), _x(L, n))
258
+ out = L.fn("+", L.fn("+", L.fn("*", _x(L, days), _xn(L, _DAY)), _x(L, L.fn("CYPHER_NS_OF_DAY", local))),
259
+ rest if sign > 0 else L.fn("-", _xn(L, 0), rest))
260
+ return L.fn("CYPHER_XINT", out)
261
+
262
+
263
+ def cell(e: Any, cells: Dict[str, "L.Cell"]) -> "L.Cell":
264
+ """`a op b` in SQL where an operand is a temporal value (rule cypher-temporal-arithmetic)."""
265
+ from prograph.plan.ir import expr as E
266
+ from prograph.plan.ir import lower as L
267
+ from prograph.plan.ir.slot import Bound, value_slot
268
+ op = e.name
269
+ a, b = (L.expr_cell(x, cells) for x in e.args)
270
+ types = [x.slot.datatype if is_temporal(x.slot) else None for x in (a, b)]
271
+ out_type = result_type(op, *types)
272
+ if a.slot.present is Bound.NEVER or b.slot.present is Bound.NEVER:
273
+ return L.nothing()
274
+ null = Bound.NEVER if a.slot.present is Bound.ALWAYS and b.slot.present is Bound.ALWAYS else Bound.MAYBE
275
+ slot = value_slot(out_type, null)
276
+ literals = list(e.args)
277
+ if op == "+" and types[0] == CYPHER_DURATION and types[1] not in (CYPHER_DURATION, None):
278
+ a, b, types, literals = b, a, [types[1], types[0]], literals[::-1]
279
+ either_null = L.any_of([L.is_null(a.value), L.is_null(b.value)])
280
+
281
+ def nullable(v: "R.Expr") -> "R.Expr":
282
+ return L.case(either_null, L.NULL, v)
283
+ if types == [CYPHER_DURATION, CYPHER_DURATION]:
284
+ sign = 1 if op == "+" else -1
285
+ x = [_x(L, v) for v in _parts(L, a.value)]
286
+ y = [_x(L, v) for v in _parts(L, b.value)]
287
+ if sign < 0:
288
+ y = [L.fn("-", _xn(L, 0), v) for v in y]
289
+ total = L.fn("+", L.fn("*", L.fn("+", x[2], y[2]), _xn(L, _NS)), L.fn("+", x[3], y[3]))
290
+ return L.Cell(slot, nullable(_from_total(L, L.fn("+", x[0], y[0]), L.fn("+", x[1], y[1]), total)))
291
+ if out_type == CYPHER_DURATION: # a duration times or divided by a number
292
+ d, number = (a, b) if types[0] == CYPHER_DURATION else (b, a)
293
+ literal = literals[1] if types[0] == CYPHER_DURATION else literals[0]
294
+ if isinstance(literal, E.Const) and _number(literal.value):
295
+ p, q = _ratio(literal.value)
296
+ if op == "/":
297
+ if p == 0:
298
+ raise NotBuilt("a duration divided by zero (an error)")
299
+ p, q = (q if p > 0 else -q), abs(p)
300
+ return L.Cell(slot, nullable(_scale_sql(L, d.value, _xn(L, p), q)))
301
+ from prograph.schema import xsd
302
+ if op == "*" and number.slot.datatype in (xsd.INTEGER,) + tuple(xsd.INTEGER_DATATYPES):
303
+ return L.Cell(slot, nullable(_scale_sql(L, d.value, _x(L, number.value), 1)))
304
+ raise NotBuilt("a duration times or divided by a number read from a column (a literal number, or "
305
+ "times an integer column, is built)")
306
+ sign = 1 if op == "+" else -1
307
+ m, dd, s, n = _parts(L, b.value)
308
+ t = a.value
309
+ if out_type == CYPHER_DATE: # months, days, then the whole days of the seconds (truncated)
310
+ days = L.fn("+", L.fn("+", L.fn("CYPHER_DAYS_PLUS_MONTHS", t, _neg(L, m, sign)), _neg(L, dd, sign)),
311
+ _neg(L, L.fn("INT_QUOTIENT", s, _long(L, 86400)), sign))
312
+ return L.Cell(slot, nullable(_guard(L, days, days, "CAST_LONG")))
313
+ if out_type in (CYPHER_LOCALTIME, CYPHER_TIME): # the seconds and nanoseconds, around the clock
314
+ nod = t if out_type == CYPHER_LOCALTIME else L.fn("CYPHER_FIELD_LONG", t, L.text("n"))
315
+ shift = L.fn("+", L.fn("*", L.fn("CYPHER_FLOOR_MOD", s, L.integer(86400)), _long(L, _NS)), n)
316
+ moved = L.fn("CYPHER_FLOOR_MOD", L.fn("+" if sign > 0 else "-", nod, shift), L.integer(_DAY))
317
+ if out_type == CYPHER_LOCALTIME:
318
+ return L.Cell(slot, nullable(moved))
319
+ return L.Cell(slot, nullable(L.fn("NAMED_STRUCT", L.text("n"), moved, L.text("o"),
320
+ L.fn("CYPHER_FIELD_LONG", t, L.text("o")))))
321
+ if out_type == CYPHER_LOCALDATETIME:
322
+ local = _local_plus(L, t, m, dd, s, n, sign)
323
+ return L.Cell(slot, nullable(_guard(L, L.fn("CYPHER_NS_DAYS", local), local, "CYPHER_XINT")))
324
+ # A date-time at an offset: its local date-time moved, the offset kept. A named zone's rules are not in
325
+ # the engines: a date-time with a zone name read here is an error, never an answer at a wrong offset.
326
+ if isinstance(getattr(_struct_fields(L, t).get("z"), "value", None), str):
327
+ raise NotBuilt("arithmetic on a date-time in a named time zone with a duration read from a column "
328
+ "(the zone's rules are not in the engines)")
329
+ o = L.fn("CYPHER_FIELD_LONG", t, L.text("o"))
330
+ o_ns = L.fn("*", _x(L, o), _xn(L, _NS))
331
+ local = L.fn("CYPHER_XINT", L.fn("+", L.fn("CYPHER_FIELD_DECIMAL", t, L.text("i")), o_ns))
332
+ moved = _local_plus(L, local, m, dd, s, n, sign)
333
+ zone = L.fn("CYPHER_FIELD_STRING", t, L.text("z"))
334
+ instant = L.case(L.is_null(zone, negated=True),
335
+ _raise(L, "ProGraph: arithmetic on a date-time in a named time zone, read from a column, "
336
+ "is not built", "CYPHER_XINT"),
337
+ _guard(L, L.fn("CYPHER_NS_DAYS", moved), L.fn("CYPHER_XINT", L.fn("-", moved, o_ns)),
338
+ "CYPHER_XINT"))
339
+ return L.Cell(slot, nullable(L.fn("NAMED_STRUCT", L.text("i"), instant, L.text("o"), o, L.text("z"), zone)))