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,785 @@
1
+ """APOC Core's apoc.text.* functions, as SQL: the rows of the cypher-apoc table (`cypher_apoc.TABLE`) for text.
2
+
3
+ Each follows APOC Core 5.26's own code (apoc/text/Strings.java: Java's String methods, java.util.regex,
4
+ commons-lang3 and commons-text), checked against Neo4j 5 with APOC Core (tests/cypher/apoc/answers.json).
5
+ Regular expressions are Java's (rule java-regex): verbatim on Spark and Databricks, rewritten for RE2 on
6
+ DuckDB. Where an engine's string function answers otherwise than Java's for some characters, the statement
7
+ raises for a text holding them ("a near miss is never an answer"):
8
+
9
+ - Java counts a text in UTF-16 units: a character past U+FFFF is two. Lengths are counted so (`_utf16_len`);
10
+ positions and edit distances over such a text raise.
11
+ - Java's toUpperCase maps some characters to several (`ß` to `SS`), and toLowerCase writes a final sigma
12
+ `ς`: Spark's upper and lower are Java's; DuckDB's map one character to one, so there such a text raises.
13
+ - commons-lang3's capitalize writes a word's first letter in title case (`dž` to `Dž`), which no engine has:
14
+ where it would differ from upper case, the statement raises.
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import unicodedata
20
+ from typing import TYPE_CHECKING, Callable, List, Optional, Tuple
21
+
22
+ from prograph.plan.ir import expr as E
23
+ from prograph.plan.ir.cypher_apoc import (_DOUBLE, ApocFunctionError, _Function, _raising, _var)
24
+ from prograph.plan.ir.facts import NotBuilt
25
+ from prograph.plan.ir.slot import LIST, NOTHING, VALUE, Bound, Slot, list_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
+ _S_STRING, _S_INTEGER, _S_BOOLEAN = ("scalar", xsd.STRING), ("scalar", xsd.INTEGER), ("scalar", xsd.BOOLEAN)
34
+ _LEVENSHTEIN_NOTE = ("needs PostgreSQL's fuzzystrmatch extension for levenshtein(); not built on PostgreSQL yet "
35
+ "(built on DuckDB, Spark and Databricks)")
36
+
37
+
38
+ # --- the characters an engine's case mapping writes otherwise than Java's ------------------------------
39
+
40
+ def _class(chars) -> str:
41
+ """A regex class of these characters, as ranges (Java's and RE2's syntax alike)."""
42
+ codes = sorted({ord(c) for c in chars})
43
+ parts, i = [], 0
44
+ while i < len(codes):
45
+ j = i
46
+ while j + 1 < len(codes) and codes[j + 1] == codes[j] + 1:
47
+ j += 1
48
+ lo, hi = codes[i], codes[j]
49
+ parts.append(f"\\x{{{lo:x}}}" + (f"-\\x{{{hi:x}}}" if hi > lo else ""))
50
+ i = j + 1
51
+ return "[" + "".join(parts) + "]"
52
+
53
+
54
+ def _bmp():
55
+ return (chr(c) for c in range(0x10000) if not 0xD800 <= c <= 0xDFFF)
56
+
57
+
58
+ _MULTI_UPPER = [c for c in _bmp() if len(c.upper()) > 1] # Java's SpecialCasing: ß -> SS
59
+ _TITLE_DIFFERS = [c for c in _bmp() if len(c.upper()) > 1 or (len(c.title()) == 1 and c.title() != c.upper())]
60
+ _TITLE_RISK = sorted(set(_TITLE_DIFFERS) | {c.upper() for c in _TITLE_DIFFERS if len(c.upper()) == 1}
61
+ | {u for u in _bmp() if len(u.lower()) == 1 and u.lower() in _TITLE_DIFFERS})
62
+ UPPER_DIFFERS = _class(_MULTI_UPPER)
63
+ """Characters Java's toUpperCase writes as several (DuckDB's upper writes one)."""
64
+ LOWER_DIFFERS = _class(["İ", "Σ"])
65
+ """İ (Java: i and a combining dot) and Σ (Java: ς at a word's end): DuckDB's lower writes otherwise."""
66
+ TITLE_DIFFERS = _class(_TITLE_RISK)
67
+ """Characters whose title case (commons-lang3's capitalize) is not their upper case, with their other cases."""
68
+ ASTRAL = "[\\x{10000}-\\x{10ffff}]"
69
+
70
+
71
+ def _stripped(c: str) -> str:
72
+ return "".join(x for x in unicodedata.normalize("NFD", c) if not unicodedata.category(x).startswith("M"))
73
+
74
+
75
+ _ACCENT_FROM, _ACCENT_TO, _ACCENT_SEVERAL = [], [], []
76
+ for _c in (chr(_x) for _x in range(0x110000) if not 0xD800 <= _x <= 0xDFFF):
77
+ if unicodedata.category(_c).startswith("M") or unicodedata.normalize("NFD", _c) == _c:
78
+ continue
79
+ _s = _stripped(_c)
80
+ if len(_s) == 1 and ord(_c) < 0x10000:
81
+ _ACCENT_FROM.append(_c)
82
+ _ACCENT_TO.append(_s)
83
+ elif _s != "":
84
+ _ACCENT_SEVERAL.append(_c)
85
+ ACCENTS_FROM, ACCENTS_TO = "".join(_ACCENT_FROM), "".join(_ACCENT_TO)
86
+ """Each character whose canonical decomposition (NFD) is one character and marks, and that character."""
87
+ SEVERAL_LETTERS = _class(_ACCENT_SEVERAL)
88
+ """Characters whose decomposition leaves several characters (Hangul syllables): clean() raises for them."""
89
+ _UMLAUTS = (("Ä", "Ae"), ("Ü", "Ue"), ("Ö", "Oe"), ("ä", "ae"), ("ü", "ue"), ("ö", "oe"), ("ß", "ss"))
90
+
91
+
92
+ # --- pieces -------------------------------------------------------------------------------------------
93
+
94
+ def _duck() -> bool:
95
+ from prograph.plan.ir.lower import DIALECT
96
+ return DIALECT.get() == "duckdb"
97
+
98
+
99
+ def _string(s: Slot, fn: str) -> None:
100
+ if s.present is not Bound.NEVER and not (s.kinds == {VALUE} and s.datatypes == {xsd.STRING}):
101
+ raise ApocFunctionError(fn, "an argument that is not a string")
102
+
103
+
104
+ def _integer(s: Slot, fn: str) -> None:
105
+ if s.present is not Bound.NEVER and not (s.kinds == {VALUE} and len(s.datatypes) == 1
106
+ and s.datatype in _INTEGERS):
107
+ raise ApocFunctionError(fn, "an argument that is not an integer")
108
+
109
+
110
+ def _const(x: E.Expr):
111
+ return x.value if isinstance(x, E.Const) else _NO
112
+
113
+
114
+ _NO = object()
115
+
116
+
117
+ def _matches(text: R.Expr, java: str, re2: Optional[str] = None) -> R.Expr:
118
+ """Whether `text` holds a match of a pattern written for both engines (the dialects' REGEX_MATCH)."""
119
+ from prograph.plan.ir import lower as L
120
+ return L.fn("REGEX_MATCH", text, L.text(java), L.text(re2 if re2 is not None else java), L.text(java))
121
+
122
+
123
+ def _regex_replace(text: R.Expr, java: str, replacement: str, re2: Optional[str] = None,
124
+ re2_replacement: Optional[str] = None) -> R.Expr:
125
+ """`text` with every match of a constant pattern replaced (Java's replaceAll; DuckDB's regexp_replace)."""
126
+ from prograph.plan.ir import lower as L
127
+ return L.fn("CYPHER_REGEX_REPLACE", text, L.text(java), L.text(re2 if re2 is not None else java),
128
+ L.text(replacement), L.text(re2_replacement if re2_replacement is not None else replacement))
129
+
130
+
131
+ def _utf16_len(text: R.Expr) -> R.Expr:
132
+ """A text's length in UTF-16 units, as Java's length(): its characters, and one more for each past
133
+ U+FFFF."""
134
+ from prograph.plan.ir import lower as L
135
+ astral = L.fn("-", L.fn("STR_LEN", text), L.fn("STR_LEN", _regex_replace(text, ASTRAL, "")))
136
+ return L.fn("+", L.fn("STR_LEN", text), astral)
137
+
138
+
139
+ def _guarded(fn: str, value: R.Expr, of: str, *checks: Tuple[R.Expr, str]) -> R.Expr:
140
+ """`value`, raising where a check holds (each check: a condition and why)."""
141
+ for bad, why in reversed(checks):
142
+ value = _raising(L_coalesce(bad), f"apoc.text.{fn}() {why}", value, of)
143
+ return value
144
+
145
+
146
+ def L_coalesce(cond: R.Expr) -> R.Expr:
147
+ from prograph.plan.ir import lower as L
148
+ return L.fn("COALESCE", cond, L.FALSE)
149
+
150
+
151
+ def _holds(text: R.Expr, cls: str) -> R.Expr:
152
+ return _matches(text, cls)
153
+
154
+
155
+ def _astral_check(*texts: R.Expr) -> Tuple[R.Expr, str]:
156
+ from prograph.plan.ir import lower as L
157
+ return (L.any_of([_holds(t, ASTRAL) for t in texts]),
158
+ "of a text holding a character past U+FFFF (Java counts it as two UTF-16 units; not built)")
159
+
160
+
161
+ def _null_raises(fn: str, *pairs: Tuple["L.Cell", str]) -> List[Tuple[R.Expr, str]]:
162
+ """Checks that raise where an argument APOC reads as a Java object is null (Neo4j's NullPointerException);
163
+ a literal null raises at planning."""
164
+ from prograph.plan.ir import lower as L
165
+ out = []
166
+ for cell, name in pairs:
167
+ if cell.slot.present is Bound.NEVER:
168
+ raise ApocFunctionError(f"apoc.text.{fn}", f"a null {name} (an error in APOC)")
169
+ if cell.slot.present is not Bound.ALWAYS:
170
+ out.append((L.is_null(cell.value), f"of a null {name} (APOC raises)"))
171
+ return out
172
+
173
+
174
+ def _upper1(x: R.Expr) -> R.Expr:
175
+ """`x` with its first character in upper case."""
176
+ from prograph.plan.ir import lower as L
177
+ return L.fn("STR_CONCAT", L.fn("STR_UPPER", L.fn("SUBSTR_CP", x, L.integer(1), L.integer(1))),
178
+ L.fn("SUBSTR_FROM", x, L.integer(2)))
179
+
180
+
181
+ def _lower1(x: R.Expr) -> R.Expr:
182
+ from prograph.plan.ir import lower as L
183
+ return L.fn("STR_CONCAT", L.fn("STR_LOWER", L.fn("SUBSTR_CP", x, L.integer(1), L.integer(1))),
184
+ L.fn("SUBSTR_FROM", x, L.integer(2)))
185
+
186
+
187
+ def _java_trim(x: R.Expr) -> R.Expr:
188
+ """Java's trim(): every character up to U+0020 removed from both ends."""
189
+ return _regex_replace(x, "^[\\x00-\\x20]+|[\\x00-\\x20]+\\z", "")
190
+
191
+
192
+ def _join(lst: R.Expr, sep: R.Expr) -> R.Expr:
193
+ from prograph.plan.ir import lower as L
194
+ return L.fn("CYPHER_STR_JOIN", lst, sep)
195
+
196
+
197
+ def _words(text: R.Expr, java: str) -> R.Expr:
198
+ """Every match of a constant pattern, in order (regexp_extract_all)."""
199
+ from prograph.plan.ir import lower as L
200
+ return L.fn("CYPHER_REGEX_ALL", text, L.text(java), L.text(java))
201
+
202
+
203
+ def _map(lst: R.Expr, body: Callable[[R.Expr], R.Expr]) -> R.Expr:
204
+ from prograph.plan.ir import lower as L
205
+ v = _var()
206
+ return L.fn("LIST_TRANSFORM", lst, v, body(v))
207
+
208
+
209
+ def _keep(lst: R.Expr, test: Callable[[R.Expr], R.Expr]) -> R.Expr:
210
+ from prograph.plan.ir import lower as L
211
+ v = _var()
212
+ return L.fn("LIST_FILTER", lst, v, test(v))
213
+
214
+
215
+ def _ne(a: R.Expr, b: R.Expr) -> R.Expr:
216
+ return R.CompareExpr(left=a, op="<>", right=b)
217
+
218
+
219
+ def _cmp(a: R.Expr, op: str, b: R.Expr) -> R.Expr:
220
+ return R.CompareExpr(left=a, op=op, right=b)
221
+
222
+
223
+ def _pattern(fn: str, x: E.Expr, cell: "L.Cell"):
224
+ """A pattern argument for each engine: (java, re2, ends_anchor). Spark reads Java's pattern itself, a
225
+ column's too; DuckDB needs it written in the query, rewritten for RE2 (rule java-regex)."""
226
+ from prograph.plan.ir import lower as L
227
+ from prograph.plan.ir.java_regex import InvalidJavaPattern, to_re2
228
+ if not _duck():
229
+ return cell.value, L.text(""), False
230
+ value = _const(x)
231
+ if not isinstance(value, str):
232
+ raise ApocFunctionError(fn, "a pattern that is not written in the query (DuckDB's RE2 needs it "
233
+ "rewritten at planning)", "duckdb")
234
+ try:
235
+ t = to_re2(value)
236
+ except InvalidJavaPattern as why:
237
+ raise ApocFunctionError(fn, f"no Java regular expression ({why}; an error in APOC)") from None
238
+ except NotBuilt as why:
239
+ raise ApocFunctionError(fn, f"a pattern RE2 reads otherwise: {str(why).removeprefix('one-plan: ')}",
240
+ "duckdb") from None
241
+ return L.text(value), L.text(t.re2), t
242
+
243
+
244
+ def _empty_matches(fn: str, t) -> None:
245
+ """Refuse on DuckDB a pattern that can match both the empty string and a non-empty one, where the
246
+ empty matches show (a split, a non-empty replacement): RE2 skips an empty match right after a non-empty
247
+ one, Java does not (rule java-regex)."""
248
+ from prograph.plan.ir.java_regex import empty_matches_differ
249
+ if t and empty_matches_differ(t):
250
+ raise ApocFunctionError(fn, "a pattern that can match the empty string and a non-empty one (RE2 skips "
251
+ "an empty match right after a non-empty one, Java does not)", "duckdb")
252
+
253
+
254
+ def _ends_check(text: R.Expr, t) -> List[Tuple[R.Expr, str]]:
255
+ if not t or not t.ends_anchor:
256
+ return []
257
+ return [(_holds(text, "[\\n\\r\\x{85}\\x{2028}\\x{2029}]\\z"),
258
+ "of a text ending with a line terminator under a pattern's $ (Java's $ matches before it; not "
259
+ "built on DuckDB)")]
260
+
261
+
262
+ # --- slots --------------------------------------------------------------------------------------------
263
+
264
+ def _text_slot(kinds: Tuple[str, ...], result: Slot, null_is: str = "null", fn: str = ""):
265
+ """A slot function: each argument's kind ("s" string, "i" integer, "l" list of strings, "a" list of any
266
+ one type), and the result's slot; `null_is` "null" (a null argument gives null) or "value" (the slot
267
+ as is: the lowering answers a null argument itself)."""
268
+ def slot(slots: List[Slot], exprs=()) -> Slot:
269
+ for k, s in zip(kinds, slots):
270
+ if k == "s":
271
+ _string(s, fn)
272
+ elif k == "i":
273
+ _integer(s, fn)
274
+ elif k in "la" and s.present is not Bound.NEVER and s.kinds != {LIST}:
275
+ raise ApocFunctionError(fn, "an argument that is not a list")
276
+ elif k == "l" and s.present is not Bound.NEVER and s.datatypes and s.datatypes != {xsd.STRING}:
277
+ raise ApocFunctionError(fn, "a list that is not of strings")
278
+ if null_is == "null" and any(s.present is Bound.NEVER for s in slots):
279
+ return NOTHING
280
+ return result
281
+ return slot
282
+
283
+
284
+ _STR = value_slot(xsd.STRING, Bound.MAYBE)
285
+ _INT = value_slot(xsd.INTEGER, Bound.MAYBE)
286
+ _BOOL = value_slot(xsd.BOOLEAN, Bound.MAYBE)
287
+ _FLOAT = value_slot(xsd.DOUBLE, Bound.MAYBE)
288
+ _STRS = list_slot(xsd.STRING, Bound.MAYBE)
289
+ _INTS = list_slot(xsd.INTEGER, Bound.MAYBE)
290
+
291
+
292
+ # --- the functions ------------------------------------------------------------------------------------
293
+
294
+ def _join_fn(args, L, exprs) -> R.Expr:
295
+ """join(texts, delimiter): String.join: a null element written "null"; null where either is null."""
296
+ texts, sep = args
297
+ if texts.slot.present is Bound.NEVER or sep.slot.present is Bound.NEVER:
298
+ return L.NULL
299
+ if not texts.slot.datatypes: # [] (a list of no element type)
300
+ return L.case(L.is_null(sep.value), L.NULL_TEXT, L.text(""))
301
+ named = _map(texts.value, lambda v: L.fn("COALESCE", v, L.text("null")))
302
+ return L.case(L.any_of([L.is_null(texts.value), L.is_null(sep.value)]), L.NULL_TEXT, _join(named, sep.value))
303
+
304
+
305
+ def _split_fn(args, L, exprs) -> R.Expr:
306
+ """split(text, regex[, limit]): String.split(regex, limit): limit 0 drops the trailing empty strings
307
+ (all of them where every piece is empty), unless the pattern does not match at all (the text, as it is);
308
+ a negative limit keeps them; a positive one splits at most limit - 1 times. Null where any is null."""
309
+ text, regex = args[0], args[1]
310
+ if any(a.slot.present is Bound.NEVER for a in args):
311
+ return L.NULL
312
+ limit = _const(exprs[2]) if len(exprs) > 2 else 0
313
+ if limit is _NO or not isinstance(limit, int) or isinstance(limit, bool):
314
+ raise ApocFunctionError("apoc.text.split", "a limit that is not an integer written in the query")
315
+ java, re2, t = _pattern("apoc.text.split", exprs[1], regex)
316
+ _empty_matches("apoc.text.split", t)
317
+ if limit > 0:
318
+ if _duck():
319
+ raise ApocFunctionError("apoc.text.split", "a positive limit (DuckDB's string_split_regex has none)",
320
+ "duckdb")
321
+ parts = L.fn("CYPHER_REGEX_SPLIT_LIMIT", text.value, java, re2, L.integer(limit))
322
+ else:
323
+ parts = L.fn("CYPHER_REGEX_SPLIT", text.value, java, re2)
324
+ if limit == 0:
325
+ parts = L.fn("CYPHER_TRIM_TRAILING_EMPTY", parts)
326
+ nulls = [L.is_null(a.value) for a in (text, regex) if a.slot.present is not Bound.ALWAYS]
327
+ out = _guarded("split", parts, ("list", xsd.STRING), *_ends_check(text.value, t))
328
+ return L.case(L.any_of(nulls), L.NULL, out) if nulls else out
329
+
330
+
331
+ def _replace_fn(args, L, exprs, fn="replace") -> R.Expr:
332
+ """replace(text, regex, replacement) (and regreplace): String.replaceAll, the replacement Java's ($n a
333
+ group, \\x the character x); null where any is null."""
334
+ from prograph.plan.ir.java_regex import InvalidJavaPattern, replacement_to_re2
335
+ text, regex, rep = args
336
+ name = f"apoc.text.{fn}"
337
+ if any(a.slot.present is Bound.NEVER for a in args):
338
+ return L.NULL
339
+ java, re2, t = _pattern(name, exprs[1], regex)
340
+ rep_java, rep_re2 = rep.value, L.text("")
341
+ if _duck():
342
+ written = _const(exprs[2])
343
+ if not isinstance(written, str):
344
+ raise ApocFunctionError(name, "a replacement that is not written in the query", "duckdb")
345
+ try:
346
+ rep_re2 = L.text(replacement_to_re2(written, t.groups))
347
+ except InvalidJavaPattern as why:
348
+ raise ApocFunctionError(name, f"{why} in the replacement (an error in APOC)") from None
349
+ except NotBuilt as why:
350
+ raise ApocFunctionError(name, str(why).removeprefix("one-plan: "), "duckdb") from None
351
+ if written:
352
+ _empty_matches(name, t)
353
+ out = L.fn("CYPHER_REGEX_REPLACE", text.value, java, re2, rep_java, rep_re2)
354
+ for bad, why in _ends_check(text.value, t):
355
+ out = _raising(L_coalesce(bad), f"{name}() {why}", out, _S_STRING)
356
+ nulls = [L.is_null(a.value) for a in args if a.slot.present is not Bound.ALWAYS]
357
+ return L.case(L.any_of(nulls), L.NULL_TEXT, out) if nulls else out
358
+
359
+
360
+ def _capitalize_fn(args, L, exprs) -> R.Expr:
361
+ """capitalize(text): the first UTF-16 unit by toUpperCase, then the rest; an error for null or ""."""
362
+ (text,) = args
363
+ checks = _null_raises("capitalize", (text, "text"))
364
+ checks.append((_cmp(L.fn("STR_LEN", text.value), "=", L.integer(0)), "of the empty string (APOC raises)"))
365
+ checks.append((_matches(text.value, "^" + ASTRAL), "of a text starting past U+FFFF (Java upper-cases half of "
366
+ "it; not built)"))
367
+ if _duck():
368
+ checks.append((_matches(text.value, "^" + UPPER_DIFFERS),
369
+ "of a text starting with a letter Java upper-cases as several (not built on DuckDB)"))
370
+ return _guarded("capitalize", _upper1(text.value), _S_STRING, *checks)
371
+
372
+
373
+ def _capitalize_all_fn(args, L, exprs) -> R.Expr:
374
+ """capitalizeAll(text): each word split at " " capitalized (commons-lang3: title case of its first
375
+ character), joined by " " and trimmed (Java's trim); an error for null."""
376
+ (text,) = args
377
+ checks = _null_raises("capitalizeAll", (text, "text"))
378
+ # Only each word's first letter is title-cased (commons-lang3, by code point).
379
+ checks.append((_matches(text.value, "(?:^| )" + TITLE_DIFFERS), "of a word starting with a letter whose title "
380
+ "case is not its upper case (not built)"))
381
+ words = L.fn("CYPHER_REGEX_SPLIT", text.value, L.text(" "), L.text(" "))
382
+ out = _java_trim(_join(_map(words, _upper1), L.text(" ")))
383
+ return _guarded("capitalizeAll", out, _S_STRING, *checks)
384
+
385
+
386
+ def _camel(text: "L.Cell", L, fn: str) -> Tuple[R.Expr, List[Tuple[R.Expr, str]]]:
387
+ """camelCase's text before its first character is lowered: each run of letters and ASCII digits,
388
+ lowered, its first character upper-cased, all joined."""
389
+ checks = _null_raises(fn, (text, "text"))
390
+ # Each word's first letter is title-cased (by code point); the answer's first UTF-16 unit is lowered,
391
+ # half of a character past U+FFFF there.
392
+ checks.append((_matches(text.value, "(?:^|[^\\p{L}0-9])" + TITLE_DIFFERS),
393
+ "of a word starting with a letter whose title case is not its upper case (not built)"))
394
+ checks.append((_matches(text.value, "^[^\\p{L}0-9]*" + ASTRAL), "of a text whose first letter is past U+FFFF "
395
+ "(Java lowers half of it; not built)"))
396
+ if _duck():
397
+ checks.append((_matches(text.value, LOWER_DIFFERS), "of a text holding İ or Σ (Java lower-cases them "
398
+ "otherwise; not built on DuckDB)"))
399
+ words = _words(text.value, "[\\p{L}0-9]+")
400
+ joined = _join(_map(words, lambda w: _upper1(L.fn("STR_LOWER", w))), L.text(""))
401
+ checks.append((_cmp(L.fn("STR_LEN", joined), "=", L.integer(0)), "of a text with no letter or digit "
402
+ "(APOC raises)"))
403
+ return joined, checks
404
+
405
+
406
+ def _camel_case_fn(args, L, exprs) -> R.Expr:
407
+ """camelCase(text): every character other than a letter or an ASCII digit a space; the words lowered,
408
+ each capitalized, joined; the first character lowered. An error for null or a text of no word."""
409
+ joined, checks = _camel(args[0], L, "camelCase")
410
+ return _guarded("camelCase", _lower1(joined), _S_STRING, *checks)
411
+
412
+
413
+ def _upper_camel_case_fn(args, L, exprs) -> R.Expr:
414
+ """upperCamelCase(text): camelCase's, its first character upper-cased (toUpperCase)."""
415
+ joined, checks = _camel(args[0], L, "upperCamelCase")
416
+ if _duck():
417
+ checks.append((_matches(joined, "^" + UPPER_DIFFERS),
418
+ "of a text whose first letter Java upper-cases as several (not built on DuckDB)"))
419
+ return _guarded("upperCamelCase", _upper1(joined), _S_STRING, *checks)
420
+
421
+
422
+ def _snake_case_fn(args, L, exprs) -> R.Expr:
423
+ """snakeCase(text): a text of upper-case letters, digits and _ only is lowered and its _ made spaces;
424
+ then split before each character that is not a lower-case letter or a digit; each piece trimmed (Java's
425
+ trim), the empty ones dropped, lowered, joined by -; all lowered and each -- made -. Null raises."""
426
+ (text,) = args
427
+ checks = _null_raises("snakeCase", (text, "text"))
428
+ if _duck():
429
+ checks.append((_matches(text.value, LOWER_DIFFERS), "of a text holding İ or Σ (Java lower-cases them "
430
+ "otherwise; not built on DuckDB)"))
431
+ shouting = _matches(text.value, "\\A[\\p{Lu}0-9_]+\\z")
432
+ t = L.case(L_coalesce(shouting), L.fn("REPLACE", L.fn("STR_LOWER", text.value), L.text("_"), L.text(" ")),
433
+ text.value)
434
+ pieces = _words(t, "[^\\p{Ll}0-9]?[\\p{Ll}0-9]*")
435
+ trimmed = _map(pieces, lambda p: L.fn("STR_LOWER", _regex_replace(p, "^[\\x00-\\x20]", "")))
436
+ kept = _keep(trimmed, lambda p: _ne(p, L.text("")))
437
+ out = L.fn("REPLACE", L.fn("STR_LOWER", _join(kept, L.text("-"))), L.text("--"), L.text("-"))
438
+ return _guarded("snakeCase", out, _S_STRING, *checks)
439
+
440
+
441
+ def _to_upper_case_fn(args, L, exprs) -> R.Expr:
442
+ """toUpperCase(text): split before each character that is not a-z or 0-9; each piece upper-cased
443
+ (toUpperCase) and stripped of all but A-Z and 0-9; the empty ones dropped; joined by _. Null raises."""
444
+ (text,) = args
445
+ checks = _null_raises("toUpperCase", (text, "text"))
446
+ if _duck():
447
+ checks.append((_matches(text.value, UPPER_DIFFERS), "of a text holding a letter Java upper-cases as "
448
+ "several (not built on DuckDB)"))
449
+ pieces = _words(text.value, "[^a-z0-9]?[a-z0-9]*")
450
+ cleaned = _map(pieces, lambda p: _regex_replace(L.fn("STR_UPPER", p), "[^A-Z0-9]+", ""))
451
+ out = _join(_keep(cleaned, lambda p: _ne(p, L.text(""))), L.text("_"))
452
+ return _guarded("toUpperCase", out, _S_STRING, *checks)
453
+
454
+
455
+ def _pad_fn(args, L, exprs, left: bool) -> R.Expr:
456
+ """lpad/rpad(text, count[, delimiter " "]): the text as it is where its length (UTF-16 units) is at
457
+ least count, else count - length copies of the delimiter's first character before (after) it. Null
458
+ text, count or delimiter, or an empty delimiter, raise."""
459
+ fn = "lpad" if left else "rpad"
460
+ text, count = args[0], args[1]
461
+ delim = args[2] if len(args) > 2 else L.Cell(value_slot(xsd.STRING, Bound.NEVER), L.text(" "))
462
+ checks = _null_raises(fn, (text, "text"), (count, "count"), (delim, "delimiter"))
463
+ checks.append((_cmp(L.fn("STR_LEN", delim.value), "=", L.integer(0)), "of an empty delimiter (APOC raises)"))
464
+ checks.append((_matches(delim.value, "^" + ASTRAL), "of a delimiter starting past U+FFFF (Java pads with half "
465
+ "of it; not built)"))
466
+ n = _utf16_len(text.value)
467
+ pad = L.fn("CYPHER_REPEAT", L.fn("SUBSTR_CP", delim.value, L.integer(1), L.integer(1)), L.fn("-", count.value, n))
468
+ padded = L.fn("STR_CONCAT", pad, text.value) if left else L.fn("STR_CONCAT", text.value, pad)
469
+ out = L.case(_cmp(n, ">=", count.value), text.value, padded)
470
+ return _guarded(fn, out, _S_STRING, *checks)
471
+
472
+
473
+ def _distance(a: R.Expr, b: R.Expr) -> R.Expr:
474
+ from prograph.plan.ir import lower as L
475
+ return L.fn("CYPHER_LEVENSHTEIN", a, b)
476
+
477
+
478
+ def _two_texts(args, L, fn: str, value: Callable[[R.Expr, R.Expr], R.Expr], of: str) -> R.Expr:
479
+ a, b = args
480
+ if a.slot.present is Bound.NEVER or b.slot.present is Bound.NEVER:
481
+ return L.NULL
482
+ out = _guarded(fn, value(a.value, b.value), of, _astral_check(a.value, b.value))
483
+ nulls = [L.is_null(c.value) for c in (a, b) if c.slot.present is not Bound.ALWAYS]
484
+ null = {_S_INTEGER: L.fn("CAST_LONG", L.NULL), _DOUBLE: L.fn("CAST_DOUBLE", L.NULL),
485
+ _S_BOOLEAN: L.NULL_BOOLEAN}[of]
486
+ return L.case(L.any_of(nulls), null, out) if nulls else out
487
+
488
+
489
+ def _levenshtein_fn(args, L, exprs) -> R.Expr:
490
+ """levenshteinDistance(a, b): commons-text's edit distance over UTF-16 units; null where either is
491
+ null."""
492
+ return _two_texts(args, L, "levenshteinDistance", _distance, _S_INTEGER)
493
+
494
+
495
+ def _similarity_fn(args, L, exprs) -> R.Expr:
496
+ """levenshteinSimilarity(a, b): (longer - distance) / longer, 1.0 for two empty texts."""
497
+ def value(a, b):
498
+ longer = L.fn("GREATEST", L.fn("STR_LEN", a), L.fn("STR_LEN", b))
499
+ return L.case(_cmp(longer, "=", L.integer(0)), L.fn("CAST_DOUBLE", L.integer(1)),
500
+ L.fn("/", L.fn("CAST_DOUBLE", L.fn("-", longer, _distance(a, b))), L.fn("CAST_DOUBLE", longer)))
501
+ return _two_texts(args, L, "levenshteinSimilarity", value, _DOUBLE)
502
+
503
+
504
+ def _fuzzy_fn(args, L, exprs) -> R.Expr:
505
+ """fuzzyMatch(a, b): the distance at most 0 for a shorter than 3, 1 shorter than 5, else 2."""
506
+ def value(a, b):
507
+ n = L.fn("STR_LEN", a)
508
+ allowed = L.case(_cmp(n, "<", L.integer(3)), L.integer(0), _cmp(n, "<", L.integer(5)), L.integer(1),
509
+ L.integer(2))
510
+ return _cmp(_distance(a, b), "<=", allowed)
511
+ return _two_texts(args, L, "fuzzyMatch", value, _S_BOOLEAN)
512
+
513
+
514
+ def _hamming_fn(args, L, exprs) -> R.Expr:
515
+ """hammingDistance(a, b): the positions where they differ; an error for two lengths (APOC raises)."""
516
+ def value(a, b):
517
+ i = _var()
518
+ n = L.fn("STR_LEN", a)
519
+ differ = L.fn("LIST_FILTER", L.fn("CYPHER_RANGE", L.integer(1), n, L.integer(1)), i,
520
+ _ne(L.fn("SUBSTR_CP", a, i, L.integer(1)), L.fn("SUBSTR_CP", b, i, L.integer(1))))
521
+ return _raising(_ne(n, L.fn("STR_LEN", b)), "apoc.text.hammingDistance() of two texts of different "
522
+ "lengths (APOC raises)", L.fn("SIZE", differ), _S_INTEGER)
523
+ return _two_texts(args, L, "hammingDistance", value, _S_INTEGER)
524
+
525
+
526
+ def _cleaned(text: R.Expr, L) -> R.Expr:
527
+ """APOC's removeNonWordCharacters: Ä Ü Ö ä ü ö ß as Ae Ue Oe ae ue oe ss; decomposed (NFD) and every mark
528
+ removed (`ACCENTS_FROM` to `ACCENTS_TO`, then \\p{M}); all but letters and digits removed; lowered."""
529
+ out = text
530
+ for a, b in _UMLAUTS:
531
+ out = L.fn("REPLACE", out, L.text(a), L.text(b))
532
+ # The decomposition only where the text is not ASCII (an engine builds the translate map per row).
533
+ out = L.case(L_coalesce(_matches(out, "[^\\x00-\\x7f]")),
534
+ L.fn("CYPHER_TRANSLATE", out, L.text(ACCENTS_FROM), L.text(ACCENTS_TO)), out)
535
+ out = _regex_replace(out, "[^\\p{L}\\p{N}]+", "") # marks are neither letters nor digits
536
+ return L.fn("STR_LOWER", out)
537
+
538
+
539
+ def _clean_checks(text: R.Expr, L) -> List[Tuple[R.Expr, str]]:
540
+ checks = [(_matches(text, SEVERAL_LETTERS), "of a text holding a character that decomposes into several "
541
+ "letters (a Hangul syllable; not built)")]
542
+ if _duck():
543
+ checks.append((_matches(text, "\\x{3a3}"), "of a text holding Σ (Java lower-cases it otherwise; not built "
544
+ "on DuckDB)"))
545
+ return checks
546
+
547
+
548
+ def _clean_fn(args, L, exprs) -> R.Expr:
549
+ """clean(text): APOC's removeNonWordCharacters (`_cleaned`); null for null."""
550
+ (text,) = args
551
+ if text.slot.present is Bound.NEVER:
552
+ return L.NULL
553
+ out = _guarded("clean", _cleaned(text.value, L), _S_STRING, *_clean_checks(text.value, L))
554
+ return L.case(L.is_null(text.value), L.NULL_TEXT, out)
555
+
556
+
557
+ def _compare_cleaned_fn(args, L, exprs) -> R.Expr:
558
+ """compareCleaned(a, b): their clean() equal; false where either is null."""
559
+ a, b = args
560
+ if a.slot.present is Bound.NEVER or b.slot.present is Bound.NEVER:
561
+ return L.FALSE
562
+ same = L.eq(_cleaned(a.value, L), _cleaned(b.value, L))
563
+ out = _guarded("compareCleaned", same, _S_BOOLEAN, *_clean_checks(a.value, L), *_clean_checks(b.value, L))
564
+ return L_coalesce(out)
565
+
566
+
567
+ def _byte_count_fn(args, L, exprs) -> R.Expr:
568
+ """byteCount(text[, charset "UTF-8"]): the text's UTF-8 bytes; null text raises; another charset is not
569
+ built."""
570
+ text = args[0]
571
+ if len(exprs) > 1:
572
+ charset = _const(exprs[1])
573
+ if charset is None:
574
+ raise ApocFunctionError("apoc.text.byteCount", "a null charset (an error in APOC)")
575
+ if not isinstance(charset, str) or charset.upper().replace("-", "").replace("_", "") != "UTF8":
576
+ raise ApocFunctionError("apoc.text.byteCount", "a charset other than UTF-8 written in the query")
577
+ checks = _null_raises("byteCount", (text, "text"))
578
+ return _guarded("byteCount", L.fn("CYPHER_UTF8_BYTES", text.value), _S_INTEGER, *checks)
579
+
580
+
581
+ def _range_args(args, exprs, fn: str, L) -> Tuple[R.Expr, R.Expr, List[Tuple[R.Expr, str]]]:
582
+ """indexOf's and indexesOf's from (0) and to (-1): Java longs, so a null one raises."""
583
+ pairs = []
584
+ from_ = args[2] if len(args) > 2 else L.Cell(value_slot(xsd.INTEGER, Bound.NEVER), L.integer(0))
585
+ to = args[3] if len(args) > 3 else L.Cell(value_slot(xsd.INTEGER, Bound.NEVER), L.integer(-1))
586
+ if len(args) > 2:
587
+ pairs.append((from_, "from"))
588
+ if len(args) > 3:
589
+ pairs.append((to, "to"))
590
+ return from_.value, to.value, _null_raises(fn, *pairs)
591
+
592
+
593
+ def _index_of_fn(args, L, exprs) -> R.Expr:
594
+ """indexOf(text, lookup[, from 0, to -1]): String.indexOf(lookup, from) over the text (cut at `to` where
595
+ 0 <= from < to <= its length); -1 where not found, for a null lookup, or to <= from; null for a null
596
+ text. Positions are UTF-16 units: a text past U+FFFF raises."""
597
+ text, lookup = args[0], args[1]
598
+ if text.slot.present is Bound.NEVER:
599
+ return L.NULL
600
+ from_, to, checks = _range_args(args, exprs, "indexOf", L)
601
+ if lookup.slot.present is Bound.NEVER:
602
+ return L.case(L.is_null(text.value), L.fn("CAST_LONG", L.NULL), L.integer(-1))
603
+ n = L.fn("STR_LEN", text.value)
604
+ whole = L.any_of([L.eq(to, L.integer(-1)), _cmp(to, ">", n)])
605
+ hay = L.case(whole, text.value, L.fn("SUBSTR_CP", text.value, L.integer(1), to))
606
+ f = L.fn("GREATEST", from_, L.integer(0))
607
+ hn = L.fn("STR_LEN", hay)
608
+ at = L.fn("STR_POS", L.fn("SUBSTR_FROM", hay, L.fn("+", f, L.integer(1))), lookup.value)
609
+ found = L.case(_cmp(f, ">", hn), L.case(L.eq(lookup.value, L.text("")), hn, L.integer(-1)),
610
+ L.eq(at, L.integer(0)), L.integer(-1), L.fn("-", L.fn("+", at, f), L.integer(1)))
611
+ cut_error = L.all_of([R.NotExpr(child=whole), _cmp(to, ">", from_),
612
+ _cmp(to, "<", L.integer(0))])
613
+ checks += [_astral_check(text.value, lookup.value),
614
+ (cut_error, "of a negative to above from (Java's substring raises)")]
615
+ out = L.case(whole, found, _cmp(to, "<=", from_), L.integer(-1), found)
616
+ out = _guarded("indexOf", L.fn("CAST_LONG", out), _S_INTEGER, *checks)
617
+ out = L.case(L.is_null(lookup.value), L.integer(-1), out) if lookup.slot.present is not Bound.ALWAYS else out
618
+ return L.case(L.is_null(text.value), L.fn("CAST_LONG", L.NULL), out)
619
+
620
+
621
+ def _indexes_of_fn(args, L, exprs) -> R.Expr:
622
+ """indexesOf(text, lookup[, from 0, to -1 (the length)]): each position at or after from, before to,
623
+ where lookup starts (overlapping matches included); [] for a null lookup; null for a null text."""
624
+ from prograph.plan.ir.cypher_lists import typed_empty
625
+ text, lookup = args[0], args[1]
626
+ if text.slot.present is Bound.NEVER:
627
+ return L.NULL
628
+ from_, to, checks = _range_args(args, exprs, "indexesOf", L)
629
+ empty = typed_empty(xsd.INTEGER, L)
630
+ if lookup.slot.present is Bound.NEVER:
631
+ return L.case(L.is_null(text.value), L.NULL, empty)
632
+ n = L.fn("STR_LEN", text.value)
633
+ end = L.fn("-", L.case(L.eq(to, L.integer(-1)), n, to), L.integer(1))
634
+ k = _var()
635
+ starts = L.fn("CYPHER_RANGE", L.fn("GREATEST", from_, L.integer(0)), L.fn("LEAST", n, end), L.integer(1))
636
+ at = L.fn("LIST_FILTER", starts, k, L.eq(L.fn("SUBSTR_CP", text.value, L.fn("+", k, L.integer(1)),
637
+ L.fn("STR_LEN", lookup.value)), lookup.value))
638
+ checks.append(_astral_check(text.value, lookup.value))
639
+ out = _raising(L_coalesce(checks[-1][0]), "apoc.text.indexesOf() " + checks[-1][1], at, ("list", xsd.INTEGER))
640
+ for bad, why in reversed(checks[:-1]):
641
+ out = _raising(L_coalesce(bad), "apoc.text.indexesOf() " + why, out, ("list", xsd.INTEGER))
642
+ out = L.case(L.is_null(lookup.value), empty, out) if lookup.slot.present is not Bound.ALWAYS else out
643
+ return L.case(L.is_null(text.value), L.NULL, out)
644
+
645
+
646
+ def _repeat_fn(args, L, exprs) -> R.Expr:
647
+ """repeat(item, count): count copies; a null item or count, or a negative count of a non-empty item,
648
+ raise (Java's NegativeArraySizeException)."""
649
+ item, count = args
650
+ checks = _null_raises("repeat", (item, "item"), (count, "count"))
651
+ checks.append((L.all_of([_cmp(count.value, "<", L.integer(0)), _ne(item.value, L.text(""))]),
652
+ "of a negative count (APOC raises)"))
653
+ return _guarded("repeat", L.fn("CYPHER_REPEAT", item.value, count.value), _S_STRING, *checks)
654
+
655
+
656
+ def _slug_fn(args, L, exprs) -> R.Expr:
657
+ """slug(text[, delimiter "-"]): trimmed (Java's trim), each run of characters other than letters, ASCII
658
+ digits and _ replaced by the delimiter (a Java replacement string); null where either is null."""
659
+ from prograph.plan.ir.java_regex import InvalidJavaPattern, replacement_to_re2
660
+ text = args[0]
661
+ delim = args[1] if len(args) > 1 else L.Cell(value_slot(xsd.STRING, Bound.NEVER), L.text("-"))
662
+ if text.slot.present is Bound.NEVER or delim.slot.present is Bound.NEVER:
663
+ return L.NULL
664
+ rep_re2 = L.text("")
665
+ if _duck():
666
+ written = _const(exprs[1]) if len(exprs) > 1 else "-"
667
+ if not isinstance(written, str):
668
+ raise ApocFunctionError("apoc.text.slug", "a delimiter that is not written in the query", "duckdb")
669
+ try:
670
+ rep_re2 = L.text(replacement_to_re2(written, 0))
671
+ except InvalidJavaPattern as why:
672
+ raise ApocFunctionError("apoc.text.slug", f"{why} in the delimiter (an error in APOC)") from None
673
+ pattern = "[^\\p{L}0-9_]+"
674
+ out = L.fn("CYPHER_REGEX_REPLACE", _java_trim(text.value), L.text(pattern), L.text(pattern), delim.value, rep_re2)
675
+ nulls = [L.is_null(c.value) for c in (text, delim) if c.slot.present is not Bound.ALWAYS]
676
+ return L.case(L.any_of(nulls), L.NULL_TEXT, out) if nulls else out
677
+
678
+
679
+ def _format_specs(text: str) -> List:
680
+ """A format string's literal pieces and specifiers: %s, %d, %% and %n are built; any other (width,
681
+ flags, an argument index, another conversion) is refused by name."""
682
+ import re
683
+ out, i = [], 0
684
+ for m in re.finditer(r"%(\d+\$)?([-#+ 0,(<]*)?(\d+)?(\.\d+)?([a-zA-Z%])", text):
685
+ out.append(text[i:m.start()])
686
+ index, flags, width, precision, conversion = m.groups()
687
+ if index or flags or width or precision or conversion not in "sdn%":
688
+ raise ApocFunctionError("apoc.text.format", f"the format specifier {m.group(0)!r} (built: %s, %d, %% "
689
+ "and %n)")
690
+ out.append({"%": "%", "n": "\n"}.get(conversion) or ("arg", conversion))
691
+ i = m.end()
692
+ out.append(text[i:])
693
+ if "%" in re.sub(r"%(\d+\$)?([-#+ 0,(<]*)?(\d+)?(\.\d+)?([a-zA-Z%])", "", text):
694
+ raise ApocFunctionError("apoc.text.format", "a '%' that opens no specifier (an error in APOC)")
695
+ return out
696
+
697
+
698
+ def _format_fn(args, L, exprs) -> R.Expr:
699
+ """format(text, params[, language "en"]): String.format with %s (an element's text: a string, an
700
+ integer, true or false; "null" for null), %d (an integer), %% and %n; null for a null text; the text as it
701
+ is for null params; an error where params has too few elements."""
702
+ text, params = args[0], args[1]
703
+ fmt = _const(exprs[0])
704
+ if text.slot.present is Bound.NEVER:
705
+ return L.NULL
706
+ if not isinstance(fmt, str):
707
+ raise ApocFunctionError("apoc.text.format", "a format string that is not written in the query")
708
+ if len(exprs) > 2:
709
+ lang = _const(exprs[2])
710
+ if lang is None:
711
+ raise ApocFunctionError("apoc.text.format", "a null language (an error in APOC)")
712
+ if not isinstance(lang, str):
713
+ raise ApocFunctionError("apoc.text.format", "a language that is not written in the query")
714
+ if params.slot.present is Bound.NEVER:
715
+ return L.text(fmt)
716
+ pieces = _format_specs(fmt)
717
+ element = params.slot.datatype if params.slot.datatypes else None
718
+ needed = sum(1 for p in pieces if not isinstance(p, str))
719
+ if needed and element is not None and element not in _INTEGERS | {xsd.STRING, xsd.BOOLEAN}:
720
+ raise ApocFunctionError("apoc.text.format", "a parameter that is not a string, an integer or a boolean")
721
+ if any(not isinstance(p, str) and p[1] == "d" for p in pieces) and element not in _INTEGERS | {None}:
722
+ raise ApocFunctionError("apoc.text.format", "%d of a parameter that is not an integer (an error in APOC)")
723
+ if any(not isinstance(p, str) and p[1] == "d" for p in pieces) and len(exprs) > 2 and _const(exprs[2]) != "en":
724
+ raise ApocFunctionError("apoc.text.format", "%d in a language other than en (its digits are the "
725
+ "language's)")
726
+ out, k = L.text(""), 0
727
+ for p in pieces:
728
+ if isinstance(p, str):
729
+ if p:
730
+ out = L.fn("STR_CONCAT", out, L.text(p))
731
+ continue
732
+ k += 1
733
+ if element is None: # [] or a list of nulls
734
+ value = L.text("null")
735
+ else:
736
+ value = L.fn("COALESCE", L.fn("CAST_STRING", L.fn("CYPHER_ELEMENT_AT", params.value, L.integer(k))),
737
+ L.text("null"))
738
+ out = L.fn("STR_CONCAT", out, value)
739
+ if needed:
740
+ out = _raising(_cmp(L.fn("SIZE", params.value), "<", L.integer(needed)),
741
+ "apoc.text.format() with fewer parameters than specifiers (APOC raises)", out, _S_STRING)
742
+ nulls = [L.is_null(text.value)] if text.slot.present is not Bound.ALWAYS else []
743
+ out = L.case(L.is_null(params.value), text.value, out) if params.slot.present is not Bound.ALWAYS else out
744
+ return L.case(L.any_of(nulls), L.NULL_TEXT, out) if nulls else out
745
+
746
+
747
+ def _f(name: str, arity, kinds, result, cell, null_is="null", note=None) -> _Function:
748
+ full = "apoc.text." + name
749
+ return _Function(full, arity, _text_slot(kinds, result, null_is, full), cell, exprs=True, note=note)
750
+
751
+
752
+ _V = "value"
753
+ FUNCTIONS = [
754
+ _f("join", (2,), "ls", _STR, _join_fn),
755
+ _f("split", (2, 3), "ssi", _STRS, _split_fn),
756
+ _f("replace", (3,), "sss", _STR, _replace_fn),
757
+ _f("regreplace", (3,), "sss", _STR, lambda a, L, x: _replace_fn(a, L, x, "regreplace")),
758
+ _f("capitalize", (1,), "s", _STR, _capitalize_fn, _V),
759
+ _f("capitalizeAll", (1,), "s", _STR, _capitalize_all_fn, _V),
760
+ _f("camelCase", (1,), "s", _STR, _camel_case_fn, _V),
761
+ _f("upperCamelCase", (1,), "s", _STR, _upper_camel_case_fn, _V),
762
+ _f("snakeCase", (1,), "s", _STR, _snake_case_fn, _V),
763
+ _f("toUpperCase", (1,), "s", _STR, _to_upper_case_fn, _V),
764
+ _f("lpad", (2, 3), "sis", _STR, lambda a, L, x: _pad_fn(a, L, x, left=True), _V),
765
+ _f("rpad", (2, 3), "sis", _STR, lambda a, L, x: _pad_fn(a, L, x, left=False), _V),
766
+ _f("levenshteinDistance", (2,), "ss", _INT, _levenshtein_fn, note=_LEVENSHTEIN_NOTE),
767
+ _f("levenshteinSimilarity", (2,), "ss", _FLOAT, _similarity_fn, note=_LEVENSHTEIN_NOTE),
768
+ _f("fuzzyMatch", (2,), "ss", _BOOL, _fuzzy_fn, note=_LEVENSHTEIN_NOTE),
769
+ _f("hammingDistance", (2,), "ss", _INT, _hamming_fn),
770
+ _f("clean", (1,), "s", _STR, _clean_fn),
771
+ _f("compareCleaned", (2,), "ss", value_slot(xsd.BOOLEAN, Bound.NEVER), _compare_cleaned_fn, _V),
772
+ _f("byteCount", (1, 2), "ss", _INT, _byte_count_fn, _V),
773
+ _f("indexOf", (2, 3, 4), "ssii", _INT, _index_of_fn, _V),
774
+ _f("indexesOf", (2, 3, 4), "ssii", _INTS, _indexes_of_fn, _V),
775
+ _f("repeat", (2,), "si", _STR, _repeat_fn, _V),
776
+ _f("slug", (1, 2), "ss", _STR, _slug_fn),
777
+ _f("format", (2, 3), "sas", _STR, _format_fn, _V),
778
+ ]
779
+ """The apoc.text.* rows of the cypher-apoc table."""
780
+
781
+ GONE = {
782
+ "apoc.text.tolowercase": "not in APOC Core 5 (Neo4j 5 answers 'Unknown function'); Cypher's toLower() does it",
783
+ "apoc.text.regexreplace": "not in APOC Core 5 (Neo4j 5 answers 'Unknown function'); apoc.text.replace() "
784
+ "replaces by a regular expression",
785
+ }