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,845 @@
1
+ """APOC Core's apoc.date.* and apoc.temporal.* functions: rows of the cypher-apoc table (`cypher_apoc.TABLE`).
2
+
3
+ Each follows APOC Core 5.26's code (apoc/date/Date.java, apoc/temporal/TemporalProcedures.java,
4
+ apoc/util/DateFormatUtil.java, DurationFormatUtil.java), checked against Neo4j 5 with APOC Core
5
+ (tests/cypher/apoc/answers.json). Patterns go through the java-datetime-pattern table; times are written
6
+ from their parts in SQL (days since 1970-01-01, nanoseconds of the day, an offset), so formatting is the same
7
+ on every engine, and parsing uses each engine's own pattern. Temporal values are the cypher-temporal home's.
8
+
9
+ Refused by name, never answered otherwise: a time zone other than UTC, GMT and a fixed GMT offset (a named
10
+ zone needs the zone rules); a pattern letter the table has no row for; a timestamp before 1582-10-15 (where
11
+ SimpleDateFormat turns Julian) or after 9999; apoc.date.systemTimezone (the server's zone).
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import datetime as _dt
17
+ import re
18
+ from typing import TYPE_CHECKING, Dict, List, Optional, Tuple
19
+
20
+ from prograph.plan.ir import expr as E
21
+ from prograph.plan.ir.cypher_apoc import _DOUBLE, ApocFunctionError, _Function, _raising
22
+ from prograph.plan.ir.facts import NotBuilt
23
+ from prograph.plan.ir.java_datetime import (DATE_KINDS, FORMATTER, NAMED, SIMPLE, TIME_KINDS, ZONE_KINDS, Field,
24
+ compile_pattern, kinds, parse_formats)
25
+ from prograph.plan.ir.slot import LIST, MAP, NOTHING, VALUE, Bound, Slot, value_slot
26
+ from prograph.plan.sql import rel_algebra as R
27
+ from prograph.schema import xsd
28
+
29
+ if TYPE_CHECKING:
30
+ from prograph.plan.ir import lower as L
31
+
32
+ _INTEGERS = frozenset({xsd.INTEGER}) | xsd.INTEGER_DATATYPES
33
+ _MS_DAY = 86_400_000
34
+ _DEFAULT = "yyyy-MM-dd HH:mm:ss"
35
+ _GREGORIAN_MS = -12_219_292_800_000 # 1582-10-15T00:00Z: before it SimpleDateFormat's calendar is Julian
36
+ _PAST_9999_MS = 253_402_300_800_000 # 10000-01-01T00:00Z
37
+ _S_STRING, _S_INTEGER = ("scalar", xsd.STRING), ("scalar", xsd.INTEGER)
38
+ _MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
39
+ "November", "December"]
40
+ _DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
41
+ _UNITS = {**{u: 1 for u in ("ms", "milli", "millis", "milliseconds")},
42
+ **{u: 1000 for u in ("s", "second", "seconds")}, **{u: 60_000 for u in ("m", "minute", "minutes")},
43
+ **{u: 3_600_000 for u in ("h", "hour", "hours")}, **{u: _MS_DAY for u in ("d", "day", "days")}}
44
+ _LONG_MAX, _LONG_MIN = 2 ** 63 - 1, -2 ** 63
45
+
46
+
47
+ def _refuse(fn: str, why: str):
48
+ raise ApocFunctionError(fn, why)
49
+
50
+
51
+ def _const(x: E.Expr):
52
+ return x.value if isinstance(x, E.Const) else _NO
53
+
54
+
55
+ _NO = object()
56
+
57
+
58
+ def _string_const(fn: str, x: Optional[E.Expr], default, what: str):
59
+ """An argument APOC reads as a setting (a unit, a pattern, a zone): written in the query."""
60
+ if x is None:
61
+ return default
62
+ v = _const(x)
63
+ if v is _NO or not (v is None or isinstance(v, str)):
64
+ _refuse(fn, f"a {what} that is not written in the query")
65
+ return v
66
+
67
+
68
+ def _unit(fn: str, x: Optional[E.Expr]) -> int:
69
+ """TimeUnit by DateUtils.unit: its milliseconds (null is ms); another name raises."""
70
+ v = _string_const(fn, x, "ms", "unit")
71
+ if v is None:
72
+ return 1
73
+ k = _UNITS.get(v.lower())
74
+ if k is None:
75
+ _refuse(fn, f"the unit {v} (an error in APOC)")
76
+ return k
77
+
78
+
79
+ # --- time zones -------------------------------------------------------------------------------------------
80
+
81
+ _GMT_OFFSET = re.compile(r"(?:GMT|UTC|UT)?([+-])(\d{1,2})(?::?(\d{2}))?")
82
+
83
+
84
+ def _simple_zone(fn: str, x: Optional[E.Expr], default: str = "") -> Tuple[int, str, str]:
85
+ """java.util.TimeZone.getTimeZone's offset and short and long names: "" (APOC's UTC), UTC, GMT, GMT+hh:mm;
86
+ text that is no zone id at all (+02:00) is GMT, as Java reads it; a named zone is refused."""
87
+ v = _string_const(fn, x, default, "time zone")
88
+ if v in (None, "", "UTC"):
89
+ return 0, "UTC", "Coordinated Universal Time"
90
+ if v == "GMT":
91
+ return 0, "GMT", "Greenwich Mean Time"
92
+ m = re.fullmatch(r"GMT([+-])(\d{1,2})(?::?(\d{2}))?", v)
93
+ if m:
94
+ seconds = (int(m.group(2)) * 3600 + int(m.group(3) or 0) * 60) * (-1 if m.group(1) == "-" else 1)
95
+ if abs(seconds) > 23 * 3600 + 59 * 60:
96
+ return 0, "GMT", "Greenwich Mean Time"
97
+ text = "GMT" + ("-" if seconds < 0 else "+") + f"{abs(seconds) // 3600:02d}:{abs(seconds) // 60 % 60:02d}"
98
+ return seconds, text, text
99
+ if re.fullmatch(r"[+\-0-9:]+", v):
100
+ return 0, "GMT", "Greenwich Mean Time" # no zone id: Java's TimeZone falls back to GMT
101
+ _refuse(fn, f"the time zone {v} (only UTC, GMT and GMT offsets are built; a named zone needs zone rules)")
102
+
103
+
104
+ def _zone_id(fn: str, x: Optional[E.Expr], default: str = "UTC") -> Tuple[int, str]:
105
+ """java.time.ZoneId.of's offset and id: Z, UTC, GMT, UT, a fixed offset (+05:30, GMT+05:30); a named
106
+ zone is refused."""
107
+ v = _string_const(fn, x, default, "time zone")
108
+ if v is None:
109
+ _refuse(fn, "a null time zone (an error in APOC)")
110
+ if v in ("Z", "UTC", "GMT", "UT"):
111
+ return 0, v
112
+ m = _GMT_OFFSET.fullmatch(v)
113
+ if m:
114
+ seconds = (int(m.group(2)) * 3600 + int(m.group(3) or 0) * 60) * (-1 if m.group(1) == "-" else 1)
115
+ return seconds, v
116
+ _refuse(fn, f"the time zone {v} (only UTC, GMT and fixed offsets are built; a named zone needs zone rules)")
117
+
118
+
119
+ # --- SQL pieces -------------------------------------------------------------------------------------------
120
+
121
+ def _floor_div(a: R.Expr, b: int, L) -> R.Expr:
122
+ q = L.fn("INT_QUOTIENT", a, L.integer(b))
123
+ r = L.fn("INT_REMAINDER", a, L.integer(b))
124
+ return L.fn("-", q, L.case(R.CompareExpr(left=r, op="<", right=L.integer(0)), L.integer(1), L.integer(0)))
125
+
126
+
127
+ def _floor_mod(a: R.Expr, b: int, L) -> R.Expr:
128
+ return L.fn("-", a, L.fn("*", _floor_div(a, b, L), L.fn("CAST_LONG", L.integer(b))))
129
+
130
+
131
+ def _pad(x: R.Expr, width: int, L) -> R.Expr:
132
+ """`x`, a non-negative integer, as decimal text of at least `width` digits (never cut)."""
133
+ t = L.fn("CAST_STRING", x)
134
+ if width <= 1:
135
+ return t
136
+ return L.case(R.CompareExpr(left=L.fn("STR_LEN", t), op=">=", right=L.integer(width)), t,
137
+ L.fn("LPAD", t, L.integer(width), L.text("0")))
138
+
139
+
140
+ def _names(n: R.Expr, names: List[str], L) -> R.Expr:
141
+ args = []
142
+ for i, name in enumerate(names, 1):
143
+ args += [L.eq(n, L.integer(i)), L.text(name)]
144
+ return L.case(*args)
145
+
146
+
147
+ def _concat(parts: List[R.Expr], L) -> R.Expr:
148
+ out = parts[0] if parts else L.text("")
149
+ for p in parts[1:]:
150
+ out = L.fn("STR_CONCAT", out, p)
151
+ return out
152
+
153
+
154
+ class _Parts:
155
+ """A local date-time's parts in SQL: days since 1970-01-01, nanoseconds of the day, an offset in
156
+ seconds, the zone's names (None where the value has none of them)."""
157
+
158
+ def __init__(self, days=None, nsod=None, offset=None, zone_short=None, zone_long=None, zone_id=None):
159
+ self.days, self.nsod, self.offset = days, nsod, offset
160
+ self.zone_short, self.zone_long, self.zone_id = zone_short, zone_long, zone_id
161
+ self.region = None
162
+
163
+
164
+ def _offset_text(o: R.Expr, form: str, L) -> R.Expr:
165
+ """An offset in seconds as Java writes it: form "hhmm" (+0530), "hh" (+05), "hh:mm" (+05:30),
166
+ "hh[mm]" (+05, or +0530 where the minutes are not zero); a zero offset as Z unless `form` ends in
167
+ "!0" (then +00...)."""
168
+ zero_text = None
169
+ if form.endswith("!0"):
170
+ form = form[:-2]
171
+ else:
172
+ zero_text = "Z"
173
+ a = L.fn("ABS", o)
174
+ sign = L.case(R.CompareExpr(left=o, op="<", right=L.integer(0)), L.text("-"), L.text("+"))
175
+ hh = _pad(L.fn("INT_QUOTIENT", a, L.integer(3600)), 2, L)
176
+ mm = _pad(L.fn("INT_REMAINDER", L.fn("INT_QUOTIENT", a, L.integer(60)), L.integer(60)), 2, L)
177
+ minutes_zero = L.eq(L.fn("INT_REMAINDER", L.fn("INT_QUOTIENT", a, L.integer(60)), L.integer(60)), L.integer(0))
178
+ text = {"hhmm": _concat([sign, hh, mm], L), "hh": _concat([sign, hh], L),
179
+ "hh:mm": _concat([sign, hh, L.text(":"), mm], L),
180
+ "hh[mm]": L.case(minutes_zero, _concat([sign, hh], L), _concat([sign, hh, mm], L))}[form]
181
+ if zero_text is None:
182
+ return text
183
+ return L.case(L.eq(o, L.integer(0)), L.text(zero_text), text)
184
+
185
+
186
+ def _render(pieces, p: _Parts, L) -> R.Expr:
187
+ """The pattern's pieces written from the parts."""
188
+ out: List[R.Expr] = []
189
+ days, nsod = p.days, p.nsod
190
+
191
+ def date_part(name: str) -> R.Expr:
192
+ return L.fn({"year": "CYPHER_DATE_YEAR", "month": "CYPHER_DATE_MONTH", "day": "CYPHER_DATE_DAY",
193
+ "doy": "CYPHER_DATE_DOY", "isoyear": "CYPHER_DATE_ISO_YEAR",
194
+ "isoweek": "CYPHER_DATE_ISO_WEEK"}[name], days)
195
+
196
+ def div(unit: int, modulus: int) -> R.Expr:
197
+ return L.fn("INT_REMAINDER", L.fn("INT_QUOTIENT", nsod, L.fn("CAST_LONG", L.integer(unit))),
198
+ L.integer(modulus))
199
+ for piece in pieces:
200
+ if isinstance(piece, str):
201
+ out.append(L.text(piece))
202
+ continue
203
+ k, w = piece.kind, piece.width
204
+ if k in ("era", "era_long"): # years 1 to 9999 only
205
+ out.append(L.text("Anno Domini" if k == "era_long" else "AD"))
206
+ elif k == "year":
207
+ out.append(_pad(date_part("year"), w, L))
208
+ elif k == "year2":
209
+ out.append(_pad(L.fn("INT_REMAINDER", date_part("year"), L.integer(100)), 2, L))
210
+ elif k == "month":
211
+ out.append(_pad(date_part("month"), w, L))
212
+ elif k in ("month_short", "month_long"):
213
+ names = _MONTHS if k == "month_long" else [m[:3] for m in _MONTHS]
214
+ out.append(_names(date_part("month"), names, L))
215
+ elif k == "day":
216
+ out.append(_pad(date_part("day"), w, L))
217
+ elif k == "doy":
218
+ out.append(_pad(date_part("doy"), w, L))
219
+ elif k in ("dow_short", "dow_long", "isodow"):
220
+ dow = L.fn("+", _floor_mod(L.fn("+", days, L.integer(3)), 7, L), L.integer(1))
221
+ if k == "isodow":
222
+ out.append(L.fn("CAST_STRING", dow))
223
+ else:
224
+ out.append(_names(dow, _DAYS if k == "dow_long" else [d[:3] for d in _DAYS], L))
225
+ elif k == "isoyear":
226
+ out.append(_pad(date_part("isoyear"), 4, L))
227
+ elif k == "isoweek":
228
+ out.append(_pad(date_part("isoweek"), 2, L))
229
+ elif k == "hour":
230
+ out.append(_pad(div(3_600_000_000_000, 24), w, L))
231
+ elif k == "hour12":
232
+ h = L.fn("INT_REMAINDER", div(3_600_000_000_000, 24), L.integer(12))
233
+ out.append(_pad(L.case(L.eq(h, L.integer(0)), L.integer(12), h), w, L))
234
+ elif k == "minute":
235
+ out.append(_pad(div(60_000_000_000, 60), w, L))
236
+ elif k == "second":
237
+ out.append(_pad(div(1_000_000_000, 60), w, L))
238
+ elif k == "millis":
239
+ out.append(_pad(div(1_000_000, 1000), w, L))
240
+ elif k == "nano":
241
+ out.append(_pad(div(1, 1_000_000_000), w, L))
242
+ elif k == "fraction":
243
+ out.append(L.fn("SUBSTR_CP", L.fn("LPAD", L.fn("CAST_STRING", div(1, 1_000_000_000)), L.integer(9),
244
+ L.text("0")), L.integer(1), L.integer(w)))
245
+ elif k == "isofrac": # ISO_LOCAL_TIME's fraction: none for zero, else without trailing zeros
246
+ n = div(1, 1_000_000_000)
247
+ digits = L.fn("CYPHER_TRIM_ZEROS", L.fn("LPAD", L.fn("CAST_STRING", n), L.integer(9), L.text("0")))
248
+ out.append(L.case(L.eq(n, L.integer(0)), L.text(""), L.fn("STR_CONCAT", L.text("."), digits)))
249
+ elif k == "ampm":
250
+ out.append(L.case(R.CompareExpr(left=div(3_600_000_000_000, 24), op="<", right=L.integer(12)),
251
+ L.text("AM"), L.text("PM")))
252
+ elif k == "offset_hhmm":
253
+ out.append(_offset_text(p.offset, "hhmm!0", L))
254
+ elif k == "offset_x1":
255
+ out.append(_offset_text(p.offset, "hh", L))
256
+ elif k in ("offset_x2", "offset_dx2"):
257
+ out.append(_offset_text(p.offset, "hhmm", L))
258
+ elif k in ("offset_x3", "offset_dx3"):
259
+ out.append(_offset_text(p.offset, "hh:mm", L))
260
+ elif k == "offset_dx1":
261
+ out.append(_offset_text(p.offset, "hh[mm]", L))
262
+ elif k in ("offset_lx1", "offset_lx2", "offset_lx3"):
263
+ out.append(_offset_text(p.offset, {"offset_lx1": "hh[mm]", "offset_lx2": "hhmm",
264
+ "offset_lx3": "hh:mm"}[k] + "!0", L))
265
+ elif k == "offset_gmt":
266
+ out.append(L.case(L.eq(p.offset, L.integer(0)), L.text("GMT"),
267
+ L.fn("STR_CONCAT", L.text("GMT"), _offset_text(p.offset, "hh:mm", L))))
268
+ elif k == "zone_short":
269
+ out.append(p.zone_short)
270
+ elif k == "zone_long":
271
+ out.append(p.zone_long)
272
+ elif k == "zone_id":
273
+ out.append(p.zone_id)
274
+ elif k == "zone_bracket": # ISO_DATE_TIME's [zone] after a named zone's offset; none for an offset
275
+ out.append(L.fn("COALESCE", L.fn("STR_CONCAT", L.fn("STR_CONCAT", L.text("["), p.region), L.text("]")),
276
+ L.text("")))
277
+ else:
278
+ raise NotBuilt(f"the pattern letter {piece.letter!r}")
279
+ return _concat(out, L)
280
+
281
+
282
+ def _check_fields(fn: str, pieces, p: _Parts) -> None:
283
+ """A pattern asking for a field the value has not (a time of a date, an offset of a local value): an
284
+ error in Java, refused."""
285
+ ks = kinds(pieces)
286
+ if p.days is None and ks & (DATE_KINDS | {"isoyear", "isoweek", "isodow"}):
287
+ _refuse(fn, "a date field of a value with no date (an error in Java)")
288
+ if p.nsod is None and ks & (TIME_KINDS | {"isofrac"}):
289
+ _refuse(fn, "a time field of a value with no time (an error in Java)")
290
+ if p.offset is None and ks & ZONE_KINDS:
291
+ _refuse(fn, "an offset or zone field of a value with no offset (an error in Java)")
292
+ if p.zone_id is None and "zone_id" in ks:
293
+ _refuse(fn, "a zone id of a value with no zone (an error in Java)")
294
+
295
+
296
+ def _ms_parts(local_ms: R.Expr, offset: int, short: str, long: str, L) -> _Parts:
297
+ days = _floor_div(local_ms, _MS_DAY, L)
298
+ msod = L.fn("-", local_ms, L.fn("*", days, L.fn("CAST_LONG", L.integer(_MS_DAY))))
299
+ nsod = L.fn("*", msod, L.fn("CAST_LONG", L.integer(1_000_000)))
300
+ return _Parts(days, nsod, L.integer(offset), L.text(short), L.text(long), None)
301
+
302
+
303
+ def _to_ms(x: R.Expr, unit_ms: int, L) -> R.Expr:
304
+ """TimeUnit.toMillis: the 64-bit product (an overflow is the database's error; Java saturates)."""
305
+ return x if unit_ms == 1 else L.fn("*", L.fn("CAST_LONG", x), L.fn("CAST_LONG", L.integer(unit_ms)))
306
+
307
+
308
+ def _calendar_checks(ms: R.Expr, L, fn: str) -> List[Tuple[R.Expr, str]]:
309
+ return [(R.CompareExpr(left=ms, op="<", right=L.fn("CAST_LONG", L.integer(_GREGORIAN_MS))),
310
+ f"apoc.date.{fn}() of a time before 1582-10-15 (SimpleDateFormat's calendar is Julian there; not built)"),
311
+ (R.CompareExpr(left=ms, op=">=", right=L.fn("CAST_LONG", L.integer(_PAST_9999_MS))),
312
+ f"apoc.date.{fn}() of a time after the year 9999 (not built)")]
313
+
314
+
315
+ def _guarded(value: R.Expr, of, checks, L) -> R.Expr:
316
+ for bad, why in reversed(checks):
317
+ value = _raising(L.fn("COALESCE", bad, L.FALSE), why, value, of)
318
+ return value
319
+
320
+
321
+ def _null_or(cells, value: R.Expr, null: R.Expr, L) -> R.Expr:
322
+ nulls = [L.is_null(c.value) for c in cells if c.slot.present is not Bound.ALWAYS]
323
+ return L.case(L.any_of(nulls), null, value) if nulls else value
324
+
325
+
326
+ def _arg(args, exprs, i):
327
+ return (args[i], exprs[i]) if i < len(args) else (None, None)
328
+
329
+
330
+ # --- apoc.date ----------------------------------------------------------------------------------------------
331
+
332
+ def _format_ms(fn: str, time: "L.Cell", unit_ms: int, pattern: Optional[str], zone, L) -> R.Expr:
333
+ offset, short, long = zone
334
+ pieces = compile_pattern(pattern or _DEFAULT, SIMPLE)
335
+ utc = _to_ms(time.value, unit_ms, L)
336
+ local = L.fn("+", utc, L.fn("CAST_LONG", L.integer(offset * 1000)))
337
+ out = _render(pieces, _ms_parts(local, offset, short, long, L), L)
338
+ out = _guarded(out, _S_STRING, _calendar_checks(local, L, fn), L)
339
+ return _null_or([time], out, L.NULL_TEXT, L)
340
+
341
+
342
+ def date_format(args, L, exprs) -> R.Expr:
343
+ """format(time, unit 'ms', format 'yyyy-MM-dd HH:mm:ss', timezone ''): SimpleDateFormat in the zone
344
+ (UTC by default); null for a null time."""
345
+ time = args[0]
346
+ if time.slot.present is Bound.NEVER:
347
+ return L.NULL
348
+ unit = _unit("apoc.date.format", exprs[1] if len(exprs) > 1 else None)
349
+ pattern = _string_const("apoc.date.format", exprs[2] if len(exprs) > 2 else None, _DEFAULT, "format")
350
+ zone = _simple_zone("apoc.date.format", exprs[3] if len(exprs) > 3 else None)
351
+ return _format_ms("format", time, unit, pattern, zone, L)
352
+
353
+
354
+ def to_iso8601(args, L, exprs) -> R.Expr:
355
+ time = args[0]
356
+ if time.slot.present is Bound.NEVER:
357
+ return L.NULL
358
+ unit = _unit("apoc.date.toISO8601", exprs[1] if len(exprs) > 1 else None)
359
+ return _format_ms("toISO8601", time, unit, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", (0, "UTC", ""), L)
360
+
361
+
362
+ def _parse_ms(fn: str, text: R.Expr, pattern: str, style: str, offset: int, L) -> R.Expr:
363
+ """The milliseconds since 1970 of text in the pattern, read by the engine's own parser at the offset;
364
+ text it does not read is the database's error (Java's lenient reading of 2023-02-30 included)."""
365
+ duck, spark = parse_formats(compile_pattern(pattern, style))
366
+ if spark is None and L.DIALECT.get() in ("spark", "databricks_sql"):
367
+ raise ApocFunctionError(fn, "parsing a day-of-week name (Spark's parser reads none)", L.DIALECT.get())
368
+ spark = spark or ""
369
+ local = L.fn("CYPHER_PARSE_LOCAL_MS", text, L.text(duck), L.text(spark))
370
+ from prograph.plan.ir.java_datetime import millis_shape
371
+ shape = millis_shape(compile_pattern(pattern, style))
372
+ if shape is not None:
373
+ from prograph.plan.ir.cypher_apoc_text import _matches
374
+ bad = R.NotExpr(child=_matches(text, shape))
375
+ local = _raising(L.fn("COALESCE", bad, L.FALSE), f"{fn}() of milliseconds not written in three digits "
376
+ "(SimpleDateFormat reads .5 as 5 ms, the engine as 500; not built)", local, _S_INTEGER)
377
+ return L.fn("-", local, L.fn("CAST_LONG", L.integer(offset * 1000))) if offset else local
378
+
379
+
380
+ def date_parse(args, L, exprs) -> R.Expr:
381
+ """parse(time, unit 'ms', format 'yyyy-MM-dd HH:mm:ss', timezone ''): the time's milliseconds in the
382
+ unit (truncated); null for a blank or null time."""
383
+ fn = "apoc.date.parse"
384
+ text = args[0]
385
+ if text.slot.present is Bound.NEVER:
386
+ return L.NULL
387
+ unit = _unit(fn, exprs[1] if len(exprs) > 1 else None)
388
+ pattern = _string_const(fn, exprs[2] if len(exprs) > 2 else None, _DEFAULT, "format") or _DEFAULT
389
+ offset = _simple_zone(fn, exprs[3] if len(exprs) > 3 else None)[0]
390
+ blank = L.eq(L.fn("TRIM", text.value), L.text(""))
391
+ absent = L.any_of([L.is_null(text.value), blank])
392
+ # The parser is given null for a blank text, not the text under a guard: DuckDB 2.0 folds the parse of a
393
+ # constant blank text (a literal) inside a nested CASE before it picks the branch, and raises.
394
+ ms = _parse_ms(fn, L.case(absent, L.NULL_TEXT, text.value), pattern, SIMPLE, offset, L)
395
+ out = ms if unit == 1 else L.fn("INT_QUOTIENT", ms, L.fn("CAST_LONG", L.integer(unit)))
396
+ out = _guarded(out, _S_INTEGER, _calendar_checks(ms, L, "parse")[:1], L)
397
+ return L.case(absent, L.fn("CAST_LONG", L.NULL), out)
398
+
399
+
400
+ _ISO_INSTANT = r"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]{1,9})?(Z|[+-][0-9]{2}:[0-9]{2})$"
401
+
402
+
403
+ def from_iso8601(args, L, exprs) -> R.Expr:
404
+ """fromISO8601(time): Instant.parse's milliseconds (seconds required; a fraction past milliseconds
405
+ dropped; Z or an offset); null for null; other text raises."""
406
+ from prograph.plan.ir.cypher_apoc_text import _matches
407
+ text = args[0]
408
+ if text.slot.present is Bound.NEVER:
409
+ return L.NULL
410
+ t = text.value
411
+ local = L.fn("CYPHER_PARSE_LOCAL_MS", L.fn("SUBSTR_CP", t, L.integer(1), L.integer(19)),
412
+ L.text("%Y-%m-%dT%H:%M:%S"), L.text("yyyy-MM-dd'T'HH:mm:ss"))
413
+ rest = L.fn("SUBSTR_FROM", t, L.integer(20)) # [.fraction](Z|+hh:mm)
414
+ has_fraction = L.fn("STR_STARTS", rest, L.text("."))
415
+ zone_len = L.case(L.fn("STR_ENDS", t, L.text("Z")), L.integer(1), L.integer(6))
416
+ digits = L.fn("-", L.fn("-", L.fn("STR_LEN", rest), L.integer(1)), zone_len) # the fraction's digits
417
+ millis = L.fn("CAST_LONG", L.fn("RPAD", L.fn("SUBSTR_CP", rest, L.integer(2), L.fn("LEAST", digits, L.integer(3))), L.integer(3),
418
+ L.text("0")))
419
+ zone = L.fn("SUBSTR_FROM", t, L.fn("-", L.fn("STR_LEN", t), L.integer(5))) # the last six characters
420
+ sign = L.case(L.fn("STR_STARTS", zone, L.text("-")), L.integer(-1), L.integer(1))
421
+ offset_ms = L.case(L.fn("STR_ENDS", t, L.text("Z")), L.integer(0),
422
+ L.fn("*", sign, L.fn("+", L.fn("*", L.fn("CAST_LONG", L.fn("SUBSTR_CP", zone, L.integer(2), L.integer(2))),
423
+ L.integer(3_600_000)),
424
+ L.fn("*", L.fn("CAST_LONG", L.fn("SUBSTR_CP", zone, L.integer(5), L.integer(2))),
425
+ L.integer(60_000)))))
426
+ ms = L.fn("-", L.fn("+", local, L.case(has_fraction, millis, L.integer(0))), offset_ms)
427
+ bad = R.NotExpr(child=_matches(t, _ISO_INSTANT))
428
+ out = _raising(L.fn("COALESCE", bad, L.FALSE), "apoc.date.fromISO8601() of text that is no ISO 8601 instant "
429
+ "(APOC raises)", ms, _S_INTEGER)
430
+ return _null_or([text], out, L.fn("CAST_LONG", L.NULL), L)
431
+
432
+
433
+ def to_years(args, L, exprs) -> R.Expr:
434
+ """toYears(value, format): a number's milliseconds as years of 365 days; a text's parsed time as 1970
435
+ plus those years."""
436
+ value = args[0]
437
+ year = L.fn("CAST_DOUBLE", L.integer(365 * _MS_DAY))
438
+ s = value.slot
439
+ if s.present is Bound.NEVER:
440
+ _refuse("apoc.date.toYears", "a null value (an error in APOC)")
441
+ if s.kinds == {VALUE} and s.datatype in _INTEGERS | {xsd.DOUBLE, xsd.FLOAT}:
442
+ x = value.value # a float's longValue: toward zero
443
+ ms = x if s.datatype in _INTEGERS else L.fn("DOUBLE_TO_BIGINT", L.case(
444
+ R.CompareExpr(left=x, op=">=", right=L.fn("CAST_DOUBLE", L.integer(0))), L.fn("FLOOR", x), L.fn("CEIL", x)))
445
+ return _null_or([value], L.fn("/", L.fn("CAST_DOUBLE", ms), year), L.fn("CAST_DOUBLE", L.NULL), L)
446
+ if not (s.kinds == {VALUE} and s.datatype == xsd.STRING):
447
+ _refuse("apoc.date.toYears", "a value that is neither a number nor a string")
448
+ pattern = _string_const("apoc.date.toYears", exprs[1] if len(exprs) > 1 else None, _DEFAULT, "format") or _DEFAULT
449
+ ms = _parse_ms("apoc.date.toYears", value.value, pattern, SIMPLE, 0, L)
450
+ return _null_or([value], L.fn("+", L.fn("CAST_DOUBLE", L.integer(1970)), L.fn("/", L.fn("CAST_DOUBLE", ms), year)),
451
+ L.fn("CAST_DOUBLE", L.NULL), L)
452
+
453
+
454
+ def _convert(x: R.Expr, from_ms: int, to_ms: int, L) -> R.Expr:
455
+ """TimeUnit.convert: to a coarser unit truncated toward zero; to a finer one multiplied, saturating at
456
+ the 64-bit bounds."""
457
+ if from_ms == to_ms:
458
+ return x
459
+ if from_ms < to_ms:
460
+ return L.fn("INT_QUOTIENT", x, L.fn("CAST_LONG", L.integer(to_ms // from_ms)))
461
+ k = from_ms // to_ms
462
+ bound = _LONG_MAX // k
463
+ return L.case(R.CompareExpr(left=x, op=">", right=L.fn("CAST_LONG", L.integer(bound))),
464
+ L.fn("CAST_LONG", L.integer(_LONG_MAX)),
465
+ R.CompareExpr(left=x, op="<", right=L.fn("CAST_LONG", L.integer(-bound))),
466
+ L.fn("CAST_LONG", L.integer(_LONG_MIN)), L.fn("*", x, L.fn("CAST_LONG", L.integer(k))))
467
+
468
+
469
+ def _long_arg(fn: str, cell: "L.Cell", what: str) -> List[Tuple[R.Expr, str]]:
470
+ from prograph.plan.ir import lower as L
471
+ if cell.slot.present is Bound.NEVER:
472
+ _refuse(fn, f"a null {what} (an error in APOC)")
473
+ if cell.slot.present is Bound.ALWAYS:
474
+ return []
475
+ return [(L.is_null(cell.value), f"{fn}() of a null {what} (APOC raises)")]
476
+
477
+
478
+ def date_convert(args, L, exprs) -> R.Expr:
479
+ fn = "apoc.date.convert"
480
+ checks = _long_arg(fn, args[0], "time")
481
+ out = _convert(L.fn("CAST_LONG", args[0].value), _unit(fn, exprs[1]), _unit(fn, exprs[2]), L)
482
+ return _guarded(out, _S_INTEGER, checks, L)
483
+
484
+
485
+ def date_add(args, L, exprs) -> R.Expr:
486
+ fn = "apoc.date.add"
487
+ checks = _long_arg(fn, args[0], "time") + _long_arg(fn, args[2], "value")
488
+ added = _convert(L.fn("CAST_LONG", args[2].value), _unit(fn, exprs[3]), _unit(fn, exprs[1]), L)
489
+ return _guarded(L.fn("+", L.fn("CAST_LONG", args[0].value), added), _S_INTEGER, checks, L)
490
+
491
+
492
+ _FIELD_UNITS = {**{u: "ms" for u in ("ms", "milli", "millis", "milliseconds")},
493
+ **{u: "s" for u in ("s", "second", "seconds")}, **{u: "m" for u in ("m", "minute", "minutes")},
494
+ **{u: "h" for u in ("h", "hour", "hours")}, **{u: "d" for u in ("d", "day", "days")},
495
+ **{u: "w" for u in ("w", "weekday", "weekdays")}, "month": "month", "months": "month",
496
+ "year": "year", "years": "year"}
497
+
498
+
499
+ def date_field(args, L, exprs) -> R.Expr:
500
+ """field(time, unit 'd', timezone 'UTC'): one field of the time in the zone (ZoneId.of)."""
501
+ fn = "apoc.date.field"
502
+ time = args[0]
503
+ if time.slot.present is Bound.NEVER:
504
+ return L.NULL
505
+ unit = _string_const(fn, exprs[1] if len(exprs) > 1 else None, "d", "unit")
506
+ which = _FIELD_UNITS.get((unit or "").lower())
507
+ if which is None:
508
+ _refuse(fn, f"the unit {unit} (an error in APOC)")
509
+ offset = _zone_id(fn, exprs[2] if len(exprs) > 2 else None)[0]
510
+ local = L.fn("+", L.fn("CAST_LONG", time.value), L.fn("CAST_LONG", L.integer(offset * 1000)))
511
+ days = _floor_div(local, _MS_DAY, L)
512
+ msod = L.fn("-", local, L.fn("*", days, L.fn("CAST_LONG", L.integer(_MS_DAY))))
513
+ value = {"ms": lambda: L.fn("INT_REMAINDER", msod, L.integer(1000)),
514
+ "s": lambda: L.fn("INT_REMAINDER", L.fn("INT_QUOTIENT", msod, L.integer(1000)), L.integer(60)),
515
+ "m": lambda: L.fn("INT_REMAINDER", L.fn("INT_QUOTIENT", msod, L.integer(60_000)), L.integer(60)),
516
+ "h": lambda: L.fn("INT_QUOTIENT", msod, L.integer(3_600_000)),
517
+ "d": lambda: L.fn("CYPHER_DATE_DAY", days), "month": lambda: L.fn("CYPHER_DATE_MONTH", days),
518
+ "year": lambda: L.fn("CYPHER_DATE_YEAR", days),
519
+ "w": lambda: L.fn("+", _floor_mod(L.fn("+", days, L.integer(3)), 7, L), L.integer(1))}[which]()
520
+ return _null_or([time], L.fn("CAST_LONG", value), L.fn("CAST_LONG", L.NULL), L)
521
+
522
+
523
+ def date_fields(args, L, exprs) -> R.Expr:
524
+ """fields(date, pattern 'yyyy-MM-dd HH:mm:ss'): the parsed date's years, months, weekdays and days where
525
+ the pattern has a full date, hours, minutes and seconds where it has hours and minutes, and zoneid UTC.
526
+ A null date answers {} (other keys), so a date that may be null is refused."""
527
+ from prograph.plan.ir.cypher_maps import map_slot
528
+ fn = "apoc.date.fields"
529
+ text = args[0]
530
+ if text.slot.present is Bound.NEVER:
531
+ return L.fn("NAMED_STRUCT")
532
+ if text.slot.present is not Bound.ALWAYS:
533
+ _refuse(fn, "a date that may be null (its answer {} has other keys)")
534
+ pattern = _string_const(fn, exprs[1] if len(exprs) > 1 else None, _DEFAULT, "pattern") or _DEFAULT
535
+ pieces = compile_pattern(pattern, FORMATTER)
536
+ ks = kinds(pieces)
537
+ if ks & ZONE_KINDS:
538
+ _refuse(fn, "a pattern with a zone (its zone id is the text's)")
539
+ has_date = {"year", "month", "day"} <= ks or {"year", "month_short", "day"} <= ks or \
540
+ {"year", "month_long", "day"} <= ks
541
+ has_time = "hour" in ks and "minute" in ks
542
+ if not has_date and not has_time:
543
+ _refuse(fn, "a pattern with neither a full date nor hours and minutes")
544
+ ms = _parse_ms(fn, text.value, pattern, FORMATTER, 0, L)
545
+ days = _floor_div(ms, _MS_DAY, L)
546
+ msod = L.fn("-", ms, L.fn("*", days, L.fn("CAST_LONG", L.integer(_MS_DAY))))
547
+ parts: List[R.Expr] = []
548
+ if has_date:
549
+ parts += [L.text("years"), L.fn("CYPHER_DATE_YEAR", days), L.text("months"), L.fn("CYPHER_DATE_MONTH", days),
550
+ L.text("weekdays"), L.fn("+", _floor_mod(L.fn("+", days, L.integer(3)), 7, L), L.integer(1)),
551
+ L.text("days"), L.fn("CYPHER_DATE_DAY", days)]
552
+ if has_time:
553
+ parts += [L.text("hours"), L.fn("INT_QUOTIENT", msod, L.integer(3_600_000)),
554
+ L.text("minutes"), L.fn("INT_REMAINDER", L.fn("INT_QUOTIENT", msod, L.integer(60_000)), L.integer(60)),
555
+ L.text("seconds"), L.fn("INT_REMAINDER", L.fn("INT_QUOTIENT", msod, L.integer(1000)), L.integer(60))]
556
+ parts += [L.text("zoneid"), L.text("UTC")]
557
+ keyed = dict(zip(parts[0::2], parts[1::2]))
558
+ ordered: List[R.Expr] = []
559
+ for k in sorted(keyed, key=lambda t: t.value):
560
+ ordered += [k, L.fn("CAST_LONG", keyed[k]) if k.value != "zoneid" else keyed[k]]
561
+ return L.fn("NAMED_STRUCT", *ordered)
562
+
563
+
564
+ def _fields_slot(slots: List[Slot], exprs) -> Slot:
565
+ from prograph.plan.ir.cypher_maps import map_slot
566
+ if slots[0].present is Bound.NEVER:
567
+ return map_slot({}, Bound.NEVER)
568
+ pattern = _const(exprs[1]) if len(exprs) > 1 else _DEFAULT
569
+ try:
570
+ ks = kinds(compile_pattern(pattern if isinstance(pattern, str) else _DEFAULT, FORMATTER))
571
+ except NotBuilt:
572
+ ks = set()
573
+ fields: Dict[str, Optional[str]] = {"zoneid": xsd.STRING}
574
+ if {"year", "day"} <= ks:
575
+ fields.update({k: xsd.INTEGER for k in ("years", "months", "weekdays", "days")})
576
+ if {"hour", "minute"} <= ks:
577
+ fields.update({k: xsd.INTEGER for k in ("hours", "minutes", "seconds")})
578
+ return map_slot(fields, Bound.NEVER)
579
+
580
+
581
+ def convert_format(args, L, exprs) -> R.Expr:
582
+ """convertFormat(temporal, currentFormat, convertTo 'yyyy-MM-dd'): the text read in one
583
+ DateTimeFormatter pattern (or named format) and written in another; null for null or ""."""
584
+ fn = "apoc.date.convertFormat"
585
+ text = args[0]
586
+ if text.slot.present is Bound.NEVER:
587
+ return L.NULL
588
+ current = _named_pattern(fn, _string_const(fn, exprs[1], None, "format"))
589
+ target = _named_pattern(fn, _string_const(fn, exprs[2] if len(exprs) > 2 else None, "yyyy-MM-dd", "format"))
590
+ source, out_pieces = compile_pattern(current, FORMATTER), compile_pattern(target, FORMATTER)
591
+ ks = kinds(source)
592
+ has_date = "year" in ks and "day" in ks and bool(ks & {"month", "month_short", "month_long"})
593
+ has_time = "hour" in ks and "minute" in ks
594
+ ms = _parse_ms(fn, text.value, current, FORMATTER, 0, L)
595
+ days = _floor_div(ms, _MS_DAY, L)
596
+ msod = L.fn("-", ms, L.fn("*", days, L.fn("CAST_LONG", L.integer(_MS_DAY))))
597
+ p = _Parts(days if has_date else None, L.fn("*", msod, L.fn("CAST_LONG", L.integer(1_000_000))) if has_time else None)
598
+ _check_fields(fn, out_pieces, p)
599
+ out = _render(out_pieces, p, L)
600
+ empty = L.eq(text.value, L.text(""))
601
+ return L.case(L.any_of([L.is_null(text.value), empty]), L.NULL_TEXT, out)
602
+
603
+
604
+ def _named_pattern(fn: str, name: Optional[str]) -> str:
605
+ if name is None:
606
+ _refuse(fn, "a null format (an error in APOC)")
607
+ if name.lower() in NAMED:
608
+ pattern = NAMED[name.lower()]
609
+ if pattern is None:
610
+ _refuse(fn, f"the named format {name} (not built for this function)")
611
+ return pattern
612
+ return name
613
+
614
+
615
+ def current_timestamp(args, L, exprs) -> R.Expr:
616
+ """currentTimestamp(): the statement's clock in milliseconds, one instant per statement, taken at planning
617
+ (as date() is)."""
618
+ now = _dt.datetime.now(_dt.timezone.utc)
619
+ return L.fn("CAST_LONG", L.integer(int(now.timestamp() * 1000)))
620
+
621
+
622
+ # --- apoc.temporal -------------------------------------------------------------------------------------
623
+
624
+ def _temporal_parts(fn: str, cell: "L.Cell", L) -> _Parts:
625
+ from prograph.plan.ir.cypher_temporal import (CYPHER_DATE, CYPHER_DATETIME, CYPHER_LOCALDATETIME,
626
+ CYPHER_LOCALTIME, CYPHER_TIME)
627
+ t, v = cell.slot.datatype, cell.value
628
+ ns = lambda x: L.fn("*", x, L.fn("CAST_LONG", L.integer(1_000_000_000))) # noqa: E731
629
+ if t == CYPHER_DATE:
630
+ return _Parts(days=v)
631
+ if t == CYPHER_LOCALTIME:
632
+ return _Parts(nsod=v)
633
+ if t == CYPHER_LOCALDATETIME:
634
+ return _Parts(days=L.fn("CYPHER_NS_DAYS", v), nsod=L.fn("CYPHER_NS_OF_DAY", v))
635
+ if t == CYPHER_DATETIME:
636
+ o = L.fn("CYPHER_FIELD_LONG", v, L.text("o"))
637
+ local = L.fn("+", L.fn("CYPHER_FIELD_DECIMAL", v, L.text("i")), ns(o))
638
+ zone = L.fn("COALESCE", L.fn("CYPHER_FIELD_STRING", v, L.text("z")), L.fn("CYPHER_OFFSET_TEXT", o))
639
+ parts = _Parts(days=L.fn("CYPHER_NS_DAYS", local), nsod=L.fn("CYPHER_NS_OF_DAY", local), offset=o,
640
+ zone_id=zone)
641
+ parts.region = L.fn("CYPHER_FIELD_STRING", v, L.text("z")) # a named zone's id; null for an offset
642
+ return parts
643
+ if t == CYPHER_TIME:
644
+ return _Parts(nsod=L.fn("CYPHER_FIELD_LONG", v, L.text("n")), offset=L.fn("CYPHER_FIELD_LONG", v, L.text("o")))
645
+ _refuse(fn, "a value that is not a date, time or duration")
646
+
647
+
648
+ def _iso(name: str, p: _Parts) -> Optional[List]:
649
+ """ISO formatters as pieces: the local date and time (seconds always, the fraction without trailing
650
+ zeros), then the offset and [zone] where the value has them."""
651
+ date = [Field("year", 4, "y"), "-", Field("month", 2, "M"), "-", Field("day", 2, "d")]
652
+ time = [Field("hour", 2, "H"), ":", Field("minute", 2, "m"), ":", Field("second", 2, "s"),
653
+ Field("isofrac", 0, "S")]
654
+ offset = [Field("offset_dx3", 3, "X")] if p.offset is not None else []
655
+ if name == "iso_week_date":
656
+ return [Field("isoyear", 4, "Y"), "-W", Field("isoweek", 2, "w"), "-", Field("isodow", 1, "e")] + offset
657
+ if name == "iso_local_date_time":
658
+ return date + ["T"] + time
659
+ if name in ("iso_date_time", "iso_zoned_date_time"):
660
+ zone = [Field("zone_bracket", 0, "V")] if p.region is not None else []
661
+ if name == "iso_zoned_date_time" and p.offset is None:
662
+ return None
663
+ return date + ["T"] + time + offset + zone
664
+ if name == "iso_offset_date_time":
665
+ return date + ["T"] + time + offset if p.offset is not None else None
666
+ if name == "iso_local_time":
667
+ return time
668
+ if name == "iso_time":
669
+ return time + offset
670
+ if name in ("iso_date", "iso_offset_date"):
671
+ if name == "iso_offset_date" and p.offset is None:
672
+ return None
673
+ return date + offset
674
+ if name == "iso_offset_time":
675
+ return time + offset if p.offset is not None else None
676
+ if name == "basic_iso_date":
677
+ return [Field("year", 4, "y"), Field("month", 2, "M"), Field("day", 2, "d")] + \
678
+ ([Field("offset_x2", 2, "X")] if p.offset is not None else [])
679
+ if name == "iso_ordinal_date":
680
+ return [Field("year", 4, "y"), "-", Field("doy", 3, "D")] + offset
681
+ return None
682
+
683
+
684
+ def temporal_format(args, L, exprs) -> R.Expr:
685
+ """temporal.format(temporal, format 'yyyy-MM-dd'): DateTimeFormatter (or a named format) on a date, time,
686
+ local time, date-time or local date-time; a duration as formatDuration; an error for null."""
687
+ from prograph.plan.ir.cypher_temporal import CYPHER_DURATION
688
+ fn = "apoc.temporal.format"
689
+ value = args[0]
690
+ if value.slot.present is Bound.NEVER:
691
+ _refuse(fn, "a null temporal (an error in APOC)")
692
+ fmt = _string_const(fn, exprs[1] if len(exprs) > 1 else None, "yyyy-MM-dd", "format")
693
+ if fmt is None:
694
+ _refuse(fn, "a null format (an error in APOC)")
695
+ if value.slot.kinds != {VALUE} or len(value.slot.datatypes) != 1:
696
+ _refuse(fn, "a value that is not one temporal type")
697
+ if value.slot.datatype == CYPHER_DURATION:
698
+ return _format_duration(fn, value, fmt, L)
699
+ p = _temporal_parts(fn, value, L)
700
+ lower = fmt.lower()
701
+ if lower in NAMED and NAMED[lower] is None:
702
+ pieces = _iso(lower, p)
703
+ if pieces is None:
704
+ _refuse(fn, f"the named format {fmt} (not built, or an error in Java for this value)")
705
+ else:
706
+ pieces = compile_pattern(NAMED.get(lower) or fmt, FORMATTER)
707
+ _check_fields(fn, pieces, p)
708
+ out = _render(pieces, p, L)
709
+ if value.slot.present is not Bound.ALWAYS:
710
+ out = _raising(L.is_null(value.value), f"{fn}() of null (APOC raises)", out, _S_STRING)
711
+ return out
712
+
713
+
714
+ ISO_DURATION_PATTERNS = {"basic_iso_date": "yyyyMMdd", "iso_date_time": "yyyy-MM-dd'T'HH:mm:ssI",
715
+ "iso_local_date_time": "yyyy-MM-dd'T'HH:mm:ssI", "iso_date": "yyyy-MM-dd",
716
+ "iso_local_date": "yyyy-MM-dd", "iso_time": "HH:mm:ssI", "iso_local_time": "HH:mm:ssI",
717
+ "iso_ordinal_date": "yyyy-DDD", "iso_week_date": "YYYY-'W'ww-e"}
718
+ _DURATION_TOKEN = re.compile(r"([yYu])\1*|([dD])\2*|([ML])\3*|([qQ])\4*|([wW])\5*|([hHkK])\6*|(m)\7*|(s)\8*|"
719
+ r"([nS])\9*|(A)\10*|(N)|(I)\12*")
720
+
721
+
722
+ def _format_duration(fn: str, value: "L.Cell", fmt: str, L) -> R.Expr:
723
+ """DurationFormatUtil: the pattern split at its quotes, every other piece literal; in the others each run
724
+ of a field's letters its value, zero-padded to the run's length (S and n cut to it; I the second's
725
+ fraction without trailing zeros, with its point). A negative part raises (Java writes its sign inside
726
+ the padding; not built)."""
727
+ from prograph.plan.ir.java_datetime import NAMED as _N
728
+ pattern = ISO_DURATION_PATTERNS.get(fmt.lower(), _N.get(fmt.lower()) or fmt)
729
+ v = value.value
730
+ m, d, s, n = (L.fn("CYPHER_FIELD_LONG", v, L.text(k)) for k in "mdsn")
731
+ q = lambda a, b: L.fn("INT_QUOTIENT", a, L.fn("CAST_LONG", L.integer(b))) # noqa: E731
732
+ r = lambda a, b: L.fn("INT_REMAINDER", a, L.fn("CAST_LONG", L.integer(b))) # noqa: E731
733
+ fields = [q(m, 12), d, r(m, 12), r(q(m, 3), 4), q(d, 7), q(s, 3600), r(q(s, 60), 60), r(s, 60), n,
734
+ L.fn("+", L.fn("*", s, L.fn("CAST_LONG", L.integer(1000))), q(n, 1_000_000)),
735
+ L.fn("+", L.fn("*", s, L.fn("CAST_LONG", L.integer(1_000_000_000))), n), n]
736
+ out: List[R.Expr] = []
737
+ for i, part in enumerate(pattern.split("'")):
738
+ if i % 2:
739
+ out.append(L.text(part))
740
+ continue
741
+ at = 0
742
+ for tok in _DURATION_TOKEN.finditer(part):
743
+ if tok.start() > at:
744
+ out.append(L.text(part[at:tok.start()]))
745
+ group = next(g for g in range(1, 13) if tok.group(g) is not None)
746
+ width = len(tok.group(0))
747
+ x = fields[group - 1]
748
+ if group == 9: # S, n: zero-padded, then cut to the run's length
749
+ out.append(L.fn("SUBSTR_CP", _pad(x, width, L), L.integer(1), L.integer(width)))
750
+ elif group == 12: # I: the fraction without trailing zeros, with its point; none for zero
751
+ digits = _pad(x, width, L)
752
+ trimmed = L.fn("CYPHER_TRIM_ZEROS", digits)
753
+ out.append(L.case(L.eq(trimmed, L.text("")), L.text(""), L.fn("STR_CONCAT", L.text("."), trimmed)))
754
+ else:
755
+ out.append(_pad(x, width, L))
756
+ at = tok.end()
757
+ if at < len(part):
758
+ out.append(L.text(part[at:]))
759
+ negative = L.any_of([R.CompareExpr(left=x, op="<", right=L.integer(0)) for x in (m, d, s, n)])
760
+ text = _raising(L.fn("COALESCE", negative, L.FALSE), f"{fn}() of a negative duration (not built)",
761
+ _concat(out, L), _S_STRING)
762
+ if value.slot.present is not Bound.ALWAYS:
763
+ text = _raising(L.is_null(v), f"{fn}() of null (APOC raises)", text, _S_STRING)
764
+ return text
765
+
766
+
767
+ def format_duration(args, L, exprs) -> R.Expr:
768
+ from prograph.plan.ir.cypher_temporal import CYPHER_DURATION
769
+ fn = "apoc.temporal.formatDuration"
770
+ value = args[0]
771
+ if value.slot.present is Bound.NEVER:
772
+ _refuse(fn, "a null duration (an error in APOC)")
773
+ if not (value.slot.kinds == {VALUE} and value.slot.datatypes == {CYPHER_DURATION}):
774
+ _refuse(fn, "a value that is not a duration (an error in APOC)")
775
+ fmt = _string_const(fn, exprs[1], None, "format")
776
+ if fmt is None:
777
+ _refuse(fn, "a null format (an error in APOC)")
778
+ return _format_duration(fn, value, fmt, L)
779
+
780
+
781
+ def to_zoned_temporal(args, L, exprs) -> R.Expr:
782
+ """toZonedTemporal(time, format 'yyyy-MM-dd HH:mm:ss', timezone 'UTC'): the text read by
783
+ SimpleDateFormat in the zone, as a date-time in that zone; null for null."""
784
+ fn = "apoc.temporal.toZonedTemporal"
785
+ text = args[0]
786
+ if text.slot.present is Bound.NEVER:
787
+ return L.NULL
788
+ pattern = _string_const(fn, exprs[1] if len(exprs) > 1 else None, _DEFAULT, "format") or _DEFAULT
789
+ zone_arg = exprs[2] if len(exprs) > 2 else None
790
+ offset, zone = _zone_id(fn, zone_arg)
791
+ if _simple_zone(fn, zone_arg, "UTC")[0] != offset:
792
+ _refuse(fn, f"the time zone {zone} (Java's TimeZone and ZoneId read it differently)")
793
+ ms = _parse_ms(fn, text.value, pattern, SIMPLE, offset, L)
794
+ ms = _guarded(ms, _S_INTEGER, _calendar_checks(ms, L, "parse")[:1], L)
795
+ struct = L.fn("NAMED_STRUCT", L.text("i"), L.fn("CYPHER_MS_TO_NS", ms), L.text("o"), L.fn("CAST_LONG", L.integer(offset)),
796
+ L.text("z"), L.text(zone))
797
+ return _null_or([text], struct, L.NULL, L)
798
+
799
+
800
+ # --- slots and the table -----------------------------------------------------------------------------
801
+
802
+ def _slot_fn(result: Slot, null_is: str = "null"):
803
+ def slot(slots: List[Slot], exprs=()) -> Slot:
804
+ if null_is == "null" and slots and slots[0].present is Bound.NEVER:
805
+ return NOTHING
806
+ return result
807
+ return slot
808
+
809
+
810
+ def _f(name: str, arity, result: Slot, cell, null_is: str = "null") -> _Function:
811
+ slot = _fields_slot if cell is date_fields else _slot_fn(result, null_is)
812
+ return _Function(name, arity, slot, cell, exprs=True)
813
+
814
+
815
+ _STR = value_slot(xsd.STRING, Bound.MAYBE)
816
+ _INT = value_slot(xsd.INTEGER, Bound.MAYBE)
817
+ _FLOAT = value_slot(xsd.DOUBLE, Bound.MAYBE)
818
+
819
+
820
+ def _datetime_slot() -> Slot:
821
+ from prograph.plan.ir.cypher_temporal import CYPHER_DATETIME
822
+ return value_slot(CYPHER_DATETIME, Bound.MAYBE)
823
+
824
+
825
+ FUNCTIONS = [
826
+ _f("apoc.date.format", (1, 2, 3, 4), _STR, date_format),
827
+ _f("apoc.date.toISO8601", (1, 2), _STR, to_iso8601),
828
+ _f("apoc.date.parse", (1, 2, 3, 4), _INT, date_parse),
829
+ _f("apoc.date.fromISO8601", (1,), _INT, from_iso8601),
830
+ _f("apoc.date.toYears", (1, 2), _FLOAT, to_years, "value"),
831
+ _f("apoc.date.convert", (3,), _INT, date_convert, "value"),
832
+ _f("apoc.date.add", (4,), _INT, date_add, "value"),
833
+ _f("apoc.date.field", (1, 2, 3), _INT, date_field),
834
+ _f("apoc.date.fields", (1, 2), _STR, date_fields, "value"),
835
+ _f("apoc.date.convertFormat", (2, 3), _STR, convert_format),
836
+ _f("apoc.date.currentTimestamp", (0,), value_slot(xsd.INTEGER, Bound.NEVER), current_timestamp, "value"),
837
+ _f("apoc.temporal.format", (1, 2), _STR, temporal_format, "value"),
838
+ _f("apoc.temporal.formatDuration", (2,), _STR, format_duration, "value"),
839
+ _Function("apoc.temporal.toZonedTemporal", (1, 2, 3),
840
+ lambda slots, exprs=(): NOTHING if slots[0].present is Bound.NEVER else _datetime_slot(),
841
+ to_zoned_temporal, exprs=True),
842
+ ]
843
+ """The apoc.date.* and apoc.temporal.* rows of the cypher-apoc table."""
844
+
845
+ GONE = {"apoc.date.systemtimezone": "the server's time zone: ProGraph's answer has no server zone (not built)"}