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,3726 @@
1
+ """SQL Dialect Configuration + Behavior.
2
+
3
+ Two layers:
4
+
5
+ - :class:`DialectConfig`, frozen dataclass with SQL syntax **flags**
6
+ (quote character, feature support, function/type mappings). These are
7
+ pure data; the serializer reads them.
8
+
9
+ - :class:`Dialect`, wraps a ``DialectConfig`` and provides SQL
10
+ **generation methods** (quoting, literals, LIMIT clauses, table
11
+ aliases). Eliminates scattered ``if dialect == 'oracle'`` checks.
12
+
13
+ Adding a new database: create one ``DialectConfig`` instance, register
14
+ it in ``get_dialect()``. The ``Dialect`` class's default methods handle
15
+ most SQL engines; override only what differs.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import re
21
+ from dataclasses import dataclass, field
22
+ from typing import Dict, List, Optional
23
+ from prograph.rules import home, module_home
24
+
25
+ module_home("exact-quotient-subquery", "An exact quotient over a subquery expression",
26
+ terms=("SPARQL_DIV", "exact AVG", "quotient", "EXISTS", "higher-order function"))
27
+
28
+
29
+ # =========================================================================
30
+ # DialectConfig, pure data
31
+ # =========================================================================
32
+
33
+
34
+ @dataclass(frozen=True)
35
+ class DialectConfig:
36
+ """Configuration flags for a SQL dialect."""
37
+
38
+ name: str
39
+
40
+ quote_char: str = '`'
41
+ """Character used to quote identifiers."""
42
+
43
+ supports_anti_join: bool = False
44
+ supports_semi_join: bool = False
45
+ supports_lateral_view: bool = False
46
+ supports_recursive_cte: bool = False
47
+ supports_cycle_detection: bool = False
48
+
49
+ supports_recursive_union: bool = False
50
+ """Whether a recursive CTE may combine its terms with UNION, which
51
+ removes duplicates, rather than only UNION ALL. With it a reachability
52
+ walk ends when no new (start, node) pair appears. PostgreSQL, DuckDB and
53
+ MySQL allow it; Spark, Databricks, SQL Server and Oracle require
54
+ UNION ALL."""
55
+
56
+ reach_by_level_statement: bool = False
57
+ """Whether an unbounded reachability walk is written as one statement
58
+ of a set number of levels that raises, inside the database, when the
59
+ graph is deeper. For a dialect with no recursive UNION that
60
+ can raise from within a statement and emit two rows from one joined row:
61
+ Spark and Databricks. SQL Server and Oracle walk trails until both are
62
+ shown there."""
63
+
64
+ kinds_beside_a_list: bool = False
65
+ """Whether a list of numbers of both kinds can be collected with its
66
+ elements' kinds: one list of (value, kind) pairs, split into the list
67
+ and an array of 1 and 0. It needs an array of
68
+ structs: Spark, Databricks and DuckDB. PostgreSQL has no anonymous
69
+ struct array, and MySQL, SQL Server and Oracle no arrays; there an
70
+ element of such a list keeps the refusal."""
71
+
72
+ float_text_is_javas: str = 'never'
73
+ """Where the database's cast of a DOUBLE to text is already Java's
74
+ Double.toString, Cypher's toString of a float:
75
+ 'always' (Spark, Databricks: `1.0E20`, `1.0E-5`, `NaN`, `Infinity`,
76
+ `4.9E-324`), 'plain_range' (DuckDB, for 10^-3 <= |x| < 10^7 only; it
77
+ writes `1e+20` beyond), or 'never'. Where it is, the cast is the text,
78
+ and `ExprTranslator._cypher_float_text` writes the rest; rewritten for
79
+ every row, the text cost 50 times the cast and Spark's codegen."""
80
+
81
+ non_finite_double: Optional[str] = None
82
+ """How a NaN or an infinity is written, `{0}` being `NaN`, `Infinity`
83
+ or `-Infinity`: a double the database reads from that text, or None
84
+ where it has none (SQL Server, MySQL). Written bare, `nan` and `inf`
85
+ were column names to the database: `"NaN"^^xsd:double` and `1e400` in
86
+ a BIND, a FILTER or VALUES were binder errors."""
87
+
88
+ words_a_table_name_quotes: frozenset = frozenset()
89
+ identifier_fold: Optional[str] = None
90
+ """How the database folds an undelimited (regular) identifier's case: 'lower' (PostgreSQL),
91
+ 'upper' (Oracle, the SQL standard), or None where names compare without regard to case or
92
+ are kept as written (DuckDB, Spark, Databricks, MySQL columns, SQL Server's default)."""
93
+ """Names a table may not take unquoted in this database: its reserved
94
+ words, lower case (on DuckDB its type_function and column_name keywords
95
+ too). A node's or relationship's source table named by one
96
+ (`show`, `order`, `group`) is written quoted, each part of a dotted name
97
+ on its own (``Dialect.table_name``). Empty where the
98
+ database takes them unquoted: Spark and Databricks enforce reserved
99
+ words only with spark.sql.ansi.enforceReservedKeywords, off by default,
100
+ and a table named `order` reads there as it is."""
101
+
102
+ union_by_field_name: bool = True
103
+ """Whether a UNION lines two struct columns up by their field names.
104
+ Spark and Databricks line them up by position and keep the first
105
+ side's names: a node map with (name, span, id) under one with (name,
106
+ bore, id) reads its span as `bore`, and sides of different lengths
107
+ are an error. There a statement
108
+ that holds a UNION and a node's map from a table of several labels
109
+ is refused (`ExprTranslator.shared_table_node_meets_a_union`)."""
110
+
111
+ decimal_result_held_in_18: bool = False
112
+ """Whether `+`, `-` and `*` of two DECIMALs of 18 digits or fewer is
113
+ held in 18 digits and raises past them: DuckDB's DECIMAL(18) (a
114
+ DECIMAL(13, 2) squared at 12345678901.25 overflowed). There one operand
115
+ is held in 19 digits where the result may pass 18
116
+ (`ExprTranslator._held_past_18_digits`). Spark and Databricks widen the
117
+ result themselves."""
118
+
119
+ lists_of_lists: bool = True
120
+ """Whether a list may hold lists. PostgreSQL's array of arrays is one
121
+ rectangular matrix: `[[1], [2, 3]]` is an error there and `[[1, 2],
122
+ [3, 4]]` is read back as the flat list [1, 2, 3, 4]. Until lists of
123
+ lists have a jsonb form on PostgreSQL they have no SQL
124
+ translation there, a guard Brice allowed on 2026-10-11."""
125
+
126
+ current_date_parens: bool = True
127
+ """Whether ``CURRENT_DATE`` requires parentheses."""
128
+
129
+ limit_style: str = 'standard'
130
+ """'standard' (LIMIT/OFFSET), 'fetch' (FETCH FIRST N ROWS ONLY),
131
+ or 'top' (SELECT TOP N)."""
132
+
133
+ has_boolean_type: bool = True
134
+ """Whether the database has a native BOOLEAN type with TRUE/FALSE
135
+ literals. SQL Server (BIT) and Oracle (NUMBER(1)) do not."""
136
+
137
+ integer_results_need_declared_type: bool = False
138
+ """Whether a computed integer reaches the result as an integer only if
139
+ the SQL declares it one. Oracle's driver reads a ``NUMBER`` of no declared
140
+ scale (a count, ``age + 1``, a literal) as a double, so a RETURN column
141
+ ProGraph knows is an integer is cast to ``NUMBER(19)``."""
142
+
143
+ max_identifier_length: Optional[int] = None
144
+ """The longest name the database keeps for a column alias, or None for
145
+ no limit. A longer one is cut short (two long names can then collide)
146
+ or rejected, so ProGraph does not send it (``Dialect.output_name_error``)."""
147
+
148
+ identifier_length_in_bytes: bool = False
149
+ """Whether ``max_identifier_length`` counts UTF-8 bytes (PostgreSQL,
150
+ Oracle) rather than characters."""
151
+
152
+ database_name: str = ''
153
+ """How an error names the database; ``name`` when empty."""
154
+
155
+ supports_unique_constraint: bool = True
156
+ """Whether CREATE TABLE supports UNIQUE constraints. Databricks SQL
157
+ accepts them only when a non-default setting is enabled."""
158
+
159
+ param_style: str = '%s'
160
+ """PEP 249 parameter placeholder style.
161
+ ``'%s'`` for mysql-connector.
162
+ ``'?'`` for pyodbc (SQL Server), DuckDB and Databricks.
163
+ ``':1'`` for oracledb (positional bind).
164
+ ``'$1'`` for PostgreSQL through ADBC (positional bind)."""
165
+
166
+ string_escape: str = 'standard'
167
+ """How a string literal escapes. 'standard' doubles a quote
168
+ (``'O''Neil'``). 'backslash' is Spark and Databricks, which read
169
+ ``'O''Neil'`` as two adjacent literals (``ONeil``) and treat a
170
+ backslash as an escape: ``'O\\'Neil'``, ``'a\\\\b'``. 'mysql' doubles a
171
+ quote but also escapes a backslash, which MySQL reads as an escape by
172
+ default."""
173
+
174
+ function_map: Dict[str, Optional[str]] = field(default_factory=dict)
175
+ """IR function name to this dialect's rendering, read by
176
+ ``Dialect.render_function`` before any serializer special case.
177
+
178
+ A plain name is a rename (``'LEN'``); a string with ``{0}``, ``{1}``...
179
+ is a template over the rendered arguments (``'CHARINDEX({1}, {0})'``);
180
+ ``None`` means the dialect has no such function, and generation refuses
181
+ with a reason instead of emitting something that answers wrongly. A name
182
+ not in the map is rendered by the serializer. Only verified entries
183
+ belong here."""
184
+
185
+ type_map: Dict[str, str] = field(default_factory=dict)
186
+ """IR type name → dialect type name for CAST."""
187
+
188
+ array_literal: Optional[str] = None
189
+ """An array value, ``{}`` the comma-separated elements (``array({})``,
190
+ ``[{}]``, ``ARRAY[{}]``); None where the database has no array type."""
191
+
192
+ timestamp_type: str = 'TIMESTAMP'
193
+ """The type a local date-time (one with no zone) is cast to. SQL
194
+ Server's TIMESTAMP is a row version, its datetime is DATETIME2. On
195
+ Spark and Databricks TIMESTAMP is a zoned instant read in the
196
+ session's zone: a local date-time written as one came back from a
197
+ warehouse labelled UTC, and on Spark a local time the session's zone
198
+ skips (02:30 on the night the clocks go forward) came back an hour
199
+ later. Their type with no zone is TIMESTAMP_NTZ."""
200
+
201
+ zoned_timestamp_type: Optional[str] = None
202
+ """The type a value that carries a zone is cast to, where that is not
203
+ `timestamp_type`: a datetime given as a parameter with its tzinfo."""
204
+
205
+
206
+ # =========================================================================
207
+ # Dialect, behavior
208
+ # =========================================================================
209
+
210
+
211
+ class Dialect:
212
+ """SQL generation behavior for a specific database.
213
+
214
+ Wraps a ``DialectConfig`` and provides methods that the OWL SQL
215
+ generators, serializers, and engine code call instead of checking
216
+ flags directly.
217
+
218
+ Subclass for deeply non-standard databases (Oracle has several);
219
+ the base class handles Postgres, DuckDB, Spark, Databricks SQL,
220
+ and SQL Server via config flags.
221
+ """
222
+
223
+ _UNMAPPED = object()
224
+
225
+ def before_a_sliced_cte(self) -> str:
226
+ """What stands between `AS` and the parenthesis of a CTE whose own
227
+ SELECT keeps rows by a LIMIT or an OFFSET: nothing, but on DuckDB
228
+ (docs/architecture/rules.md, "A CTE that holds a slice"). Where it
229
+ is a word, a CTE that holds a slice is written with it."""
230
+ return ""
231
+
232
+ def binds_a_union_cte_as_recursive(self) -> bool:
233
+ """Whether a WITH RECURSIVE gives each of its CTEs whose SELECT is
234
+ a UNION the types of the UNION's first branch alone, as it does a
235
+ recursive CTE's first term: no, but on DuckDB (docs/architecture/
236
+ rules.md, "A union CTE beside a recursive one"). Where it does,
237
+ `SparkSQLSerializer._cte_body` writes such a CTE as a SELECT over
238
+ the UNION."""
239
+ return False
240
+
241
+ def sorts_by_a_column_its_select_list_names_by_itself(self) -> bool:
242
+ """Whether an ORDER BY key may read, by its source's name, a column
243
+ the select list projects under that same name (`SELECT u.o AS o
244
+ FROM u ORDER BY u.o IS NULL, o`). Yes, but on Spark and Databricks:
245
+ there `SparkSQLSerializer._read_by_its_own_select_alias` writes the
246
+ read as the alias, `ORDER BY o IS NULL, o`."""
247
+ return True
248
+
249
+ def over_a_sorted_slice(self, source: str) -> str:
250
+ """The SELECT that must stand between a sorted slice (a SELECT
251
+ with an ORDER BY and a LIMIT, an OFFSET or both) and whatever
252
+ reads it, or '' where the database needs none: none, but on Spark
253
+ and Databricks (docs/architecture/rules.md, "A CTE that holds a
254
+ slice"). `source` is what the SELECT reads the slice from, a CTE's
255
+ name or a subquery with its alias. The barrier's text is written
256
+ here and nowhere else."""
257
+ return ""
258
+
259
+ def refuses_unmapped(self, name: str) -> bool:
260
+ """Whether ``name`` is one of the plan's own functions (some dialect maps it) that this dialect
261
+ does not map: written out raw, the database would fail on an unknown name (ORA-00936), so the
262
+ serializer refuses it by name (`render_function`). Checked for Oracle only, where a table of
263
+ its functions is tested to be whole (`tests/plan/test_oracle_function_table.py`)."""
264
+ if self.name != 'oracle' or name in self.config.function_map:
265
+ return False
266
+ return name in _internal_function_names()
267
+
268
+ def maps_function(self, name: str) -> bool:
269
+ """Whether ``function_map`` decides how ``name`` renders."""
270
+ return name in self.config.function_map
271
+
272
+ def render_function(self, name: str, args: list) -> str:
273
+ """Render function ``name`` over already-rendered ``args`` from
274
+ ``function_map``: a rename, a positional template, or a refusal."""
275
+ entry = self.config.function_map.get(name, self._UNMAPPED)
276
+ if entry is None:
277
+ from prograph.utils.exceptions import SQLGenerationError
278
+ raise SQLGenerationError(
279
+ f"{name}() has no equivalent in {self.name}",
280
+ reason=f"unsupported_function:{name}",
281
+ )
282
+ if entry is self._UNMAPPED:
283
+ return f"{name}({', '.join(args)})"
284
+ # A bare name is a rename; anything else (`{0}` placeholders, or a
285
+ # constant such as `[]`) is a template.
286
+ if '{' in entry or not entry.replace('.', '_').isidentifier():
287
+ # Some templates leave out an argument on purpose (what one dialect needs, another does
288
+ # not); one that reads every argument, such as a list of sort keys, must not lose one
289
+ # silently (a sixth key was dropped), so more than it reads is refused.
290
+ import string
291
+ read = [int(f) for _, f, _, _ in string.Formatter().parse(entry) if f is not None and f.isdigit()]
292
+ if name in EVERY_ARGUMENT and read and len(args) > max(read) + 1:
293
+ from prograph.utils.exceptions import SQLGenerationError
294
+ raise SQLGenerationError(
295
+ f"{name}() in {self.name} reads {max(read) + 1} arguments, given {len(args)}",
296
+ reason=f"template_arity:{name}")
297
+ return entry.format(*args)
298
+ return f"{entry}({', '.join(args)})"
299
+
300
+ def __init__(self, config: DialectConfig):
301
+ self.config = config
302
+
303
+ def render_string(self, value: str) -> str:
304
+ """A string literal, escaped the way this dialect reads it. A NUL character has no literal on every database
305
+ (PostgreSQL text cannot hold one) and raises NulCharacterError (a ValueError)."""
306
+ # A subclass of str could change replace() and leave a quote unescaped: work on the exact str.
307
+ value = str.__str__(value)
308
+ if '\x00' in value:
309
+ from prograph.utils.exceptions import NulCharacterError
310
+ raise NulCharacterError()
311
+ style = self.config.string_escape
312
+ if style == 'postgres' and '\\' in value:
313
+ # A backslash is read as an escape where a caller's connection has standard_conforming_strings
314
+ # off; an escape string (E'') reads the same either way. Without one, '' quoting alone is exact.
315
+ return "E'" + value.replace('\\', '\\\\').replace("'", "''") + "'"
316
+ if style == 'backslash':
317
+ # Spark substitutes ${...} in a statement's text before it parses it, inside string literals too
318
+ # (spark.sql.variable.substitute): `$\{` is read back as `${` and is never substituted.
319
+ return "'" + value.replace('\\', '\\\\').replace("'", "\\'").replace('${', '$\\{') + "'"
320
+ if style == 'mysql':
321
+ return "'" + value.replace('\\', '\\\\').replace("'", "''") + "'"
322
+ return "'" + value.replace("'", "''") + "'"
323
+
324
+ @property
325
+ def name(self) -> str:
326
+ return self.config.name
327
+
328
+ # ─── Identifier quoting ──────────────────────────────────────
329
+
330
+ @home("sql-identifier-quoting", "SQL identifiers",
331
+ terms=("identifier", "quote a name", "backtick", "alias", "safe_alias", "internal_name",
332
+ "table alias", "scope", "scope_name", "CTE name", "subquery alias", "alias namespace",
333
+ "_v_", "name collision", "name clash"))
334
+ def quote(self, name: str) -> str:
335
+ """Quote an identifier (column or alias name).
336
+
337
+ The closing quote inside a name is doubled, the standard escape in
338
+ every supported dialect, as render_string does for literals: a
339
+ property named ``a"b`` ended the identifier early.
340
+ """
341
+ q = self.config.quote_char
342
+ if q == '[':
343
+ return '[' + name.replace(']', ']]') + ']'
344
+ return q + name.replace(q, q + q) + q
345
+
346
+ unwritable_name_characters: tuple = ('\x00',)
347
+ """Characters a quoted name cannot hold here."""
348
+
349
+ def type_name(self, name: str) -> str:
350
+ """The dialect's name for a type in a CAST (``LONG``, ``STRING``,
351
+ ``DOUBLE``): ``type_map``'s entry, or the name as written where the
352
+ dialect spells it that way (Spark's ``LONG``)."""
353
+ return self.config.type_map.get(name, name)
354
+
355
+ @home("regular-identifier-fold", "An undelimited SQL identifier's name in the database",
356
+ terms=("delimited identifier", "regular identifier", "case folding", "rr:column", "rr:tableName",
357
+ "mixed case", "identifier_fold", "exported column name", "r2rml_identifier"))
358
+ def fold_identifier(self, name: str) -> str:
359
+ """The name an undelimited (regular) SQL identifier denotes here (SQL:2008 5.2, which
360
+ R2RML 5 cites): folded as the database folds it (`identifier_fold`: PostgreSQL to lower
361
+ case, Oracle to upper), written quoted after; a delimited one is its text exactly. An
362
+ R2RML mapping's `rr:column "ID"` reads column id on PostgreSQL, `rr:column "\\"ID\\""`
363
+ column ID. YAML names are exact."""
364
+ fold = self.config.identifier_fold
365
+ # ASCII letters only: PostgreSQL folds A-Z alone in a multibyte encoding (NOMÉ is stored as
366
+ # nomÉ; Python's lower() made it nomé), and an upper-case fold must not turn ß into SS.
367
+ if fold == 'lower':
368
+ return ''.join(chr(ord(c) + 32) if 'A' <= c <= 'Z' else c for c in name)
369
+ if fold == 'upper':
370
+ return ''.join(chr(ord(c) - 32) if 'a' <= c <= 'z' else c for c in name)
371
+ return name
372
+
373
+ @staticmethod
374
+ def r2rml_identifier(name: str) -> str:
375
+ """A column name as an R2RML mapping writes it (rr:column, a template's braces, a join
376
+ condition; R2RML 5): bare where every database reads the bare name as that column (a plain
377
+ lower-case name no dialect reserves: a database that folds reads `firstName` as
378
+ `firstname`, whichever one wrote the mapping), delimited, `"firstName"`, otherwise.
379
+ `sql_identifier` reads either back as the exact name."""
380
+ import re
381
+ if re.fullmatch(r"[a-z_][a-z0-9_]*", name) and name not in _WORDS_AN_ALIAS_AVOIDS:
382
+ return name
383
+ return '"' + name.replace('"', '""') + '"'
384
+
385
+ def quote_exact(self, name: str) -> str:
386
+ """Quote a name the database must keep exactly as written, case
387
+ included: a result column's name. ``quote`` is the same everywhere
388
+ but Oracle, whose ``quote`` leaves a simple name unquoted so it
389
+ matches the upper-case catalog."""
390
+ return Dialect.quote(self, name)
391
+
392
+ def quote_qualified(self, name: str) -> str:
393
+ """A dotted name (``catalog.schema.table``) with each part quoted:
394
+ quoting the whole made one identifier with dots in it."""
395
+ return '.'.join(self.quote(part) for part in str(name).split('.'))
396
+
397
+ @home("source-table-names", "A source table's name in a query",
398
+ terms=("table name", "reserved word", "qualified name", "quote_qualified"))
399
+ def table_name(self, name: str) -> str:
400
+ """A source table's name as a query reads it: each part of a dotted
401
+ name (``catalog.schema.table``) quoted where the database would read
402
+ it as a reserved word (``words_a_table_name_quotes``), and as written
403
+ otherwise, so a name's case means what it meant before (a quoted name
404
+ is case-sensitive on PostgreSQL). A part the mapping already quotes,
405
+ and a dot inside one, are kept as they are. A part
406
+ that is no plain name, one that starts with a digit or holds a
407
+ character outside letters, digits and `_` (`2nd`, `my-table`), is
408
+ quoted on every dialect: DuckDB read it as a syntax error."""
409
+ import re
410
+ if str(name).startswith('('):
411
+ # A derived table, a mapping's source query
412
+ # (`TableMapping.qualified_source_table`, R2RML 5.2): written
413
+ # as it is.
414
+ return str(name)
415
+ words = self.config.words_a_table_name_quotes or ()
416
+ parts, part, closing = [], '', None
417
+ for ch in str(name):
418
+ if closing:
419
+ part += ch
420
+ if ch == closing:
421
+ closing = None
422
+ elif ch in '"`[':
423
+ part += ch
424
+ closing = ']' if ch == '[' else ch
425
+ elif ch == '.':
426
+ parts.append(part)
427
+ part = ''
428
+ else:
429
+ part += ch
430
+ parts.append(part)
431
+
432
+ # A part's surrounding spaces are not its name: `main. people` read
433
+ # as `main.people` before the quoting, and quoted they were part of
434
+ # it.
435
+ parts = [p if p[:1] in '"`[' else p.strip() for p in parts]
436
+ # A part delimited the SQL standard's way, "Name" (an R2RML mapping's rr:tableName as
437
+ # written), is that exact name in this dialect's quotes.
438
+ if self.config.quote_char != '"':
439
+ from prograph.r2rml.parser import sql_identifier
440
+ parts = [self.quote(sql_identifier(p)) if p[:1] == '"' and p[-1:] == '"' and len(p) > 1 else p
441
+ for p in parts]
442
+
443
+ def bare(p):
444
+ return p[:1] in '"`[' or (re.fullmatch(r'[^\W\d]\w*', p) is not None
445
+ and p.lower() not in words)
446
+ return '.'.join(p if bare(p) else self.quote(p) for p in parts)
447
+
448
+ def create_or_replace_view(self) -> str:
449
+ """The statement head that creates a view or replaces it."""
450
+ return 'CREATE OR REPLACE VIEW'
451
+
452
+ # ─── Literals ────────────────────────────────────────────────
453
+
454
+ def true_literal(self) -> str:
455
+ """The SQL literal for boolean TRUE."""
456
+ return 'TRUE' if self.config.has_boolean_type else '1'
457
+
458
+ def false_literal(self) -> str:
459
+ """The SQL literal for boolean FALSE."""
460
+ return 'FALSE' if self.config.has_boolean_type else '0'
461
+
462
+ @home("sql-string-literals", "SQL string literals",
463
+ terms=("string literal", "escape", "quote", "boolean literal", "NaN"))
464
+ def literal(self, value) -> str:
465
+ """A Python value as this dialect's SQL literal: the one way a
466
+ value enters SQL text (docs/architecture/rules.md). None is NULL; a
467
+ date or datetime a cast of its ISO text; a list an array where the
468
+ database has one; anything else its string. A NaN or an infinity
469
+ is the dialect's `non_finite_double`, and raises ValueError where
470
+ it has none, as does a list where there is no array type."""
471
+ import datetime
472
+ import math
473
+ if value is None:
474
+ return 'NULL'
475
+ if isinstance(value, bool):
476
+ return self.true_literal() if value else self.false_literal()
477
+ if isinstance(value, int):
478
+ return str(int(value))
479
+ if isinstance(value, float):
480
+ if not math.isfinite(value):
481
+ form = self.config.non_finite_double
482
+ if form is None:
483
+ raise ValueError(f"{value!r} has no SQL literal in {self.name}")
484
+ from prograph.schema.types import is_missing
485
+ text = 'NaN' if is_missing(value) else ('Infinity' if value > 0 else '-Infinity')
486
+ return form.format(text)
487
+ return repr(float(value))
488
+ if isinstance(value, datetime.datetime) or hasattr(value, 'to_pydatetime'):
489
+ text = value.isoformat(sep=' ') if isinstance(value, datetime.datetime) \
490
+ else str(value)
491
+ zoned = getattr(value, 'tzinfo', None) is not None
492
+ kind = (self.config.zoned_timestamp_type if zoned else None) or self.config.timestamp_type
493
+ return f"CAST({self.render_string(text)} AS {kind})"
494
+ if isinstance(value, datetime.date):
495
+ return f"CAST({self.render_string(value.isoformat())} AS DATE)"
496
+ if isinstance(value, (list, tuple)):
497
+ if self.config.array_literal is None:
498
+ raise ValueError(f"{self.name} has no array literal")
499
+ return self.config.array_literal.format(', '.join(self.literal(v) for v in value))
500
+ return self.render_string(str(value))
501
+
502
+ # ─── LIMIT / pagination ─────────────────────────────────────
503
+
504
+ # ─── Parameters ──────────────────────────────────────────────
505
+
506
+ def param_placeholder(self, position: int = 1) -> str:
507
+ """Return the parameter placeholder for this dialect.
508
+
509
+ Args:
510
+ position: 1-based position (used by Oracle's ``:1``, ``:2`` and
511
+ PostgreSQL's ``$1``, ``$2`` styles).
512
+ """
513
+ style = self.config.param_style
514
+ if style in (':1', '$1'):
515
+ return f"{style[0]}{position}"
516
+ return style
517
+
518
+ # ─── Convenience ─────────────────────────────────────────────
519
+
520
+ def __repr__(self) -> str:
521
+ return f"Dialect({self.config.name!r})"
522
+
523
+
524
+ class OracleDialect(Dialect):
525
+ """Oracle-specific overrides.
526
+
527
+ Oracle stores unquoted identifiers as uppercase. When we generate
528
+ SQL with ``"id"`` (quoted lowercase), it must match the DDL. The
529
+ safe approach: don't quote simple column names unless they collide
530
+ with reserved words. For the OWL SQL generators, we use unquoted
531
+ names so Oracle's default uppercase matching works.
532
+ """
533
+
534
+ unwritable_name_characters = ('\x00', '"') # a quoted Oracle name holds no `"`
535
+
536
+ def quote(self, name: str) -> str:
537
+ """Oracle: leave simple names unquoted when safe.
538
+
539
+ Oracle stores unquoted identifiers as uppercase. Names that
540
+ start with a letter and contain only alphanumerics/underscores
541
+ (and aren't reserved) can be left unquoted, Oracle uppercases
542
+ them to match the catalog.
543
+
544
+ Names starting with underscore (like ``_inferred``) must be
545
+ quoted because Oracle doesn't allow unquoted identifiers
546
+ starting with underscore.
547
+ """
548
+ if (
549
+ name[0:1].isalpha()
550
+ and name.isidentifier()
551
+ and not _is_oracle_reserved(name)
552
+ ):
553
+ return name
554
+ # Quote and preserve case
555
+ return '"' + name.replace('"', '""') + '"'
556
+
557
+ def returned_name(self, name: str) -> str:
558
+ """The name Oracle gives a result column the SQL names ``name``: in
559
+ upper case where ``quote`` leaves it unquoted (Oracle folds it), as
560
+ written where ``quote`` quotes it."""
561
+ return name.upper() if self.quote(name) == name else name
562
+
563
+ class SQLServerDialect(Dialect):
564
+
565
+ def create_or_replace_view(self) -> str:
566
+ return 'CREATE OR ALTER VIEW'
567
+
568
+ """SQL Server-specific overrides."""
569
+
570
+ class SparkDialect(Dialect):
571
+ """Spark SQL, and what Databricks SQL shares with it."""
572
+
573
+ def sorts_by_a_column_its_select_list_names_by_itself(self) -> bool:
574
+ """No: Spark 4.1's optimizer drops the alias `u.o AS o` as redundant
575
+ (RemoveRedundantAliases) and leaves a sort that reads `u.o` beside
576
+ it unresolved where `u` is a UNION ALL (an unlabelled node read
577
+ over two tables, `MATCH (x) RETURN x.name AS name ORDER BY
578
+ toLower(x.name)`; a SPARQL UNION whose branches read one CTE
579
+ through a join), and fails the statement
580
+ (PLAN_VALIDATION_FAILED_RULE_IN_BATCH). A key
581
+ that reads the alias plans, and is the same value: Spark reads a
582
+ select alias inside an ORDER BY expression too."""
583
+ return False
584
+
585
+ def over_a_sorted_slice(self, source: str) -> str:
586
+ """`SELECT * FROM <slice> DISTRIBUTE BY 1`. Spark's
587
+ optimizer (PushProjectionThroughLimitAndOffset) pushes a
588
+ projection that holds a subquery expression under the LIMIT or
589
+ OFFSET of the slice it reads; the subquery becomes a join there,
590
+ the join does not keep the sort, and the slice keeps other rows:
591
+ `WITH p ORDER BY p.age DESC LIMIT 3 RETURN p.name, EXISTS {
592
+ (p)-[:KNOWS]->(:P) }` answered for rows that are not the oldest
593
+ three, on open-source Spark and on a Databricks warehouse, where
594
+ no configuration can be set (reported as
595
+ https://github.com/apache/spark/issues/59438: the LIMIT half
596
+ since Spark 3.4.0, the OFFSET half since 4.1.0). An exchange above
597
+ the slice stops the push. The barrier must stand ABOVE the slice,
598
+ between it and its reader: a hint inside the sliced SELECT does
599
+ not protect it, and neither does a second CTE that only selects
600
+ from it, which Spark inlines. DISTRIBUTE BY is an operator of the
601
+ language, where a hint may be dropped unsaid (and is, with
602
+ spark.sql.optimizer.disableHints). The key is the CONSTANT,
603
+ always: Spark keeps it as a repartition into one partition above
604
+ the limit (Spark 4.1.3), and a warehouse given a
605
+ column of the slice as the key planned the hash exchange BELOW
606
+ the slice's aggregate and LIMIT (117 M rows shuffled where the
607
+ slice keeps 50: 42.6 s for main's 16.3, and 12.3 s with the
608
+ constant). What it costs: the
609
+ slice's rows go through one partition, the whole input past the
610
+ SKIP where the slice is a SKIP with no LIMIT. Spark's own fault
611
+ with this text: over a CACHED relation derived from range(), with
612
+ the repartition in a CTE over another CTE, the statement fails
613
+ inside Spark (NoSuchElementException; reproduced
614
+ with no ProGraph; the hint form fails the same)."""
615
+ return f"SELECT * FROM {source} DISTRIBUTE BY 1"
616
+
617
+
618
+ class DatabricksSQLDialect(SparkDialect):
619
+ """Databricks SQL-specific overrides."""
620
+
621
+ class DuckDBDialect(Dialect):
622
+ """DuckDB-specific overrides."""
623
+
624
+ def before_a_sliced_cte(self) -> str:
625
+ """`MATERIALIZED`. DuckDB (1.5.2) drops a LEFT JOIN whose right
626
+ side nothing reads when a DISTINCT stands over it, and does so
627
+ through a LIMIT or OFFSET between them, which counts the rows the
628
+ join gave: `SELECT DISTINCT g FROM (SELECT p.id, p.g FROM p LEFT
629
+ JOIN k ON p.id = k.a ORDER BY p.g DESC, p.id LIMIT 5)` returns a
630
+ g the five rows do not hold. That is what an OPTIONAL MATCH whose
631
+ node nothing reads, a WITH with a SKIP or LIMIT and a DISTINCT
632
+ after it write, and the answer was wrong with a right text. A materialized CTE's rows are fixed before
633
+ anything reads them, which is what a slice means."""
634
+ return "MATERIALIZED "
635
+
636
+ def binds_a_union_cte_as_recursive(self) -> bool:
637
+ """Yes. DuckDB (1.5.2) binds a CTE of a WITH RECURSIVE whose SELECT
638
+ is a UNION of two SELECTs as a recursive CTE even when it does not
639
+ read itself: the result takes the first branch's types and casts
640
+ the second's to them. A first branch's NULL is an INTEGER there, so
641
+ `WITH RECURSIVE r AS (...), a AS (SELECT NULL AS n UNION ALL SELECT
642
+ 'x') SELECT * FROM a` fails converting 'x' to INT32, and an integer
643
+ first branch rounds the second's 2.5 to 3. A SPARQL UNION whose
644
+ first branch leaves a variable unbound, beside a property path
645
+ walk, was that error. The same UNION read from a
646
+ subquery keeps the types both branches share."""
647
+ return True
648
+
649
+ class MySQLDialect(Dialect):
650
+ """MySQL-specific overrides."""
651
+
652
+ def _is_oracle_reserved(name: str) -> bool:
653
+ """Quick check for Oracle reserved words that need quoting."""
654
+ return name.upper() in _ORACLE_RESERVED
655
+
656
+
657
+ _ORACLE_RESERVED = frozenset({
658
+ 'SELECT', 'FROM', 'WHERE', 'INSERT', 'UPDATE', 'DELETE', 'TABLE',
659
+ 'INDEX', 'CREATE', 'DROP', 'ALTER', 'INTO', 'VALUES', 'SET',
660
+ 'ORDER', 'BY', 'GROUP', 'HAVING', 'JOIN', 'ON', 'AS', 'AND',
661
+ 'OR', 'NOT', 'NULL', 'IN', 'EXISTS', 'BETWEEN', 'LIKE', 'IS',
662
+ 'DISTINCT', 'ALL', 'ANY', 'UNION', 'INTERSECT', 'MINUS',
663
+ 'GRANT', 'REVOKE', 'WITH', 'CHECK', 'OPTION', 'VIEW',
664
+ 'COMMENT', 'LEVEL', 'CONNECT', 'START', 'PRIOR',
665
+ # The rest of Oracle 21c's reserved words (V$RESERVED_WORDS, reserved = 'Y').
666
+ 'ASC', 'CHAR', 'CLUSTER', 'COMPRESS', 'DATE', 'DECIMAL', 'DEFAULT',
667
+ 'DESC', 'ELSE', 'EXCEPT', 'EXCLUSIVE', 'FLOAT', 'FOR', 'IDENTIFIED',
668
+ 'INTEGER', 'LOCK', 'LONG', 'MODE', 'NOCOMPRESS', 'NOWAIT', 'NUMBER',
669
+ 'OF', 'PCTFREE', 'PUBLIC', 'RAW', 'RENAME', 'RESOURCE', 'SHARE',
670
+ 'SIZE', 'SMALLINT', 'SYNONYM', 'THEN', 'TO', 'TRIGGER', 'UNIQUE',
671
+ 'VARCHAR',
672
+ })
673
+
674
+
675
+ # =========================================================================
676
+ # Pre-built dialect configs
677
+ # =========================================================================
678
+
679
+ # The error an integer divided by zero, or its remainder, raises: one name
680
+ # on every engine that can raise from an expression (630
681
+ # and 632). It is Neo4j's. A database whose own division raises (PostgreSQL,
682
+ # SQL Server, Oracle) says it in its own words.
683
+ ZERO_DIVISOR = '/ by zero'
684
+
685
+
686
+ def _zero_divisor(raise_function: str, quotient: str) -> str:
687
+ """`quotient` of {0} and {1}, raising ZERO_DIVISOR when {1} is zero.
688
+ A null dividend is null whatever the divisor: an operator with a null
689
+ operand is null in Cypher, and an OPTIONAL MATCH's unbound row divided
690
+ by zero has divided nothing."""
691
+ return (f"(CASE WHEN ({{1}}) = 0 AND ({{0}}) IS NOT NULL "
692
+ f"THEN {raise_function}('{ZERO_DIVISOR}') ELSE {quotient} END)")
693
+
694
+
695
+ def _once(templates: dict, bind: str) -> dict:
696
+ """The _ONCE form of each division template: {0} and {1} read once
697
+ each, as the fields of a struct a lambda is applied to, so that an
698
+ operand that is not deterministic (rand()) has one value in the test
699
+ for zero and in the division. `bind` holds the struct and the lambda,
700
+ with BODY where the template goes."""
701
+ return {name + '_ONCE': bind.replace('BODY', text.replace('({0})', '_p.n').replace('({1})', '_p.d'))
702
+ for name, text in templates.items() if '{1}' in text}
703
+
704
+
705
+ # Two integers whose divisor is written in the query as a number other than
706
+ # zero (`t.n / 2`, `t.n % 10`): INT_QUOTIENT and INT_REMAINDER, with no zero
707
+ # to test for. INT_QUOTIENT is each dialect's own integer division.
708
+ _WRITTEN_DIVISOR = {'INT_REMAINDER': "(({0}) % ({1}))"}
709
+
710
+ # Spark and Databricks. A float over zero: pow(0, -1) is Infinity and
711
+ # pow(-0.0, -1) is -Infinity, so the product is IEEE's quotient (Infinity,
712
+ # -Infinity, NaN, or NULL for a NULL dividend) whatever ANSI mode says; `/`
713
+ # raises there with it on and answers NULL with it off. The remainder by
714
+ # zero is NaN.
715
+ _SPARK_DIVISION = {
716
+ # `div` divides two integers as integers, truncated toward zero;
717
+ # `CAST(a / b AS BIGINT)` divided doubles, which hold 53 bits:
718
+ # 9223372036854775807 / 2 came back ...904, and / -1 the smallest
719
+ # integer.
720
+ 'INT_DIV': _zero_divisor('raise_error', '({0}) div ({1})'),
721
+ 'INT_MOD': _zero_divisor('raise_error', '({0}) % ({1})'),
722
+ **_WRITTEN_DIVISOR,
723
+ 'INT_QUOTIENT': "(({0}) div ({1}))",
724
+ 'FLOAT_DIV': "(CASE WHEN ({1}) = 0 THEN ({0}) * pow(({1}), -1) ELSE ({0}) / ({1}) END)",
725
+ 'FLOAT_MOD': "(CASE WHEN ({1}) = 0 THEN ({0}) * double('NaN') ELSE ({0}) % ({1}) END)",
726
+ }
727
+ _SPARK_DIVISION.update(_once(
728
+ {k: v for k, v in _SPARK_DIVISION.items() if k in ('INT_DIV', 'INT_MOD', 'FLOAT_DIV', 'FLOAT_MOD')},
729
+ "element_at(transform(array(named_struct('n', {0}, 'd', {1})), _p -> BODY), 1)"))
730
+ # Spark and Databricks read a regex with Java's syntax. A Databricks SQL
731
+ # warehouse (DBSQL 2026.39) lets `.` match a line break when the string
732
+ # is a column's value, though not a literal's: 'x\nb' RLIKE 'x.b' held
733
+ # over a column. Every regex these dialects write therefore starts
734
+ # `(?-s)`, the default Java and XPath (and Cypher's Java regex) share; a
735
+ # later `(?s)`, from the s flag or written in the pattern, still wins.
736
+ # Local Spark already reads `.` so; the text is the
737
+ # same for both.
738
+ _JAVA_REGEX = {
739
+ # SPARQL REGEX (XPath fn:matches) searches.
740
+ 'REGEX_SEARCH': "{0} RLIKE CONCAT('(?-s)', {1})",
741
+ # =~ matches the whole string. Java's `$` also
742
+ # matches before a final line break, so 'a\n' =~ 'a' held; `\z` is
743
+ # the end of the text.
744
+ 'REGEX_FULL': "({0} RLIKE CONCAT('(?-s)^(?:', {1}, ')\\\\z'))",
745
+ 'REGEX_REPLACE': "regexp_replace({0}, CONCAT('(?-s)', {1}), {2})",
746
+ 'REGEX_REPLACE_I': "regexp_replace({0}, CONCAT('(?-s)(?i)', {1}), {2})",
747
+ }
748
+ # The databases whose `/` and `%` are already IEEE for floats, or raise in
749
+ # their own words.
750
+ _PLAIN_DIVISION = {
751
+ 'INT_MOD': "(({0}) % ({1}))",
752
+ 'INT_REMAINDER': "(({0}) % ({1}))",
753
+ 'FLOAT_DIV': "(({0}) / ({1}))",
754
+ 'FLOAT_MOD': "(({0}) % ({1}))",
755
+ # Each operand is read once here already.
756
+ 'INT_MOD_ONCE': "(({0}) % ({1}))",
757
+ 'FLOAT_DIV_ONCE': "(({0}) / ({1}))",
758
+ 'FLOAT_MOD_ONCE': "(({0}) % ({1}))",
759
+ }
760
+
761
+ # op:numeric-divide of two integers or decimals (SPARQL 1.1 17.3): an exact
762
+ # decimal, DECIMAL(38, 18), its rule `prograph.schema.xsd.quotient`: the
763
+ # exact quotient rounded half away from zero at the 18th digit after the
764
+ # point. {0} and {1} are exact: an integer operand
765
+ # is written DECIMAL(38, 0) first (`ExprTranslator.exact_quotient`). A zero
766
+ # divisor is an error, the result unbound. A quotient past 20 digits before
767
+ # the point is an error. docs/architecture/rules.md, "SPARQL numeric type
768
+ # promotion".
769
+ _QUOTIENT = 'DECIMAL(38,18)'
770
+ _INTEGER = 'DECIMAL(38,0)'
771
+
772
+
773
+ def _double_text(string_type: str, extract, candidates=(), double_type: str = "DOUBLE",
774
+ guarded: bool = False, cast: str = "CAST") -> str:
775
+ """A DOUBLE's canonical text (XML Schema 1.1, sparql-choices.md D3;
776
+ `xsd.canonical_lexical` writes it in Python): the shortest digits that
777
+ read back as the value, one before the point, at least one after, then
778
+ `E` and the exponent (`1.0E20`, `1.05E1`, `-2.5E-3`, `0.0E0`,
779
+ `-0.0E0`), and `INF`, `-INF`, `NaN`. Rewritten from a text of the
780
+ value (`1e+20` on DuckDB and PostgreSQL, Java's `1.0E20` on Spark): its
781
+ digits, leading zeros dropped, the exponent moved to the first digit.
782
+ The database's own text is shortest on DuckDB; PostgreSQL 16 writes
783
+ 1e23 as 9.999999999999999e+22 and Java 17 writes 4.9E-324 and some
784
+ 18-digit forms, so there `candidates` (templates of the value correctly
785
+ rounded to 15, 16, 17 digits) come first, the first that reads back as
786
+ the value taken; `guarded`, within the normal range only (on PostgreSQL
787
+ a candidate rounded past the largest double, or below the smallest
788
+ normal one, is an error). `extract(s, pattern)` is the
789
+ dialect's first group of a regular expression's match ('' or NULL where
790
+ none)."""
791
+ # The value as a DOUBLE first: a FLOAT (REAL) column's own text is the float's shortest form
792
+ # (0.1 for 0.10000000149011612), not the double's (M2 review).
793
+ native = f"lower(CAST(CAST({{0}} AS {double_type}) AS {string_type}))"
794
+ t = native
795
+ if candidates:
796
+ # `cast` a TRY_CAST where the dialect has one: a candidate of NULL is the text 'null'
797
+ # (format_string), which a CAST raises on under ANSI mode wherever the optimizer evaluates it,
798
+ # a CASE around it being no guarantee on Spark.
799
+ tried = " ".join(f"WHEN {cast}(lower(trim({c})) AS {double_type}) = {{0}} THEN lower(trim({c}))"
800
+ for c in candidates)
801
+ t = f"(CASE {tried} ELSE {native} END)"
802
+ if guarded:
803
+ t = f"(CASE WHEN ABS({{0}}) < 1e308 AND ABS({{0}}) > 1e-307 THEN {t} ELSE {native} END)"
804
+ whole = extract(t, "^-?([0-9]*)")
805
+ every = f"({whole} || {extract(t, '^-?[0-9]*[.]?([0-9]*)')})"
806
+ power = f"CAST(COALESCE(NULLIF({extract(t, 'e([-+]?[0-9]+)$')}, ''), '0') AS INT)"
807
+ digits = f"TRIM(TRAILING '0' FROM TRIM(LEADING '0' FROM {every}))"
808
+ zeros = f"(length({every}) - length(TRIM(LEADING '0' FROM {every})))"
809
+ exponent = f"({power} + length({whole}) - 1 - {zeros})"
810
+ sign = f"(CASE WHEN {native} LIKE '-%' THEN '-' ELSE '' END)"
811
+ finite = (f"{sign} || substr({digits}, 1, 1) || '.' || COALESCE(NULLIF(substr({digits}, 2), ''), '0') "
812
+ f"|| 'E' || CAST({exponent} AS {string_type})")
813
+ # A NaN's text: 'nan', or '-nan' where its sign bit is set (DuckDB prints 0.0/0.0 so): one NaN in XSD.
814
+ return (f"(CASE WHEN {native} IN ('nan', '-nan') THEN 'NaN' WHEN {native} IN ('inf', 'infinity') THEN 'INF' "
815
+ f"WHEN {native} IN ('-inf', '-infinity') THEN '-INF' WHEN {{0}} = 0 THEN {sign} || '0.0E0' "
816
+ f"ELSE {finite} END)")
817
+
818
+
819
+ def _long_division(dialect: str) -> str:
820
+ """The exact quotient as long division over whole numbers, for DuckDB,
821
+ Spark and Databricks, where `_native_quotient` does not fit the
822
+ operands' sizes (`ExprTranslator.exact_quotient`). Neither has a decimal division that is exact at
823
+ 18 digits for every operand: DuckDB's `/` of two DECIMALs is a DOUBLE,
824
+ and Spark's keeps as few as 6 digits after the point when the operands'
825
+ declared precision is wide (its decimalOperations rules, a session
826
+ setting the user owns). Spark also lowers the scale of any product or
827
+ sum of a DECIMAL whose declared precision passes 38 and whose scale is
828
+ not 0, so every step here is on DECIMAL(38, 0) whole numbers, which no
829
+ rule rounds.
830
+
831
+ Each operand's digits and scale are read from its text (a DECIMAL is
832
+ written in full, no exponent, on all three): |a| = A 10^-sa,
833
+ |b| = B 10^-sb. The quotient times 10^18 is A 10^e / B with
834
+ e = sb - sa + 18: the digits of A, followed by e zeros (or less its last
835
+ -e digits), are divided by B one digit at a time, the remainder R below
836
+ B. A digit is the count of k in 1..9 with k B <= 10 R + g, decided with
837
+ B = 10 Bh + Bl so that no step passes 38 digits whatever B is (R - k Bh
838
+ of 9 or more holds, below 0 fails, else 10 (R - k Bh) + g - k Bl is
839
+ small). The last digit rounds up when 2 R >= B, written (B - R) - R <= 0,
840
+ and, where digits of A were dropped, when (B - R) - R is 1 and the first
841
+ dropped digit is 5 or more. The whole number N is written with its point
842
+ 18 digits from the end and cast to DECIMAL(38, 18), which raises past 20
843
+ digits before the point."""
844
+ duck = dialect == 'duckdb'
845
+
846
+ def bind(value: str, name: str, body: str) -> str:
847
+ # `value` computed once, read as `name` in `body`.
848
+ if duck:
849
+ return f"list_transform([{value}], lambda {name}: {body})[1]"
850
+ return f"element_at(transform(array({value}), {name} -> {body}), 1)"
851
+
852
+ def struct(**fields: str) -> str:
853
+ if duck:
854
+ return "struct_pack(" + ", ".join(f"{k} := {v}" for k, v in fields.items()) + ")"
855
+ return "named_struct(" + ", ".join(f"'{k}', {v}" for k, v in fields.items()) + ")"
856
+
857
+ text = 'VARCHAR' if duck else 'STRING'
858
+ idiv = '//' if duck else 'div'
859
+
860
+ def digits_of(x: str) -> str:
861
+ if not duck:
862
+ # Spark's ABS (and unary minus) of a DECIMAL of more than 34
863
+ # digits rounds it to 34 and fails: the digits are read from
864
+ # the text, its sign dropped.
865
+ return f"replace(replace(CAST({x} AS {text}), '-', ''), '.', '')"
866
+ return f"replace(CAST(ABS({x}) AS {text}), '.', '')"
867
+
868
+ def scale_of(x: str) -> str:
869
+ t = f"CAST(ABS({x}) AS {text})" if duck else f"CAST({x} AS {text})"
870
+ return f"(CASE WHEN instr({t}, '.') > 0 THEN length({t}) - instr({t}, '.') ELSE 0 END)"
871
+
872
+ def num(x: str) -> str:
873
+ return f"CAST({x} AS {_INTEGER})"
874
+
875
+ # _qv: the operands; _qw: their digits, B, Bh, Bl, the digit count n.
876
+ operands = struct(a="A_ARG", b="B_ARG")
877
+ sb_digits = digits_of("_qv.b")
878
+ known = struct(
879
+ sa=digits_of("_qv.a"),
880
+ bd=sb_digits,
881
+ n=f"(length({digits_of('_qv.a')}) + {scale_of('_qv.b')} - {scale_of('_qv.a')} + 18)",
882
+ neg="((_qv.a < 0) <> (_qv.b < 0))")
883
+ # B, Bh, Bl from the divisor's digits.
884
+ big = struct(
885
+ sa="_qw.sa", n="_qw.n", neg="_qw.neg",
886
+ b=num("_qw.bd"),
887
+ bh=num(f"COALESCE(NULLIF(substring(_qw.bd, 1, length(_qw.bd) - 1), ''), '0')"),
888
+ bl=f"CAST(substring(_qw.bd, length(_qw.bd), 1) AS INT)")
889
+ digit = "CAST(COALESCE(NULLIF(substring(_qk.sa, _qi, 1), ''), '0') AS INT)"
890
+
891
+ def fits(k: int) -> str:
892
+ t = f"(_qa.r - {k} * _qk.bh)"
893
+ return (f"(CASE WHEN {t} >= 9 THEN 1 WHEN {t} < 0 THEN 0 "
894
+ f"WHEN 10 * {t} + _qg - {k} * _qk.bl >= 0 THEN 1 ELSE 0 END)")
895
+
896
+ count = " + ".join(fits(k) for k in range(1, 10))
897
+ c = "(_qg - _qd * _qk.bl)"
898
+ c0 = f"((({c}) % 10 + 10) % 10)"
899
+ remainder = num(f"(_qa.r - _qd * _qk.bh + ({c} - {c0}) {idiv} 10) * 10 + {c0}")
900
+ zero = num("0")
901
+ if duck:
902
+ # DuckDB (1.5.2) reads a value a list_reduce lambda captures from
903
+ # the wrong row when the rows' lists differ in length, so each step
904
+ # carries what it reads: its digit, B, Bh and Bl.
905
+ local = lambda x: (x.replace("_qk.bh", "_qs.bh").replace("_qk.bl", "_qs.bl")
906
+ .replace("_qg", "_qs.g"))
907
+ count_ = local(count)
908
+ d_rem = local(remainder).replace("_qd", f"({count_})")
909
+ carried = dict(b="_qk.b", bh="_qk.bh", bl="_qk.bl")
910
+ steps = (f"list_transform(range(1, _qk.n + 1), lambda _qx: "
911
+ f"{struct(q=zero, r=zero, g=digit.replace('_qi', '_qx'), **carried)})")
912
+ init = struct(q=zero, r=zero, g="CAST(0 AS INT)", **carried)
913
+ step = struct(q=num(f"_qa.q * 10 + ({count_})"), r=d_rem, g="_qs.g", b="_qs.b",
914
+ bh="_qs.bh", bl="_qs.bl")
915
+ division = f"list_reduce({steps}, lambda _qa, _qs: {step}, {init})"
916
+ else:
917
+ steps = "slice(sequence(1, greatest(_qk.n, 1)), 1, greatest(_qk.n, 0))"
918
+ init = struct(q=zero, r=zero)
919
+ step = bind(digit, "_qg", bind(count, "_qd", struct(q=num("_qa.q * 10 + _qd"), r=remainder)))
920
+ division = f"aggregate({steps}, {init}, (_qa, _qi) -> {step})"
921
+ dropped = "CAST(substring(_qk.sa, _qk.n + 1, 1) AS INT)"
922
+ up = (f"(CASE WHEN (_qk.b - _qz.r) - _qz.r <= 0 THEN 1 WHEN (_qk.b - _qz.r) - _qz.r = 1 "
923
+ f"AND _qk.n >= 0 AND _qk.n < length(_qk.sa) AND {dropped} >= 5 THEN 1 ELSE 0 END)")
924
+ whole = num(f"_qz.q + {up}")
925
+ t = f"CAST(_qN AS {text})"
926
+ padded = f"(CASE WHEN length({t}) < 19 THEN lpad({t}, 19, '0') ELSE {t} END)"
927
+ written = (f"concat(CASE WHEN _qk.neg AND _qN <> 0 THEN '-' ELSE '' END, "
928
+ f"substring(_qP, 1, length(_qP) - 18), '.', substring(_qP, length(_qP) - 17, 18))")
929
+ final = bind(whole, "_qN", bind(padded, "_qP", f"CAST({written} AS {_QUOTIENT})"))
930
+ body = bind(operands, "_qv",
931
+ f"CASE WHEN _qv.a IS NULL OR _qv.b IS NULL OR _qv.b = 0 THEN NULL ELSE "
932
+ + bind(known, "_qw", bind(big, "_qk", bind(division, "_qz", final))) + " END")
933
+ if duck:
934
+ # DuckDB (1.5.2) binds `_qv.b` in a HAVING as a column b of the
935
+ # group; struct_extract reads the lambda's field.
936
+ import re
937
+ body = re.sub(r"\b(_q[A-Za-z]+)\.([a-z]+)\b", r"struct_extract(\1, '\2')", body)
938
+ return body.replace("{", "{{").replace("}", "}}").replace("A_ARG", "{0}").replace("B_ARG", "{1}")
939
+
940
+
941
+ def _native_quotient(dialect: str, small: bool = False, split: bool = False) -> str:
942
+ """The exact quotient in native whole-number division, for operands
943
+ whose sizes fit it (`ExprTranslator.exact_quotient` decides, from the
944
+ operands' declared sizes or row by row; `_long_division` is the rest).
945
+ {0} and {1} are the operands, {2} and {3} the DECIMAL(k + s, s) each is
946
+ read as, s the scale both are brought to, and {4} 10^s as a
947
+ DECIMAL(s + 1, 0). X = |a| 10^s and Y = |b| 10^s are whole numbers;
948
+ the quotient's whole part is I = X div Y, its remainder R = X mod Y,
949
+ and its 18 digits after the point, rounded half away from zero, are
950
+ F = (2 R 10^18 + Y) div 2Y, at most 10^18 (a carry into I). The
951
+ quotient is sign (I + F 10^-18), written as two DECIMAL(37, 18)s, whose
952
+ sum is a DECIMAL(38, 18) on DuckDB, Spark and Databricks (two
953
+ DECIMAL(38, 18)s sum to a DECIMAL(38, 17) on Spark). Every step is
954
+ exact, and each product's precision stays within 38 digits, so no
955
+ decimalOperations rule rounds it.
956
+
957
+ `small`: X below 10^(17 - s) and Y below 10^9, every step a BIGINT
958
+ (DuckDB's `//`, Spark's `div`), and F taken in two halves of 9 digits,
959
+ F1 = R 10^9 div Y and F2 = (2 (R 10^9 mod Y) 10^9 + Y) div 2Y, so that
960
+ nothing passes 2 10^18. A decimal of 18 digits or fewer is a 64-bit
961
+ number on both, and its arithmetic is a tenth the cost of a wider
962
+ one's. Otherwise X is below 10^(36 - s), Y below 10^19 and I below
963
+ 10^18: DuckDB's HUGEINTs, Spark's DECIMAL(38, 0)s and its `div`, a
964
+ BIGINT (I and F are below 2^63).
965
+
966
+ `split`: X written by `_split_whole` from the dividend as it stands,
967
+ for a dividend of up to 38 - s digits before the point
968
+ (`ExprTranslator.exact_quotient`), X below 10^38, and {2} left out
969
+ (`_without_dividend_size`)."""
970
+ duck = dialect == 'duckdb'
971
+ whole = 'BIGINT' if small else ('HUGEINT' if duck else _INTEGER)
972
+ idiv = '//' if duck else 'div'
973
+ x = (_split_whole("{0}", "{4}", whole, duck) if split
974
+ else f"CAST(CAST(ABS({{0}}) AS {{2}}) * {{4}} AS {whole})")
975
+ y = f"CAST(CAST(ABS({{1}}) AS {{3}}) * {{4}} AS {whole})"
976
+ if small:
977
+ e9 = f"CAST(1000000000 AS {whole})"
978
+ held = f"(({x} % {y}) * {e9})"
979
+ fraction = (f"(({held} {idiv} {y}) * {e9} + ((({held} % {y}) * {e9} * CAST(2 AS {whole}) + {y}) "
980
+ f"{idiv} ({y} * CAST(2 AS {whole}))))")
981
+ else:
982
+ two_e18 = f"CAST(2000000000000000000 AS {whole})"
983
+ fraction = f"((({x} % {y}) * {two_e18} + {y}) {idiv} ({y} * CAST(2 AS {whole})))"
984
+ sign = "(CASE WHEN ({0} < 0) <> ({1} < 0) THEN -1 ELSE 1 END)"
985
+ value = (f"(CAST({sign} * ({x} {idiv} {y}) AS DECIMAL(37,18)) + CAST(CAST({sign} * {fraction} "
986
+ f"AS DECIMAL(19,0)) * CAST(0.000000000000000001 AS DECIMAL(18,18)) AS DECIMAL(37,18)))")
987
+ text = f"CAST(CASE WHEN {{1}} = 0 THEN NULL ELSE {value} END AS {_QUOTIENT})"
988
+ return _without_dividend_size(text) if split else text
989
+
990
+
991
+ def _without_dividend_size(text: str) -> str:
992
+ """A _SPLIT form's template with {2}, the size the dividend is read as,
993
+ taken out and the later arguments moved down one: `_split_whole` reads
994
+ the dividend as it stands."""
995
+ import re
996
+ assert "{2}" not in text
997
+ return re.sub(r"\{(\d+)\}", lambda m: "{%d}" % (int(m.group(1)) - (int(m.group(1)) > 2)), text)
998
+
999
+
1000
+ def _split_whole(value: str, ten_s: str, whole: str, duck: bool) -> str:
1001
+ """|value| 10^s as the whole number `whole` (DuckDB's HUGEINT, Spark's
1002
+ DECIMAL(38, 0)), `ten_s` 10^s as a DECIMAL(s + 1, 0): its whole part
1003
+ times 10^s, plus its digits after the point times 10^s. Neither
1004
+ product's declared type passes 38 digits with a scale: a DECIMAL(38, 0)
1005
+ times a DECIMAL(s + 1, 0) is a DECIMAL(38, 0) on Spark and Databricks
1006
+ (allowPrecisionLoss on or off), and |value| % 1 is a DECIMAL(s + 1, s).
1007
+ The scale-s product of |value| with 10^s that `_native_quotient`
1008
+ writes otherwise is a DECIMAL(38, s) once its declared precision
1009
+ passes 38, which holds only 38 - s digits, so the dividend there is
1010
+ below 10^(36 - 2s); here it is below 10^(38 - s).
1011
+
1012
+ On Spark and Databricks nothing of scale s is negated: their ABS (and
1013
+ unary minus) of a DECIMAL of more than 34 digits rounds it to 34 and
1014
+ fails, and 0 - value of a DECIMAL(38, s) is a DECIMAL(39, s), which
1015
+ allowPrecisionLoss (the default) cuts to DECIMAL(38, max(s - 1, 6)),
1016
+ rounding the last digit away where s is 7 or more (fresh review of
1017
+ 712eff62). There value 10^s is taken with its sign, the whole part
1018
+ truncated toward zero (CEIL below zero, FLOOR above) and value % 1,
1019
+ which keeps the sign, a DECIMAL(s + 1, s); only the DECIMAL(38, 0) it
1020
+ gives is negated, 0 - X a DECIMAL(38, 0) still."""
1021
+ if duck:
1022
+ return (f"CAST(CAST(FLOOR(ABS({value})) AS {whole}) * {ten_s} "
1023
+ f"+ CAST((ABS({value}) % 1) * {ten_s} AS {whole}) AS {whole})")
1024
+ truncated = f"(CASE WHEN {value} < 0 THEN CEIL({value}) ELSE FLOOR({value}) END)"
1025
+ signed = (f"(CAST({truncated} AS {whole}) * {ten_s} "
1026
+ f"+ CAST(({value} % 1) * {ten_s} AS {whole}))")
1027
+ return f"CAST(CASE WHEN {signed} < 0 THEN 0 - {signed} ELSE {signed} END AS {whole})"
1028
+
1029
+
1030
+ def _native_quotient_by_integer(dialect: str, split: bool = False) -> str:
1031
+ """The exact quotient over a whole-number divisor (a COUNT, an integer)
1032
+ in native division, the divisor not scaled: an AVG's Sum / Count of a
1033
+ DECIMAL(15, 2) fits it whatever the count, where `_native_quotient`
1034
+ needs the count below 10^17. {0} the dividend, {1} the divisor (a
1035
+ whole number), {2} the DECIMAL(k + s, s) the dividend is read as,
1036
+ {3} 10^s as a DECIMAL(s + 1, 0), {4} 2 10^(18 - s), {5} 10^s, {6}
1037
+ 10^(18 - s). With X = |a| 10^s and C = |b|: Q = X div C, which is the
1038
+ quotient times 10^s and a BIGINT (`ExprTranslator.exact_quotient`
1039
+ proves it or tests it), R = X mod C, and the last 18 - s digits,
1040
+ rounded half away from zero, F = (2 R 10^(18 - s) + C) div 2C. The
1041
+ quotient is sign (Q div 10^s + ((Q mod 10^s) 10^(18 - s) + F) 10^-18).
1042
+ X is below 10^(36 - s) (or 10^38 for s = 0) and C below 10^19.
1043
+ `split`: X written by `_split_whole`, below 10^38, and {2} left out
1044
+ (`_without_dividend_size`)."""
1045
+ duck = dialect == 'duckdb'
1046
+ whole = 'HUGEINT' if duck else _INTEGER
1047
+ idiv = '//' if duck else 'div'
1048
+ x = (_split_whole("{0}", "{3}", whole, duck) if split
1049
+ else f"CAST(CAST(ABS({{0}}) AS {{2}}) * {{3}} AS {whole})")
1050
+ c = f"CAST(ABS({{1}}) AS {whole})"
1051
+ q = f"({x} {idiv} {c})"
1052
+ last = f"(((({x} % {c}) * CAST({{4}} AS {whole}) + {c}) {idiv} ({c} * CAST(2 AS {whole}))))"
1053
+ sign = "(CASE WHEN ({0} < 0) <> ({1} < 0) THEN -1 ELSE 1 END)"
1054
+ fraction = f"(({q} % CAST({{5}} AS BIGINT)) * CAST({{6}} AS BIGINT) + {last})"
1055
+ value = (f"(CAST({sign} * ({q} {idiv} CAST({{5}} AS BIGINT)) AS DECIMAL(37,18)) + CAST(CAST({sign} * "
1056
+ f"{fraction} AS DECIMAL(19,0)) * CAST(0.000000000000000001 AS DECIMAL(18,18)) AS DECIMAL(37,18)))")
1057
+ text = f"CAST(CASE WHEN {{1}} = 0 THEN NULL ELSE {value} END AS {_QUOTIENT})"
1058
+ return _without_dividend_size(text) if split else text
1059
+
1060
+
1061
+ # Row by row, whether an operand's magnitude is below a bound, or the
1062
+ # quotient's below 5 10^17 ({2}: 5e17, or less): read as doubles, whose
1063
+ # error is far inside the bounds' margin of a half
1064
+ # (`ExprTranslator.exact_quotient`).
1065
+ _QUOTIENT_FITS = {
1066
+ 'QUOTIENT_BOUND': "(ABS(CAST({0} AS DOUBLE)) < {1})",
1067
+ 'QUOTIENT_RATIO': "(ABS(CAST({0} AS DOUBLE)) < ABS(CAST({1} AS DOUBLE)) * {2})",
1068
+ }
1069
+
1070
+ _EXACT_QUOTIENT = {
1071
+ 'INTEGER_AS_DECIMAL': f'CAST({{0}} AS {_INTEGER})',
1072
+ 'EXACT_QUOTIENT': _long_division('spark'),
1073
+ 'NATIVE_QUOTIENT': _native_quotient('spark'),
1074
+ 'NATIVE_QUOTIENT_SMALL': _native_quotient('spark', small=True),
1075
+ 'NATIVE_QUOTIENT_BY_INTEGER': _native_quotient_by_integer('spark'),
1076
+ 'NATIVE_QUOTIENT_SPLIT': _native_quotient('spark', split=True),
1077
+ 'NATIVE_QUOTIENT_BY_INTEGER_SPLIT': _native_quotient_by_integer('spark', split=True),
1078
+ **_QUOTIENT_FITS,
1079
+ }
1080
+ _DUCKDB_EXACT_QUOTIENT = {
1081
+ 'INTEGER_AS_DECIMAL': _EXACT_QUOTIENT['INTEGER_AS_DECIMAL'],
1082
+ 'EXACT_QUOTIENT': _long_division('duckdb'),
1083
+ 'NATIVE_QUOTIENT': _native_quotient('duckdb'),
1084
+ 'NATIVE_QUOTIENT_SMALL': _native_quotient('duckdb', small=True),
1085
+ 'NATIVE_QUOTIENT_BY_INTEGER': _native_quotient_by_integer('duckdb'),
1086
+ 'NATIVE_QUOTIENT_SPLIT': _native_quotient('duckdb', split=True),
1087
+ 'NATIVE_QUOTIENT_BY_INTEGER_SPLIT': _native_quotient_by_integer('duckdb', split=True),
1088
+ **_QUOTIENT_FITS,
1089
+ }
1090
+ _POSTGRESQL_EXACT_QUOTIENT = {
1091
+ 'INTEGER_AS_DECIMAL': _EXACT_QUOTIENT['INTEGER_AS_DECIMAL'],
1092
+ # NUMERIC division picks its own result scale (16 significant digits:
1093
+ # 123456789012345678 / 7 lost its fraction); a numerator of scale 20
1094
+ # gives every one of the quotient's 18 places, rounded half away from
1095
+ # zero as D7's quotient is (checked on PostgreSQL 16, 2026-11-09).
1096
+ 'EXACT_QUOTIENT': (f"(CASE WHEN ({{1}}) = 0 THEN NULL ELSE "
1097
+ f"CAST(ROUND(CAST({{0}} AS NUMERIC(1000, 20)) / ({{1}}), 18) AS {_QUOTIENT}) END)"),
1098
+ }
1099
+
1100
+ SPARK_DIALECT = DialectConfig(
1101
+ float_text_is_javas='always',
1102
+ non_finite_double="CAST('{0}' AS DOUBLE)",
1103
+ name='spark',
1104
+ timestamp_type='TIMESTAMP_NTZ',
1105
+ zoned_timestamp_type='TIMESTAMP',
1106
+ type_map={'TIMESTAMP': 'TIMESTAMP_NTZ'},
1107
+ kinds_beside_a_list=True,
1108
+ union_by_field_name=False,
1109
+ database_name='Spark',
1110
+ max_identifier_length=255,
1111
+ array_literal='array({})',
1112
+ string_escape='backslash',
1113
+ quote_char='`',
1114
+ supports_anti_join=True,
1115
+ supports_semi_join=True,
1116
+ supports_lateral_view=True,
1117
+ supports_recursive_cte=True,
1118
+ reach_by_level_statement=True,
1119
+ current_date_parens=True,
1120
+ limit_style='standard',
1121
+ function_map={
1122
+ # SPARQL's string functions (m2-functions.md 3): code points, text comparisons (no LIKE),
1123
+ # REGEX over the pattern as this engine reads it (strings.py, xpath_regex.py), which is
1124
+ # the second argument (Java), the third (RE2) or the fourth (PostgreSQL's ARE);
1125
+ # None where not built.
1126
+ 'STR_STARTS': 'startswith({0}, {1})',
1127
+ 'STR_ENDS': 'endswith({0}, {1})',
1128
+ 'STR_CONTAINS': 'contains({0}, {1})',
1129
+ 'STR_POS': 'instr({0}, {1})',
1130
+ 'STR_LEN': 'char_length({0})',
1131
+ 'STR_UPPER': 'upper({0})',
1132
+ 'STR_LOWER': 'lower({0})',
1133
+ 'SUBSTR_CP': 'substring({0}, CAST({1} AS INT), CAST({2} AS INT))',
1134
+ 'SUBSTR_FROM': 'substring({0}, CAST({1} AS INT))',
1135
+ 'REGEX_MATCH': '({0} RLIKE {1})',
1136
+ # A native date's and dateTime's XSD text (m2-functions.md 2): years 1 to
1137
+ # 9999, an error outside them; None where not built.
1138
+ 'XSD_DATE_TEXT': "(CASE WHEN {0} IS NULL THEN NULL WHEN {0} BETWEEN DATE'0001-01-01' AND DATE'9999-12-31' THEN date_format({0}, 'yyyy-MM-dd') ELSE raise_error('ProGraph: a date or dateTime outside years 1 to 9999 has no XSD text here') END)",
1139
+ 'XSD_DATETIME_TEXT': '(CASE WHEN {0} IS NULL THEN NULL WHEN {0} BETWEEN TIMESTAMP\'0001-01-01 00:00:00\' AND TIMESTAMP\'9999-12-31 23:59:59.999999\' THEN concat(date_format({0}, "yyyy-MM-dd\'T\'HH:mm:ss"), CASE WHEN date_format({0}, \'SSSSSS\') = \'000000\' THEN \'\' ELSE concat(\'.\', regexp_replace(date_format({0}, \'SSSSSS\'), \'0+$\', \'\')) END) ELSE raise_error(\'ProGraph: a date or dateTime outside years 1 to 9999 has no XSD text here\') END)',
1140
+ # A decimal's canonical text, XML Schema 1.1's, R2RML 10.2's edition ('5', '1.5'; D3).
1141
+ 'XSD_DECIMAL_TEXT': "(CASE WHEN {0} = 0 THEN '0' WHEN NOT (instr(CAST({0} AS STRING), '.') > 0) THEN CAST({0} AS STRING) ELSE TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM CAST({0} AS STRING))) END)",
1142
+ 'XSD_DOUBLE_TEXT': _double_text('STRING', lambda s, x: f"regexp_extract({s}, '{x}', 1)",
1143
+ [f"format_string('%.{k - 1}e', {{0}})" for k in (15, 16, 17)],
1144
+ cast="TRY_CAST"),
1145
+ # YEAR, MONTH, DAY, HOURS, MINUTES and SECONDS of a native DATE or local TIMESTAMP
1146
+ # (m2-functions.md 6): the field, SECONDS a DECIMAL(8, 6) with its fraction; None where not built.
1147
+ 'DT_YEAR': 'EXTRACT(YEAR FROM {0})',
1148
+ 'DT_MONTH': 'EXTRACT(MONTH FROM {0})',
1149
+ 'DT_DAY': 'EXTRACT(DAY FROM {0})',
1150
+ 'DT_HOUR': 'EXTRACT(HOUR FROM {0})',
1151
+ 'DT_MINUTE': 'EXTRACT(MINUTE FROM {0})',
1152
+ 'DT_SECOND': 'CAST(EXTRACT(SECOND FROM {0}) AS DECIMAL(8,6))',
1153
+ # A zoned date-time's instant as a date-time in UTC, for its text
1154
+ # (ExprTranslator._as_text). A TIMESTAMP is an
1155
+ # instant shown in the session's zone and is read
1156
+ # as zoned (types.of_sql_type): convert_timezone
1157
+ # reads it as its wall clock in the session's zone and gives UTC's.
1158
+ 'DATETIME_IN_UTC': "convert_timezone('UTC', {0})",
1159
+ # A Cypher float literal is a float: a bare 2.0 is DECIMAL in SQL,
1160
+ # and 7 / 2.0 came back as a decimal.
1161
+ 'FLOAT_LITERAL': '{0}D',
1162
+ # Whether a double is NaN (a comparison with one is false); FALSE where the type holds none (SQL Server,
1163
+ # MySQL).
1164
+ 'IS_NAN': 'isnan({0})',
1165
+ **_EXACT_QUOTIENT,
1166
+ # An empty property map (decision D8). A struct, as a non-empty
1167
+ # property set is and as Databricks writes it: Spark refuses a MAP
1168
+ # in DISTINCT, GROUP BY and collect_set, so `RETURN DISTINCT r` of a
1169
+ # relationship with no properties was a database error. The answer's type is the same `struct<>` either way.
1170
+ 'EMPTY_MAP': "struct()",
1171
+ # List comprehension and quantifiers: {0} the list, {1} the loop
1172
+ # variable, {2} the body. Lambdas where the dialect has them.
1173
+ # Integer division, truncated toward zero as Cypher's is.
1174
+ # Temporal constructors from components; None refuses.
1175
+ 'MAKE_DATE': "make_date({0}, {1}, {2})",
1176
+ 'MAKE_TIME': None,
1177
+ # The time of day now in UTC, Cypher's default zone (localtime(),
1178
+ # time()), and a time of day read from text (localtime('02:30')):
1179
+ # a TIME, which Spark 4.1 refuses (UNSUPPORTED_TIME_TYPE): None refuses. Databricks SQL has one, below.
1180
+ 'TIME_NOW': None,
1181
+ # localdatetime(): now's wall clock in UTC, with no zone.
1182
+ 'LOCAL_NOW': "convert_timezone('UTC', current_timestamp())",
1183
+ # datetime(...) compared: the instant of a UTC text (ExprTranslator._compared).
1184
+ 'ZONED_OF_TEXT': "CAST({0} AS TIMESTAMP)",
1185
+ 'TIME_OF_TEXT': None,
1186
+ 'MAKE_TIMESTAMP': "make_timestamp_ntz({0}, {1}, {2}, {3}, {4}, {5})",
1187
+ # A part of a date or a local date-time. Spark's
1188
+ # own functions return INT; Cypher's integer is a BIGINT. SECOND of
1189
+ # a timestamp is a decimal with its fraction, which the sub-second
1190
+ # parts are cut from; each counts from the start of the second.
1191
+ 'YEAR': "CAST(YEAR({0}) AS BIGINT)",
1192
+ 'MONTH': "CAST(MONTH({0}) AS BIGINT)",
1193
+ 'DAY': "CAST(DAY({0}) AS BIGINT)",
1194
+ 'HOUR': "CAST(HOUR({0}) AS BIGINT)",
1195
+ 'MINUTE': "CAST(MINUTE({0}) AS BIGINT)",
1196
+ 'SECOND': "CAST(SECOND({0}) AS BIGINT)",
1197
+ 'MILLISECOND': "CAST((date_part('SECOND', {0}) % 1) * 1000 AS BIGINT)",
1198
+ 'MICROSECOND': "CAST((date_part('SECOND', {0}) % 1) * 1000000 AS BIGINT)",
1199
+ 'NANOSECOND': "CAST((date_part('SECOND', {0}) % 1) * 1000000000 AS BIGINT)",
1200
+ 'QUARTER': "CAST(QUARTER({0}) AS BIGINT)",
1201
+ # The ISO week and the ISO day of the week, Monday 1 (weekday()
1202
+ # counts Monday 0).
1203
+ 'WEEK': "CAST(WEEKOFYEAR({0}) AS BIGINT)",
1204
+ 'DAY_OF_WEEK': "CAST(WEEKDAY({0}) + 1 AS BIGINT)",
1205
+ 'ORDINAL_DAY': "CAST(DAYOFYEAR({0}) AS BIGINT)",
1206
+ # A string's length and a list's size (SPARQL STRLEN, Cypher
1207
+ # size()): Spark's return INT; an integer is a BIGINT, as on every
1208
+ # other engine. Spark's string and list functions
1209
+ # take a BIGINT length too, but element_at, whose index LIST_GET casts.
1210
+ 'LENGTH': "CAST(LENGTH({0}) AS BIGINT)",
1211
+ 'SIZE': "CAST(SIZE({0}) AS BIGINT)",
1212
+ # A date moved by a duration's months, then its days: add_months clamps to the month's end as Cypher does.
1213
+ 'DATE_PLUS': "date_add(add_months({0}, CAST({1} AS INT)), CAST({2} AS INT))",
1214
+ # A timestamp moved by months, days and microseconds in that order,
1215
+ # and the microseconds from one timestamp to another.
1216
+ 'TS_PLUS': "timestampadd(MICROSECOND, {3}, timestampadd(DAY, {2}, timestampadd(MONTH, {1}, {0})))",
1217
+ 'MICROS_BETWEEN': "timestampdiff(MICROSECOND, {0}, {1})",
1218
+ # Two edges differ (relationship uniqueness); see _different_edges.
1219
+ 'EDGE_DIFFERS': "(NOT ({0} = {2} AND {1} = {3}))",
1220
+ # The same with their row numbers.
1221
+ 'EDGE_ROW_DIFFERS': "(NOT ({0} = {3} AND {1} = {4} AND {2} = {5}))",
1222
+ # List equality, three-valued (ExprTranslator._list_equality): whether
1223
+ # some pair of elements at one position differs, and whether a list
1224
+ # holds a null.
1225
+ 'LIST_PAIR_DIFFERS': "exists(zip_with({0}, {1}, (_x, _y) -> _x <> _y), _d -> _d)",
1226
+ 'COLLECT_PAIRS': ("COALESCE(collect_list(struct({0} AS v, {1} AS i)) FILTER (WHERE {0} IS NOT NULL), array())"),
1227
+ 'PAIR_VALUES': "transform({0}, lv_p -> lv_p.v)",
1228
+ 'PAIR_KINDS': "transform({0}, lv_p -> lv_p.i)",
1229
+ 'PAIR': "struct({0} AS v, {1} AS i)",
1230
+ 'LIST_PAIR_ANY': "exists(zip_with({0}, {1}, ({2}, {3}) -> {4}), {2}_d -> {2}_d)",
1231
+ 'LIST_PAIR_BOTH_VALUES': "exists(zip_with({0}, {1}, (_x, _y) -> _x IS NOT NULL AND _y IS NOT NULL), _d -> _d)",
1232
+ 'LIST_HAS_NULL': "exists({0}, _x -> _x IS NULL)",
1233
+ # A list no element gives a type ([] or [null]), read as a list of
1234
+ # the other operand's elements (ExprTranslator._typed_list).
1235
+ 'LIST_OF_INTEGER': "{0}",
1236
+ 'LIST_OF_FLOAT': "{0}",
1237
+ 'LIST_OF_STRING': "{0}",
1238
+ 'LIST_OF_BOOLEAN': "{0}",
1239
+ # The key of a collected node's map, and a key to compare with it
1240
+ # (see ExprTranslator._node_equality).
1241
+ 'NODE_MAP_KEY': "{0}.`id`",
1242
+ 'AS_NODE_MAP_KEY': "{0}",
1243
+ # The labels of a collected node's map (ExprTranslator._labels).
1244
+ 'NODE_MAP_LABELS': "{0}.`labels`",
1245
+ # Two lists joined (Cypher's + on lists); None where there is no list.
1246
+ 'LIST_CONCAT': 'concat({0}, {1})',
1247
+ **_SPARK_DIVISION,
1248
+ 'LIST_FILTER': "filter({0}, {1} -> {2})",
1249
+ 'LIST_TRANSFORM': "transform({0}, {1} -> {2})",
1250
+ **_JAVA_REGEX,
1251
+ # Functions the base serializer spells the Spark way.
1252
+ 'PERCENTILE_DISC': 'percentile_disc({1}) WITHIN GROUP (ORDER BY {0})',
1253
+ 'PERCENTILE_CONT': 'percentile_cont({1}) WITHIN GROUP (ORDER BY {0})',
1254
+ # SPARQL GROUP_CONCAT: {0} the string form, {1} the separator; an empty
1255
+ # group is '' (18.5.1.7). collect_list skips nulls and Spark's array_join
1256
+ # of none is '' already; the COALESCE is for the engines that read this
1257
+ # text as ARRAY_TO_STRING(ARRAY_AGG(..)), where none is NULL.
1258
+ 'GROUP_CONCAT': "COALESCE(array_join(collect_list({0}), {1}), '')",
1259
+ 'GROUP_CONCAT_DISTINCT': "COALESCE(array_join(collect_set({0}), {1}), '')",
1260
+ # range()'s elements are BIGINTs: SEQUENCE of two INT literals is an
1261
+ # array of INT, so the start is cast.
1262
+ # Cypher's range() is empty when the step leads away from the end
1263
+ # (range(1, 0), range(1, 3, -1)). sequence() counts down when the
1264
+ # end is below the start, [1, 0], and raises on a step of the wrong
1265
+ # sign. The filter keeps all or nothing. Where it
1266
+ # keeps nothing the end given to sequence() is the start, so one
1267
+ # element is built and dropped: a countdown of the whole span would
1268
+ # be built first, and past the array limit that raises.
1269
+ 'SEQUENCE': ('filter(SEQUENCE(CAST({0} AS BIGINT), CASE WHEN ({0}) <= ({1}) THEN {1} ELSE {0} END), '
1270
+ '_rng -> ({0}) <= ({1}))'),
1271
+ # A step of 0 is an error in Cypher, raised here as the Python
1272
+ # path raises it; sequence() would have answered [a] for (a, a, 0).
1273
+ 'SEQUENCE_STEP': ('filter(SEQUENCE(CAST({0} AS BIGINT), CASE WHEN ({2}) = 0 THEN '
1274
+ "raise_error('NumberOutOfRange: step must not be 0 for range()') "
1275
+ 'WHEN (({2}) > 0 AND ({0}) <= ({1})) '
1276
+ 'OR (({2}) < 0 AND ({0}) >= ({1})) THEN {1} ELSE {0} END, {2}), '
1277
+ '_rng -> (({2}) > 0 AND ({0}) <= ({1})) '
1278
+ 'OR (({2}) < 0 AND ({0}) >= ({1})))'),
1279
+ 'LIST_REVERSE': 'REVERSE({0})',
1280
+ 'STR_REVERSE': 'REVERSE({0})',
1281
+ # String `+`: null if either side is null.
1282
+ 'STR_CONCAT': "({0} || {1})",
1283
+ 'MD5': "md5({0})",
1284
+ 'SHA1': "sha1({0})",
1285
+ 'SHA256': "sha2({0}, 256)",
1286
+ 'SHA384': "sha2({0}, 384)",
1287
+ 'SHA512': "sha2({0}, 512)",
1288
+ 'SPARQL_UUID': "CONCAT('urn:uuid:', uuid())",
1289
+ 'BNODE': "CONCAT('_:b', REPLACE(CAST(uuid() AS STRING), '-', ''))",
1290
+ 'RANDOMUUID': "uuid()",
1291
+ # url_encode is Java's URLEncoder: a space '+', '*' and '~' as the
1292
+ # form encoding has them; ENCODE_FOR_URI (SPARQL 1.1 17.4.3.11)
1293
+ # percent-encodes all but RFC 3986's unreserved characters. A '+'
1294
+ # it encodes is '%2B', so the replacements touch only these.
1295
+ 'ENCODE_FOR_URI': "replace(replace(replace(url_encode({0}), '+', '%20'), '*', '%2A'), "
1296
+ "'%7E', '~')",
1297
+ # collect() over no rows is [].
1298
+ 'EMPTY_LIST': "array()",
1299
+ # The index is cast: element_at takes an INT, and an index that is
1300
+ # a BIGINT (a column, a count, arithmetic on either) was text Spark
1301
+ # refused, DATATYPE_MISMATCH. TRY_CAST, so that an index past an
1302
+ # INT is no element (NULL) and not a wrapped position.
1303
+ 'LIST_GET': 'TRY_ELEMENT_AT({0}, TRY_CAST(CASE WHEN {1} >= 0 THEN {1} + 1 ELSE {1} END AS INT))',
1304
+ 'SPLIT': "SPLIT({0}, CONCAT('\\\\Q', {1}, '\\\\E'))",
1305
+ 'TO_FLOAT': 'TRY_CAST({0} AS DOUBLE)',
1306
+ # A DECIMAL's canonical text (`ExprTranslator._as_text`), its value
1307
+ # read once: written in full, where with ANSI mode off Spark casts one
1308
+ # below 10^-6 in scientific form (0E-18, 1.00000000000E-7,
1309
+ # BigDecimal.toString's), so that one is written from its digits;
1310
+ # then no trailing zero after the point, one digit kept. {1} is its
1311
+ # scale.
1312
+ 'DECIMAL_TEXT': ("transform(array(transform(array({0}), _dec -> CASE WHEN _dec = 0 THEN '0' "
1313
+ "WHEN abs(_dec) < 0.000001 THEN concat(if(_dec < 0, '-', ''), '0.', "
1314
+ "lpad(CAST(CAST(abs(_dec) * CAST(power(10, {1}) AS DECIMAL(38,0)) AS DECIMAL(38,0)) "
1315
+ "AS STRING), {1}, '0')) ELSE CAST(_dec AS STRING) END)[0]), _txt -> "
1316
+ "CASE WHEN instr(_txt, '.') > 0 THEN regexp_replace(regexp_replace(_txt, '0+$', ''), "
1317
+ "'\\\\.$', '.0') ELSE concat(_txt, '.0') END)[0]"),
1318
+ 'TO_INTEGER': 'CAST(CASE WHEN TRY_CAST({0} AS DOUBLE) >= 0 THEN FLOOR(TRY_CAST({0} AS DOUBLE)) ELSE CEIL(TRY_CAST({0} AS DOUBLE)) END AS BIGINT)',
1319
+ 'TO_BOOLEAN': "CASE LOWER(CAST({0} AS STRING)) WHEN 'true' THEN TRUE WHEN 'false' THEN FALSE END",
1320
+ # xsd:integer and xsd:dateTime of text already in the lexical
1321
+ # space (ExprTranslator._xsd_cast); a zoned dateTime is its
1322
+ # instant in UTC, held with no zone.
1323
+ 'XSD_TO_INTEGER': 'TRY_CAST({0} AS BIGINT)',
1324
+ # An integral double within the 64-bit range, exactly (Cypher's integer-against-float comparison).
1325
+ 'DOUBLE_TO_BIGINT': 'CAST({0} AS BIGINT)',
1326
+ 'XSD_TO_DECIMAL': 'TRY_CAST({0} AS DECIMAL(38,18))',
1327
+ 'XSD_TO_DECIMAL_SIZED': 'TRY_CAST({0} AS DECIMAL({1},{2}))',
1328
+ # A decimal + - * declared past 38 digits (D7, m2-functions.md 9): DECIMAL(38, {2}),
1329
+ # or NULL (unbound, an expression error) where the value needs more; {3} is 10^(38 - {2}).
1330
+ 'TRY_DECIMAL_ADD': 'try_cast(try_add({0}, {1}) AS DECIMAL(38,{2}))',
1331
+ 'TRY_DECIMAL_SUB': 'try_cast(try_subtract({0}, {1}) AS DECIMAL(38,{2}))',
1332
+ 'TRY_DECIMAL_MUL': 'try_cast(try_multiply({0}, {1}) AS DECIMAL(38,{2}))',
1333
+ # A decimal SUM, NULL where it passes 38 digits ({1} is 10^(38 - scale)); DuckDB's raises
1334
+ # (its SUM cannot be wrapped in TRY).
1335
+ 'TRY_DECIMAL_SUM': 'try_sum(CAST({0} AS DECIMAL(38,{2})))',
1336
+ # An operand of a decimal + - * (lower.exact_decimal): widened to 19 digits on DuckDB,
1337
+ # given its declared type on Databricks (no precision-loss setting there).
1338
+ 'DECIMAL_WIDENED': '{0}',
1339
+ 'DECIMAL_NARROWED': '{0}',
1340
+ 'XSD_TO_DATETIME': "TIMESTAMPADD(MINUTE, -(CASE WHEN {0} RLIKE '[+-][0-9][0-9]:[0-9][0-9]$' THEN (CASE WHEN LEFT(RIGHT({0}, 6), 1) = '-' THEN -1 ELSE 1 END) * (CAST(LEFT(RIGHT({0}, 5), 2) AS INT) * 60 + CAST(RIGHT({0}, 2) AS INT)) ELSE 0 END), TRY_CAST(REGEXP_REPLACE({0}, '(Z|[+-][0-9][0-9]:[0-9][0-9])$', '') AS TIMESTAMP_NTZ))",
1341
+ 'XSD_TO_DATE': "TRY_CAST({0} AS DATE)",
1342
+ },
1343
+ )
1344
+
1345
+ POSTGRESQL_DIALECT = DialectConfig(
1346
+ identifier_fold='lower',
1347
+ non_finite_double="CAST('{0}' AS DOUBLE PRECISION)",
1348
+ name='postgresql',
1349
+ string_escape='postgres',
1350
+ lists_of_lists=False,
1351
+ database_name='PostgreSQL',
1352
+ max_identifier_length=63,
1353
+ identifier_length_in_bytes=True,
1354
+ param_style='$1',
1355
+ array_literal='ARRAY[{}]',
1356
+ quote_char='"',
1357
+ supports_recursive_cte=True,
1358
+ supports_recursive_union=True,
1359
+ supports_cycle_detection=True,
1360
+ current_date_parens=False,
1361
+ limit_style='standard',
1362
+ function_map={
1363
+ # SPARQL's string functions (m2-functions.md 3): code points, text comparisons (no LIKE),
1364
+ # REGEX over the pattern as this engine reads it (strings.py, xpath_regex.py), which is
1365
+ # the second argument (Java), the third (RE2) or the fourth (PostgreSQL's ARE);
1366
+ # None where not built.
1367
+ 'STR_STARTS': '(left({0}, char_length({1})) = {1})',
1368
+ 'STR_ENDS': '(right({0}, char_length({1})) = {1})',
1369
+ 'STR_CONTAINS': '(strpos({0}, {1}) > 0)',
1370
+ 'STR_POS': 'strpos({0}, {1})',
1371
+ 'STR_LEN': 'CAST(char_length({0}) AS BIGINT)',
1372
+ 'STR_UPPER': 'upper({0})',
1373
+ 'STR_LOWER': 'lower({0})',
1374
+ 'SUBSTR_CP': 'substring({0}, CAST({1} AS INTEGER), CAST({2} AS INTEGER))',
1375
+ 'SUBSTR_FROM': 'substring({0}, CAST({1} AS INTEGER))',
1376
+ 'REGEX_MATCH': '({0} ~ {3})',
1377
+ # A native date's and dateTime's XSD text (m2-functions.md 2): years 1 to
1378
+ # 9999, an error outside them; None where not built.
1379
+ 'XSD_DATE_TEXT': "(CASE WHEN {0} IS NULL THEN NULL WHEN {0} BETWEEN DATE '0001-01-01' AND DATE '9999-12-31' THEN to_char({0}, 'YYYY-MM-DD') ELSE CAST('ProGraph: a date or dateTime outside years 1 to 9999 has no XSD text here: ' || CAST({0} AS TEXT) AS INTEGER)::TEXT END)",
1380
+ 'XSD_DATETIME_TEXT': '(CASE WHEN {0} IS NULL THEN NULL WHEN {0} BETWEEN TIMESTAMP \'0001-01-01 00:00:00\' AND TIMESTAMP \'9999-12-31 23:59:59.999999\' THEN to_char({0}, \'YYYY-MM-DD"T"HH24:MI:SS\') || CASE WHEN to_char({0}, \'US\') = \'000000\' THEN \'\' ELSE \'.\' || rtrim(to_char({0}, \'US\'), \'0\') END ELSE CAST(\'ProGraph: a date or dateTime outside years 1 to 9999 has no XSD text here: \' || CAST({0} AS TEXT) AS INTEGER)::TEXT END)',
1381
+ # A decimal's canonical text, XML Schema 1.1's, R2RML 10.2's edition ('5', '1.5'; D3).
1382
+ # A NUMERIC 'NaN' or infinity is no xsd:decimal: a data error, {1} its message (`lower.decimal_text`).
1383
+ 'XSD_DECIMAL_TEXT': "(CASE WHEN {0} = 0 THEN '0' WHEN CAST({0} AS TEXT) IN ('NaN', 'Infinity', '-Infinity') THEN CAST({1} || CAST({0} AS TEXT) AS INTEGER)::TEXT WHEN NOT (position('.' in CAST({0} AS TEXT)) > 0) THEN CAST({0} AS TEXT) ELSE TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM CAST({0} AS TEXT))) END)",
1384
+ 'XSD_DOUBLE_TEXT': _double_text('TEXT', lambda s, x: f"substring({s} from '{x}')",
1385
+ [f"to_char({{0}}, '9.{'9' * (k - 1)}EEEE')" for k in (15, 16, 17)],
1386
+ 'DOUBLE PRECISION', guarded=True),
1387
+ # YEAR, MONTH, DAY, HOURS, MINUTES and SECONDS of a native DATE or local TIMESTAMP
1388
+ # (m2-functions.md 6): the field, SECONDS a DECIMAL(8, 6) with its fraction; None where not built.
1389
+ 'DT_YEAR': 'EXTRACT(YEAR FROM {0})',
1390
+ 'DT_MONTH': 'EXTRACT(MONTH FROM {0})',
1391
+ 'DT_DAY': 'EXTRACT(DAY FROM {0})',
1392
+ 'DT_HOUR': 'EXTRACT(HOUR FROM {0})',
1393
+ 'DT_MINUTE': 'EXTRACT(MINUTE FROM {0})',
1394
+ 'DT_SECOND': 'CAST(EXTRACT(SECOND FROM {0}) AS NUMERIC(8,6))',
1395
+ # A zoned date-time's instant as a date-time in UTC, for its text
1396
+ # (ExprTranslator._as_text).
1397
+ 'DATETIME_IN_UTC': "({0} AT TIME ZONE 'UTC')",
1398
+ # A Cypher float literal is a float: a bare 2.0 is DECIMAL in SQL,
1399
+ # and 7 / 2.0 came back as a decimal.
1400
+ 'FLOAT_LITERAL': 'CAST({0} AS DOUBLE PRECISION)',
1401
+ 'IS_NAN': "({0} = CAST('NaN' AS DOUBLE PRECISION))",
1402
+ **_POSTGRESQL_EXACT_QUOTIENT,
1403
+ # An empty property map (decision D8).
1404
+ 'EMPTY_MAP': "'{{}}'::jsonb",
1405
+ # List comprehension and quantifiers: no lambdas, so a subquery over
1406
+ # the unnested list, its order kept by ordinality.
1407
+ # Integer division, truncated toward zero as Cypher's is.
1408
+ # Temporal constructors from components; None refuses.
1409
+ 'MAKE_DATE': "make_date({0}, {1}, {2})",
1410
+ 'MAKE_TIME': "make_time({0}, {1}, {2})",
1411
+ 'TIME_NOW': "CAST((CURRENT_TIMESTAMP AT TIME ZONE 'UTC') AS TIME)",
1412
+ # localdatetime(): now's wall clock in UTC, with no zone.
1413
+ 'LOCAL_NOW': "(CURRENT_TIMESTAMP AT TIME ZONE 'UTC')",
1414
+ # datetime(...) compared: the instant of a UTC text (ExprTranslator._compared).
1415
+ 'ZONED_OF_TEXT': "CAST({0} AS TIMESTAMPTZ)",
1416
+ 'TIME_OF_TEXT': "CAST({0} AS TIME)",
1417
+ 'MAKE_TIMESTAMP': "make_timestamp({0}, {1}, {2}, {3}, {4}, {5})",
1418
+ # A part of a date or a local date-time past the six SQL names:
1419
+ # not written for this database yet.
1420
+ 'MILLISECOND': None,
1421
+ 'MICROSECOND': None,
1422
+ 'NANOSECOND': None,
1423
+ 'QUARTER': None,
1424
+ 'WEEK': None,
1425
+ 'DAY_OF_WEEK': None,
1426
+ 'ORDINAL_DAY': None,
1427
+ # A date moved by a duration's months, then its days.
1428
+ 'DATE_PLUS': "(CAST(({0}) + ({1}) * INTERVAL '1 month' AS DATE) + CAST(({2}) AS INTEGER))",
1429
+ 'TS_PLUS': "((({0}) + ({1}) * INTERVAL '1 month') + ({2}) * INTERVAL '1 day' + ({3}) * INTERVAL '1 microsecond')",
1430
+ 'MICROS_BETWEEN': "CAST(EXTRACT(EPOCH FROM ({1}) - ({0})) * 1000000 AS BIGINT)",
1431
+ # Two edges differ (relationship uniqueness); see _different_edges.
1432
+ 'EDGE_DIFFERS': "(NOT ({0} = {2} AND {1} = {3}))",
1433
+ # The same with their row numbers.
1434
+ 'EDGE_ROW_DIFFERS': "(NOT ({0} = {3} AND {1} = {4} AND {2} = {5}))",
1435
+ # List equality, three-valued (ExprTranslator._list_equality): whether
1436
+ # some pair of elements at one position differs, and whether a list
1437
+ # holds a null.
1438
+ 'LIST_PAIR_DIFFERS': "EXISTS (SELECT 1 FROM unnest({0}, {1}) AS _p(_x, _y) WHERE _x <> _y)",
1439
+ 'COLLECT_PAIRS': None,
1440
+ 'PAIR_VALUES': None,
1441
+ 'PAIR_KINDS': None,
1442
+ 'PAIR': None,
1443
+ 'LIST_PAIR_ANY': None,
1444
+ 'LIST_PAIR_BOTH_VALUES': "EXISTS (SELECT 1 FROM unnest({0}, {1}) AS _p(_x, _y) WHERE _x IS NOT NULL AND _y IS NOT NULL)",
1445
+ 'LIST_HAS_NULL': "(array_position({0}, NULL) IS NOT NULL)",
1446
+ # A list no element gives a type ([] or [null]), read as a list of
1447
+ # the other operand's elements (ExprTranslator._typed_list). ARRAY[] has
1448
+ # no type here and ARRAY[NULL] is text[], which no integer compares with.
1449
+ 'LIST_OF_INTEGER': "CAST({0} AS BIGINT[])",
1450
+ 'LIST_OF_FLOAT': "CAST({0} AS DOUBLE PRECISION[])",
1451
+ 'LIST_OF_STRING': "CAST({0} AS TEXT[])",
1452
+ 'LIST_OF_BOOLEAN': "CAST({0} AS BOOLEAN[])",
1453
+ # The key of a collected node's map, and a key to compare with it
1454
+ # (see ExprTranslator._node_equality).
1455
+ 'NODE_MAP_KEY': "({0}->'id')",
1456
+ 'AS_NODE_MAP_KEY': "to_jsonb({0})",
1457
+ # The labels of a collected node's map (ExprTranslator._labels).
1458
+ 'NODE_MAP_LABELS': "ARRAY(SELECT jsonb_array_elements_text({0}->'labels'))",
1459
+ # Two lists joined (Cypher's + on lists); None where there is no list.
1460
+ 'LIST_CONCAT': 'array_cat({0}, {1})',
1461
+ 'INT_DIV': "(({0}) / ({1}))",
1462
+ 'INT_QUOTIENT': "(({0}) / ({1}))",
1463
+ 'INT_DIV_ONCE': "(({0}) / ({1}))",
1464
+ **_PLAIN_DIVISION,
1465
+ 'LIST_FILTER': ("ARRAY(SELECT {1} FROM unnest({0}) WITH ORDINALITY AS _lv({1}, _lv_o) "
1466
+ "WHERE {2} ORDER BY _lv_o)"),
1467
+ 'LIST_TRANSFORM': ("ARRAY(SELECT {2} FROM unnest({0}) WITH ORDINALITY AS _lv({1}, _lv_o) "
1468
+ "ORDER BY _lv_o)"),
1469
+ # =~ matches the whole string.
1470
+ 'REGEX_FULL': "({0} ~ ('^(?:' || {1} || ')$'))",
1471
+ # ROW has no field names to read back (a map literal loses them).
1472
+ '.': None,
1473
+ # Functions the base serializer spells the Spark way.
1474
+ 'PERCENTILE_DISC': 'percentile_disc({1}) WITHIN GROUP (ORDER BY {0})',
1475
+ 'PERCENTILE_CONT': 'percentile_cont({1}) WITHIN GROUP (ORDER BY {0})',
1476
+ # SPARQL GROUP_CONCAT: {0} the string form, {1} the separator; an empty
1477
+ # group is '' (18.5.1.7), not string_agg's NULL.
1478
+ 'GROUP_CONCAT': "COALESCE(string_agg({0}, {1}), '')",
1479
+ 'GROUP_CONCAT_DISTINCT': "COALESCE(string_agg(DISTINCT {0}, {1}), '')",
1480
+ 'SEQUENCE': 'ARRAY(SELECT generate_series({0}, {1}))',
1481
+ 'SEQUENCE_STEP': 'ARRAY(SELECT generate_series({0}, {1}, {2}))',
1482
+ 'SLICE': '({0})[{1}:{1} + {2} - 1]',
1483
+ # PostgreSQL has no array reverse; unnest in order and gather back.
1484
+ # ARRAY(SELECT ...) of no rows is the empty array, and unnest of a
1485
+ # NULL array gives no rows: without the test a null list reversed
1486
+ # was [] (an array column's reverse reaches here
1487
+ # now that its type is known).
1488
+ 'LIST_REVERSE': ('CASE WHEN {0} IS NULL THEN NULL ELSE ARRAY(SELECT _v FROM unnest({0}) '
1489
+ 'WITH ORDINALITY AS _u(_v, _i) ORDER BY _i DESC) END'),
1490
+ 'STR_REVERSE': 'reverse({0})',
1491
+ 'E': 'exp(1)',
1492
+ 'RAND': 'random()',
1493
+ 'SQRT': "CASE WHEN {0} < 0 THEN CAST('NaN' AS DOUBLE PRECISION) ELSE SQRT({0}) END",
1494
+ 'YEAR': 'CAST(EXTRACT(YEAR FROM {0}) AS BIGINT)',
1495
+ 'MONTH': 'CAST(EXTRACT(MONTH FROM {0}) AS BIGINT)',
1496
+ 'DAY': 'CAST(EXTRACT(DAY FROM {0}) AS BIGINT)',
1497
+ 'HOUR': 'CAST(EXTRACT(HOUR FROM {0}) AS BIGINT)',
1498
+ 'MINUTE': 'CAST(EXTRACT(MINUTE FROM {0}) AS BIGINT)',
1499
+ 'SECOND': 'CAST(EXTRACT(SECOND FROM {0}) AS BIGINT)',
1500
+ # String `+`: null if either side is null.
1501
+ 'STR_CONCAT': "({0} || {1})",
1502
+ # SPARQL REGEX searches, as PostgreSQL's `~` does.
1503
+ 'REGEX_SEARCH': "{0} ~ {1}",
1504
+ 'REGEX_REPLACE': "regexp_replace({0}, {1}, regexp_replace({2}, '\\$([0-9])', '\\\\\\1', 'g'), 'g')",
1505
+ 'REGEX_REPLACE_I': "regexp_replace({0}, {1}, regexp_replace({2}, '\\$([0-9])', '\\\\\\1', 'g'), 'gi')",
1506
+ 'MD5': "md5({0})",
1507
+ 'SHA1': None,
1508
+ 'SHA256': "encode(sha256(convert_to({0}, 'UTF8')), 'hex')",
1509
+ 'SHA384': "encode(sha384(convert_to({0}, 'UTF8')), 'hex')",
1510
+ 'SHA512': "encode(sha512(convert_to({0}, 'UTF8')), 'hex')",
1511
+ 'SPARQL_UUID': "('urn:uuid:' || CAST(gen_random_uuid() AS TEXT))",
1512
+ 'BNODE': "('_:b' || REPLACE(CAST(gen_random_uuid() AS TEXT), '-', ''))",
1513
+ 'RANDOMUUID': "CAST(gen_random_uuid() AS TEXT)",
1514
+ 'ENCODE_FOR_URI': None,
1515
+ # collect() over no rows is [].
1516
+ 'EMPTY_LIST': "'{{}}'",
1517
+ 'LIST_GET': '({0})[CASE WHEN {1} >= 0 THEN {1} + 1 ELSE array_length({0}, 1) + {1} + 1 END]',
1518
+ 'SPLIT': 'string_to_array({0}, {1})',
1519
+ 'TO_FLOAT': "CASE WHEN CAST({0} AS TEXT) ~ '^\\s*[-+]?([0-9]+\\.?[0-9]*|\\.[0-9]+)([eE][-+]?[0-9]+)?\\s*$' THEN CAST(CAST({0} AS TEXT) AS DOUBLE PRECISION) END",
1520
+ 'TO_INTEGER': "CAST(TRUNC(CASE WHEN CAST({0} AS TEXT) ~ '^\\s*[-+]?([0-9]+\\.?[0-9]*|\\.[0-9]+)([eE][-+]?[0-9]+)?\\s*$' THEN CAST(CAST({0} AS TEXT) AS DOUBLE PRECISION) END) AS BIGINT)",
1521
+ 'TO_BOOLEAN': "CASE LOWER(CAST({0} AS TEXT)) WHEN 'true' THEN TRUE WHEN 'false' THEN FALSE END",
1522
+ # xsd:T(?v) of text already in the lexical space (# ExprTranslator._xsd_cast); a zoned dateTime is its instant in UTC.
1523
+ 'XSD_TO_INTEGER': 'CASE WHEN ABS(CAST({0} AS NUMERIC)) <= 9223372036854775807 THEN CAST(CAST({0} AS NUMERIC) AS BIGINT) END',
1524
+ # An integral double within the 64-bit range, exactly (Cypher's integer-against-float comparison).
1525
+ 'DOUBLE_TO_BIGINT': 'CAST({0} AS BIGINT)',
1526
+ 'XSD_TO_DECIMAL': 'CAST(CAST({0} AS NUMERIC) AS NUMERIC(38,18))',
1527
+ 'XSD_TO_DECIMAL_SIZED': 'CAST(CAST({0} AS NUMERIC) AS NUMERIC({1},{2}))',
1528
+ # A decimal + - * declared past 38 digits (D7, m2-functions.md 9): DECIMAL(38, {2}),
1529
+ # or NULL (unbound, an expression error) where the value needs more; {3} is 10^(38 - {2}).
1530
+ 'TRY_DECIMAL_ADD': '(CASE WHEN ABS(({0}) + ({1})) < {3} THEN CAST(({0}) + ({1}) AS NUMERIC(38,{2})) END)',
1531
+ 'TRY_DECIMAL_SUB': '(CASE WHEN ABS(({0}) - ({1})) < {3} THEN CAST(({0}) - ({1}) AS NUMERIC(38,{2})) END)',
1532
+ 'TRY_DECIMAL_MUL': '(CASE WHEN ABS(({0}) * ({1})) < {3} THEN CAST(({0}) * ({1}) AS NUMERIC(38,{2})) END)',
1533
+ # A decimal SUM, NULL where it passes 38 digits ({1} is 10^(38 - scale)); DuckDB's raises
1534
+ # (its SUM cannot be wrapped in TRY).
1535
+ 'TRY_DECIMAL_SUM': '(CASE WHEN ABS(SUM({0})) < {1} THEN SUM({0}) END)',
1536
+ # An operand of a decimal + - * (lower.exact_decimal): widened to 19 digits on DuckDB,
1537
+ # given its declared type on Databricks (no precision-loss setting there).
1538
+ 'DECIMAL_WIDENED': '{0}',
1539
+ 'DECIMAL_NARROWED': '{0}',
1540
+ 'XSD_TO_DATETIME': "(CAST(regexp_replace({0}, '(Z|[+-][0-9][0-9]:[0-9][0-9])$', '') AS TIMESTAMP) - (CASE WHEN {0} ~ '[+-][0-9][0-9]:[0-9][0-9]$' THEN (CASE WHEN LEFT(RIGHT({0}, 6), 1) = '-' THEN -1 ELSE 1 END) * (CAST(LEFT(RIGHT({0}, 5), 2) AS INT) * 60 + CAST(RIGHT({0}, 2) AS INT)) ELSE 0 END) * INTERVAL '1 minute')",
1541
+ 'XSD_TO_DATE': "CAST({0} AS DATE)",
1542
+ 'INSTR': 'STRPOS',
1543
+ },
1544
+ type_map={
1545
+ 'STRING': 'VARCHAR',
1546
+ 'DOUBLE': 'DOUBLE PRECISION',
1547
+ 'INT': 'INTEGER',
1548
+ 'LONG': 'BIGINT',
1549
+ },
1550
+ )
1551
+
1552
+ # Databricks SQL: same as Spark but no UNIQUE constraints in DDL.
1553
+ # Kept as a separate config for managed-edges DDL generation.
1554
+ DATABRICKS_SQL_DIALECT = DialectConfig(
1555
+ float_text_is_javas='always',
1556
+ non_finite_double="CAST('{0}' AS DOUBLE)",
1557
+ name='databricks_sql',
1558
+ timestamp_type='TIMESTAMP_NTZ',
1559
+ zoned_timestamp_type='TIMESTAMP',
1560
+ type_map={'TIMESTAMP': 'TIMESTAMP_NTZ'},
1561
+ kinds_beside_a_list=True,
1562
+ union_by_field_name=False,
1563
+ database_name='Databricks',
1564
+ max_identifier_length=255,
1565
+ array_literal='array({})',
1566
+ string_escape='backslash',
1567
+ quote_char='`',
1568
+ supports_anti_join=True,
1569
+ supports_semi_join=True,
1570
+ supports_lateral_view=True,
1571
+ supports_recursive_cte=True,
1572
+ reach_by_level_statement=True,
1573
+ current_date_parens=True,
1574
+ limit_style='standard',
1575
+ supports_unique_constraint=False,
1576
+ # databricks-sql-connector (use_inline_params=False, the v4 default)
1577
+ # requires qmark placeholders, not pyformat (%s).
1578
+ param_style='?',
1579
+ function_map={
1580
+ # SPARQL's string functions (m2-functions.md 3): code points, text comparisons (no LIKE),
1581
+ # REGEX over the pattern as this engine reads it (strings.py, xpath_regex.py), which is
1582
+ # the second argument (Java), the third (RE2) or the fourth (PostgreSQL's ARE);
1583
+ # None where not built.
1584
+ 'STR_STARTS': 'startswith({0}, {1})',
1585
+ 'STR_ENDS': 'endswith({0}, {1})',
1586
+ 'STR_CONTAINS': 'contains({0}, {1})',
1587
+ 'STR_POS': 'instr({0}, {1})',
1588
+ 'STR_LEN': 'char_length({0})',
1589
+ 'STR_UPPER': 'upper({0})',
1590
+ 'STR_LOWER': 'lower({0})',
1591
+ 'SUBSTR_CP': 'substring({0}, CAST({1} AS INT), CAST({2} AS INT))',
1592
+ 'SUBSTR_FROM': 'substring({0}, CAST({1} AS INT))',
1593
+ 'REGEX_MATCH': '({0} RLIKE {1})',
1594
+ # A native date's and dateTime's XSD text (m2-functions.md 2): years 1 to
1595
+ # 9999, an error outside them; None where not built.
1596
+ 'XSD_DATE_TEXT': "(CASE WHEN {0} IS NULL THEN NULL WHEN {0} BETWEEN DATE'0001-01-01' AND DATE'9999-12-31' THEN date_format({0}, 'yyyy-MM-dd') ELSE raise_error('ProGraph: a date or dateTime outside years 1 to 9999 has no XSD text here') END)",
1597
+ 'XSD_DATETIME_TEXT': '(CASE WHEN {0} IS NULL THEN NULL WHEN {0} BETWEEN TIMESTAMP\'0001-01-01 00:00:00\' AND TIMESTAMP\'9999-12-31 23:59:59.999999\' THEN concat(date_format({0}, "yyyy-MM-dd\'T\'HH:mm:ss"), CASE WHEN date_format({0}, \'SSSSSS\') = \'000000\' THEN \'\' ELSE concat(\'.\', regexp_replace(date_format({0}, \'SSSSSS\'), \'0+$\', \'\')) END) ELSE raise_error(\'ProGraph: a date or dateTime outside years 1 to 9999 has no XSD text here\') END)',
1598
+ # A decimal's canonical text, XML Schema 1.1's, R2RML 10.2's edition ('5', '1.5'; D3).
1599
+ 'XSD_DECIMAL_TEXT': "(CASE WHEN {0} = 0 THEN '0' WHEN NOT (instr(CAST({0} AS STRING), '.') > 0) THEN CAST({0} AS STRING) ELSE TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM CAST({0} AS STRING))) END)",
1600
+ 'XSD_DOUBLE_TEXT': _double_text('STRING', lambda s, x: f"regexp_extract({s}, '{x}', 1)",
1601
+ [f"format_string('%.{k - 1}e', {{0}})" for k in (15, 16, 17)],
1602
+ cast="TRY_CAST"),
1603
+ # YEAR, MONTH, DAY, HOURS, MINUTES and SECONDS of a native DATE or local TIMESTAMP
1604
+ # (m2-functions.md 6): the field, SECONDS a DECIMAL(8, 6) with its fraction; None where not built.
1605
+ 'DT_YEAR': 'EXTRACT(YEAR FROM {0})',
1606
+ 'DT_MONTH': 'EXTRACT(MONTH FROM {0})',
1607
+ 'DT_DAY': 'EXTRACT(DAY FROM {0})',
1608
+ 'DT_HOUR': 'EXTRACT(HOUR FROM {0})',
1609
+ 'DT_MINUTE': 'EXTRACT(MINUTE FROM {0})',
1610
+ 'DT_SECOND': 'CAST(EXTRACT(SECOND FROM {0}) AS DECIMAL(8,6))',
1611
+ # A zoned date-time's instant as a date-time in UTC, for its text
1612
+ # (ExprTranslator._as_text). A TIMESTAMP is an
1613
+ # instant shown in the session's zone and is read
1614
+ # as zoned (types.of_sql_type): convert_timezone
1615
+ # reads it as its wall clock in the session's zone and gives UTC's.
1616
+ 'DATETIME_IN_UTC': "convert_timezone('UTC', {0})",
1617
+ # A Cypher float literal is a float: a bare 2.0 is DECIMAL in SQL,
1618
+ # and 7 / 2.0 came back as a decimal.
1619
+ 'FLOAT_LITERAL': '{0}D',
1620
+ 'IS_NAN': 'isnan({0})',
1621
+ **_EXACT_QUOTIENT,
1622
+ # An empty property map (decision D8).
1623
+ # A struct, as a non-empty property set is: the connector returns a
1624
+ # MAP as a list of pairs, so map() came back as [] where {} was meant.
1625
+ 'EMPTY_MAP': "struct()",
1626
+ # List comprehension and quantifiers: {0} the list, {1} the loop
1627
+ # variable, {2} the body. Lambdas where the dialect has them.
1628
+ # Integer division, truncated toward zero as Cypher's is.
1629
+ # Temporal constructors from components; None refuses.
1630
+ 'MAKE_DATE': "make_date({0}, {1}, {2})",
1631
+ 'MAKE_TIME': None,
1632
+ # Databricks SQL has TIME (DBSQL 2026.42); the time now in UTC whatever the session's zone.
1633
+ 'TIME_NOW': "CAST(convert_timezone('UTC', current_timestamp()) AS TIME)",
1634
+ # localdatetime(): now's wall clock in UTC, with no zone.
1635
+ 'LOCAL_NOW': "convert_timezone('UTC', current_timestamp())",
1636
+ # datetime(...) compared: the instant of a UTC text (ExprTranslator._compared).
1637
+ 'ZONED_OF_TEXT': "CAST({0} AS TIMESTAMP)",
1638
+ 'TIME_OF_TEXT': "CAST({0} AS TIME)",
1639
+ 'MAKE_TIMESTAMP': "make_timestamp_ntz({0}, {1}, {2}, {3}, {4}, {5})",
1640
+ # A part of a date or a local date-time. Spark's
1641
+ # own functions return INT; Cypher's integer is a BIGINT. SECOND of
1642
+ # a timestamp is a decimal with its fraction, which the sub-second
1643
+ # parts are cut from; each counts from the start of the second.
1644
+ 'YEAR': "CAST(YEAR({0}) AS BIGINT)",
1645
+ 'MONTH': "CAST(MONTH({0}) AS BIGINT)",
1646
+ 'DAY': "CAST(DAY({0}) AS BIGINT)",
1647
+ 'HOUR': "CAST(HOUR({0}) AS BIGINT)",
1648
+ 'MINUTE': "CAST(MINUTE({0}) AS BIGINT)",
1649
+ 'SECOND': "CAST(SECOND({0}) AS BIGINT)",
1650
+ 'MILLISECOND': "CAST((date_part('SECOND', {0}) % 1) * 1000 AS BIGINT)",
1651
+ 'MICROSECOND': "CAST((date_part('SECOND', {0}) % 1) * 1000000 AS BIGINT)",
1652
+ 'NANOSECOND': "CAST((date_part('SECOND', {0}) % 1) * 1000000000 AS BIGINT)",
1653
+ 'QUARTER': "CAST(QUARTER({0}) AS BIGINT)",
1654
+ # The ISO week and the ISO day of the week, Monday 1 (weekday()
1655
+ # counts Monday 0).
1656
+ 'WEEK': "CAST(WEEKOFYEAR({0}) AS BIGINT)",
1657
+ 'DAY_OF_WEEK': "CAST(WEEKDAY({0}) + 1 AS BIGINT)",
1658
+ 'ORDINAL_DAY': "CAST(DAYOFYEAR({0}) AS BIGINT)",
1659
+ # A string's length and a list's size (SPARQL STRLEN, Cypher
1660
+ # size()): Spark's return INT; an integer is a BIGINT, as on every
1661
+ # other engine. Spark's string and list functions
1662
+ # take a BIGINT length too, but element_at, whose index LIST_GET casts.
1663
+ 'LENGTH': "CAST(LENGTH({0}) AS BIGINT)",
1664
+ 'SIZE': "CAST(SIZE({0}) AS BIGINT)",
1665
+ # A date moved by a duration's months, then its days: add_months clamps to the month's end as Cypher does.
1666
+ 'DATE_PLUS': "date_add(add_months({0}, CAST({1} AS INT)), CAST({2} AS INT))",
1667
+ # A timestamp moved by months, days and microseconds in that order,
1668
+ # and the microseconds from one timestamp to another.
1669
+ 'TS_PLUS': "timestampadd(MICROSECOND, {3}, timestampadd(DAY, {2}, timestampadd(MONTH, {1}, {0})))",
1670
+ 'MICROS_BETWEEN': "timestampdiff(MICROSECOND, {0}, {1})",
1671
+ # Two edges differ (relationship uniqueness); see _different_edges.
1672
+ 'EDGE_DIFFERS': "(NOT ({0} = {2} AND {1} = {3}))",
1673
+ # The same with their row numbers.
1674
+ 'EDGE_ROW_DIFFERS': "(NOT ({0} = {3} AND {1} = {4} AND {2} = {5}))",
1675
+ # List equality, three-valued (ExprTranslator._list_equality): whether
1676
+ # some pair of elements at one position differs, and whether a list
1677
+ # holds a null.
1678
+ 'LIST_PAIR_DIFFERS': "exists(zip_with({0}, {1}, (_x, _y) -> _x <> _y), _d -> _d)",
1679
+ 'COLLECT_PAIRS': ("COALESCE(collect_list(struct({0} AS v, {1} AS i)) FILTER (WHERE {0} IS NOT NULL), array())"),
1680
+ 'PAIR_VALUES': "transform({0}, lv_p -> lv_p.v)",
1681
+ 'PAIR_KINDS': "transform({0}, lv_p -> lv_p.i)",
1682
+ 'PAIR': "struct({0} AS v, {1} AS i)",
1683
+ 'LIST_PAIR_ANY': "exists(zip_with({0}, {1}, ({2}, {3}) -> {4}), {2}_d -> {2}_d)",
1684
+ 'LIST_PAIR_BOTH_VALUES': "exists(zip_with({0}, {1}, (_x, _y) -> _x IS NOT NULL AND _y IS NOT NULL), _d -> _d)",
1685
+ 'LIST_HAS_NULL': "exists({0}, _x -> _x IS NULL)",
1686
+ # A list no element gives a type ([] or [null]), read as a list of
1687
+ # the other operand's elements (ExprTranslator._typed_list).
1688
+ 'LIST_OF_INTEGER': "{0}",
1689
+ 'LIST_OF_FLOAT': "{0}",
1690
+ 'LIST_OF_STRING': "{0}",
1691
+ 'LIST_OF_BOOLEAN': "{0}",
1692
+ # The key of a collected node's map, and a key to compare with it
1693
+ # (see ExprTranslator._node_equality).
1694
+ 'NODE_MAP_KEY': "{0}.`id`",
1695
+ 'AS_NODE_MAP_KEY': "{0}",
1696
+ # The labels of a collected node's map (ExprTranslator._labels).
1697
+ 'NODE_MAP_LABELS': "{0}.`labels`",
1698
+ # Two lists joined (Cypher's + on lists); None where there is no list.
1699
+ 'LIST_CONCAT': 'concat({0}, {1})',
1700
+ **_SPARK_DIVISION,
1701
+ 'LIST_FILTER': "filter({0}, {1} -> {2})",
1702
+ 'LIST_TRANSFORM': "transform({0}, {1} -> {2})",
1703
+ **_JAVA_REGEX,
1704
+ # Functions the base serializer spells the Spark way.
1705
+ 'PERCENTILE_DISC': 'percentile_disc({1}) WITHIN GROUP (ORDER BY {0})',
1706
+ 'PERCENTILE_CONT': 'percentile_cont({1}) WITHIN GROUP (ORDER BY {0})',
1707
+ # SPARQL GROUP_CONCAT: as for Spark.
1708
+ 'GROUP_CONCAT': "COALESCE(array_join(collect_list({0}), {1}), '')",
1709
+ 'GROUP_CONCAT_DISTINCT': "COALESCE(array_join(collect_set({0}), {1}), '')",
1710
+ # range()'s elements are BIGINTs: SEQUENCE of two INT literals is an
1711
+ # array of INT, so the start is cast.
1712
+ # Cypher's range() is empty when the step leads away from the end
1713
+ # (range(1, 0), range(1, 3, -1)). sequence() counts down when the
1714
+ # end is below the start, [1, 0], and raises on a step of the wrong
1715
+ # sign. The filter keeps all or nothing. Where it
1716
+ # keeps nothing the end given to sequence() is the start, so one
1717
+ # element is built and dropped: a countdown of the whole span would
1718
+ # be built first, and past the array limit that raises.
1719
+ 'SEQUENCE': ('filter(SEQUENCE(CAST({0} AS BIGINT), CASE WHEN ({0}) <= ({1}) THEN {1} ELSE {0} END), '
1720
+ '_rng -> ({0}) <= ({1}))'),
1721
+ # A step of 0 is an error in Cypher, raised here as the Python
1722
+ # path raises it; sequence() would have answered [a] for (a, a, 0).
1723
+ 'SEQUENCE_STEP': ('filter(SEQUENCE(CAST({0} AS BIGINT), CASE WHEN ({2}) = 0 THEN '
1724
+ "raise_error('NumberOutOfRange: step must not be 0 for range()') "
1725
+ 'WHEN (({2}) > 0 AND ({0}) <= ({1})) '
1726
+ 'OR (({2}) < 0 AND ({0}) >= ({1})) THEN {1} ELSE {0} END, {2}), '
1727
+ '_rng -> (({2}) > 0 AND ({0}) <= ({1})) '
1728
+ 'OR (({2}) < 0 AND ({0}) >= ({1})))'),
1729
+ 'LIST_REVERSE': 'REVERSE({0})',
1730
+ 'STR_REVERSE': 'REVERSE({0})',
1731
+ # String `+`: null if either side is null.
1732
+ 'STR_CONCAT': "({0} || {1})",
1733
+ 'MD5': "md5({0})",
1734
+ 'SHA1': "sha1({0})",
1735
+ 'SHA256': "sha2({0}, 256)",
1736
+ 'SHA384': "sha2({0}, 384)",
1737
+ 'SHA512': "sha2({0}, 512)",
1738
+ 'SPARQL_UUID': "CONCAT('urn:uuid:', uuid())",
1739
+ 'BNODE': "CONCAT('_:b', REPLACE(CAST(uuid() AS STRING), '-', ''))",
1740
+ 'RANDOMUUID': "uuid()",
1741
+ # url_encode is Java's URLEncoder: a space '+', '*' and '~' as the
1742
+ # form encoding has them; ENCODE_FOR_URI (SPARQL 1.1 17.4.3.11)
1743
+ # percent-encodes all but RFC 3986's unreserved characters. A '+'
1744
+ # it encodes is '%2B', so the replacements touch only these.
1745
+ 'ENCODE_FOR_URI': "replace(replace(replace(url_encode({0}), '+', '%20'), '*', '%2A'), "
1746
+ "'%7E', '~')",
1747
+ # collect() over no rows is [].
1748
+ 'EMPTY_LIST': "array()",
1749
+ # The index is cast: element_at takes an INT, and an index that is
1750
+ # a BIGINT (a column, a count, arithmetic on either) was text Spark
1751
+ # refused, DATATYPE_MISMATCH. TRY_CAST, so that an index past an
1752
+ # INT is no element (NULL) and not a wrapped position.
1753
+ 'LIST_GET': 'TRY_ELEMENT_AT({0}, TRY_CAST(CASE WHEN {1} >= 0 THEN {1} + 1 ELSE {1} END AS INT))',
1754
+ 'SPLIT': "SPLIT({0}, CONCAT('\\\\Q', {1}, '\\\\E'))",
1755
+ 'TO_FLOAT': 'TRY_CAST({0} AS DOUBLE)',
1756
+ # A DECIMAL's canonical text (`ExprTranslator._as_text`), its value
1757
+ # read once: written in full, where with ANSI mode off Spark casts one
1758
+ # below 10^-6 in scientific form (0E-18, 1.00000000000E-7,
1759
+ # BigDecimal.toString's), so that one is written from its digits;
1760
+ # then no trailing zero after the point, one digit kept. {1} is its
1761
+ # scale.
1762
+ 'DECIMAL_TEXT': ("transform(array(transform(array({0}), _dec -> CASE WHEN _dec = 0 THEN '0' "
1763
+ "WHEN abs(_dec) < 0.000001 THEN concat(if(_dec < 0, '-', ''), '0.', "
1764
+ "lpad(CAST(CAST(abs(_dec) * CAST(power(10, {1}) AS DECIMAL(38,0)) AS DECIMAL(38,0)) "
1765
+ "AS STRING), {1}, '0')) ELSE CAST(_dec AS STRING) END)[0]), _txt -> "
1766
+ "CASE WHEN instr(_txt, '.') > 0 THEN regexp_replace(regexp_replace(_txt, '0+$', ''), "
1767
+ "'\\\\.$', '.0') ELSE concat(_txt, '.0') END)[0]"),
1768
+ 'TO_INTEGER': 'CAST(CASE WHEN TRY_CAST({0} AS DOUBLE) >= 0 THEN FLOOR(TRY_CAST({0} AS DOUBLE)) ELSE CEIL(TRY_CAST({0} AS DOUBLE)) END AS BIGINT)',
1769
+ 'TO_BOOLEAN': "CASE LOWER(CAST({0} AS STRING)) WHEN 'true' THEN TRUE WHEN 'false' THEN FALSE END",
1770
+ # xsd:integer and xsd:dateTime of text already in the lexical
1771
+ # space (ExprTranslator._xsd_cast); a zoned dateTime is its
1772
+ # instant in UTC, held with no zone.
1773
+ 'XSD_TO_INTEGER': 'TRY_CAST({0} AS BIGINT)',
1774
+ # An integral double within the 64-bit range, exactly (Cypher's integer-against-float comparison).
1775
+ 'DOUBLE_TO_BIGINT': 'CAST({0} AS BIGINT)',
1776
+ 'XSD_TO_DECIMAL': 'TRY_CAST({0} AS DECIMAL(38,18))',
1777
+ 'XSD_TO_DECIMAL_SIZED': 'TRY_CAST({0} AS DECIMAL({1},{2}))',
1778
+ # A decimal + - * declared past 38 digits (D7, m2-functions.md 9): DECIMAL(38, {2}),
1779
+ # or NULL (unbound, an expression error) where the value needs more; {3} is 10^(38 - {2}).
1780
+ 'TRY_DECIMAL_ADD': 'try_cast(try_add({0}, {1}) AS DECIMAL(38,{2}))',
1781
+ 'TRY_DECIMAL_SUB': 'try_cast(try_subtract({0}, {1}) AS DECIMAL(38,{2}))',
1782
+ 'TRY_DECIMAL_MUL': 'try_cast(try_multiply({0}, {1}) AS DECIMAL(38,{2}))',
1783
+ # A decimal SUM, NULL where it passes 38 digits ({1} is 10^(38 - scale)); DuckDB's raises
1784
+ # (its SUM cannot be wrapped in TRY).
1785
+ 'TRY_DECIMAL_SUM': 'try_sum(CAST({0} AS DECIMAL(38,{2})))',
1786
+ # An operand of a decimal + - * (lower.exact_decimal): widened to 19 digits on DuckDB,
1787
+ # given its declared type on Databricks (no precision-loss setting there).
1788
+ 'DECIMAL_WIDENED': '{0}',
1789
+ 'DECIMAL_NARROWED': 'TRY_CAST({0} AS DECIMAL({1},{2}))',
1790
+ 'XSD_TO_DATETIME': "TIMESTAMPADD(MINUTE, -(CASE WHEN {0} RLIKE '[+-][0-9][0-9]:[0-9][0-9]$' THEN (CASE WHEN LEFT(RIGHT({0}, 6), 1) = '-' THEN -1 ELSE 1 END) * (CAST(LEFT(RIGHT({0}, 5), 2) AS INT) * 60 + CAST(RIGHT({0}, 2) AS INT)) ELSE 0 END), TRY_CAST(REGEXP_REPLACE({0}, '(Z|[+-][0-9][0-9]:[0-9][0-9])$', '') AS TIMESTAMP_NTZ))",
1791
+ 'XSD_TO_DATE': "TRY_CAST({0} AS DATE)",
1792
+ },
1793
+ )
1794
+
1795
+ DUCKDB_DIALECT = DialectConfig(
1796
+ float_text_is_javas='plain_range',
1797
+ non_finite_double="CAST('{0}' AS DOUBLE)",
1798
+ name='duckdb',
1799
+ decimal_result_held_in_18=True,
1800
+ # DuckDB's keywords a table name cannot be unquoted: duckdb_keywords()
1801
+ # WHERE keyword_category = 'reserved', and the
1802
+ # 'type_function' ones, which the grammar reads as a join's words or a
1803
+ # function's name (`FROM at AS a`, `JOIN left AS _r` were parser
1804
+ # errors); the 'column_name' ones too, which DuckDB
1805
+ # 1.5 reads as a table but its grammar keeps for a column's place.
1806
+ # Quoting a name on DuckDB does not change it: its quoted names match
1807
+ # without regard to case.
1808
+ words_a_table_name_quotes=frozenset((
1809
+ 'all', 'analyse', 'analyze', 'and', 'any', 'array', 'as', 'asc',
1810
+ 'asymmetric', 'both', 'case', 'cast', 'check', 'collate',
1811
+ 'column', 'constraint', 'create', 'default', 'deferrable',
1812
+ 'desc', 'describe', 'distinct', 'do', 'else', 'end', 'except',
1813
+ 'false', 'fetch', 'for', 'foreign', 'from', 'group', 'having',
1814
+ 'in', 'initially', 'intersect', 'into', 'lambda', 'lateral',
1815
+ 'leading', 'limit', 'not', 'null', 'offset', 'on', 'only', 'or',
1816
+ 'order', 'pivot', 'pivot_longer', 'pivot_wider', 'placing',
1817
+ 'primary', 'qualify', 'references', 'returning', 'select',
1818
+ 'show', 'some', 'summarize', 'symmetric', 'table', 'then', 'to',
1819
+ 'trailing', 'true', 'union', 'unique', 'unpivot', 'using',
1820
+ 'variadic', 'when', 'where', 'window', 'with',
1821
+ # type_function
1822
+ 'anti', 'asof', 'at', 'authorization', 'binary', 'by', 'collation',
1823
+ 'columns', 'concurrently', 'cross', 'freeze', 'full', 'generated',
1824
+ 'glob', 'ilike', 'inner', 'is', 'isnull', 'join', 'left', 'like',
1825
+ 'map', 'natural', 'notnull', 'outer', 'overlaps', 'positional',
1826
+ 'right', 'semi', 'similar', 'struct', 'tablesample', 'try_cast',
1827
+ 'unpack', 'verbose',
1828
+ # column_name
1829
+ 'between', 'bigint', 'bit', 'boolean', 'char', 'character',
1830
+ 'coalesce', 'dec', 'decimal', 'exists', 'extract', 'float',
1831
+ 'grouping', 'grouping_id', 'inout', 'int', 'integer', 'interval',
1832
+ 'national', 'nchar', 'none', 'nullif', 'numeric', 'out', 'overlay',
1833
+ 'position', 'precision', 'real', 'row', 'setof', 'smallint',
1834
+ 'substring', 'time', 'timestamp', 'treat', 'trim', 'values',
1835
+ 'varchar', 'xmlattributes', 'xmlconcat', 'xmlelement', 'xmlexists',
1836
+ 'xmlforest', 'xmlnamespaces', 'xmlparse', 'xmlpi', 'xmlroot',
1837
+ 'xmlserialize', 'xmltable',
1838
+ )),
1839
+ kinds_beside_a_list=True,
1840
+ database_name='DuckDB',
1841
+ array_literal='[{}]',
1842
+ quote_char='"',
1843
+ supports_anti_join=True,
1844
+ supports_semi_join=True,
1845
+ supports_recursive_cte=True,
1846
+ supports_recursive_union=True,
1847
+ param_style='?',
1848
+ current_date_parens=False,
1849
+ limit_style='standard',
1850
+ function_map={
1851
+ # SPARQL's string functions (m2-functions.md 3): code points, text comparisons (no LIKE),
1852
+ # REGEX over the pattern as this engine reads it (strings.py, xpath_regex.py), which is
1853
+ # the second argument (Java), the third (RE2) or the fourth (PostgreSQL's ARE);
1854
+ # None where not built.
1855
+ 'STR_STARTS': 'starts_with({0}, {1})',
1856
+ 'STR_ENDS': 'ends_with({0}, {1})',
1857
+ 'STR_CONTAINS': 'contains({0}, {1})',
1858
+ 'STR_POS': 'instr({0}, {1})',
1859
+ 'STR_LEN': 'length({0})',
1860
+ 'STR_UPPER': 'upper({0})',
1861
+ 'STR_LOWER': 'lower({0})',
1862
+ 'SUBSTR_CP': 'substring({0}, {1}, {2})',
1863
+ 'SUBSTR_FROM': 'substring({0}, {1})',
1864
+ 'REGEX_MATCH': 'regexp_matches({0}, {2})',
1865
+ # A native date's and dateTime's XSD text (m2-functions.md 2): years 1 to
1866
+ # 9999, an error outside them; None where not built.
1867
+ 'XSD_DATE_TEXT': "(CASE WHEN {0} IS NULL THEN NULL WHEN {0} BETWEEN DATE '0001-01-01' AND DATE '9999-12-31' THEN strftime({0}, '%Y-%m-%d') ELSE error('ProGraph: a date or dateTime outside years 1 to 9999 has no XSD text here') END)",
1868
+ 'XSD_DATETIME_TEXT': "(CASE WHEN {0} IS NULL THEN NULL WHEN CAST({0} AS TIMESTAMP) BETWEEN TIMESTAMP '0001-01-01 00:00:00' AND TIMESTAMP '9999-12-31 23:59:59.999999' THEN strftime({0}, '%Y-%m-%dT%H:%M:%S') || CASE WHEN strftime({0}, '%f') = '000000' THEN '' ELSE '.' || rtrim(strftime({0}, '%f'), '0') END ELSE error('ProGraph: a date or dateTime outside years 1 to 9999 has no XSD text here') END)",
1869
+ # A decimal's canonical text, XML Schema 1.1's, R2RML 10.2's edition ('5', '1.5'; D3).
1870
+ # DuckDB writes a DECIMAL(n, n) without its leading 0 ('-.5'): restored (long fuzz).
1871
+ 'XSD_DECIMAL_TEXT': "(CASE WHEN {0} = 0 THEN '0' WHEN NOT contains(regexp_replace(CAST({0} AS VARCHAR), '^(-?)[.]', '\\10.'), '.') THEN regexp_replace(CAST({0} AS VARCHAR), '^(-?)[.]', '\\10.') ELSE TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM regexp_replace(CAST({0} AS VARCHAR), '^(-?)[.]', '\\10.'))) END)",
1872
+ 'XSD_DOUBLE_TEXT': _double_text('VARCHAR', lambda s, x: f"regexp_extract({s}, '{x}', 1)"),
1873
+ # YEAR, MONTH, DAY, HOURS, MINUTES and SECONDS of a native DATE or local TIMESTAMP
1874
+ # (m2-functions.md 6): the field, SECONDS a DECIMAL(8, 6) with its fraction; None where not built.
1875
+ 'DT_YEAR': 'EXTRACT(YEAR FROM {0})',
1876
+ 'DT_MONTH': 'EXTRACT(MONTH FROM {0})',
1877
+ 'DT_DAY': 'EXTRACT(DAY FROM {0})',
1878
+ 'DT_HOUR': 'EXTRACT(HOUR FROM {0})',
1879
+ 'DT_MINUTE': 'EXTRACT(MINUTE FROM {0})',
1880
+ 'DT_SECOND': "CAST(CAST(date_part('microsecond', {0}) AS DECIMAL(18,0)) * CAST(0.000001 AS DECIMAL(7,6)) AS DECIMAL(8,6))",
1881
+ # A zoned date-time's instant as a date-time in UTC, for its text
1882
+ # (ExprTranslator._as_text).
1883
+ 'DATETIME_IN_UTC': "timezone('UTC', {0})",
1884
+ # A Cypher float literal is a float: a bare 2.0 is DECIMAL in SQL,
1885
+ # and 7 / 2.0 came back as a decimal.
1886
+ 'FLOAT_LITERAL': 'CAST({0} AS DOUBLE)',
1887
+ 'IS_NAN': 'isnan({0})',
1888
+ **_DUCKDB_EXACT_QUOTIENT,
1889
+ # An empty property map (decision D8).
1890
+ 'EMPTY_MAP': "MAP {{}}",
1891
+ # List comprehension and quantifiers: {0} the list, {1} the loop
1892
+ # variable, {2} the body.
1893
+ # Integer division, truncated toward zero as Cypher's is.
1894
+ # Temporal constructors from components; None refuses.
1895
+ 'MAKE_DATE': "make_date({0}, {1}, {2})",
1896
+ 'MAKE_TIME': "make_time({0}, {1}, {2})",
1897
+ 'TIME_NOW': "CAST(timezone('UTC', CURRENT_TIMESTAMP) AS TIME)",
1898
+ # localdatetime(): now's wall clock in UTC, with no zone.
1899
+ 'LOCAL_NOW': "timezone('UTC', CURRENT_TIMESTAMP)",
1900
+ # datetime(...) compared: the instant of a UTC text (ExprTranslator._compared).
1901
+ 'ZONED_OF_TEXT': "CAST({0} AS TIMESTAMPTZ)",
1902
+ 'TIME_OF_TEXT': "CAST({0} AS TIME)",
1903
+ 'MAKE_TIMESTAMP': "make_timestamp({0}, {1}, {2}, {3}, {4}, {5})",
1904
+ # A part of a date or a local date-time.
1905
+ # microsecond() and millisecond() count the whole seconds in.
1906
+ 'MILLISECOND': "(millisecond({0}) % 1000)",
1907
+ 'MICROSECOND': "(microsecond({0}) % 1000000)",
1908
+ 'NANOSECOND': "((microsecond({0}) % 1000000) * 1000)",
1909
+ 'QUARTER': "quarter({0})",
1910
+ 'WEEK': "weekofyear({0})",
1911
+ 'DAY_OF_WEEK': "isodow({0})",
1912
+ 'ORDINAL_DAY': "dayofyear({0})",
1913
+ # A date moved by a duration's months, then its days; a date plus an interval is a timestamp here, plus an
1914
+ # integer a date. The months clamp to the month's end as Cypher's.
1915
+ 'DATE_PLUS': "(CAST(({0}) + to_months(CAST(({1}) AS INTEGER)) AS DATE) + CAST(({2}) AS INTEGER))",
1916
+ # A timestamp moved by months, days and microseconds in that order,
1917
+ # and the microseconds from one timestamp to another.
1918
+ 'TS_PLUS': "((({0}) + to_months(CAST(({1}) AS INTEGER))) + to_days(CAST(({2}) AS INTEGER)) + to_microseconds(CAST(({3}) AS BIGINT)))",
1919
+ 'MICROS_BETWEEN': "(epoch_us({1}) - epoch_us({0}))",
1920
+ # Two edges differ (relationship uniqueness); see _different_edges.
1921
+ 'EDGE_DIFFERS': "(({0}, {1}) <> ({2}, {3}))",
1922
+ # The same with their row numbers.
1923
+ 'EDGE_ROW_DIFFERS': "(({0}, {1}, {2}) <> ({3}, {4}, {5}))",
1924
+ # List equality, three-valued (ExprTranslator._list_equality): whether
1925
+ # some pair of elements at one position differs, and whether a list
1926
+ # holds a null.
1927
+ 'LIST_PAIR_DIFFERS': "list_contains(list_transform({0}, lambda _x, _i: _x <> ({1})[_i]), TRUE)",
1928
+ 'COLLECT_PAIRS': ("COALESCE(list({{'v': {0}, 'i': {1}}}) FILTER (WHERE {0} IS NOT NULL), [])"),
1929
+ 'PAIR_VALUES': "list_transform({0}, lambda lv_p: lv_p.v)",
1930
+ 'PAIR_KINDS': "list_transform({0}, lambda lv_p: lv_p.i)",
1931
+ 'PAIR': "{{'v': {0}, 'i': {1}}}",
1932
+ 'LIST_PAIR_ANY': ("list_contains(list_transform({0}, lambda {2}, {2}_i: "
1933
+ "list_transform([({1})[{2}_i]], lambda {3}: {4})[1]), TRUE)"),
1934
+ 'LIST_PAIR_BOTH_VALUES': "list_contains(list_transform({0}, lambda _x, _i: _x IS NOT NULL AND ({1})[_i] IS NOT NULL), TRUE)",
1935
+ 'LIST_HAS_NULL': "(len(list_filter({0}, lambda _x: _x IS NULL)) > 0)",
1936
+ # A list no element gives a type ([] or [null]), read as a list of
1937
+ # the other operand's elements (ExprTranslator._typed_list).
1938
+ 'LIST_OF_INTEGER': "{0}",
1939
+ 'LIST_OF_FLOAT': "{0}",
1940
+ 'LIST_OF_STRING': "{0}",
1941
+ 'LIST_OF_BOOLEAN': "{0}",
1942
+ # The key of a collected node's map, and a key to compare with it
1943
+ # (see ExprTranslator._node_equality).
1944
+ 'NODE_MAP_KEY': '({0})."id"',
1945
+ 'AS_NODE_MAP_KEY': "{0}",
1946
+ # The labels of a collected node's map (ExprTranslator._labels).
1947
+ 'NODE_MAP_LABELS': '({0})."labels"',
1948
+ # Two lists joined (Cypher's + on lists); None where there is no list.
1949
+ 'LIST_CONCAT': 'list_concat({0}, {1})',
1950
+ # Two integers divided, truncated toward zero. `//` divides them
1951
+ # as integers (and truncates, -7 // 2 is -3); `/` divided doubles,
1952
+ # which hold 53 bits: 9223372036854775807 / 2 came back ...904.
1953
+ # `//` by zero is NULL, where the division of doubles raised on its
1954
+ # cast and a zero divisor is an error, so it is raised here.
1955
+ 'INT_DIV': "CAST(" + _zero_divisor('error', '({0}) // ({1})') + " AS BIGINT)",
1956
+ # `%` by zero is NULL there too. The smallest integer % -1 is 0,
1957
+ # and DuckDB raises an overflow for it.
1958
+ 'INT_MOD': "CAST(" + _zero_divisor(
1959
+ 'error', 'CASE WHEN ({1}) = -1 THEN ({0}) * 0 ELSE ({0}) % ({1}) END') + " AS BIGINT)",
1960
+ 'INT_QUOTIENT': "CAST(({0}) // ({1}) AS BIGINT)",
1961
+ # A divisor written as -1 overflows there as a column's does.
1962
+ 'INT_REMAINDER': "CAST(CASE WHEN ({1}) = -1 THEN ({0}) * 0 ELSE ({0}) % ({1}) END AS BIGINT)",
1963
+ 'FLOAT_DIV': _PLAIN_DIVISION['FLOAT_DIV'],
1964
+ 'FLOAT_MOD': _PLAIN_DIVISION['FLOAT_MOD'],
1965
+ 'FLOAT_DIV_ONCE': _PLAIN_DIVISION['FLOAT_DIV'],
1966
+ 'FLOAT_MOD_ONCE': _PLAIN_DIVISION['FLOAT_MOD'],
1967
+ **_once({'INT_DIV': "CAST(" + _zero_divisor('error', '({0}) // ({1})') + " AS BIGINT)",
1968
+ 'INT_MOD': "CAST(" + _zero_divisor(
1969
+ 'error', 'CASE WHEN ({1}) = -1 THEN ({0}) * 0 ELSE ({0}) % ({1}) END') + " AS BIGINT)"},
1970
+ "list_transform([{{'n': {0}, 'd': {1}}}], lambda _p: BODY)[1]"),
1971
+ 'LIST_FILTER': "list_filter({0}, lambda {1}: {2})",
1972
+ 'LIST_TRANSFORM': "list_transform({0}, lambda {1}: {2})",
1973
+ # =~ matches the whole string.
1974
+ 'REGEX_FULL': "regexp_full_match({0}, {1})",
1975
+ # Functions the base serializer spells the Spark way.
1976
+ # Neo4j's discrete percentile: element ceil(p * n) of the sorted non-null values (the first for
1977
+ # p = 0), computed in doubles as PostgreSQL and Spark do; DuckDB's quantile_disc picks another
1978
+ # element where p * n is within an ulp of an integer (n = 100, p = 0.55). None for no value.
1979
+ 'PERCENTILE_DISC': ("list_sort(list({0}) FILTER (WHERE {0} IS NOT NULL))"
1980
+ "[greatest(CAST(ceil(({1}) * count({0})) AS BIGINT), 1)]"),
1981
+ 'PERCENTILE_CONT': 'percentile_cont({1}) WITHIN GROUP (ORDER BY {0})',
1982
+ # SPARQL GROUP_CONCAT: as for PostgreSQL.
1983
+ 'GROUP_CONCAT': "COALESCE(string_agg({0}, {1}), '')",
1984
+ 'GROUP_CONCAT_DISTINCT': "COALESCE(string_agg(DISTINCT {0}, {1}), '')",
1985
+ 'SEQUENCE': 'generate_series({0}, {1})',
1986
+ # A step of 0 is an error in Cypher; generate_series answers an
1987
+ # empty list for it. PostgreSQL's raises by itself.
1988
+ 'SEQUENCE_STEP': ("CASE WHEN ({2}) = 0 THEN "
1989
+ "error('NumberOutOfRange: step must not be 0 for range()') "
1990
+ "ELSE generate_series({0}, {1}, {2}) END"),
1991
+ 'SLICE': 'list_slice({0}, {1}, {1} + {2} - 1)',
1992
+ 'LIST_REVERSE': 'list_reverse({0})',
1993
+ # Cypher reverses a string's code points, DuckDB's reverse() its grapheme clusters
1994
+ # (rule string-functions: code points); string_split on '' splits code points.
1995
+ 'STR_REVERSE': "array_to_string(list_reverse(string_split({0}, '')), '')",
1996
+ 'E': 'exp(1)',
1997
+ 'RAND': 'random()',
1998
+ 'SQRT': "CASE WHEN {0} < 0 THEN CAST('NaN' AS DOUBLE) ELSE SQRT({0}) END",
1999
+ # String `+`: null if either side is null.
2000
+ 'STR_CONCAT': "({0} || {1})",
2001
+ # SPARQL REGEX searches; DuckDB's `~` is a full match.
2002
+ 'REGEX_SEARCH': "regexp_matches({0}, {1})",
2003
+ 'REGEX_REPLACE': "regexp_replace({0}, {1}, regexp_replace({2}, '\\$([0-9])', '\\\\\\1', 'g'), 'g')",
2004
+ 'REGEX_REPLACE_I': "regexp_replace({0}, {1}, regexp_replace({2}, '\\$([0-9])', '\\\\\\1', 'g'), 'gi')",
2005
+ 'MD5': "md5({0})",
2006
+ 'SHA1': "sha1({0})",
2007
+ 'SHA256': "sha256({0})",
2008
+ 'SHA384': None,
2009
+ 'SHA512': None,
2010
+ 'SPARQL_UUID': "('urn:uuid:' || CAST(uuid() AS VARCHAR))",
2011
+ 'BNODE': "('_:b' || REPLACE(CAST(uuid() AS VARCHAR), '-', ''))",
2012
+ 'RANDOMUUID': "CAST(uuid() AS VARCHAR)",
2013
+ 'ENCODE_FOR_URI': "url_encode({0})",
2014
+ # collect() over no rows is [].
2015
+ 'EMPTY_LIST': "[]",
2016
+ 'LIST_GET': 'list_extract({0}, CASE WHEN {1} >= 0 THEN {1} + 1 ELSE {1} END)',
2017
+ 'SPLIT': 'string_split({0}, {1})',
2018
+ 'TO_FLOAT': 'TRY_CAST({0} AS DOUBLE)',
2019
+ 'TO_INTEGER': 'CAST(TRUNC(TRY_CAST({0} AS DOUBLE)) AS BIGINT)',
2020
+ 'TO_BOOLEAN': "CASE LOWER(CAST({0} AS VARCHAR)) WHEN 'true' THEN TRUE WHEN 'false' THEN FALSE END",
2021
+ # xsd:integer and xsd:dateTime of text already in the lexical
2022
+ # space (ExprTranslator._xsd_cast); a zoned dateTime is its
2023
+ # instant in UTC, held with no zone.
2024
+ 'XSD_TO_INTEGER': 'TRY_CAST({0} AS BIGINT)',
2025
+ # An integral double within the 64-bit range, exactly (Cypher's integer-against-float comparison).
2026
+ 'DOUBLE_TO_BIGINT': 'CAST({0} AS BIGINT)',
2027
+ 'XSD_TO_DECIMAL': 'TRY_CAST({0} AS DECIMAL(38,18))',
2028
+ 'XSD_TO_DECIMAL_SIZED': 'TRY_CAST({0} AS DECIMAL({1},{2}))',
2029
+ # A decimal + - * declared past 38 digits (D7, m2-functions.md 9): DECIMAL(38, {2}),
2030
+ # or NULL (unbound, an expression error) where the value needs more; {3} is 10^(38 - {2}).
2031
+ 'TRY_DECIMAL_ADD': 'TRY(CAST(({0}) + ({1}) AS DECIMAL(38,{2})))',
2032
+ 'TRY_DECIMAL_SUB': 'TRY(CAST(({0}) - ({1}) AS DECIMAL(38,{2})))',
2033
+ 'TRY_DECIMAL_MUL': 'TRY(CAST(({0}) * ({1}) AS DECIMAL(38,{2})))',
2034
+ # A decimal SUM, NULL where it passes 38 digits ({1} is 10^(38 - scale), D7). DuckDB's SUM over
2035
+ # a DECIMAL past 18 digits is an int128 that raises past it on some plans and wraps silently on
2036
+ # others (a SUM over a DISTINCT: two 10^20 - 10^-18 summed to -1.4E20). The exact sum is kept
2037
+ # only where its whole part, as a HUGEINT, is within {1}, and it agrees with a DOUBLE sum of the
2038
+ # rows to within 1e-9 of the rows' absolute sum, plus 1: rows that cancel (1.5E18 + 2.5E18 - 4E18)
2039
+ # keep their exact sum, the double's error being far inside that. A wrap moves the sum by
2040
+ # 2^128 10^-s (3.4E38 at s = 0, 3.4E20 at s = 18), and stays outside the tolerance unless the
2041
+ # rows' absolute sum passes 3.4E47 10^-s, which, every row below 10^(38 - s), takes more than
2042
+ # 3.4E9 rows each near the 38-digit limit. Where the plan raises, it still raises (no TRY
2043
+ # around SUM).
2044
+ 'TRY_DECIMAL_SUM': ("(CASE WHEN ABS(CAST(SUM({0}) AS DOUBLE) - SUM(CAST({0} AS DOUBLE))) "
2045
+ "<= 1e-9 * SUM(ABS(CAST({0} AS DOUBLE))) + 1 "
2046
+ "AND CAST(FLOOR(SUM({0})) AS HUGEINT) < CAST('{1}' AS HUGEINT) "
2047
+ "AND CAST(CEIL(SUM({0})) AS HUGEINT) > -CAST('{1}' AS HUGEINT) THEN SUM({0}) END)"),
2048
+ # An operand of a decimal + - * (lower.exact_decimal): widened to 19 digits on DuckDB,
2049
+ # given its declared type on Databricks (no precision-loss setting there).
2050
+ 'DECIMAL_WIDENED': 'CAST({0} AS DECIMAL({1},{2}))',
2051
+ 'DECIMAL_NARROWED': '{0}',
2052
+ 'XSD_TO_DATETIME': "(TRY_CAST(regexp_replace({0}, '(Z|[+-][0-9][0-9]:[0-9][0-9])$', '') AS TIMESTAMP) - to_minutes((CASE WHEN regexp_matches({0}, '[+-][0-9][0-9]:[0-9][0-9]$') THEN (CASE WHEN LEFT(RIGHT({0}, 6), 1) = '-' THEN -1 ELSE 1 END) * (CAST(LEFT(RIGHT({0}, 5), 2) AS INT) * 60 + CAST(RIGHT({0}, 2) AS INT)) ELSE 0 END)))",
2053
+ 'XSD_TO_DATE': "TRY_CAST({0} AS DATE)",
2054
+ 'INSTR': 'STRPOS',
2055
+ },
2056
+ type_map={
2057
+ 'STRING': 'VARCHAR',
2058
+ 'LONG': 'BIGINT',
2059
+ },
2060
+ )
2061
+
2062
+ SQLSERVER_DIALECT = DialectConfig(
2063
+ name='sqlserver',
2064
+ database_name='SQL Server',
2065
+ max_identifier_length=128,
2066
+ timestamp_type='DATETIME2',
2067
+ # WITH, not WITH RECURSIVE; bounded paths do not recurse at all.
2068
+ supports_recursive_cte=True,
2069
+ quote_char='[',
2070
+ current_date_parens=False,
2071
+ limit_style='fetch',
2072
+ has_boolean_type=False,
2073
+ param_style='?',
2074
+ function_map={
2075
+ # SPARQL's string functions (m2-functions.md 3): code points, text comparisons (no LIKE),
2076
+ # REGEX over the pattern as this engine reads it (strings.py, xpath_regex.py), which is
2077
+ # the second argument (Java), the third (RE2) or the fourth (PostgreSQL's ARE);
2078
+ # None where not built.
2079
+ 'STR_STARTS': None,
2080
+ 'STR_ENDS': None,
2081
+ 'STR_CONTAINS': None,
2082
+ 'STR_POS': None,
2083
+ 'STR_LEN': None,
2084
+ 'STR_UPPER': None,
2085
+ 'STR_LOWER': None,
2086
+ 'SUBSTR_CP': None,
2087
+ 'SUBSTR_FROM': None,
2088
+ 'REGEX_MATCH': None,
2089
+ # A native date's and dateTime's XSD text (m2-functions.md 2): years 1 to
2090
+ # 9999, an error outside them; None where not built.
2091
+ 'XSD_DATE_TEXT': 'CONVERT(VARCHAR(10), {0}, 23)',
2092
+ 'XSD_DATETIME_TEXT': None,
2093
+ # A decimal's canonical text, XML Schema 1.1's, R2RML 10.2's edition ('5', '1.5'; D3).
2094
+ 'XSD_DECIMAL_TEXT': None,
2095
+ 'XSD_DOUBLE_TEXT': None,
2096
+ # YEAR, MONTH, DAY, HOURS, MINUTES and SECONDS of a native DATE or local TIMESTAMP
2097
+ # (m2-functions.md 6): the field, SECONDS a DECIMAL(8, 6) with its fraction; None where not built.
2098
+ 'DT_YEAR': None,
2099
+ 'DT_MONTH': None,
2100
+ 'DT_DAY': None,
2101
+ 'DT_HOUR': None,
2102
+ 'DT_MINUTE': None,
2103
+ 'DT_SECOND': None,
2104
+ # A zoned date-time's instant as a date-time in UTC, for its text
2105
+ # (ExprTranslator._as_text). No column type of
2106
+ # this database is read as zoned (types.of_sql_type).
2107
+ 'DATETIME_IN_UTC': None,
2108
+ # A Cypher float literal is a float: a bare 2.0 is DECIMAL in SQL,
2109
+ # and 7 / 2.0 came back as a decimal.
2110
+ 'FLOAT_LITERAL': 'CAST({0} AS FLOAT)',
2111
+ 'IS_NAN': '(1 = 0)',
2112
+ # An empty property map (decision D8).
2113
+ 'EMPTY_MAP': None,
2114
+ # No arrays: list comprehension and quantifiers refuse.
2115
+ # Integer division, truncated toward zero as Cypher's is.
2116
+ # Temporal constructors from components; None refuses.
2117
+ 'MAKE_DATE': "DATEFROMPARTS({0}, {1}, {2})",
2118
+ 'MAKE_TIME': None,
2119
+ 'TIME_NOW': None,
2120
+ # localdatetime(): now's wall clock in UTC, with no zone.
2121
+ 'LOCAL_NOW': 'SYSUTCDATETIME()',
2122
+ # datetime(...) compared: the instant of a UTC text (ExprTranslator._compared).
2123
+ 'ZONED_OF_TEXT': None,
2124
+ 'TIME_OF_TEXT': None,
2125
+ 'MAKE_TIMESTAMP': None,
2126
+ # A part of a date or a local date-time past the six SQL names:
2127
+ # not written for this database yet.
2128
+ 'MILLISECOND': None,
2129
+ 'MICROSECOND': None,
2130
+ 'NANOSECOND': None,
2131
+ 'QUARTER': None,
2132
+ 'WEEK': None,
2133
+ 'DAY_OF_WEEK': None,
2134
+ 'ORDINAL_DAY': None,
2135
+ 'DATE_PLUS': None,
2136
+ 'TS_PLUS': None,
2137
+ 'MICROS_BETWEEN': None,
2138
+ # Two edges differ (relationship uniqueness); see _different_edges.
2139
+ 'EDGE_DIFFERS': "(NOT ({0} = {2} AND {1} = {3}))",
2140
+ # The same with their row numbers.
2141
+ 'EDGE_ROW_DIFFERS': "(NOT ({0} = {3} AND {1} = {4} AND {2} = {5}))",
2142
+ # List equality, three-valued (ExprTranslator._list_equality): whether
2143
+ # some pair of elements at one position differs, and whether a list
2144
+ # holds a null.
2145
+ 'LIST_PAIR_DIFFERS': None,
2146
+ 'COLLECT_PAIRS': None,
2147
+ 'PAIR_VALUES': None,
2148
+ 'PAIR_KINDS': None,
2149
+ 'PAIR': None,
2150
+ 'LIST_PAIR_ANY': None,
2151
+ 'LIST_PAIR_BOTH_VALUES': None,
2152
+ 'LIST_HAS_NULL': None,
2153
+ # A list no element gives a type ([] or [null]), read as a list of
2154
+ # the other operand's elements (ExprTranslator._typed_list).
2155
+ 'LIST_OF_INTEGER': "{0}",
2156
+ 'LIST_OF_FLOAT': "{0}",
2157
+ 'LIST_OF_STRING': "{0}",
2158
+ 'LIST_OF_BOOLEAN': "{0}",
2159
+ # The key of a collected node's map, and a key to compare with it
2160
+ # (see ExprTranslator._node_equality).
2161
+ 'NODE_MAP_KEY': None,
2162
+ 'AS_NODE_MAP_KEY': None,
2163
+ # The labels of a collected node's map (ExprTranslator._labels).
2164
+ 'NODE_MAP_LABELS': None,
2165
+ # Two lists joined (Cypher's + on lists); None where there is no list.
2166
+ 'LIST_CONCAT': None,
2167
+ 'INT_DIV': "(({0}) / ({1}))",
2168
+ 'INT_QUOTIENT': "(({0}) / ({1}))",
2169
+ 'INT_DIV_ONCE': "(({0}) / ({1}))",
2170
+ **_PLAIN_DIVISION,
2171
+ 'LIST_FILTER': None,
2172
+ 'LIST_TRANSFORM': None,
2173
+ # =~ matches the whole string.
2174
+ 'REGEX_FULL': None,
2175
+ # No list or map type.
2176
+ 'SIZE': None,
2177
+ 'ARRAY': None,
2178
+ 'COLLECT_LIST': None,
2179
+ 'COLLECT_SET': None,
2180
+ 'NAMED_STRUCT': None,
2181
+ # LEN ignores trailing spaces; size(' a ') is 3.
2182
+ 'LENGTH': "(LEN({0} + N'x') - 1)",
2183
+ # Functions the base serializer spells the Spark way.
2184
+ 'PERCENTILE_DISC': None,
2185
+ 'PERCENTILE_CONT': None,
2186
+ # SPARQL GROUP_CONCAT: STRING_AGG has no DISTINCT; held with SQL Server.
2187
+ 'GROUP_CONCAT': None,
2188
+ 'GROUP_CONCAT_DISTINCT': None,
2189
+ 'SEQUENCE': None,
2190
+ 'SEQUENCE_STEP': None,
2191
+ 'SLICE': None,
2192
+ 'LIST_REVERSE': None,
2193
+ 'STR_REVERSE': None,
2194
+ 'E': 'EXP(1)',
2195
+ # String `+`: null if either side is null.
2196
+ 'STR_CONCAT': "({0} + {1})",
2197
+ 'REGEX_REPLACE': None,
2198
+ 'REGEX_REPLACE_I': None,
2199
+ 'MD5': None,
2200
+ 'SHA1': None,
2201
+ 'SHA256': None,
2202
+ 'SHA384': None,
2203
+ 'SHA512': None,
2204
+ 'SPARQL_UUID': None,
2205
+ 'BNODE': None,
2206
+ 'ENCODE_FOR_URI': None,
2207
+ # collect() over no rows is [].
2208
+ 'EMPTY_LIST': None,
2209
+ 'LIST_GET': None,
2210
+ 'ELEMENT_AT': None,
2211
+ 'SPLIT': None,
2212
+ 'TO_FLOAT': 'TRY_CAST({0} AS FLOAT)',
2213
+ 'TO_INTEGER': 'CAST(ROUND(TRY_CAST({0} AS FLOAT), 0, 1) AS BIGINT)',
2214
+ 'TO_BOOLEAN': "CASE LOWER(CAST({0} AS NVARCHAR(MAX))) WHEN 'true' THEN CAST(1 AS BIT) WHEN 'false' THEN CAST(0 AS BIT) END",
2215
+ # xsd:T(?v) of text: written for DuckDB, Spark,
2216
+ # Databricks and PostgreSQL.
2217
+ 'XSD_TO_INTEGER': None,
2218
+ 'DOUBLE_TO_BIGINT': None,
2219
+ 'XSD_TO_DECIMAL': 'TRY_CAST({0} AS DECIMAL(38,18))',
2220
+ 'XSD_TO_DECIMAL_SIZED': 'TRY_CAST({0} AS DECIMAL({1},{2}))',
2221
+ 'XSD_TO_DATETIME': None,
2222
+ 'XSD_TO_DATE': None,
2223
+ 'INSTR': 'CHARINDEX({1}, {0})',
2224
+ },
2225
+ type_map={
2226
+ 'LONG': 'BIGINT',
2227
+ 'STRING': 'NVARCHAR(MAX)',
2228
+ 'DOUBLE': 'FLOAT',
2229
+ 'BOOLEAN': 'BIT',
2230
+ },
2231
+ )
2232
+
2233
+ MYSQL_DIALECT = DialectConfig(
2234
+ name='mysql',
2235
+ database_name='MySQL',
2236
+ max_identifier_length=256,
2237
+ timestamp_type='DATETIME',
2238
+ string_escape='mysql',
2239
+ quote_char='`',
2240
+ supports_recursive_cte=True, # MySQL 8.0+
2241
+ supports_recursive_union=True,
2242
+ current_date_parens=True,
2243
+ limit_style='standard',
2244
+ has_boolean_type=True,
2245
+ function_map={
2246
+ # SPARQL's string functions (m2-functions.md 3): code points, text comparisons (no LIKE),
2247
+ # REGEX over the pattern as this engine reads it (strings.py, xpath_regex.py), which is
2248
+ # the second argument (Java), the third (RE2) or the fourth (PostgreSQL's ARE);
2249
+ # None where not built.
2250
+ 'STR_STARTS': None,
2251
+ 'STR_ENDS': None,
2252
+ 'STR_CONTAINS': None,
2253
+ 'STR_POS': None,
2254
+ 'STR_LEN': None,
2255
+ 'STR_UPPER': None,
2256
+ 'STR_LOWER': None,
2257
+ 'SUBSTR_CP': None,
2258
+ 'SUBSTR_FROM': None,
2259
+ 'REGEX_MATCH': None,
2260
+ # A native date's and dateTime's XSD text (m2-functions.md 2): years 1 to
2261
+ # 9999, an error outside them; None where not built.
2262
+ 'XSD_DATE_TEXT': None,
2263
+ 'XSD_DATETIME_TEXT': None,
2264
+ # A decimal's canonical text, XML Schema 1.1's, R2RML 10.2's edition ('5', '1.5'; D3).
2265
+ 'XSD_DECIMAL_TEXT': None,
2266
+ 'XSD_DOUBLE_TEXT': None,
2267
+ # YEAR, MONTH, DAY, HOURS, MINUTES and SECONDS of a native DATE or local TIMESTAMP
2268
+ # (m2-functions.md 6): the field, SECONDS a DECIMAL(8, 6) with its fraction; None where not built.
2269
+ 'DT_YEAR': None,
2270
+ 'DT_MONTH': None,
2271
+ 'DT_DAY': None,
2272
+ 'DT_HOUR': None,
2273
+ 'DT_MINUTE': None,
2274
+ 'DT_SECOND': None,
2275
+ # A zoned date-time's instant as a date-time in UTC, for its text
2276
+ # (ExprTranslator._as_text). No column type of
2277
+ # this database is read as zoned (types.of_sql_type).
2278
+ 'DATETIME_IN_UTC': None,
2279
+ # No array type: a list written in the query has no form here, and
2280
+ # `ARRAY[...]` was sent to the database (ORA-00923).
2281
+ 'ARRAY': None,
2282
+ # A Cypher float literal is a float: a bare 2.0 is DECIMAL in SQL,
2283
+ # and 7 / 2.0 came back as a decimal.
2284
+ 'FLOAT_LITERAL': 'CAST({0} AS DOUBLE)',
2285
+ 'IS_NAN': '(1 = 0)',
2286
+ # No map type: a map, a node or a relationship value refuses (D8).
2287
+ 'NAMED_STRUCT': None,
2288
+ # An empty property map (decision D8).
2289
+ 'EMPTY_MAP': None,
2290
+ # No arrays: list comprehension and quantifiers refuse.
2291
+ # Integer division, truncated toward zero as Cypher's is.
2292
+ # Temporal constructors from components; None refuses.
2293
+ 'MAKE_DATE': None,
2294
+ 'MAKE_TIME': None,
2295
+ 'TIME_NOW': None,
2296
+ # localdatetime(): now's wall clock in UTC, with no zone.
2297
+ 'LOCAL_NOW': 'UTC_TIMESTAMP(6)',
2298
+ # datetime(...) compared: the instant of a UTC text (ExprTranslator._compared).
2299
+ 'ZONED_OF_TEXT': None,
2300
+ 'TIME_OF_TEXT': None,
2301
+ 'MAKE_TIMESTAMP': None,
2302
+ # A part of a date or a local date-time past the six SQL names:
2303
+ # not written for this database yet.
2304
+ 'MILLISECOND': None,
2305
+ 'MICROSECOND': None,
2306
+ 'NANOSECOND': None,
2307
+ 'QUARTER': None,
2308
+ 'WEEK': None,
2309
+ 'DAY_OF_WEEK': None,
2310
+ 'ORDINAL_DAY': None,
2311
+ 'DATE_PLUS': None,
2312
+ 'TS_PLUS': None,
2313
+ 'MICROS_BETWEEN': None,
2314
+ # Two edges differ (relationship uniqueness); see _different_edges.
2315
+ 'EDGE_DIFFERS': "(NOT ({0} = {2} AND {1} = {3}))",
2316
+ # The same with their row numbers.
2317
+ 'EDGE_ROW_DIFFERS': "(NOT ({0} = {3} AND {1} = {4} AND {2} = {5}))",
2318
+ # List equality, three-valued (ExprTranslator._list_equality): whether
2319
+ # some pair of elements at one position differs, and whether a list
2320
+ # holds a null.
2321
+ 'LIST_PAIR_DIFFERS': None,
2322
+ 'COLLECT_PAIRS': None,
2323
+ 'PAIR_VALUES': None,
2324
+ 'PAIR_KINDS': None,
2325
+ 'PAIR': None,
2326
+ 'LIST_PAIR_ANY': None,
2327
+ 'LIST_PAIR_BOTH_VALUES': None,
2328
+ 'LIST_HAS_NULL': None,
2329
+ # A list no element gives a type ([] or [null]), read as a list of
2330
+ # the other operand's elements (ExprTranslator._typed_list).
2331
+ 'LIST_OF_INTEGER': "{0}",
2332
+ 'LIST_OF_FLOAT': "{0}",
2333
+ 'LIST_OF_STRING': "{0}",
2334
+ 'LIST_OF_BOOLEAN': "{0}",
2335
+ # The key of a collected node's map, and a key to compare with it
2336
+ # (see ExprTranslator._node_equality).
2337
+ 'NODE_MAP_KEY': None,
2338
+ 'AS_NODE_MAP_KEY': None,
2339
+ # The labels of a collected node's map (ExprTranslator._labels).
2340
+ 'NODE_MAP_LABELS': None,
2341
+ # Two lists joined (Cypher's + on lists); None where there is no list.
2342
+ 'LIST_CONCAT': None,
2343
+ 'INT_DIV': "(({0}) DIV ({1}))",
2344
+ 'INT_QUOTIENT': "(({0}) DIV ({1}))",
2345
+ 'INT_DIV_ONCE': "(({0}) DIV ({1}))",
2346
+ **_PLAIN_DIVISION,
2347
+ 'LIST_FILTER': None,
2348
+ 'LIST_TRANSFORM': None,
2349
+ # =~ matches the whole string.
2350
+ 'REGEX_FULL': "REGEXP_LIKE({0}, CONCAT('^(?:', {1}, ')$'))",
2351
+ # Functions the base serializer spells the Spark way.
2352
+ 'PERCENTILE_DISC': None,
2353
+ 'PERCENTILE_CONT': None,
2354
+ # SPARQL GROUP_CONCAT: it cuts a result at group_concat_max_len (1024)
2355
+ # without an error; held with MySQL.
2356
+ 'GROUP_CONCAT': None,
2357
+ 'GROUP_CONCAT_DISTINCT': None,
2358
+ 'SEQUENCE': None,
2359
+ 'SEQUENCE_STEP': None,
2360
+ 'SLICE': None,
2361
+ 'LIST_REVERSE': None,
2362
+ 'STR_REVERSE': None,
2363
+ 'E': 'EXP(1)',
2364
+ # String `+`: null if either side is null.
2365
+ 'STR_CONCAT': "CONCAT({0}, {1})",
2366
+ 'REGEX_REPLACE': None,
2367
+ 'REGEX_REPLACE_I': None,
2368
+ 'MD5': "MD5({0})",
2369
+ 'SHA1': "SHA1({0})",
2370
+ 'SHA256': "SHA2({0}, 256)",
2371
+ 'SHA384': "SHA2({0}, 384)",
2372
+ 'SHA512': "SHA2({0}, 512)",
2373
+ 'SPARQL_UUID': "CONCAT('urn:uuid:', UUID())",
2374
+ 'BNODE': "CONCAT('_:b', REPLACE(UUID(), '-', ''))",
2375
+ 'RANDOMUUID': "UUID()",
2376
+ 'ENCODE_FOR_URI': None,
2377
+ # collect() over no rows is [].
2378
+ 'EMPTY_LIST': None,
2379
+ 'LIST_GET': None,
2380
+ 'ELEMENT_AT': None,
2381
+ 'SPLIT': None,
2382
+ 'TO_FLOAT': "CASE WHEN CAST({0} AS CHAR) REGEXP '^[[:space:]]*[-+]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eE][-+]?[0-9]+)?[[:space:]]*$' THEN CAST(CAST({0} AS CHAR) AS DOUBLE) END",
2383
+ 'TO_INTEGER': "CAST(TRUNCATE(CASE WHEN CAST({0} AS CHAR) REGEXP '^[[:space:]]*[-+]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eE][-+]?[0-9]+)?[[:space:]]*$' THEN CAST(CAST({0} AS CHAR) AS DOUBLE) END, 0) AS SIGNED)",
2384
+ 'TO_BOOLEAN': "CASE LOWER(CAST({0} AS CHAR)) WHEN 'true' THEN TRUE WHEN 'false' THEN FALSE END",
2385
+ # xsd:T(?v) of text: written for DuckDB, Spark,
2386
+ # Databricks and PostgreSQL.
2387
+ 'XSD_TO_INTEGER': None,
2388
+ 'DOUBLE_TO_BIGINT': None,
2389
+ 'XSD_TO_DECIMAL': 'CAST({0} AS DECIMAL(38,18))',
2390
+ 'XSD_TO_DECIMAL_SIZED': 'CAST({0} AS DECIMAL({1},{2}))',
2391
+ 'XSD_TO_DATETIME': None,
2392
+ 'XSD_TO_DATE': None,
2393
+ 'LENGTH': 'CHAR_LENGTH',
2394
+ },
2395
+ type_map={
2396
+ # MySQL's CAST takes CHAR, not VARCHAR(n).
2397
+ 'STRING': 'CHAR',
2398
+ 'PATH_STRING': 'CHAR(16383)',
2399
+ 'LONG': 'SIGNED',
2400
+ # MySQL's CAST takes SIGNED, not INTEGER (the write rule's flag).
2401
+ 'INTEGER': 'SIGNED',
2402
+ 'DOUBLE': 'DOUBLE',
2403
+ },
2404
+ )
2405
+
2406
+ ORACLE_DIALECT_CONFIG = DialectConfig(
2407
+ identifier_fold='upper',
2408
+ # A table named like a reserved word (`comment`, `level`) exists only quoted.
2409
+ words_a_table_name_quotes=frozenset(w.lower() for w in _ORACLE_RESERVED),
2410
+ name='oracle',
2411
+ database_name='Oracle',
2412
+ max_identifier_length=128,
2413
+ identifier_length_in_bytes=True,
2414
+ quote_char='"',
2415
+ supports_recursive_cte=True,
2416
+ current_date_parens=False,
2417
+ limit_style='fetch',
2418
+ has_boolean_type=False,
2419
+ integer_results_need_declared_type=True,
2420
+ param_style=':1',
2421
+ function_map={
2422
+ # SPARQL's string functions (m2-functions.md 3): code points, text comparisons (no LIKE),
2423
+ # REGEX over the pattern as this engine reads it (strings.py, xpath_regex.py), which is
2424
+ # the second argument (Java), the third (RE2) or the fourth (PostgreSQL's ARE);
2425
+ # None where not built.
2426
+ 'STR_STARTS': None,
2427
+ 'STR_ENDS': None,
2428
+ 'STR_CONTAINS': None,
2429
+ 'STR_POS': None,
2430
+ 'STR_LEN': None,
2431
+ 'STR_UPPER': None,
2432
+ 'STR_LOWER': None,
2433
+ 'SUBSTR_CP': None,
2434
+ 'SUBSTR_FROM': None,
2435
+ 'REGEX_MATCH': None,
2436
+ # A native date's and dateTime's XSD text (m2-functions.md 2): years 1 to
2437
+ # 9999, an error outside them; None where not built.
2438
+ 'XSD_DATE_TEXT': None,
2439
+ 'XSD_DATETIME_TEXT': None,
2440
+ # A decimal's canonical text, XML Schema 1.1's, R2RML 10.2's edition ('5', '1.5'; D3).
2441
+ # TO_CHAR's text model TM9 writes a NUMBER by its value in fixed notation, with no trailing zero
2442
+ # and no leading one before the point: '.5', '-.5', which a value under 1 gets a 0 for.
2443
+ 'XSD_DECIMAL_TEXT': ("(CASE WHEN {0} = 0 THEN '0' WHEN ABS({0}) < 1 THEN REPLACE(TO_CHAR({0}, 'TM9', "
2444
+ "'NLS_NUMERIC_CHARACTERS=''.,'''), '.', '0.') "
2445
+ "ELSE TO_CHAR({0}, 'TM9', 'NLS_NUMERIC_CHARACTERS=''.,''') END)"),
2446
+ 'XSD_DOUBLE_TEXT': None,
2447
+ # YEAR, MONTH, DAY, HOURS, MINUTES and SECONDS of a native DATE or local TIMESTAMP
2448
+ # (m2-functions.md 6): the field, SECONDS a DECIMAL(8, 6) with its fraction; None where not built.
2449
+ 'DT_YEAR': None,
2450
+ 'DT_MONTH': None,
2451
+ 'DT_DAY': None,
2452
+ 'DT_HOUR': None,
2453
+ 'DT_MINUTE': None,
2454
+ 'DT_SECOND': None,
2455
+ # A zoned date-time's instant as a date-time in UTC, for its text
2456
+ # (ExprTranslator._as_text).
2457
+ 'DATETIME_IN_UTC': 'SYS_EXTRACT_UTC({0})',
2458
+ # No array type: a list written in the query has no form here, and
2459
+ # `ARRAY[...]` was sent to the database (ORA-00923).
2460
+ 'ARRAY': None,
2461
+ # Oracle has no `%` operator (ORA-00911). MOD takes
2462
+ # the dividend's sign, as Cypher's `%` does. MOD(x, 0) is x there, a
2463
+ # number where no other database gives one, so a zero divisor is
2464
+ # made NULL, DuckDB's answer.
2465
+ '%': 'MOD({0}, NULLIF({1}, 0))',
2466
+ # A Cypher float literal is a float: a bare 2.0 is DECIMAL in SQL,
2467
+ # and 7 / 2.0 came back as a decimal.
2468
+ 'FLOAT_LITERAL': 'CAST({0} AS BINARY_DOUBLE)',
2469
+ 'IS_NAN': '({0} IS NAN)',
2470
+ # No map type: a map, a node or a relationship value refuses (D8).
2471
+ 'NAMED_STRUCT': None,
2472
+ # An empty property map (decision D8).
2473
+ 'EMPTY_MAP': None,
2474
+ # No arrays: list comprehension and quantifiers refuse.
2475
+ # Integer division, truncated toward zero as Cypher's is.
2476
+ # Temporal constructors from components; None refuses.
2477
+ 'MAKE_DATE': None,
2478
+ 'MAKE_TIME': None,
2479
+ 'TIME_NOW': None,
2480
+ # localdatetime(): now's wall clock in UTC, with no zone.
2481
+ 'LOCAL_NOW': 'SYS_EXTRACT_UTC(SYSTIMESTAMP)',
2482
+ # datetime(...) compared: the instant of a UTC text (ExprTranslator._compared).
2483
+ 'ZONED_OF_TEXT': None,
2484
+ 'TIME_OF_TEXT': None,
2485
+ 'MAKE_TIMESTAMP': None,
2486
+ # A part of a date or a local date-time past the six SQL names:
2487
+ # not written for this database yet.
2488
+ 'MILLISECOND': None,
2489
+ 'MICROSECOND': None,
2490
+ 'NANOSECOND': None,
2491
+ 'QUARTER': None,
2492
+ 'WEEK': None,
2493
+ 'DAY_OF_WEEK': None,
2494
+ 'ORDINAL_DAY': None,
2495
+ 'DATE_PLUS': None,
2496
+ 'TS_PLUS': None,
2497
+ 'MICROS_BETWEEN': None,
2498
+ # Two edges differ (relationship uniqueness); see _different_edges.
2499
+ 'EDGE_DIFFERS': "(NOT ({0} = {2} AND {1} = {3}))",
2500
+ # The same with their row numbers.
2501
+ 'EDGE_ROW_DIFFERS': "(NOT ({0} = {3} AND {1} = {4} AND {2} = {5}))",
2502
+ # List equality, three-valued (ExprTranslator._list_equality): whether
2503
+ # some pair of elements at one position differs, and whether a list
2504
+ # holds a null.
2505
+ 'LIST_PAIR_DIFFERS': None,
2506
+ 'COLLECT_PAIRS': None,
2507
+ 'PAIR_VALUES': None,
2508
+ 'PAIR_KINDS': None,
2509
+ 'PAIR': None,
2510
+ 'LIST_PAIR_ANY': None,
2511
+ 'LIST_PAIR_BOTH_VALUES': None,
2512
+ 'LIST_HAS_NULL': None,
2513
+ # A list no element gives a type ([] or [null]), read as a list of
2514
+ # the other operand's elements (ExprTranslator._typed_list).
2515
+ 'LIST_OF_INTEGER': "{0}",
2516
+ 'LIST_OF_FLOAT': "{0}",
2517
+ 'LIST_OF_STRING': "{0}",
2518
+ 'LIST_OF_BOOLEAN': "{0}",
2519
+ # The key of a collected node's map, and a key to compare with it
2520
+ # (see ExprTranslator._node_equality).
2521
+ 'NODE_MAP_KEY': None,
2522
+ 'AS_NODE_MAP_KEY': None,
2523
+ # The labels of a collected node's map (ExprTranslator._labels).
2524
+ 'NODE_MAP_LABELS': None,
2525
+ # Two lists joined (Cypher's + on lists); None where there is no list.
2526
+ 'LIST_CONCAT': None,
2527
+ 'INT_DIV': "TRUNC(({0}) / ({1}))",
2528
+ 'INT_QUOTIENT': "TRUNC(({0}) / ({1}))",
2529
+ 'INT_DIV_ONCE': "TRUNC(({0}) / ({1}))",
2530
+ **_PLAIN_DIVISION,
2531
+ # Oracle's remainder is MOD, as for '%' above.
2532
+ 'INT_MOD': 'MOD({0}, NULLIF({1}, 0))',
2533
+ 'INT_REMAINDER': 'MOD({0}, {1})',
2534
+ 'INT_MOD_ONCE': 'MOD({0}, NULLIF({1}, 0))',
2535
+ 'FLOAT_MOD_ONCE': 'MOD({0}, NULLIF({1}, 0))',
2536
+ 'FLOAT_MOD': 'MOD({0}, NULLIF({1}, 0))',
2537
+ 'LIST_FILTER': None,
2538
+ 'LIST_TRANSFORM': None,
2539
+ # =~ matches the whole string.
2540
+ 'REGEX_FULL': "REGEXP_LIKE({0}, '^(' || {1} || ')$')",
2541
+ # A search for the pattern anywhere in the text (an `RLIKE` comparison): no operator, a condition.
2542
+ 'REGEX_SEARCH': "REGEXP_LIKE({0}, {1})",
2543
+ # Oracle's type names in CAST.
2544
+ 'CAST_STRING': 'CAST({0} AS VARCHAR2(4000))',
2545
+ 'CAST_PATH_STRING': 'CAST({0} AS VARCHAR2(4000))',
2546
+ 'CAST_LONG': 'CAST({0} AS NUMBER(19))',
2547
+ 'CAST_WHOLE_DECIMAL': 'CAST({0} AS NUMBER(38))',
2548
+ 'CAST_INT': 'CAST({0} AS NUMBER(10))',
2549
+ 'CAST_DOUBLE': 'CAST({0} AS BINARY_DOUBLE)',
2550
+ # Functions the base serializer spells the Spark way.
2551
+ 'PERCENTILE_DISC': 'percentile_disc({1}) WITHIN GROUP (ORDER BY {0})',
2552
+ 'PERCENTILE_CONT': 'percentile_cont({1}) WITHIN GROUP (ORDER BY {0})',
2553
+ # SPARQL GROUP_CONCAT: LISTAGG needs WITHIN GROUP and raises past 4000
2554
+ # bytes; not verified on Oracle.
2555
+ 'GROUP_CONCAT': None,
2556
+ 'GROUP_CONCAT_DISTINCT': None,
2557
+ 'SEQUENCE': None,
2558
+ 'SEQUENCE_STEP': None,
2559
+ 'SLICE': None,
2560
+ 'LIST_REVERSE': None,
2561
+ 'STR_REVERSE': None,
2562
+ 'E': 'EXP(1)',
2563
+ 'RAND': None,
2564
+ # String `+`: null if either side is null.
2565
+ 'STR_CONCAT': "CASE WHEN {0} IS NULL OR {1} IS NULL THEN NULL ELSE {0} || {1} END",
2566
+ 'REGEX_REPLACE': None,
2567
+ 'REGEX_REPLACE_I': None,
2568
+ 'MD5': None,
2569
+ 'SHA1': None,
2570
+ 'SHA256': None,
2571
+ 'SHA384': None,
2572
+ 'SHA512': None,
2573
+ 'SPARQL_UUID': None,
2574
+ 'BNODE': None,
2575
+ 'ENCODE_FOR_URI': None,
2576
+ # collect() over no rows is [].
2577
+ 'EMPTY_LIST': None,
2578
+ 'LIST_GET': None,
2579
+ 'ELEMENT_AT': None,
2580
+ 'SPLIT': None,
2581
+ 'TO_FLOAT': 'CAST({0} AS BINARY_DOUBLE DEFAULT NULL ON CONVERSION ERROR)',
2582
+ 'TO_INTEGER': 'TRUNC(CAST({0} AS BINARY_DOUBLE DEFAULT NULL ON CONVERSION ERROR))',
2583
+ 'TO_BOOLEAN': "CASE LOWER(TO_CHAR({0})) WHEN 'true' THEN 1 WHEN 'false' THEN 0 END",
2584
+ # xsd:T(?v) of text: written for DuckDB, Spark,
2585
+ # Databricks and PostgreSQL.
2586
+ 'XSD_TO_INTEGER': None,
2587
+ 'DOUBLE_TO_BIGINT': None,
2588
+ # A mean of integers or of decimals of 18 digits or fewer: the exact quotient, as PostgreSQL's.
2589
+ 'NATIVE_QUOTIENT_BY_INTEGER': ("(CASE WHEN ({1}) = 0 THEN NULL WHEN ABS(ROUND(({0}) / ({1}), 18)) < 100000000000000000000 "
2590
+ "THEN CAST(ROUND(({0}) / ({1}), 18) AS NUMBER(38,18)) END)"),
2591
+ # A sum of 64-bit integers, exact: NUMBER keeps 38 digits.
2592
+ 'EXACT_INTEGER_SUM': 'CAST(SUM({0}) AS NUMBER(38,0))',
2593
+ # A 64-bit sum, difference or product, NULL (unbound, D7) past the 64-bit bounds. NUMBER arithmetic
2594
+ # does not overflow there (a product of two longs fits its 38 digits), so the guard is a range check.
2595
+ 'TRY_INTEGER_ADD': '(CASE WHEN ({0}) + ({1}) BETWEEN -9223372036854775808 AND 9223372036854775807 THEN ({0}) + ({1}) END)',
2596
+ 'TRY_INTEGER_SUB': '(CASE WHEN ({0}) - ({1}) BETWEEN -9223372036854775808 AND 9223372036854775807 THEN ({0}) - ({1}) END)',
2597
+ 'TRY_INTEGER_MUL': '(CASE WHEN ({0}) * ({1}) BETWEEN -9223372036854775808 AND 9223372036854775807 THEN ({0}) * ({1}) END)',
2598
+ # Two whole totals added, NULL past 38 digits.
2599
+ 'TRY_TOTAL_ADD': '(CASE WHEN ABS(({0}) + ({1})) < 1' + '0' * 38 + ' THEN ({0}) + ({1}) END)',
2600
+ # The parts of a decimal a split product would add (Databricks alone uses it, `NARROWED_ALTERNATIVE`
2601
+ # is NULL here); written so that the argument it is handed is valid SQL.
2602
+ 'DECIMAL_WHOLE_PART': 'TRUNC({0})',
2603
+ 'DECIMAL_FRACTION_PART': '(({0}) - TRUNC({0}))',
2604
+ 'LENGTH': 'LENGTH',
2605
+ 'SQRT': 'SQRT',
2606
+ # An integer count or total as the decimal an average divides: NUMBER holds 38 digits.
2607
+ 'INTEGER_AS_DECIMAL': 'CAST({0} AS NUMBER(38))',
2608
+ # D7's quotient: 18 places, rounded half away from zero, NULL (unbound) past 20 whole digits or
2609
+ # by zero. NUMBER division keeps 38 significant digits, so the quotient is exact to its 18 places
2610
+ # (a tie hidden beyond 38 digits aside).
2611
+ 'EXACT_QUOTIENT': ("(CASE WHEN ({1}) = 0 THEN NULL WHEN ABS(ROUND(({0}) / ({1}), 18)) < 100000000000000000000 "
2612
+ "THEN CAST(ROUND(({0}) / ({1}), 18) AS NUMBER(38,18)) END)"),
2613
+ # A decimal + - * declared past 38 digits (D7), as PostgreSQL's: NUMBER(38, {2}), or NULL
2614
+ # (unbound) where the value needs more ({3} is 10^(38 - {2})). NUMBER arithmetic is exact
2615
+ # to 38 significant digits; a value inside the bound has no more where its scale is the
2616
+ # operands' (a result scale past 18 is rounded in the NUMBER first, then at {2}).
2617
+ 'TRY_DECIMAL_ADD': '(CASE WHEN ABS(({0}) + ({1})) < {3} THEN CAST(({0}) + ({1}) AS NUMBER(38,{2})) END)',
2618
+ 'TRY_DECIMAL_SUB': '(CASE WHEN ABS(({0}) - ({1})) < {3} THEN CAST(({0}) - ({1}) AS NUMBER(38,{2})) END)',
2619
+ 'TRY_DECIMAL_MUL': '(CASE WHEN ABS(({0}) * ({1})) < {3} THEN CAST(({0}) * ({1}) AS NUMBER(38,{2})) END)',
2620
+ 'TRY_DECIMAL_SUM': '(CASE WHEN ABS(SUM({0})) < {1} THEN SUM({0}) END)',
2621
+ # Operands keep their own digits: NUMBER holds any of them, and its first candidate is exact.
2622
+ 'DECIMAL_WIDENED': '{0}',
2623
+ 'DECIMAL_NARROWED': '{0}',
2624
+ 'DECIMAL_RESCALED': 'CAST(NULL AS NUMBER)',
2625
+ 'NARROWED_ALTERNATIVE': '(NULL)',
2626
+ # A decimal answer column at the digits the plan knows, so the driver reads it as
2627
+ # decimal128({1}, {2}) (`lower.exact_answer`): NULL where the value, rounded to {2} places,
2628
+ # needs more than {1} digits ({3} is 10^({1} - {2})), never ORA-01438.
2629
+ 'DECIMAL_ANSWER': '(CASE WHEN ABS(ROUND({0}, {2})) < {3} THEN CAST({0} AS NUMBER({1},{2})) END)',
2630
+ # A DECIMAL column as its declared digits write it ('12.50'; `decimal_text: database`): {2} is
2631
+ # the format model of its scale.
2632
+ 'DATABASE_DECIMAL_TEXT': "TO_CHAR({0}, {2}, 'NLS_NUMERIC_CHARACTERS=''.,''')",
2633
+ 'XSD_TO_DECIMAL': 'CAST({0} AS NUMBER(38,18) DEFAULT NULL ON CONVERSION ERROR)',
2634
+ 'XSD_TO_DECIMAL_SIZED': 'CAST({0} AS NUMBER({1},{2}) DEFAULT NULL ON CONVERSION ERROR)',
2635
+ 'XSD_TO_DATETIME': None,
2636
+ 'XSD_TO_DATE': None,
2637
+ 'LEFT': 'SUBSTR({0}, 1, {1})',
2638
+ 'RIGHT': 'SUBSTR({0}, -({1}))',
2639
+ },
2640
+ type_map={
2641
+ 'STRING': 'VARCHAR2(4000)',
2642
+ 'INT': 'NUMBER(10)',
2643
+ 'LONG': 'NUMBER(19)',
2644
+ 'DOUBLE': 'NUMBER',
2645
+ 'BOOLEAN': 'NUMBER(1)',
2646
+ },
2647
+ )
2648
+
2649
+ def _internal_function_names() -> frozenset:
2650
+ """The names of the plan's own functions: every name some dialect's function map holds."""
2651
+ return frozenset(n for c in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT, DUCKDB_DIALECT, POSTGRESQL_DIALECT,
2652
+ SQLSERVER_DIALECT, MYSQL_DIALECT, ORACLE_DIALECT_CONFIG) for n in c.function_map)
2653
+
2654
+
2655
+ # For backward compat, code that references ORACLE_DIALECT as a config
2656
+ ORACLE_DIALECT = ORACLE_DIALECT_CONFIG
2657
+
2658
+
2659
+ # =========================================================================
2660
+ # Dialect instances (behavior wrappers)
2661
+ # =========================================================================
2662
+
2663
+ _DIALECT_INSTANCES: Dict[str, Dialect] = {}
2664
+
2665
+
2666
+ def standard_string(value: str) -> str:
2667
+ """`value` as a standard SQL string literal, its quotes doubled: how a
2668
+ tree holds a string, which each serializer writes again the way its
2669
+ database reads it (`render_string`; Spark reads 'O''Neil' as two
2670
+ literals)."""
2671
+ return "'" + value.replace("'", "''") + "'"
2672
+
2673
+
2674
+ def get_dialect(name: str) -> DialectConfig:
2675
+ """Look up a dialect configuration by name.
2676
+
2677
+ Returns the ``DialectConfig`` (data). For behavior methods, use
2678
+ ``get_dialect_instance(name)`` instead.
2679
+ """
2680
+ configs = {
2681
+ 'spark': SPARK_DIALECT,
2682
+ 'databricks_sql': DATABRICKS_SQL_DIALECT,
2683
+ 'databricks': DATABRICKS_SQL_DIALECT,
2684
+ 'duckdb': DUCKDB_DIALECT,
2685
+ 'postgresql': POSTGRESQL_DIALECT,
2686
+ 'postgres': POSTGRESQL_DIALECT,
2687
+ 'sqlserver': SQLSERVER_DIALECT,
2688
+ 'mssql': SQLSERVER_DIALECT,
2689
+ 'mysql': MYSQL_DIALECT,
2690
+ 'oracle': ORACLE_DIALECT_CONFIG,
2691
+ }
2692
+ config = configs.get(name)
2693
+ if config is None:
2694
+ available = ', '.join(sorted(configs.keys()))
2695
+ raise ValueError(f"Unknown SQL dialect: {name!r}. Available: {available}")
2696
+ return config
2697
+
2698
+
2699
+ # The names the plan builders, the rewrites and the MATCH handlers make:
2700
+ # each begins with an underscore and ends in a number (`_anon0`, `_anon_r1`,
2701
+ # `_n0`, `_r1`, `_back_to_s_0`, `_rel_2`), or begins with two underscores.
2702
+ _BUILDER_NAME = re.compile(r'_\w*\d|__.*')
2703
+
2704
+
2705
+ def internal_name(name: str) -> str:
2706
+ """A column named by itself in the interpreter's internal backtick
2707
+ form, `` `name` ``."""
2708
+ return "`" + name.replace("`", "``") + "`"
2709
+
2710
+
2711
+ def unquoted_name(name: str) -> str:
2712
+ """A name as one identifier in any dialect's quoting reads (`` `a``b` ``,
2713
+ ``"a""b"``, ``[a]]b]``): its quotes taken off, the quote character
2714
+ doubled inside it read as one. The reading back of `Dialect.quote` and
2715
+ `internal_name`; a name with no quotes is returned as it is."""
2716
+ if name[:1] == '[' and name[-1:] == ']':
2717
+ return name[1:-1].replace(']]', ']')
2718
+ if len(name) > 1 and name[0] in '`"' and name[-1] == name[0]:
2719
+ return name[1:-1].replace(name[0] * 2, name[0])
2720
+ return name
2721
+
2722
+
2723
+ def get_dialect_instance(name: str) -> Dialect:
2724
+ """Look up a ``Dialect`` instance (behavior wrapper) by name.
2725
+
2726
+ Cached per name. Eight names have a subclass: ``spark`` (SparkDialect),
2727
+ ``databricks_sql`` and ``databricks`` (DatabricksSQLDialect),
2728
+ ``duckdb`` (DuckDBDialect), ``oracle`` (OracleDialect), ``sqlserver``
2729
+ and ``mssql`` (SQLServerDialect) and ``mysql`` (MySQLDialect). All
2730
+ others, PostgreSQL among them, get the base ``Dialect`` class.
2731
+ """
2732
+ if name not in _DIALECT_INSTANCES:
2733
+ config = get_dialect(name)
2734
+ dialect_classes = {
2735
+ 'spark': SparkDialect,
2736
+ 'databricks_sql': DatabricksSQLDialect,
2737
+ 'databricks': DatabricksSQLDialect,
2738
+ 'duckdb': DuckDBDialect,
2739
+ 'oracle': OracleDialect,
2740
+ 'sqlserver': SQLServerDialect,
2741
+ 'mssql': SQLServerDialect,
2742
+ 'mysql': MySQLDialect,
2743
+ }
2744
+ cls = dialect_classes.get(name, Dialect)
2745
+ _DIALECT_INSTANCES[name] = cls(config)
2746
+ return _DIALECT_INSTANCES[name]
2747
+
2748
+
2749
+ _PLAIN_NAME = re.compile(r'[A-Za-z_][A-Za-z0-9_]*')
2750
+
2751
+
2752
+ def safe_alias(name: str) -> str:
2753
+ """A table or subquery alias as SQL text: the name where every
2754
+ database reads it as a plain name, else `_v_` and the name, `_` written
2755
+ `__` and any other character that is no ASCII letter or digit
2756
+ `_<hex>_` (`_b7_` for `·`, `_e9_` for `é`), so two names never give
2757
+ one alias. A letter outside ASCII is no plain name on Spark and
2758
+ Databricks (`?né`). The aliases of
2759
+ the plan are the query's variables (`?order`, `` (`left`) ``, `?1x`);
2760
+ written as they are, `people AS order` was a syntax error on every
2761
+ database. Only the SQL text changes: the plan, its
2762
+ column identities and the result's column names keep the variable's
2763
+ name."""
2764
+ # Always in the alias namespace `_v_`: a plain name was kept as it was, and inside a
2765
+ # correlated EXISTS Spark read a column of that name (the variable ?t's `t`) for the outer alias t.
2766
+ # No plan column starts `_v_` (`lower.sql_name` escapes one that would), whatever its case.
2767
+ return '_v_' + ''.join(
2768
+ '__' if c == '_' else c if c.isascii() and c.isalnum() else f'_{ord(c):x}_' for c in name)
2769
+
2770
+
2771
+ _ESCAPED = re.compile(r'__|_([0-9a-f]+)_')
2772
+
2773
+
2774
+ def alias_name(text: str) -> str:
2775
+ """The name `safe_alias` wrote as the first name of `text`: a scope
2776
+ read back from SQL text is the plan's alias again."""
2777
+ if not text.startswith('_v_'):
2778
+ return text
2779
+ head, dot, rest = text.partition('.')
2780
+ return _ESCAPED.sub(lambda m: chr(int(m.group(1), 16)) if m.group(1) else '_', head[3:]) + dot + rest
2781
+
2782
+
2783
+ _BACKTICK_NAME = re.compile(r'`(?:[^`]|``)+`')
2784
+
2785
+
2786
+ def scope_name(scope: str) -> str:
2787
+ """A scope (a table alias or a
2788
+ CTE's name) as SQL text: `safe_alias` of the name, kept in the backtick
2789
+ form when written in it, so a plain name's text is as it was. Of a
2790
+ dotted scope (a map's field, `m.a`) the first name is the alias."""
2791
+ if _BACKTICK_NAME.fullmatch(scope):
2792
+ return internal_name(safe_alias(unquoted_name(scope)))
2793
+ if scope[:1] == '`':
2794
+ return scope
2795
+ head, dot, rest = scope.partition('.')
2796
+ return safe_alias(head) + dot + rest
2797
+ return safe_alias(unquoted_name(scope))
2798
+
2799
+
2800
+ # Every dialect's words a table name quotes, Oracle's reserved words, and
2801
+ # the words DuckDB 1.5 and Spark take as a column's name but not as an
2802
+ # alias: DuckDB's `type_function` and `column_name` keywords
2803
+ # (`duckdb_keywords()`), Spark's strict non-reserved ones.
2804
+ _WORDS_AN_ALIAS_AVOIDS = frozenset().union(
2805
+ *(c.words_a_table_name_quotes for c in (
2806
+ SPARK_DIALECT, POSTGRESQL_DIALECT, DATABRICKS_SQL_DIALECT, DUCKDB_DIALECT,
2807
+ SQLSERVER_DIALECT, MYSQL_DIALECT, ORACLE_DIALECT_CONFIG)),
2808
+ (w.lower() for w in _ORACLE_RESERVED),
2809
+ """anti asof at authorization between bigint binary bit boolean by char
2810
+ character coalesce collation columns concurrently cross dec decimal
2811
+ exists extract float freeze full generated glob grouping grouping_id
2812
+ ilike inner inout int integer interval is isnull join left like map
2813
+ national natural nchar none notnull nullif numeric out outer overlaps
2814
+ overlay position positional precision real right row semi setof similar
2815
+ smallint struct substring tablesample time timestamp treat trim try_cast
2816
+ unpack values varchar verbose xmlattributes xmlconcat xmlelement
2817
+ xmlexists xmlforest xmlnamespaces xmlparse xmlpi xmlroot xmlserialize
2818
+ xmltable except intersect lateral minus on union using""".split())
2819
+
2820
+
2821
+ # A zoned dateTime (TIMESTAMPTZ, Spark's TIMESTAMP, a pandas column with a
2822
+ # zone) is held as its instant: its UTC wall-clock time as a local value,
2823
+ # and its text that time's canonical text ending `Z` (rule value-text;
2824
+ # m2-functions.md 11).
2825
+ _ZONED_TO_UTC = {
2826
+ 'spark': "CAST(to_utc_timestamp({0}, current_timezone()) AS TIMESTAMP_NTZ)",
2827
+ 'databricks_sql': "CAST(to_utc_timestamp({0}, current_timezone()) AS TIMESTAMP_NTZ)",
2828
+ 'duckdb': "timezone('UTC', {0})",
2829
+ 'postgresql': "({0} AT TIME ZONE 'UTC')",
2830
+ }
2831
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT, DUCKDB_DIALECT, POSTGRESQL_DIALECT):
2832
+ _utc = _ZONED_TO_UTC[_config.name]
2833
+ _config.function_map['ZONED_TO_UTC'] = _utc
2834
+ _config.function_map['XSD_ZONED_DATETIME_TEXT'] = (
2835
+ "(" + _config.function_map['XSD_DATETIME_TEXT'].replace("{0}", "{1}").replace("{1}", _utc)
2836
+ + " || 'Z')")
2837
+
2838
+ # MIN and MAX of booleans (false before true): PostgreSQL has no MIN(boolean).
2839
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT, DUCKDB_DIALECT):
2840
+ _config.function_map['MIN_BOOLEAN'] = 'MIN({0})'
2841
+ _config.function_map['MAX_BOOLEAN'] = 'MAX({0})'
2842
+ POSTGRESQL_DIALECT.function_map['MIN_BOOLEAN'] = 'bool_and({0})'
2843
+ POSTGRESQL_DIALECT.function_map['MAX_BOOLEAN'] = 'bool_or({0})'
2844
+
2845
+ # A decimal operand at fewer places (lower.exact_decimal's candidates, used
2846
+ # only where the places dropped are zero): {1} the precision on DuckDB and
2847
+ # Spark, {2} the places, {3} the precision on Databricks (its product's types
2848
+ # must fit 38 digits); PostgreSQL's NUMERIC never needs it. And a candidate
2849
+ # only Databricks needs, NULL elsewhere.
2850
+ for _config in (SPARK_DIALECT, DUCKDB_DIALECT):
2851
+ _config.function_map['DECIMAL_RESCALED'] = 'TRY_CAST({0} AS DECIMAL({1},{2}))'
2852
+ _config.function_map['NARROWED_ALTERNATIVE'] = '(NULL)'
2853
+ DATABRICKS_SQL_DIALECT.function_map['DECIMAL_RESCALED'] = 'TRY_CAST({0} AS DECIMAL({3},{2}))'
2854
+ DATABRICKS_SQL_DIALECT.function_map['NARROWED_ALTERNATIVE'] = '{0}'
2855
+ POSTGRESQL_DIALECT.function_map['DECIMAL_RESCALED'] = 'CAST(NULL AS NUMERIC)'
2856
+ POSTGRESQL_DIALECT.function_map['NARROWED_ALTERNATIVE'] = '(NULL)'
2857
+ # A decimal's whole part as DECIMAL({1}, 0) and its fraction as DECIMAL({1}, {1}),
2858
+ # both with its sign (lower._split_product; `x % 1` is exact, DECIMAL(s + 1, s)).
2859
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT, DUCKDB_DIALECT):
2860
+ _config.function_map['DECIMAL_WHOLE_PART'] = 'TRY_CAST(({0}) - ({0}) % 1 AS DECIMAL({1},0))'
2861
+ _config.function_map['DECIMAL_FRACTION_PART'] = 'TRY_CAST(({0}) % 1 AS DECIMAL({1},{1}))'
2862
+ POSTGRESQL_DIALECT.function_map['DECIMAL_WHOLE_PART'] = 'CAST(TRUNC({0}) AS NUMERIC)'
2863
+ POSTGRESQL_DIALECT.function_map['DECIMAL_FRACTION_PART'] = 'CAST(({0}) % 1 AS NUMERIC)'
2864
+
2865
+ # PostgreSQL's NUMERIC division is native already: its exact quotient serves.
2866
+ POSTGRESQL_DIALECT.function_map['NATIVE_QUOTIENT_BY_INTEGER'] = POSTGRESQL_DIALECT.function_map['EXACT_QUOTIENT']
2867
+
2868
+ # Integer arithmetic and SUM (D7 as built 2026-11-09, m2-functions.md 12): 64-bit,
2869
+ # a result past it NULL (unbound, an expression error) per operator; and an
2870
+ # integer column's exact sum, for AVG, as a DECIMAL(38, 0) (Spark sums each
2871
+ # value in three pieces of 21 bits as longs, which stays at long speed where a
2872
+ # DECIMAL(38, 0) sum is BigDecimal arithmetic; a piece is under 2^22 in size, so
2873
+ # its long sum cannot overflow below 2^41 rows a group, where two 32-bit halves'
2874
+ # could past 2^31).
2875
+ _TWO21, _TWO42 = '2097152', '4398046511104'
2876
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
2877
+ _config.function_map.update({
2878
+ 'TRY_INTEGER_ADD': 'try_add({0}, {1})',
2879
+ 'TRY_INTEGER_SUB': 'try_subtract({0}, {1})',
2880
+ 'TRY_INTEGER_MUL': 'try_multiply({0}, {1})',
2881
+ 'TRY_INTEGER_SUM': 'try_sum({0})',
2882
+ 'EXACT_INTEGER_SUM': (f'(CAST(SUM(CAST({{0}} AS BIGINT) div {_TWO42}) AS DECIMAL(38,0)) * {_TWO42} '
2883
+ f'+ CAST(SUM((CAST({{0}} AS BIGINT) % {_TWO42}) div {_TWO21}) AS DECIMAL(38,0)) * {_TWO21} '
2884
+ f'+ CAST(SUM(CAST({{0}} AS BIGINT) % {_TWO21}) AS DECIMAL(38,0)))'),
2885
+ })
2886
+ DUCKDB_DIALECT.function_map.update({
2887
+ 'TRY_INTEGER_ADD': 'TRY(CAST({0} AS BIGINT) + CAST({1} AS BIGINT))',
2888
+ 'TRY_INTEGER_SUB': 'TRY(CAST({0} AS BIGINT) - CAST({1} AS BIGINT))',
2889
+ 'TRY_INTEGER_MUL': 'TRY(CAST({0} AS BIGINT) * CAST({1} AS BIGINT))',
2890
+ 'TRY_INTEGER_SUM': 'TRY_CAST(SUM({0}) AS BIGINT)',
2891
+ 'EXACT_INTEGER_SUM': 'TRY_CAST(SUM({0}) AS DECIMAL(38,0))',
2892
+ })
2893
+ # Two whole totals (DECIMAL(38, 0), aggregates) added, NULL past 38 digits (lower._sum_of_products):
2894
+ # the decimal add elsewhere; DuckDB cannot wrap an aggregate in TRY and raises on an overflowing
2895
+ # add, so the bound is compared first by subtraction, which cannot overflow.
2896
+ _NINES38 = '9' * 38
2897
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT, POSTGRESQL_DIALECT):
2898
+ _config.function_map['TRY_TOTAL_ADD'] = _config.function_map['TRY_DECIMAL_ADD'].replace('{2}', '0').replace('{3}', '1' + '0' * 38)
2899
+ DUCKDB_DIALECT.function_map['TRY_TOTAL_ADD'] = (
2900
+ f'(CASE WHEN ({{1}}) > 0 AND ({{0}}) > {_NINES38} - ({{1}}) THEN NULL '
2901
+ f'WHEN ({{1}}) < 0 AND ({{0}}) < -{_NINES38} - ({{1}}) THEN NULL ELSE ({{0}}) + ({{1}}) END)')
2902
+ # A decimal SUM of rows of 18 digits or fewer (`lower.decimal_sum`): it cannot pass 38 digits, so DuckDB's
2903
+ # plain SUM; elsewhere the dialect's TRY_DECIMAL_SUM.
2904
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT, POSTGRESQL_DIALECT, SQLSERVER_DIALECT, MYSQL_DIALECT,
2905
+ ORACLE_DIALECT_CONFIG):
2906
+ if 'TRY_DECIMAL_SUM' in _config.function_map:
2907
+ _config.function_map.setdefault('TRY_DECIMAL_SUM_NARROW', _config.function_map['TRY_DECIMAL_SUM'])
2908
+ DUCKDB_DIALECT.function_map['TRY_DECIMAL_SUM_NARROW'] = 'SUM({0})'
2909
+ # A CHAR(n) column's value as text with its padding: PostgreSQL drops the padding in
2910
+ # a cast to text and keeps it in its output text (concat); Spark, Databricks and SQL Server read
2911
+ # CHAR padded; DuckDB stores CHAR as VARCHAR, unpadded.
2912
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT, DUCKDB_DIALECT, SQLSERVER_DIALECT, MYSQL_DIALECT,
2913
+ ORACLE_DIALECT_CONFIG):
2914
+ _config.function_map.setdefault('CHAR_TEXT', '{0}')
2915
+ POSTGRESQL_DIALECT.function_map['CHAR_TEXT'] = 'concat({0})'
2916
+ # A DECIMAL column read as the database's own text, under a schema's `decimal_text: database`
2917
+ # (sparql_front.held_under; Ontop's text: numeric(6,2) 1.50 is '1.50'). Not built elsewhere.
2918
+ DUCKDB_DIALECT.function_map['DATABASE_DECIMAL_TEXT'] = 'CAST({0} AS VARCHAR)'
2919
+ # A PostgreSQL NUMERIC read as its value (`entry._held`, a scan's condition `lower`): TRUE, or for 'NaN'
2920
+ # or an infinity, the values equal to themselves plus one (PostgreSQL's NaN equals NaN), no xsd:decimal,
2921
+ # a data error, {1} its message. Only PostgreSQL reads a column so.
2922
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT, DUCKDB_DIALECT, SQLSERVER_DIALECT, MYSQL_DIALECT,
2923
+ ORACLE_DIALECT_CONFIG):
2924
+ _config.function_map.setdefault('NUMERIC_CHECK', '({0} IS NULL OR TRUE)')
2925
+ POSTGRESQL_DIALECT.function_map['NUMERIC_CHECK'] = (
2926
+ "(CASE WHEN {0} = {0} + 1 THEN CAST({1} || CAST({0} AS TEXT) AS INTEGER) = 0 ELSE TRUE END)")
2927
+ # A NUMERIC 'NaN' or infinity is no xsd:decimal: a data error, {1} its message (`lower.decimal_text`).
2928
+ POSTGRESQL_DIALECT.function_map['DATABASE_DECIMAL_TEXT'] = (
2929
+ "(CASE WHEN CAST({0} AS TEXT) IN ('NaN', 'Infinity', '-Infinity') THEN CAST({1} || CAST({0} AS TEXT) AS INTEGER)::TEXT "
2930
+ "ELSE CAST({0} AS TEXT) END)")
2931
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
2932
+ _config.function_map['DATABASE_DECIMAL_TEXT'] = 'CAST({0} AS STRING)'
2933
+ # A string under a collation that is not binary, read under the binary one (`CypherType.collated`):
2934
+ # Spark and Databricks compare and match it by its collation otherwise (UTF8_LCASE: 'a' = 'A'; *_RTRIM:
2935
+ # 'a' = 'a '), and refuse RLIKE on it. Only they report such a type.
2936
+ for _config in (SQLSERVER_DIALECT, MYSQL_DIALECT, ORACLE_DIALECT_CONFIG):
2937
+ _config.function_map.setdefault('BINARY_TEXT', '{0}')
2938
+ # DuckDB (NOCASE, NOACCENT) and PostgreSQL (a nondeterministic collation, which also refuses `~`) read such a
2939
+ # column under their byte-order collation (`collations.mark_bytewise`); PostgreSQL's as text first, since a
2940
+ # CITEXT compares without case under any collation.
2941
+ DUCKDB_DIALECT.function_map['BINARY_TEXT'] = '({0} COLLATE "binary")'
2942
+ POSTGRESQL_DIALECT.function_map['BINARY_TEXT'] = '(CAST({0} AS TEXT) COLLATE "C")'
2943
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
2944
+ _config.function_map['BINARY_TEXT'] = '({0} COLLATE UTF8_BINARY)'
2945
+ # A uuid column read as its text (rule uuid-text): lower-case 8-4-4-4-12 on PostgreSQL and DuckDB; SQL Server's
2946
+ # uniqueidentifier is written upper case, and its default collation compares without case, so its text
2947
+ # is read under a binary one (UUID_TEXT_UPPER). UUID_VALUE reads a canonical text as the native value, so a
2948
+ # comparison is made on the column (an index serves it): `lower.eq`.
2949
+ DUCKDB_DIALECT.function_map['UUID_TEXT'] = 'CAST({0} AS VARCHAR)'
2950
+ POSTGRESQL_DIALECT.function_map['UUID_TEXT'] = 'CAST({0} AS TEXT)'
2951
+ SQLSERVER_DIALECT.function_map['UUID_TEXT_UPPER'] = '(CAST({0} AS VARCHAR(36)) COLLATE Latin1_General_100_BIN2)'
2952
+ DUCKDB_DIALECT.function_map['UUID_VALUE'] = 'CAST({0} AS UUID)'
2953
+ POSTGRESQL_DIALECT.function_map['UUID_VALUE'] = 'CAST({0} AS UUID)'
2954
+ SQLSERVER_DIALECT.function_map['UUID_VALUE'] = 'CAST({0} AS UNIQUEIDENTIFIER)'
2955
+ # A value of a type with no natural RDF datatype as the database's own text (`CypherType.textual`; R2RML 10.2).
2956
+ for _config, _cast in ((SPARK_DIALECT, 'STRING'), (DATABRICKS_SQL_DIALECT, 'STRING'), (DUCKDB_DIALECT, 'VARCHAR'),
2957
+ (POSTGRESQL_DIALECT, 'TEXT'), (MYSQL_DIALECT, 'CHAR'),
2958
+ (SQLSERVER_DIALECT, 'NVARCHAR(MAX)')):
2959
+ _config.function_map['CAST_TEXT'] = 'CAST({0} AS ' + _cast + ')'
2960
+ ORACLE_DIALECT_CONFIG.function_map['CAST_TEXT'] = 'TO_CHAR({0})'
2961
+ # Bytes as upper-case hex, xsd:hexBinary's canonical text.
2962
+ DUCKDB_DIALECT.function_map['HEX_TEXT'] = 'upper(hex({0}))'
2963
+ POSTGRESQL_DIALECT.function_map['HEX_TEXT'] = "upper(encode({0}, 'hex'))"
2964
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
2965
+ _config.function_map['HEX_TEXT'] = 'upper(hex({0}))'
2966
+ # A TIME column as xsd:time's text: hh:mm:ss, a fraction without trailing zeros (R2RML 10.2).
2967
+ for _config, _cast in ((DUCKDB_DIALECT, 'VARCHAR'), (POSTGRESQL_DIALECT, 'TEXT')):
2968
+ _config.function_map['XSD_TIME_TEXT'] = ("regexp_replace(CAST({0} AS " + _cast
2969
+ + r"), '(\.[0-9]*[1-9])0+$|\.0+$', '\1')")
2970
+ # Elsewhere a TIME column's text is not built: refused by name (SQLGenerationError), never sent as an unknown function.
2971
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT, SQLSERVER_DIALECT, MYSQL_DIALECT, ORACLE_DIALECT):
2972
+ _config.function_map['XSD_TIME_TEXT'] = None
2973
+ # A 32-bit float column's value as the double its shortest text names: 70.22, not
2974
+ # 70.22000122070312 (each engine writes a float's shortest digits: DuckDB, PostgreSQL 12+, Java's
2975
+ # Float.toString). SQL Server's and MySQL's FLOAT names differ in width: read as they are there.
2976
+ for _config in (SQLSERVER_DIALECT, MYSQL_DIALECT, ORACLE_DIALECT_CONFIG):
2977
+ _config.function_map.setdefault('FLOAT32_VALUE', '{0}')
2978
+ DUCKDB_DIALECT.function_map['FLOAT32_VALUE'] = 'CAST(CAST({0} AS VARCHAR) AS DOUBLE)'
2979
+ POSTGRESQL_DIALECT.function_map['FLOAT32_VALUE'] = 'CAST(CAST({0} AS TEXT) AS DOUBLE PRECISION)'
2980
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
2981
+ _config.function_map['FLOAT32_VALUE'] = 'CAST(CAST({0} AS STRING) AS DOUBLE)'
2982
+ _PG_LONG = '9223372036854775808'
2983
+ POSTGRESQL_DIALECT.function_map.update({
2984
+ 'TRY_INTEGER_ADD': f'(CASE WHEN ABS(CAST({{0}} AS NUMERIC) + CAST({{1}} AS NUMERIC)) < {_PG_LONG} THEN CAST({{0}} AS BIGINT) + CAST({{1}} AS BIGINT) END)',
2985
+ 'TRY_INTEGER_SUB': f'(CASE WHEN ABS(CAST({{0}} AS NUMERIC) - CAST({{1}} AS NUMERIC)) < {_PG_LONG} THEN CAST({{0}} AS BIGINT) - CAST({{1}} AS BIGINT) END)',
2986
+ 'TRY_INTEGER_MUL': f'(CASE WHEN ABS(CAST({{0}} AS NUMERIC) * CAST({{1}} AS NUMERIC)) < {_PG_LONG} THEN CAST({{0}} AS BIGINT) * CAST({{1}} AS BIGINT) END)',
2987
+ 'TRY_INTEGER_SUM': f'(CASE WHEN ABS(SUM({{0}})) < {_PG_LONG} THEN CAST(SUM({{0}}) AS BIGINT) END)',
2988
+ 'EXACT_INTEGER_SUM': 'CAST(SUM({0}) AS NUMERIC(38,0))',
2989
+ })
2990
+
2991
+ # An R2RML data error (R2RML 11), which aborts the query: the dialect's way to
2992
+ # raise from inside a statement, {0} the message and {1} the value. The value
2993
+ # is read so the expression is not a constant, which PostgreSQL's planner
2994
+ # would fold, raising for every row whatever the CASE around it says.
2995
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
2996
+ _config.function_map['DATA_ERROR'] = 'raise_error(concat({0}, CAST({1} AS STRING)))'
2997
+ DUCKDB_DIALECT.function_map['DATA_ERROR'] = 'error({0} || CAST({1} AS VARCHAR))'
2998
+ POSTGRESQL_DIALECT.function_map['DATA_ERROR'] = 'CAST({0} || CAST({1} AS TEXT) AS INTEGER)::TEXT'
2999
+
3000
+ # Cypher's split(s, d) (rule cypher-scalar-functions): s cut at each d, every piece kept (empty ones too,
3001
+ # '' is ['']), d a non-empty literal (`cypher_functions`). Spark's split reads a regex: d quoted.
3002
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3003
+ _config.function_map['CYPHER_SPLIT'] = "split({0}, CONCAT('\\\\Q', {1}, '\\\\E'), -1)"
3004
+ DUCKDB_DIALECT.function_map['CYPHER_SPLIT'] = 'string_split({0}, {1})'
3005
+ POSTGRESQL_DIALECT.function_map['CYPHER_SPLIT'] = (
3006
+ "(CASE WHEN {0} = '' THEN ARRAY['']::TEXT[] ELSE string_to_array({0}, {1}) END)")
3007
+
3008
+ # A text compared or ordered by code point (D8, rule codepoint-string-order; `lower.codepoint`):
3009
+ # DuckDB, Spark and Databricks compare text by code point already; PostgreSQL, MySQL and SQL
3010
+ # Server use the database's collation, which may be linguistic (en_US: 'a' < 'B' < 'b').
3011
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT, DUCKDB_DIALECT, POSTGRESQL_DIALECT,
3012
+ SQLSERVER_DIALECT, MYSQL_DIALECT, ORACLE_DIALECT_CONFIG):
3013
+ _config.function_map.setdefault('CODEPOINT_TEXT', '{0}')
3014
+ POSTGRESQL_DIALECT.function_map['CODEPOINT_TEXT'] = '({0} COLLATE "C")'
3015
+ MYSQL_DIALECT.function_map['CODEPOINT_TEXT'] = '({0} COLLATE utf8mb4_bin)'
3016
+ SQLSERVER_DIALECT.function_map['CODEPOINT_TEXT'] = '({0} COLLATE Latin1_General_100_BIN2)'
3017
+
3018
+ # An error raised from inside a statement: {0} the message.
3019
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3020
+ _config.function_map['RAISE_ERROR'] = 'raise_error({0})'
3021
+ DUCKDB_DIALECT.function_map['RAISE_ERROR'] = 'error({0})'
3022
+ # PostgreSQL has no raise function: the message cast to an integer fails with the message in the error. A
3023
+ # constant message would be folded while the statement is planned (a CASE arm too), raising whether or not a
3024
+ # row reaches it; the empty text of a STABLE setting appended keeps it to the rows that read it.
3025
+ POSTGRESQL_DIALECT.function_map['RAISE_ERROR'] = \
3026
+ "CAST(CAST({0} AS TEXT) || left(current_setting('server_version'), 0) AS INTEGER)::TEXT"
3027
+
3028
+ # GROUP_CONCAT over the group's distinct values (18.5.1.7 with DISTINCT).
3029
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3030
+ _config.function_map['GROUP_CONCAT_DISTINCT'] = "COALESCE(array_join(collect_set({0}), {1}), '')"
3031
+ for _config in (DUCKDB_DIALECT, POSTGRESQL_DIALECT):
3032
+ _config.function_map['GROUP_CONCAT_DISTINCT'] = "COALESCE(string_agg(DISTINCT {0}, {1}), '')"
3033
+
3034
+ # A text's double, NULL where it is none; and a whole DECIMAL as a 64-bit integer, NULL past it.
3035
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3036
+ _config.function_map['CAST_DOUBLE_TRY'] = 'try_cast({0} AS DOUBLE)'
3037
+ _config.function_map['TRY_CAST_LONG'] = 'try_cast({0} AS BIGINT)'
3038
+ DUCKDB_DIALECT.function_map['CAST_DOUBLE_TRY'] = 'TRY_CAST({0} AS DOUBLE)'
3039
+ DUCKDB_DIALECT.function_map['TRY_CAST_LONG'] = 'TRY_CAST({0} AS BIGINT)'
3040
+ POSTGRESQL_DIALECT.function_map['CAST_DOUBLE_TRY'] = (
3041
+ "(CASE WHEN {0} ~ '^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eE][+-]?[0-9]+)?$' OR {0} IN ('INF', '-INF', 'NaN') "
3042
+ "THEN CAST(REPLACE({0}, 'INF', 'Infinity') AS DOUBLE PRECISION) END)")
3043
+ POSTGRESQL_DIALECT.function_map['TRY_CAST_LONG'] = (
3044
+ "(CASE WHEN ABS({0}) < 9223372036854775808 THEN CAST({0} AS BIGINT) END)")
3045
+
3046
+ # A text's xsd:integer (64 bits) and xsd:decimal (DECIMAL(38, 18)) value, NULL where the text is
3047
+ # none (`lower.mixed_sum`): PostgreSQL has no TRY_CAST, so its text is matched first, leading zeros
3048
+ # not counted (DuckDB and Spark read '0000000000000000000000000000000000001' as 1).
3049
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3050
+ _config.function_map['TRY_INTEGER_TEXT'] = 'try_cast({0} AS BIGINT)'
3051
+ _config.function_map['TRY_DECIMAL_TEXT'] = 'try_cast({0} AS DECIMAL(38,18))'
3052
+ DUCKDB_DIALECT.function_map['TRY_INTEGER_TEXT'] = 'TRY_CAST({0} AS BIGINT)'
3053
+ DUCKDB_DIALECT.function_map['TRY_DECIMAL_TEXT'] = 'TRY_CAST({0} AS DECIMAL(38,18))'
3054
+ POSTGRESQL_DIALECT.function_map['TRY_INTEGER_TEXT'] = (
3055
+ "(CASE WHEN {0} ~ '^[+-]?0*[0-9]{{1,19}}$' THEN CASE WHEN CAST({0} AS NUMERIC) "
3056
+ "BETWEEN -9223372036854775808 AND 9223372036854775807 THEN CAST({0} AS BIGINT) END END)")
3057
+ POSTGRESQL_DIALECT.function_map['TRY_DECIMAL_TEXT'] = (
3058
+ "(CASE WHEN {0} ~ '^[+-]?0*([0-9]{{1,20}}([.][0-9]*)?|[.][0-9]+)$' THEN CAST({0} AS NUMERIC(38,18)) END)")
3059
+
3060
+ # A decimal held as DECIMAL({1},{2}) (a CEIL, FLOOR or ROUND of a decimal, scale 0).
3061
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT, DUCKDB_DIALECT):
3062
+ _config.function_map['DECIMAL_SIZED'] = 'CAST({0} AS DECIMAL({1},{2}))'
3063
+ POSTGRESQL_DIALECT.function_map['DECIMAL_SIZED'] = 'CAST({0} AS NUMERIC({1},{2}))'
3064
+ ORACLE_DIALECT_CONFIG.function_map['DECIMAL_SIZED'] = 'CAST({0} AS NUMBER({1},{2}))'
3065
+
3066
+ # A double's value as DECIMAL(38, 18), NULL where it is none (past 20 whole digits, INF, NaN):
3067
+ # the exact key that orders numbers of several datatypes by value (`lower.order_key`, D8).
3068
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3069
+ _config.function_map['TRY_DOUBLE_DECIMAL'] = 'try_cast({0} AS DECIMAL(38,18))'
3070
+ DUCKDB_DIALECT.function_map['TRY_DOUBLE_DECIMAL'] = 'TRY_CAST({0} AS DECIMAL(38,18))'
3071
+ POSTGRESQL_DIALECT.function_map['TRY_DOUBLE_DECIMAL'] = (
3072
+ "(CASE WHEN ABS({0}) < 1e20 THEN CAST({0} AS NUMERIC(38,18)) END)")
3073
+
3074
+ # Templates that read every argument they are given (`Dialect.render_function` refuses a surplus).
3075
+ EVERY_ARGUMENT = frozenset({"ARG_MIN_BY_KEYS", "ARG_MAX_BY_KEYS"})
3076
+
3077
+ # MIN and MAX of a mixed column (`lower._extreme_term`, D8): {0} from the group's row whose keys
3078
+ # {1} .. {5} (`lower.order_key`, ascending, NULL first) are least, or greatest. Text keys compare
3079
+ # by codepoint (PostgreSQL's "C" collation).
3080
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3081
+ _config.function_map['ARG_MIN_BY_KEYS'] = 'min_by({0}, struct({1}, {2}, {3}, {4}, {5}))'
3082
+ _config.function_map['ARG_MAX_BY_KEYS'] = 'max_by({0}, struct({1}, {2}, {3}, {4}, {5}))'
3083
+ DUCKDB_DIALECT.function_map['ARG_MIN_BY_KEYS'] = (
3084
+ 'first({0} ORDER BY {1} ASC NULLS FIRST, {2} ASC NULLS FIRST, {3} ASC NULLS FIRST, '
3085
+ '{4} ASC NULLS FIRST, {5} ASC NULLS FIRST)')
3086
+ DUCKDB_DIALECT.function_map['ARG_MAX_BY_KEYS'] = (
3087
+ 'first({0} ORDER BY {1} DESC NULLS LAST, {2} DESC NULLS LAST, {3} DESC NULLS LAST, '
3088
+ '{4} DESC NULLS LAST, {5} DESC NULLS LAST)')
3089
+ POSTGRESQL_DIALECT.function_map['ARG_MIN_BY_KEYS'] = (
3090
+ '(array_agg({0} ORDER BY {1} ASC NULLS FIRST, {2} ASC NULLS FIRST, {3} ASC NULLS FIRST, '
3091
+ '{4} COLLATE "C" ASC NULLS FIRST, {5} COLLATE "C" ASC NULLS FIRST))[1]')
3092
+ POSTGRESQL_DIALECT.function_map['ARG_MAX_BY_KEYS'] = (
3093
+ '(array_agg({0} ORDER BY {1} DESC NULLS LAST, {2} DESC NULLS LAST, {3} DESC NULLS LAST, '
3094
+ '{4} COLLATE "C" DESC NULLS LAST, {5} COLLATE "C" DESC NULLS LAST))[1]')
3095
+
3096
+ # A text's DATE, NULL where it is none (`lower._against_date`; the text is matched as a date first).
3097
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3098
+ _config.function_map['CAST_DATE_TRY'] = 'try_cast({0} AS DATE)'
3099
+ DUCKDB_DIALECT.function_map['CAST_DATE_TRY'] = 'TRY_CAST({0} AS DATE)'
3100
+ POSTGRESQL_DIALECT.function_map['CAST_DATE_TRY'] = (
3101
+ "(CASE WHEN {0} ~ '^[0-9]{{4}}-[0-9]{{2}}-[0-9]{{2}}$' THEN CAST({0} AS DATE) END)")
3102
+
3103
+ # ENCODE_FOR_URI (17.4.3.11: every UTF-8 byte but A-Z a-z 0-9 - . _ ~ as %XX), REPLACE's regex
3104
+ # replacement (`strings.replace_pieces`: {1}..{3} the pattern, {4}..{6} the replacement, for Java,
3105
+ # RE2 and PostgreSQL) and the hashes (17.4.6, lower-case hex of the UTF-8 text). A hash an engine
3106
+ # lacks is None: not built there (DuckDB has no SHA-384 or SHA-512, PostgreSQL no SHA-1 without
3107
+ # pgcrypto).
3108
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3109
+ _config.function_map.update({
3110
+ # Java's URLEncoder writes a space as + and leaves * and encodes ~.
3111
+ 'ENCODE_FOR_URI': "replace(replace(replace(url_encode({0}), '+', '%20'), '*', '%2A'), '%7E', '~')",
3112
+ 'REGEX_REPLACE': 'regexp_replace({0}, {1}, {4})',
3113
+ 'MD5': 'md5({0})', 'SHA1': 'sha1({0})', 'SHA256': 'sha2({0}, 256)',
3114
+ 'SHA384': 'sha2({0}, 384)', 'SHA512': 'sha2({0}, 512)',
3115
+ })
3116
+ DUCKDB_DIALECT.function_map.update({
3117
+ 'ENCODE_FOR_URI': 'url_encode({0})',
3118
+ 'REGEX_REPLACE': "regexp_replace({0}, {2}, {5}, 'g')",
3119
+ 'MD5': 'md5({0})', 'SHA1': 'sha1({0})', 'SHA256': 'sha256({0})', 'SHA384': None, 'SHA512': None,
3120
+ })
3121
+ POSTGRESQL_DIALECT.function_map.update({
3122
+ 'ENCODE_FOR_URI': (
3123
+ "(CASE WHEN {0} IS NOT NULL THEN (SELECT COALESCE(string_agg(CASE WHEN _b BETWEEN 65 AND 90 "
3124
+ "OR _b BETWEEN 97 AND 122 OR _b BETWEEN 48 AND 57 OR _b IN (45, 46, 95, 126) THEN chr(_b) "
3125
+ "ELSE '%' || upper(lpad(to_hex(_b), 2, '0')) END, '' ORDER BY _g), '') FROM (SELECT "
3126
+ "get_byte(convert_to({0}, 'UTF8'), _g) AS _b, _g FROM generate_series(0, "
3127
+ "octet_length(convert_to({0}, 'UTF8')) - 1) AS _g) AS _e) END)"),
3128
+ 'REGEX_REPLACE': "regexp_replace({0}, {3}, {6}, 'g')",
3129
+ 'MD5': 'md5({0})',
3130
+ # pgcrypto's digest, inline: the database's DBA runs CREATE EXTENSION pgcrypto (D6: ProGraph creates nothing).
3131
+ 'SHA1': "encode(digest(convert_to({0}, 'UTF8'), 'sha1'), 'hex')",
3132
+ 'SHA256': "encode(sha256(convert_to({0}, 'UTF8')), 'hex')",
3133
+ 'SHA384': "encode(sha384(convert_to({0}, 'UTF8')), 'hex')",
3134
+ 'SHA512': "encode(sha512(convert_to({0}, 'UTF8')), 'hex')",
3135
+ })
3136
+
3137
+ # A text's TIMESTAMP with no zone, NULL where it is none (`casts`; the text is matched as a
3138
+ # dateTime with no zone first).
3139
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3140
+ _config.function_map['CAST_TIMESTAMP_TRY'] = 'try_cast({0} AS TIMESTAMP_NTZ)'
3141
+ DUCKDB_DIALECT.function_map['CAST_TIMESTAMP_TRY'] = 'TRY_CAST({0} AS TIMESTAMP)'
3142
+ POSTGRESQL_DIALECT.function_map['CAST_TIMESTAMP_TRY'] = (
3143
+ "(CASE WHEN {0} ~ '^[0-9]{{4}}-[0-9]{{2}}-[0-9]{{2}}T[0-9]{{2}}:[0-9]{{2}}:[0-9]{{2}}' "
3144
+ "THEN CAST({0} AS TIMESTAMP) END)")
3145
+
3146
+ # A DATE's midnight as a TIMESTAMP with no zone: xsd:dateTime(?date) (F&O 3.1 19.1.4; `casts`,
3147
+ # M2 review B7).
3148
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3149
+ _config.function_map['DATE_AT_MIDNIGHT'] = 'CAST({0} AS TIMESTAMP_NTZ)'
3150
+ for _config in (DUCKDB_DIALECT, POSTGRESQL_DIALECT, ORACLE_DIALECT_CONFIG):
3151
+ _config.function_map['DATE_AT_MIDNIGHT'] = 'CAST({0} AS TIMESTAMP)'
3152
+ MYSQL_DIALECT.function_map['DATE_AT_MIDNIGHT'] = 'CAST({0} AS DATETIME)'
3153
+ SQLSERVER_DIALECT.function_map['DATE_AT_MIDNIGHT'] = 'CAST({0} AS DATETIME2)'
3154
+
3155
+ # Fresh values (plan/ir/fresh.py): a column's piece of a row's key (length-prefixed text, `~` for
3156
+ # NULL), a row's ordinal among equal keys, a UUID's text from 32 hex digits (version 4, variant 8),
3157
+ # and a double in [0, 1) from the first 13 (52 bits).
3158
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3159
+ _config.function_map.update({
3160
+ 'FRESH_PIECE': "COALESCE(CONCAT(CAST(length(CAST({0} AS STRING)) AS STRING), ':', CAST({0} AS STRING)), '~')",
3161
+ 'ROW_ORDINAL': 'ROW_NUMBER() OVER (PARTITION BY {0} ORDER BY {0})',
3162
+ 'ROW_ORDINAL_IN': 'ROW_NUMBER() OVER (PARTITION BY {0} ORDER BY {1})',
3163
+ 'UUID_OF_HEX': "regexp_replace({0}, '^(.{{8}})(.{{4}}).(.{{3}}).(.{{3}})(.{{12}})$', '$1-$2-4$3-8$4-$5')",
3164
+ 'UNIT_OF_HEX': 'CAST(CAST(conv(substr({0}, 1, 13), 16, 10) AS BIGINT) AS DOUBLE) / 4503599627370496.0',
3165
+ })
3166
+ DUCKDB_DIALECT.function_map.update({
3167
+ 'FRESH_PIECE': "COALESCE(CAST(length(CAST({0} AS VARCHAR)) AS VARCHAR) || ':' || CAST({0} AS VARCHAR), '~')",
3168
+ 'ROW_ORDINAL': 'ROW_NUMBER() OVER (PARTITION BY {0} ORDER BY {0})',
3169
+ 'ROW_ORDINAL_IN': 'ROW_NUMBER() OVER (PARTITION BY {0} ORDER BY {1})',
3170
+ 'UUID_OF_HEX': "regexp_replace({0}, '^(.{{8}})(.{{4}}).(.{{3}}).(.{{3}})(.{{12}})$', '\\1-\\2-4\\3-8\\4-\\5')",
3171
+ 'UNIT_OF_HEX': "CAST(CAST('0x' || substr({0}, 1, 13) AS BIGINT) AS DOUBLE) / 4503599627370496.0",
3172
+ })
3173
+ POSTGRESQL_DIALECT.function_map.update({
3174
+ 'FRESH_PIECE': "COALESCE(CAST(length(CAST({0} AS TEXT)) AS TEXT) || ':' || CAST({0} AS TEXT), '~')",
3175
+ 'ROW_ORDINAL': 'ROW_NUMBER() OVER (PARTITION BY {0} ORDER BY {0})',
3176
+ 'ROW_ORDINAL_IN': 'ROW_NUMBER() OVER (PARTITION BY {0} ORDER BY {1})',
3177
+ # A uuid's own text writes the dashes (regexp_replace costs 30x more here, measured 2026-11-10).
3178
+ 'UUID_OF_HEX': "CAST(CAST(overlay(overlay({0} placing '4' from 13 for 1) placing '8' from 17 for 1) AS uuid) AS TEXT)",
3179
+ 'UNIT_OF_HEX': "CAST(('x' || lpad(substr({0}, 1, 13), 16, '0'))::bit(64)::bigint AS DOUBLE PRECISION) / 4503599627370496.0",
3180
+ })
3181
+
3182
+ # Cypher's remainder of doubles (rule cypher-arithmetic): the sign of the dividend, as Java's. DuckDB's,
3183
+ # Spark's and Databricks' % of doubles is that; PostgreSQL has no % of double precision.
3184
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT, DUCKDB_DIALECT):
3185
+ _config.function_map['CYPHER_FLOAT_MOD'] = '(({0}) % ({1}))'
3186
+ POSTGRESQL_DIALECT.function_map['CYPHER_FLOAT_MOD'] = '(({0}) - ({1}) * trunc(({0}) / ({1})))'
3187
+
3188
+ # Cypher's lists (rule cypher-lists): an element in a list (null where it is not and the list holds a
3189
+ # null, as `= ANY` has it; the caller spells out Cypher's null cases); range(a, b, step), inclusive, []
3190
+ # where the step goes away from b, an error for a zero step or a null bound (openCypher: an error);
3191
+ # the element at a 1-based position known to be in the list.
3192
+ _RANGE_ERROR = "'ProGraph: range() of a null bound or a zero step'"
3193
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3194
+ _config.function_map['CYPHER_LIST_CONTAINS'] = 'array_contains({0}, {1})'
3195
+ _config.function_map['CYPHER_RANGE'] = (
3196
+ "(CASE WHEN ({0}) IS NULL OR ({1}) IS NULL OR ({2}) IS NULL OR ({2}) = 0 "
3197
+ f"THEN raise_error({_RANGE_ERROR}) "
3198
+ "WHEN ({2}) > 0 AND ({0}) > ({1}) OR ({2}) < 0 AND ({0}) < ({1}) "
3199
+ "THEN CAST(array() AS ARRAY<BIGINT>) ELSE sequence(CAST({0} AS BIGINT), CAST({1} AS BIGINT), "
3200
+ "CAST({2} AS BIGINT)) END)")
3201
+ _config.function_map['CYPHER_ELEMENT_AT'] = 'element_at({0}, CAST({1} AS INT))'
3202
+ DUCKDB_DIALECT.function_map['CYPHER_LIST_CONTAINS'] = 'list_contains({0}, {1})'
3203
+ DUCKDB_DIALECT.function_map['CYPHER_RANGE'] = (
3204
+ "(CASE WHEN ({0}) IS NULL OR ({1}) IS NULL OR ({2}) IS NULL OR ({2}) = 0 "
3205
+ f"THEN error({_RANGE_ERROR}) "
3206
+ "ELSE generate_series(CAST({0} AS BIGINT), CAST({1} AS BIGINT), CAST({2} AS BIGINT)) END)")
3207
+ DUCKDB_DIALECT.function_map['CYPHER_ELEMENT_AT'] = '(({0})[{1}])'
3208
+ POSTGRESQL_DIALECT.function_map['CYPHER_LIST_CONTAINS'] = '(({1}) = ANY({0}))'
3209
+ # PostgreSQL raises for a zero step itself; a null bound raises through a cast of the message (with every
3210
+ # bound read in it, so the planner folds it only where the whole CASE is constant, and drops a false arm first).
3211
+ POSTGRESQL_DIALECT.function_map['CYPHER_RANGE'] = (
3212
+ "(CASE WHEN ({0}) IS NULL OR ({1}) IS NULL OR ({2}) IS NULL "
3213
+ f"THEN ARRAY[CAST({_RANGE_ERROR} || COALESCE(CAST({{0}} AS TEXT), '') || COALESCE(CAST({{1}} AS TEXT), '') "
3214
+ "|| COALESCE(CAST({2} AS TEXT), '') AS BIGINT)] "
3215
+ "ELSE ARRAY(SELECT generate_series(CAST({0} AS BIGINT), CAST({1} AS BIGINT), CAST({2} AS BIGINT))) END)")
3216
+ POSTGRESQL_DIALECT.function_map['CYPHER_ELEMENT_AT'] = '(({0})[{1}])'
3217
+
3218
+ # APOC's list functions (rule cypher-apoc): the three the engines' list functions give and the Cypher list
3219
+ # home cannot build from filters and transforms. A list of lists one level flatter (no element null: the
3220
+ # caller raises first); a list of plain values ascending (no element null; NaN after every number; strings
3221
+ # by code point, the engines' binary order); its elements added as doubles left to right, from 0.0 (Java's
3222
+ # sum, rounding at each step as it does). PostgreSQL's arrays are not built for APOC yet.
3223
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3224
+ _config.function_map['CYPHER_LIST_FLATTEN'] = 'flatten({0})'
3225
+ _config.function_map['CYPHER_LIST_SORT'] = 'array_sort({0})'
3226
+ _config.function_map['CYPHER_LIST_SUM'] = (
3227
+ 'aggregate({0}, CAST(0 AS DOUBLE), (_sa, _sx) -> _sa + CAST(_sx AS DOUBLE))')
3228
+ DUCKDB_DIALECT.function_map['CYPHER_LIST_FLATTEN'] = 'flatten({0})'
3229
+ DUCKDB_DIALECT.function_map['CYPHER_LIST_SORT'] = "list_sort({0}, 'ASC')"
3230
+ DUCKDB_DIALECT.function_map['CYPHER_LIST_SUM'] = (
3231
+ 'list_reduce(list_prepend(CAST(0 AS DOUBLE), list_transform({0}, lambda _sx: CAST(_sx AS DOUBLE))), '
3232
+ 'lambda _sa, _sx: _sa + _sx)')
3233
+ # Each element once, in the order it first appears (equal by value, nulls one, lists element by element),
3234
+ # and a list's elements in (not in) another, each once, in its order. Each template reads each argument
3235
+ # once: an argument that is itself a filter or a concatenation is computed once, not per element (DuckDB
3236
+ # binds it to a lambda over a one-element list; Spark's set functions are hash sets). Spark's keep -0.0
3237
+ # apart from 0.0, so a list of floats there is de-duplicated by array_position (null, which it does not
3238
+ # find, by its first position).
3239
+ _DUCK_DISTINCT = "list_filter(_dl, lambda _dx, _di: list_position(_dl, _dx) = _di)"
3240
+ DUCKDB_DIALECT.function_map.update({
3241
+ 'CYPHER_LIST_DISTINCT': f"list_transform([{{0}}], lambda _dl: {_DUCK_DISTINCT})[1]",
3242
+ 'CYPHER_LIST_DISTINCT_FLOAT': f"list_transform([{{0}}], lambda _dl: {_DUCK_DISTINCT})[1]",
3243
+ 'CYPHER_LIST_INTERSECT': ("list_transform([{1}], lambda _ib: list_transform([list_filter({0}, lambda _ix: "
3244
+ f"list_position(_ib, _ix) IS NOT NULL)], lambda _dl: {_DUCK_DISTINCT})[1])[1]"),
3245
+ 'CYPHER_LIST_EXCEPT': ("list_transform([{1}], lambda _ib: list_transform([list_filter({0}, lambda _ix: "
3246
+ f"list_position(_ib, _ix) IS NULL)], lambda _dl: {_DUCK_DISTINCT})[1])[1]"),})
3247
+ DUCKDB_DIALECT.function_map['CYPHER_LIST_INTERSECT_FLOAT'] = DUCKDB_DIALECT.function_map['CYPHER_LIST_INTERSECT']
3248
+ DUCKDB_DIALECT.function_map['CYPHER_LIST_EXCEPT_FLOAT'] = DUCKDB_DIALECT.function_map['CYPHER_LIST_EXCEPT']
3249
+ _SPARK_DISTINCT_FLOAT = (
3250
+ "transform(array({0}), _dl -> filter(_dl, (_dx, _di) -> CASE WHEN _dx IS NULL THEN "
3251
+ "array_position(transform(_dl, _dn -> _dn IS NULL), true) = _di + 1 "
3252
+ "ELSE array_position(_dl, _dx) = _di + 1 END))[0]")
3253
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3254
+ _config.function_map.update({
3255
+ 'CYPHER_LIST_DISTINCT': 'array_distinct({0})',
3256
+ 'CYPHER_LIST_DISTINCT_FLOAT': _SPARK_DISTINCT_FLOAT,
3257
+ 'CYPHER_LIST_INTERSECT': 'array_intersect({0}, {1})',
3258
+ 'CYPHER_LIST_EXCEPT': 'array_except({0}, {1})', 'CYPHER_LIST_INTERSECT_FLOAT': (
3259
+ "transform(array({1}), _ib -> " + _SPARK_DISTINCT_FLOAT.replace("{0}", "filter({0}, _ix -> exists(_ib, _iy -> _iy <=> _ix))") + ")[0]"),
3260
+ 'CYPHER_LIST_EXCEPT_FLOAT': (
3261
+ "transform(array({1}), _ib -> " + _SPARK_DISTINCT_FLOAT.replace("{0}", "filter({0}, _ix -> NOT exists(_ib, _iy -> _iy <=> _ix))") + ")[0]"),
3262
+ })
3263
+ # APOC's text functions (rules cypher-apoc, java-regex). A pattern and a replacement come as {1}/{3} in
3264
+ # Java's syntax (Spark and Databricks read it themselves) and {2}/{4} rewritten for RE2 (DuckDB). Splitting
3265
+ # keeps every piece (Java's negative limit; the caller drops trailing empty pieces where APOC's limit is 0);
3266
+ # a list's strings joined; each match in order; Levenshtein's distance by character (DuckDB's levenshtein
3267
+ # counts bytes, so a text that is not ASCII takes the dynamic programme, row by row); UTF-8 bytes; repeat;
3268
+ # translate.
3269
+ _DUCK_LEVENSHTEIN = (
3270
+ "list_reduce(list_transform(range(1, length({0}) + 1), lambda _li: [_li]), "
3271
+ "lambda _lp, _lx: list_reduce(list_transform(range(1, length({1}) + 1), lambda _lj: [_lj]), "
3272
+ "lambda _lr, _ly: list_append(_lr, least(_lp[_ly[1] + 1] + 1, _lr[_ly[1]] + 1, _lp[_ly[1]] + "
3273
+ "CASE WHEN substr({0}, _lx[1], 1) = substr({1}, _ly[1], 1) THEN 0 ELSE 1 END)), [_lx[1]]), "
3274
+ "range(0, length({1}) + 1))[length({1}) + 1]")
3275
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3276
+ _config.function_map.update({
3277
+ 'CYPHER_REGEX_SPLIT': 'split({0}, {1}, -1)',
3278
+ 'CYPHER_REGEX_SPLIT_LIMIT': 'split({0}, {1}, CAST({3} AS INT))',
3279
+ 'CYPHER_REGEX_REPLACE': 'regexp_replace({0}, {1}, {3})',
3280
+ 'CYPHER_REGEX_ALL': 'regexp_extract_all({0}, {1}, 0)',
3281
+ 'CYPHER_TRIM_TRAILING_EMPTY': (
3282
+ "transform(array({0}), _sl -> CASE WHEN size(_sl) <= 1 THEN _sl ELSE slice(_sl, 1, "
3283
+ "coalesce(array_max(filter(sequence(1, size(_sl)), _si -> element_at(_sl, _si) <> '')), 0)) END)[0]"),
3284
+ 'CYPHER_STR_JOIN': 'array_join({0}, {1})',
3285
+ 'CYPHER_LEVENSHTEIN': 'CAST(levenshtein({0}, {1}) AS BIGINT)',
3286
+ 'CYPHER_UTF8_BYTES': 'CAST(octet_length({0}) AS BIGINT)',
3287
+ 'CYPHER_REPEAT': 'repeat({0}, CAST({1} AS INT))',
3288
+ 'CYPHER_TRANSLATE': 'translate({0}, {1}, {2})',
3289
+ })
3290
+ DUCKDB_DIALECT.function_map.update({
3291
+ 'CYPHER_REGEX_SPLIT': 'string_split_regex({0}, {2})',
3292
+ 'CYPHER_REGEX_SPLIT_LIMIT': None,
3293
+ 'CYPHER_REGEX_REPLACE': "regexp_replace({0}, {2}, {4}, 'g')",
3294
+ 'CYPHER_REGEX_ALL': 'regexp_extract_all({0}, {2})',
3295
+ 'CYPHER_TRIM_TRAILING_EMPTY': (
3296
+ "list_transform([{0}], lambda _sl: CASE WHEN len(_sl) <= 1 THEN _sl ELSE _sl[1:coalesce(list_max("
3297
+ "list_filter(range(1, len(_sl) + 1), lambda _si: _sl[_si] <> '')), 0)] END)[1]"),
3298
+ 'CYPHER_STR_JOIN': 'array_to_string({0}, {1})',
3299
+ 'CYPHER_LEVENSHTEIN': (f"CAST(CASE WHEN regexp_matches({{0}} || {{1}}, '[^\\x00-\\x7f]') THEN {_DUCK_LEVENSHTEIN} "
3300
+ "ELSE levenshtein({0}, {1}) END AS BIGINT)"),
3301
+ 'CYPHER_UTF8_BYTES': 'CAST(strlen({0}) AS BIGINT)',
3302
+ 'CYPHER_REPEAT': 'repeat({0}, CAST({1} AS BIGINT))',
3303
+ 'CYPHER_TRANSLATE': 'translate({0}, {1}, {2})',
3304
+ })
3305
+ # APOC's date functions (rules cypher-apoc, java-datetime-pattern): a date's day of the year, ISO week-based
3306
+ # year and week (days since 1970-01-01); a local date-time's milliseconds since 1970-01-01T00:00 parsed from
3307
+ # text by the engine's own pattern ({1} DuckDB's strptime format, {2} Spark's datetime pattern), an error
3308
+ # for text the pattern does not read.
3309
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3310
+ _config.function_map.update({
3311
+ 'CYPHER_DATE_DOY': "CAST(dayofyear(date_add(DATE'1970-01-01', CAST({0} AS INT))) AS BIGINT)",
3312
+ 'CYPHER_DATE_ISO_WEEK': "CAST(weekofyear(date_add(DATE'1970-01-01', CAST({0} AS INT))) AS BIGINT)",
3313
+ 'CYPHER_DATE_ISO_YEAR': ("CAST(year(date_add(DATE'1970-01-01', CAST({0} AS INT) + 3 - "
3314
+ "weekday(date_add(DATE'1970-01-01', CAST({0} AS INT))))) AS BIGINT)"),
3315
+ 'CYPHER_PARSE_LOCAL_MS': ("timestampdiff(MILLISECOND, TIMESTAMP_NTZ'1970-01-01 00:00:00', "
3316
+ "to_timestamp_ntz({0}, {2}))"),
3317
+ 'CYPHER_TRIM_ZEROS': "TRIM(TRAILING '0' FROM {0})",
3318
+ 'CYPHER_MS_TO_NS': "CAST({0} AS DECIMAL(38,0)) * 1000000",
3319
+ })
3320
+ DUCKDB_DIALECT.function_map.update({
3321
+ 'CYPHER_DATE_DOY': "CAST(dayofyear(DATE '1970-01-01' + CAST({0} AS INTEGER)) AS BIGINT)",
3322
+ 'CYPHER_DATE_ISO_WEEK': "CAST(weekofyear(DATE '1970-01-01' + CAST({0} AS INTEGER)) AS BIGINT)",
3323
+ 'CYPHER_DATE_ISO_YEAR': "CAST(isoyear(DATE '1970-01-01' + CAST({0} AS INTEGER)) AS BIGINT)",
3324
+ 'CYPHER_PARSE_LOCAL_MS': "epoch_ms(strptime({0}, {1}))",
3325
+ 'CYPHER_TRIM_ZEROS': "TRIM(TRAILING '0' FROM {0})",
3326
+ 'CYPHER_MS_TO_NS': "CAST({0} AS DECIMAL(38,0)) * 1000000",
3327
+ })
3328
+ for _name in ('CYPHER_LIST_FLATTEN', 'CYPHER_LIST_SORT', 'CYPHER_LIST_SUM', 'CYPHER_LIST_DISTINCT',
3329
+ 'CYPHER_LIST_DISTINCT_FLOAT', 'CYPHER_LIST_INTERSECT', 'CYPHER_LIST_EXCEPT',
3330
+ 'CYPHER_LIST_INTERSECT_FLOAT', 'CYPHER_LIST_EXCEPT_FLOAT', 'CYPHER_REGEX_SPLIT',
3331
+ 'CYPHER_REGEX_SPLIT_LIMIT', 'CYPHER_REGEX_REPLACE', 'CYPHER_REGEX_ALL', 'CYPHER_TRIM_TRAILING_EMPTY',
3332
+ 'CYPHER_STR_JOIN', 'CYPHER_LEVENSHTEIN', 'CYPHER_UTF8_BYTES', 'CYPHER_REPEAT', 'CYPHER_TRANSLATE',
3333
+ 'CYPHER_DATE_DOY', 'CYPHER_DATE_ISO_WEEK', 'CYPHER_DATE_ISO_YEAR', 'CYPHER_PARSE_LOCAL_MS',
3334
+ 'CYPHER_TRIM_ZEROS', 'CYPHER_MS_TO_NS'):
3335
+ POSTGRESQL_DIALECT.function_map[_name] = None
3336
+
3337
+ # Cypher's maps (rule cypher-maps): a key of a map value, typed. A struct on DuckDB, Spark and Databricks;
3338
+ # a jsonb object on PostgreSQL (`NAMED_STRUCT`), read as text and cast.
3339
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3340
+ for _t in ('LONG', 'DOUBLE', 'STRING', 'BOOLEAN'):
3341
+ _config.function_map[f'CYPHER_FIELD_{_t}'] = '({0})[{1}]'
3342
+ for _t in ('LONG', 'DOUBLE', 'STRING', 'BOOLEAN'):
3343
+ DUCKDB_DIALECT.function_map[f'CYPHER_FIELD_{_t}'] = 'struct_extract({0}, {1})'
3344
+ POSTGRESQL_DIALECT.function_map.update({
3345
+ 'CYPHER_FIELD_LONG': 'CAST(({0}) ->> {1} AS BIGINT)',
3346
+ 'CYPHER_FIELD_DOUBLE': 'CAST(({0}) ->> {1} AS DOUBLE PRECISION)',
3347
+ 'CYPHER_FIELD_STRING': '(({0}) ->> {1})',
3348
+ 'CYPHER_FIELD_BOOLEAN': 'CAST(({0}) ->> {1} AS BOOLEAN)',
3349
+ })
3350
+
3351
+ # The properties of a node or relationship as one map of the keys it has (rule cypher-result-values): a
3352
+ # MAP on DuckDB, `map_from_arrays` on Spark and Databricks, a jsonb object with its nulls stripped on
3353
+ # PostgreSQL (its `NAMED_STRUCT`).
3354
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3355
+ _config.function_map['CYPHER_MAP'] = 'map_from_arrays({0}, {1})'
3356
+ DUCKDB_DIALECT.function_map['CYPHER_MAP'] = 'MAP({0}, {1})'
3357
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3358
+ _config.function_map['MAP_ENTRIES'] = 'map_entries({0})'
3359
+ _config.function_map['MAP_FROM_ENTRIES'] = 'map_from_entries({0})'
3360
+ POSTGRESQL_DIALECT.function_map['CYPHER_MAP'] = None
3361
+ POSTGRESQL_DIALECT.function_map['CYPHER_STRIP_NULLS'] = 'jsonb_strip_nulls({0})'
3362
+ # A key of that map, read back from a node or relationship held as a value (a list's element): null where
3363
+ # the map lacks the key (Spark's try_element_at, which ANSI mode does not raise on); PostgreSQL reads its
3364
+ # jsonb object's `properties` and casts the key's text (`CYPHER_FIELD_<type>`).
3365
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3366
+ _config.function_map['CYPHER_MAP_VALUE'] = 'try_element_at({0}, {1})'
3367
+ DUCKDB_DIALECT.function_map['CYPHER_MAP_VALUE'] = 'map_extract_value({0}, {1})'
3368
+ POSTGRESQL_DIALECT.function_map['CYPHER_MAP_VALUE'] = None
3369
+ POSTGRESQL_DIALECT.function_map['CYPHER_JSON_GET'] = '(({0}) -> {1})'
3370
+ POSTGRESQL_DIALECT.function_map['JSONB_EMPTY_ARRAY'] = 'CAST(ARRAY[] AS JSONB[])'
3371
+ # A Cypher value of several types (rule cypher-mixed-values) on PostgreSQL, its tagged struct a jsonb object: the
3372
+ # answer's text, the kind's tag then the member's text (`cypher_mixed.answer_text`); a sort key's five parts as one
3373
+ # row, compared field by field (`cypher_mixed._key`).
3374
+ POSTGRESQL_DIALECT.function_map['CYPHER_MIXED_TEXT'] = (
3375
+ "((({0}) ->> 'pg_kind') || COALESCE(({0}) ->> 'pg_int', ({0}) ->> 'pg_float', ({0}) ->> 'pg_str', "
3376
+ "({0}) ->> 'pg_bool'))")
3377
+ POSTGRESQL_DIALECT.function_map['CYPHER_ORDER_ROW'] = 'ROW({0}, {1}, {2}, {3}, {4})'
3378
+ # min() and max() of such values: the first non-null value in the key's order, ties in the jsonb's own order.
3379
+ POSTGRESQL_DIALECT.function_map['CYPHER_FIRST_BY'] = (
3380
+ '(array_agg({0} ORDER BY {1} ASC, {0} ASC) FILTER (WHERE ({0}) IS NOT NULL))[1]')
3381
+ POSTGRESQL_DIALECT.function_map['CYPHER_FIRST_BY_DESC'] = (
3382
+ '(array_agg({0} ORDER BY {1} DESC, {0} DESC) FILTER (WHERE ({0}) IS NOT NULL))[1]')
3383
+
3384
+ # Cypher's list equality (rule cypher-lists): over the positions both lists have (the caller compares
3385
+ # sizes first), the count of pairs that differ with both elements present, of pairs where an element is
3386
+ # null, and of pairs where both are present (lists of incomparable types: every such pair differs).
3387
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3388
+ _pos = "(CASE WHEN size({0}) = 0 THEN CAST(array() AS ARRAY<INT>) ELSE sequence(1, size({0})) END)"
3389
+ _config.function_map['CYPHER_LIST_MISMATCHES'] = (
3390
+ f"size(filter({_pos}, _i -> element_at({{0}}, _i) <> element_at({{1}}, _i)))")
3391
+ _config.function_map['CYPHER_LIST_NULL_PAIRS'] = (
3392
+ f"size(filter({_pos}, _i -> element_at({{0}}, _i) IS NULL OR element_at({{1}}, _i) IS NULL))")
3393
+ _config.function_map['CYPHER_LIST_VALUE_PAIRS'] = (
3394
+ f"size(filter({_pos}, _i -> element_at({{0}}, _i) IS NOT NULL AND element_at({{1}}, _i) IS NOT NULL))")
3395
+ DUCKDB_DIALECT.function_map.update({
3396
+ 'CYPHER_LIST_MISMATCHES': "len(list_filter(range(1, len({0}) + 1), lambda _i: ({0})[_i] <> ({1})[_i]))",
3397
+ 'CYPHER_LIST_NULL_PAIRS': "len(list_filter(range(1, len({0}) + 1), lambda _i: ({0})[_i] IS NULL OR ({1})[_i] IS NULL))",
3398
+ 'CYPHER_LIST_VALUE_PAIRS': "len(list_filter(range(1, len({0}) + 1), lambda _i: ({0})[_i] IS NOT NULL AND ({1})[_i] IS NOT NULL))",
3399
+ })
3400
+ POSTGRESQL_DIALECT.function_map.update({
3401
+ 'CYPHER_LIST_MISMATCHES': "(SELECT count(*) FROM generate_series(1, cardinality({0})) AS _i WHERE ({0})[_i] <> ({1})[_i])",
3402
+ 'CYPHER_LIST_NULL_PAIRS': "(SELECT count(*) FROM generate_series(1, cardinality({0})) AS _i WHERE ({0})[_i] IS NULL OR ({1})[_i] IS NULL)",
3403
+ 'CYPHER_LIST_VALUE_PAIRS': "(SELECT count(*) FROM generate_series(1, cardinality({0})) AS _i WHERE ({0})[_i] IS NOT NULL AND ({1})[_i] IS NOT NULL)",
3404
+ })
3405
+
3406
+ # Cypher's dates (rule cypher-temporal): a date held as its days since 1970-01-01, as ISO 8601 text and
3407
+ # its year, month and day by the engine's calendar (years 1 to 9999).
3408
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3409
+ _config.function_map.update({
3410
+ 'CYPHER_DATE_TEXT': "date_format(date_add(DATE'1970-01-01', CAST({0} AS INT)), 'yyyy-MM-dd')",
3411
+ 'CYPHER_DATE_YEAR': "CAST(year(date_add(DATE'1970-01-01', CAST({0} AS INT))) AS BIGINT)",
3412
+ 'CYPHER_DATE_MONTH': "CAST(month(date_add(DATE'1970-01-01', CAST({0} AS INT))) AS BIGINT)",
3413
+ 'CYPHER_DATE_DAY': "CAST(day(date_add(DATE'1970-01-01', CAST({0} AS INT))) AS BIGINT)",
3414
+ })
3415
+ DUCKDB_DIALECT.function_map.update({
3416
+ 'CYPHER_DATE_TEXT': "strftime(DATE '1970-01-01' + CAST({0} AS INTEGER), '%Y-%m-%d')",
3417
+ 'CYPHER_DATE_YEAR': "CAST(year(DATE '1970-01-01' + CAST({0} AS INTEGER)) AS BIGINT)",
3418
+ 'CYPHER_DATE_MONTH': "CAST(month(DATE '1970-01-01' + CAST({0} AS INTEGER)) AS BIGINT)",
3419
+ 'CYPHER_DATE_DAY': "CAST(day(DATE '1970-01-01' + CAST({0} AS INTEGER)) AS BIGINT)",
3420
+ })
3421
+ POSTGRESQL_DIALECT.function_map.update({
3422
+ 'CYPHER_DATE_TEXT': "to_char(DATE '1970-01-01' + CAST({0} AS INTEGER), 'YYYY-MM-DD')",
3423
+ 'CYPHER_DATE_YEAR': "CAST(EXTRACT(YEAR FROM DATE '1970-01-01' + CAST({0} AS INTEGER)) AS BIGINT)",
3424
+ 'CYPHER_DATE_MONTH': "CAST(EXTRACT(MONTH FROM DATE '1970-01-01' + CAST({0} AS INTEGER)) AS BIGINT)",
3425
+ 'CYPHER_DATE_DAY': "CAST(EXTRACT(DAY FROM DATE '1970-01-01' + CAST({0} AS INTEGER)) AS BIGINT)",
3426
+ })
3427
+
3428
+ # Cypher's local times (rule cypher-temporal): nanoseconds of the day as ISO 8601 text, HH:MM, then :SS
3429
+ # where the seconds or their fraction are not zero, then the fraction without its trailing zeros.
3430
+ def _localtime_text(div, mod, cast: str, trim) -> str:
3431
+ h = f"lpad(CAST({div('{0}', '3600000000000')} AS {cast}), 2, '0')"
3432
+ mi = f"lpad(CAST({mod(div('{0}', '60000000000'), '60')} AS {cast}), 2, '0')"
3433
+ se = f"lpad(CAST({mod(div('{0}', '1000000000'), '60')} AS {cast}), 2, '0')"
3434
+ fr = trim(f"lpad(CAST({mod('{0}', '1000000000')} AS {cast}), 9, '0')")
3435
+ return (f"({h} || ':' || {mi} || CASE WHEN {mod('{0}', '60000000000')} = 0 THEN '' ELSE ':' || {se} || "
3436
+ f"CASE WHEN {mod('{0}', '1000000000')} = 0 THEN '' ELSE '.' || {fr} END END)")
3437
+
3438
+
3439
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3440
+ _config.function_map['CYPHER_LOCALTIME_TEXT'] = _localtime_text(
3441
+ lambda a, b: f"(({a}) div {b})", lambda a, b: f"(({a}) % {b})", "STRING",
3442
+ lambda x: f"trim(TRAILING '0' FROM {x})")
3443
+ DUCKDB_DIALECT.function_map['CYPHER_LOCALTIME_TEXT'] = _localtime_text(
3444
+ lambda a, b: f"(({a}) // {b})", lambda a, b: f"(({a}) % {b})", "VARCHAR", lambda x: f"rtrim({x}, '0')")
3445
+ POSTGRESQL_DIALECT.function_map['CYPHER_LOCALTIME_TEXT'] = _localtime_text(
3446
+ lambda a, b: f"div({a}, {b})", lambda a, b: f"mod({a}, {b})", "TEXT", lambda x: f"rtrim({x}, '0')")
3447
+
3448
+ # Cypher's local date-times (rule cypher-temporal): DECIMAL(38,0) nanoseconds since 1970-01-01T00:00, split
3449
+ # into days (floor) and nanoseconds of the day, exactly.
3450
+ _NS_DAY = '86400000000000'
3451
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3452
+ _config.function_map['CYPHER_NS_OF_DAY'] = f"CAST(pmod({{0}}, {_NS_DAY}) AS BIGINT)"
3453
+ _config.function_map['CYPHER_NS_DAYS'] = f"CAST(({{0}} - pmod({{0}}, {_NS_DAY})) div {_NS_DAY} AS BIGINT)"
3454
+ DUCKDB_DIALECT.function_map['CYPHER_NS_OF_DAY'] = (
3455
+ f"CAST(((CAST({{0}} AS HUGEINT) % {_NS_DAY}) + {_NS_DAY}) % {_NS_DAY} AS BIGINT)")
3456
+ DUCKDB_DIALECT.function_map['CYPHER_NS_DAYS'] = (
3457
+ f"CAST((CAST({{0}} AS HUGEINT) - (((CAST({{0}} AS HUGEINT) % {_NS_DAY}) + {_NS_DAY}) % {_NS_DAY})) // {_NS_DAY} AS BIGINT)")
3458
+ POSTGRESQL_DIALECT.function_map['CYPHER_NS_OF_DAY'] = f"CAST(mod(mod({{0}}, {_NS_DAY}) + {_NS_DAY}, {_NS_DAY}) AS BIGINT)"
3459
+ POSTGRESQL_DIALECT.function_map['CYPHER_NS_DAYS'] = (
3460
+ f"CAST(div({{0}} - mod(mod({{0}}, {_NS_DAY}) + {_NS_DAY}, {_NS_DAY}), {_NS_DAY}) AS BIGINT)")
3461
+
3462
+ # Cypher's times and date-times (rule cypher-temporal): a struct's DECIMAL part read exactly (jsonb on
3463
+ # PostgreSQL as NUMERIC, never through a double); an offset in seconds as ISO 8601 text (Z for zero,
3464
+ # +HH:MM, :SS where the seconds are not zero).
3465
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3466
+ _config.function_map['CYPHER_FIELD_DECIMAL'] = '({0})[{1}]'
3467
+ DUCKDB_DIALECT.function_map['CYPHER_FIELD_DECIMAL'] = 'struct_extract({0}, {1})'
3468
+ # A map's list or map, as the engine's own value (a jsonb one on PostgreSQL is not read back yet).
3469
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3470
+ _config.function_map['CYPHER_FIELD_VALUE'] = '({0})[{1}]'
3471
+ DUCKDB_DIALECT.function_map['CYPHER_FIELD_VALUE'] = 'struct_extract({0}, {1})'
3472
+ POSTGRESQL_DIALECT.function_map['CYPHER_FIELD_VALUE'] = None
3473
+ POSTGRESQL_DIALECT.function_map['CYPHER_FIELD_DECIMAL'] = 'CAST(({0}) ->> {1} AS NUMERIC(38,0))'
3474
+
3475
+
3476
+ def _offset_text(div, mod, cast: str) -> str:
3477
+ a = "abs({0})"
3478
+ hh = f"lpad(CAST({div(a, '3600')} AS {cast}), 2, '0')"
3479
+ mm = f"lpad(CAST({mod(div(a, '60'), '60')} AS {cast}), 2, '0')"
3480
+ ss = f"lpad(CAST({mod(a, '60')} AS {cast}), 2, '0')"
3481
+ return (f"(CASE WHEN {{0}} = 0 THEN 'Z' ELSE (CASE WHEN {{0}} < 0 THEN '-' ELSE '+' END) || {hh} || ':' || {mm} || "
3482
+ f"CASE WHEN {mod(a, '60')} = 0 THEN '' ELSE ':' || {ss} END END)")
3483
+
3484
+
3485
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3486
+ _config.function_map['CYPHER_OFFSET_TEXT'] = _offset_text(lambda a, b: f"(({a}) div {b})", lambda a, b: f"(({a}) % {b})", "STRING")
3487
+ DUCKDB_DIALECT.function_map['CYPHER_OFFSET_TEXT'] = _offset_text(lambda a, b: f"(({a}) // {b})", lambda a, b: f"(({a}) % {b})", "VARCHAR")
3488
+ POSTGRESQL_DIALECT.function_map['CYPHER_OFFSET_TEXT'] = _offset_text(lambda a, b: f"div({a}, {b})", lambda a, b: f"mod({a}, {b})", "TEXT")
3489
+
3490
+ # Cypher's temporal arithmetic (rule cypher-temporal-arithmetic): exact wide integers (DECIMAL(38,0); NUMERIC
3491
+ # on PostgreSQL) and their quotient and remainder truncated toward zero; a duration's text from its months,
3492
+ # days, seconds and nanoseconds (the nanoseconds in [0, 10^9)), as `CypherDuration.text` writes it.
3493
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3494
+ _config.function_map['CYPHER_XINT'] = 'CAST({0} AS DECIMAL(38,0))'
3495
+ _config.function_map['CYPHER_XTDIV'] = 'CAST(CAST({0} AS DECIMAL(38,0)) div CAST({1} AS DECIMAL(38,0)) AS DECIMAL(38,0))'
3496
+ _config.function_map['CYPHER_XMOD'] = 'CAST(CAST({0} AS DECIMAL(38,0)) % CAST({1} AS DECIMAL(38,0)) AS DECIMAL(38,0))'
3497
+ DUCKDB_DIALECT.function_map['CYPHER_XINT'] = 'CAST({0} AS DECIMAL(38,0))'
3498
+ DUCKDB_DIALECT.function_map['CYPHER_XTDIV'] = 'CAST(CAST({0} AS HUGEINT) // CAST({1} AS HUGEINT) AS DECIMAL(38,0))'
3499
+ DUCKDB_DIALECT.function_map['CYPHER_XMOD'] = 'CAST(CAST({0} AS HUGEINT) % CAST({1} AS HUGEINT) AS DECIMAL(38,0))'
3500
+ POSTGRESQL_DIALECT.function_map['CYPHER_XINT'] = 'CAST({0} AS NUMERIC(38,0))'
3501
+ POSTGRESQL_DIALECT.function_map['CYPHER_XTDIV'] = 'div(CAST({0} AS NUMERIC), CAST({1} AS NUMERIC))'
3502
+ POSTGRESQL_DIALECT.function_map['CYPHER_XMOD'] = 'mod(CAST({0} AS NUMERIC), CAST({1} AS NUMERIC))'
3503
+
3504
+
3505
+ # A date (days since 1970-01-01) plus a number of months by the engine's calendar, the day clamped to the
3506
+ # new month's end as Java's plusMonths (Spark 3's add_months no longer moves a month's last day to the
3507
+ # next month's last); the floor quotient and remainder of 64-bit integers by a positive constant.
3508
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3509
+ _config.function_map['CYPHER_DAYS_PLUS_MONTHS'] = (
3510
+ "CAST(datediff(add_months(date_add(DATE'1970-01-01', CAST({0} AS INT)), CAST({1} AS INT)), DATE'1970-01-01') AS BIGINT)")
3511
+ _config.function_map['CYPHER_FLOOR_DIV'] = "CAST((({0}) - pmod({0}, {1})) div {1} AS BIGINT)"
3512
+ _config.function_map['CYPHER_FLOOR_MOD'] = "CAST(pmod({0}, {1}) AS BIGINT)"
3513
+ DUCKDB_DIALECT.function_map['CYPHER_DAYS_PLUS_MONTHS'] = (
3514
+ "CAST(CAST((DATE '1970-01-01' + CAST({0} AS INTEGER)) + to_months(CAST({1} AS INTEGER)) AS DATE) - DATE '1970-01-01' AS BIGINT)")
3515
+ DUCKDB_DIALECT.function_map['CYPHER_FLOOR_DIV'] = "CAST((({0}) - ((({0}) % {1}) + {1}) % {1}) // {1} AS BIGINT)"
3516
+ DUCKDB_DIALECT.function_map['CYPHER_FLOOR_MOD'] = "CAST(((({0}) % {1}) + {1}) % {1} AS BIGINT)"
3517
+ POSTGRESQL_DIALECT.function_map['CYPHER_DAYS_PLUS_MONTHS'] = (
3518
+ "CAST(CAST((DATE '1970-01-01' + CAST({0} AS INTEGER)) + make_interval(months => CAST({1} AS INTEGER)) AS DATE)"
3519
+ " - DATE '1970-01-01' AS BIGINT)")
3520
+ POSTGRESQL_DIALECT.function_map['CYPHER_FLOOR_DIV'] = "CAST(div(({0}) - mod(mod({0}, {1}) + {1}, {1}), {1}) AS BIGINT)"
3521
+ POSTGRESQL_DIALECT.function_map['CYPHER_FLOOR_MOD'] = "CAST(mod(mod({0}, {1}) + {1}, {1}) AS BIGINT)"
3522
+
3523
+
3524
+ def _duration_text(div, mod, cast: str, trim) -> str:
3525
+ """{0} months, {1} days, {2} seconds, {3} nanoseconds: P[nY][nM][nD][T[nH][nM][n[.f]S]], each part with
3526
+ the sign of its whole (the seconds' and nanoseconds' together), PT0S for none. `div` and `mod`
3527
+ truncate toward zero."""
3528
+ def num(x: str) -> str:
3529
+ return f"CAST({x} AS {cast})"
3530
+ years, months = div('{0}', '12'), mod('{0}', '12')
3531
+ secs = "(CASE WHEN {2} >= 0 THEN {2} WHEN {3} = 0 THEN -({2}) ELSE -({2}) - 1 END)"
3532
+ nanos = "(CASE WHEN {2} >= 0 OR {3} = 0 THEN {3} ELSE 1000000000 - ({3}) END)"
3533
+ sign = "(CASE WHEN {2} < 0 THEN '-' ELSE '' END)"
3534
+ h, mi, se = div(secs, '3600'), mod(div(secs, '60'), '60'), mod(secs, '60')
3535
+ frac = f"(CASE WHEN {nanos} = 0 THEN '' ELSE '.' || {trim(f'lpad({num(nanos)}, 9, {chr(39)}0{chr(39)})')} END)"
3536
+ time = (f"(CASE WHEN {{2}} = 0 AND {{3}} = 0 THEN '' ELSE 'T' || "
3537
+ f"(CASE WHEN {h} = 0 THEN '' ELSE {sign} || {num(h)} || 'H' END) || "
3538
+ f"(CASE WHEN {mi} = 0 THEN '' ELSE {sign} || {num(mi)} || 'M' END) || "
3539
+ f"(CASE WHEN {se} = 0 AND {nanos} = 0 THEN '' ELSE {sign} || {num(se)} || {frac} || 'S' END) END)")
3540
+ return (f"(CASE WHEN {{0}} = 0 AND {{1}} = 0 AND {{2}} = 0 AND {{3}} = 0 THEN 'PT0S' ELSE 'P' || "
3541
+ f"(CASE WHEN {years} = 0 THEN '' ELSE {num(years)} || 'Y' END) || "
3542
+ f"(CASE WHEN {months} = 0 THEN '' ELSE {num(months)} || 'M' END) || "
3543
+ f"(CASE WHEN {{1}} = 0 THEN '' ELSE {num('{1}')} || 'D' END) || {time} END)")
3544
+
3545
+
3546
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3547
+ _config.function_map['CYPHER_DURATION_TEXT'] = _duration_text(
3548
+ lambda a, b: f"(({a}) div {b})", lambda a, b: f"(({a}) % {b})", "STRING",
3549
+ lambda x: f"trim(TRAILING '0' FROM {x})")
3550
+ DUCKDB_DIALECT.function_map['CYPHER_DURATION_TEXT'] = _duration_text(
3551
+ lambda a, b: f"(({a}) // {b})", lambda a, b: f"(({a}) % {b})", "VARCHAR", lambda x: f"rtrim({x}, '0')")
3552
+ POSTGRESQL_DIALECT.function_map['CYPHER_DURATION_TEXT'] = _duration_text(
3553
+ lambda a, b: f"div({a}, {b})", lambda a, b: f"mod({a}, {b})", "TEXT", lambda x: f"rtrim({x}, '0')")
3554
+
3555
+ # Cypher's collect() in the order of the WITH just before it: the non-null values ordered by a
3556
+ # key, null keys last ascending and first descending (rule cypher-order).
3557
+ _SPARK_CMP = ("(l, r) -> CASE WHEN l.k IS NULL AND r.k IS NULL THEN 0 WHEN l.k IS NULL THEN {nl} "
3558
+ "WHEN r.k IS NULL THEN {nr} WHEN l.k < r.k THEN {lt} WHEN l.k > r.k THEN {gt} ELSE 0 END")
3559
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3560
+ for _name, _sign in (('CYPHER_COLLECT_ORDERED', 1), ('CYPHER_COLLECT_ORDERED_DESC', -1)):
3561
+ _cmp = _SPARK_CMP.format(nl=_sign, nr=-_sign, lt=-_sign, gt=_sign)
3562
+ _config.function_map[_name] = (
3563
+ "transform(array_sort(collect_list(CASE WHEN ({0}) IS NOT NULL THEN named_struct('k', {1}, 'v', {0}) END), "
3564
+ + _cmp.replace("{", "{{").replace("}", "}}") + "), _s -> _s.v)")
3565
+ for _config in (DUCKDB_DIALECT,):
3566
+ _config.function_map['CYPHER_COLLECT_ORDERED'] = 'list({0} ORDER BY {1} ASC NULLS LAST) FILTER (WHERE ({0}) IS NOT NULL)'
3567
+ _config.function_map['CYPHER_COLLECT_ORDERED_DESC'] = 'list({0} ORDER BY {1} DESC NULLS FIRST) FILTER (WHERE ({0}) IS NOT NULL)'
3568
+ POSTGRESQL_DIALECT.function_map['CYPHER_COLLECT_ORDERED'] = (
3569
+ 'array_agg({0} ORDER BY {1} ASC NULLS LAST) FILTER (WHERE ({0}) IS NOT NULL)')
3570
+ POSTGRESQL_DIALECT.function_map['CYPHER_COLLECT_ORDERED_DESC'] = (
3571
+ 'array_agg({0} ORDER BY {1} DESC NULLS FIRST) FILTER (WHERE ({0}) IS NOT NULL)')
3572
+
3573
+ # The same over several keys (a WITH ... ORDER BY a, b DESC): `CYPHER_COLLECT_ORDERED_BY_<A|D per key>`, the
3574
+ # value then the keys; each key's nulls placed as one key's are, ties broken by the next key.
3575
+ COLLECT_ORDERED_BY = "CYPHER_COLLECT_ORDERED_BY_"
3576
+ COLLECT_ORDERED_MAX_KEYS = 4
3577
+
3578
+
3579
+ def _collect_ordered_by(directions: str) -> Dict[str, str]:
3580
+ """The templates of `COLLECT_ORDERED_BY + directions` per dialect name: {0} the value, {i} key i."""
3581
+ order = ", ".join(f"{{{i}}} {'ASC NULLS LAST' if d == 'A' else 'DESC NULLS FIRST'}"
3582
+ for i, d in enumerate(directions, 1))
3583
+ out = {"duckdb": f"list({{0}} ORDER BY {order}) FILTER (WHERE ({{0}}) IS NOT NULL)",
3584
+ "postgresql": f"array_agg({{0}} ORDER BY {order}) FILTER (WHERE ({{0}}) IS NOT NULL)"}
3585
+ cmp = "0"
3586
+ for i, d in reversed(list(enumerate(directions, 1))):
3587
+ sign = 1 if d == "A" else -1
3588
+ cmp = (f"CASE WHEN l.k{i} IS NULL AND r.k{i} IS NULL THEN {cmp} WHEN l.k{i} IS NULL THEN {sign} "
3589
+ f"WHEN r.k{i} IS NULL THEN {-sign} WHEN l.k{i} < r.k{i} THEN {-sign} WHEN l.k{i} > r.k{i} THEN {sign} "
3590
+ f"ELSE {cmp} END")
3591
+ fields = ", ".join(f"'k{i}', {{{i}}}" for i in range(1, len(directions) + 1))
3592
+ out["spark"] = ("transform(array_sort(collect_list(CASE WHEN ({0}) IS NOT NULL THEN named_struct("
3593
+ + fields + ", 'v', {0}) END), (l, r) -> " + cmp.replace("{", "{{").replace("}", "}}")
3594
+ + "), _s -> _s.v)")
3595
+ return out
3596
+
3597
+
3598
+ def _directions(n: int) -> List[str]:
3599
+ return [""] if n == 0 else [p + d for p in _directions(n - 1) for d in "AD"]
3600
+
3601
+
3602
+ COLLECT_ORDERED_BY_NAMES = frozenset(COLLECT_ORDERED_BY + d for n in range(2, COLLECT_ORDERED_MAX_KEYS + 1)
3603
+ for d in _directions(n))
3604
+ for _name in COLLECT_ORDERED_BY_NAMES:
3605
+ _forms = _collect_ordered_by(_name[len(COLLECT_ORDERED_BY):])
3606
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3607
+ _config.function_map[_name] = _forms["spark"]
3608
+ DUCKDB_DIALECT.function_map[_name] = _forms["duckdb"]
3609
+ POSTGRESQL_DIALECT.function_map[_name] = _forms["postgresql"]
3610
+
3611
+
3612
+ # A source table's temporal columns as Cypher's temporal values (rule cypher-source-temporal): a DATE's
3613
+ # days since 1970-01-01, a TIME's nanoseconds of the day, a TIMESTAMP's (local) and a TIMESTAMPTZ's
3614
+ # (an instant) nanoseconds since 1970-01-01T00:00 as DECIMAL(38,0); microsecond columns, times 1000.
3615
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3616
+ _config.function_map['CYPHER_SOURCE_DAYS'] = "CAST(datediff({0}, DATE'1970-01-01') AS BIGINT)"
3617
+ _config.function_map['CYPHER_SOURCE_TIME_NS'] = None # Spark has no TIME column
3618
+ _config.function_map['CYPHER_SOURCE_LOCAL_NS'] = (
3619
+ "CAST(timestampdiff(MICROSECOND, TIMESTAMP_NTZ'1970-01-01 00:00:00', {0}) AS DECIMAL(38,0)) * 1000")
3620
+ _config.function_map['CYPHER_SOURCE_INSTANT_NS'] = "CAST(unix_micros({0}) AS DECIMAL(38,0)) * 1000"
3621
+ DUCKDB_DIALECT.function_map['CYPHER_SOURCE_DAYS'] = "CAST(({0}) - DATE '1970-01-01' AS BIGINT)"
3622
+ DUCKDB_DIALECT.function_map['CYPHER_SOURCE_TIME_NS'] = (
3623
+ "CAST(datediff('microsecond', TIME '00:00:00', CAST({0} AS TIME)) AS BIGINT) * 1000")
3624
+ # DuckDB: the whole seconds from epoch_us, then the exact nanoseconds of the second `nanosecond` holds (epoch_ns
3625
+ # overflows past 2262). DuckDB 2.0's epoch_us of a TIMESTAMP_NS rounds to the microsecond where 1.5's cut, so the
3626
+ # microseconds are read only to find the second, rounded to the nearest: the same value on both.
3627
+ _DUCK_SOURCE_NS = ("(CAST(round((CAST(epoch_us({0}) AS DECIMAL(38,0)) * 1000 - (nanosecond({0}) % 1000000000))"
3628
+ " / 1000000000) AS DECIMAL(38,0)) * 1000000000 + (nanosecond({0}) % 1000000000))")
3629
+ DUCKDB_DIALECT.function_map['CYPHER_SOURCE_LOCAL_NS'] = _DUCK_SOURCE_NS
3630
+ DUCKDB_DIALECT.function_map['CYPHER_SOURCE_INSTANT_NS'] = _DUCK_SOURCE_NS
3631
+ POSTGRESQL_DIALECT.function_map['CYPHER_SOURCE_DAYS'] = "CAST(({0}) - DATE '1970-01-01' AS BIGINT)"
3632
+ POSTGRESQL_DIALECT.function_map['CYPHER_SOURCE_TIME_NS'] = (
3633
+ "CAST(EXTRACT(EPOCH FROM CAST({0} AS TIME)) * 1000000 AS BIGINT) * 1000")
3634
+ POSTGRESQL_DIALECT.function_map['CYPHER_SOURCE_LOCAL_NS'] = "CAST(EXTRACT(EPOCH FROM {0}) * 1000000 AS DECIMAL(38,0)) * 1000"
3635
+ POSTGRESQL_DIALECT.function_map['CYPHER_SOURCE_INSTANT_NS'] = "CAST(EXTRACT(EPOCH FROM {0}) * 1000000 AS DECIMAL(38,0)) * 1000"
3636
+ # A TIME WITH TIME ZONE's offset, in seconds east of UTC (its local time is CYPHER_SOURCE_TIME_NS's: the
3637
+ # local time of day, which DuckDB's and PostgreSQL's cast to TIME keeps). Spark has no such column.
3638
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3639
+ _config.function_map['CYPHER_SOURCE_OFFSET'] = None
3640
+ DUCKDB_DIALECT.function_map['CYPHER_SOURCE_OFFSET'] = "CAST(extract('timezone' FROM {0}) AS BIGINT)"
3641
+ POSTGRESQL_DIALECT.function_map['CYPHER_SOURCE_OFFSET'] = "CAST(EXTRACT(TIMEZONE FROM {0}) AS BIGINT)"
3642
+
3643
+ # A list property in the owned store (rule cypher-writes): its text a JSON array of its elements; whether a
3644
+ # list holds a null; and the text read back as a list of {1} (the element's SQL type). Writes run on
3645
+ # DuckDB and PostgreSQL alone.
3646
+ DUCKDB_DIALECT.function_map['STORE_LIST_TEXT'] = 'CAST(to_json({0}) AS VARCHAR)'
3647
+ DUCKDB_DIALECT.function_map['STORE_LIST_HAS_NULL'] = '(list_position({0}, NULL) IS NOT NULL)'
3648
+ DUCKDB_DIALECT.function_map['STORE_LIST_READ'] = 'CAST(CAST({0} AS JSON) AS {1}[])'
3649
+ # Each element by to_json (a float's -0 keeps its sign, which jsonb's numeric drops), read back as json too.
3650
+ POSTGRESQL_DIALECT.function_map['STORE_LIST_TEXT'] = (
3651
+ "('[' || array_to_string(ARRAY(SELECT CAST(to_json(_e.v) AS TEXT) FROM unnest({0}) WITH ORDINALITY AS _e(v, i) "
3652
+ "ORDER BY _e.i), ',') || ']')")
3653
+ POSTGRESQL_DIALECT.function_map['STORE_LIST_HAS_NULL'] = '(array_position({0}, NULL) IS NOT NULL)'
3654
+ POSTGRESQL_DIALECT.function_map['STORE_LIST_READ'] = (
3655
+ 'CASE WHEN {0} IS NULL THEN NULL ELSE ARRAY(SELECT CAST(_j.v AS {1}) FROM '
3656
+ 'json_array_elements_text(CAST({0} AS JSON)) WITH ORDINALITY AS _j(v, i) ORDER BY _j.i) END')
3657
+
3658
+ # A temporal property in the owned store (rule cypher-writes): its text (`cypher_temporal.stored_text`, one
3659
+ # fixed JSON form) read back as the struct of its parts ({1} its fields, `name TYPE, ...`; a jsonb object on
3660
+ # PostgreSQL, as its structs are), or a list's as a list of them; a list of texts joined by {1}, in order.
3661
+ DUCKDB_DIALECT.function_map['STORE_PARTS_READ'] = 'CAST(CAST({0} AS JSON) AS STRUCT({1}))'
3662
+ DUCKDB_DIALECT.function_map['STORE_PARTS_LIST_READ'] = 'CAST(CAST({0} AS JSON) AS STRUCT({1})[])'
3663
+ DUCKDB_DIALECT.function_map['STORE_TEXT_JOIN'] = 'array_to_string({0}, {1})'
3664
+ POSTGRESQL_DIALECT.function_map['STORE_PARTS_READ'] = 'CAST({0} AS JSONB)'
3665
+ POSTGRESQL_DIALECT.function_map['STORE_PARTS_LIST_READ'] = (
3666
+ 'CASE WHEN {0} IS NULL THEN NULL ELSE ARRAY(SELECT CAST(_j.v AS JSONB) FROM '
3667
+ 'json_array_elements_text(CAST({0} AS JSON)) WITH ORDINALITY AS _j(v, i) ORDER BY _j.i) END')
3668
+ POSTGRESQL_DIALECT.function_map['STORE_TEXT_JOIN'] = 'array_to_string({0}, {1})'
3669
+ for _config in (SPARK_DIALECT, DATABRICKS_SQL_DIALECT):
3670
+ _config.function_map['STORE_PARTS_READ'] = "from_json({0}, 'STRUCT<{1}>')"
3671
+ _config.function_map['STORE_TEXT_JOIN'] = 'array_join({0}, {1})'
3672
+
3673
+ # An element's parts as flat columns on PostgreSQL (rule cypher-result-values): collect() of each part in the one
3674
+ # order of the jsonb value's collect, `COLLECT_PART + <A|D per key>`: {0} the part, {1} the element's id (a
3675
+ # null one is not collected), {2}... the keys, the id last. `COLLECT_PART_LABELS`: the labels of every
3676
+ # element in order as one flat text[], from each element's candidate labels (a null where it lacks one).
3677
+ COLLECT_PART = "CYPHER_COLLECT_PART_"
3678
+ COLLECT_PART_LABELS = "CYPHER_COLLECT_PART_LABELS_"
3679
+ COLLECT_PART_KEYS = COLLECT_ORDERED_MAX_KEYS # the keys including the id
3680
+
3681
+
3682
+ COLLECT_PART_NAMES = frozenset(prefix + d for prefix in (COLLECT_PART, COLLECT_PART_LABELS)
3683
+ for n in range(1, COLLECT_PART_KEYS + 1) for d in _directions(n))
3684
+ for _name in COLLECT_PART_NAMES:
3685
+ _prefix = COLLECT_PART_LABELS if _name.startswith(COLLECT_PART_LABELS) else COLLECT_PART
3686
+ _order = ", ".join(f"{{{i}}} {'ASC NULLS LAST' if d == 'A' else 'DESC NULLS FIRST'}"
3687
+ for i, d in enumerate(_name[len(_prefix):], 2))
3688
+ _agg = f"array_agg({{0}} ORDER BY {_order}) FILTER (WHERE ({{1}}) IS NOT NULL)"
3689
+ POSTGRESQL_DIALECT.function_map[_name] = (
3690
+ _agg if _prefix == COLLECT_PART else
3691
+ f"ARRAY(SELECT _cl FROM unnest({_agg}) WITH ORDINALITY AS _ct(_cl, _co) WHERE _cl IS NOT NULL ORDER BY _co)")
3692
+ POSTGRESQL_DIALECT.function_map['CYPHER_CARDINALITY'] = 'COALESCE(cardinality({0}), 0)'
3693
+ POSTGRESQL_DIALECT.function_map['CYPHER_NUMERIC'] = 'CAST({0} AS NUMERIC)'
3694
+ # One element's labels out of a list's flat labels and its counts (the offsets form): {0} the flat text[], {1} the
3695
+ # counts int[], {2} the element's 1-based position.
3696
+ POSTGRESQL_DIALECT.function_map['CYPHER_LABELS_AT'] = (
3697
+ "ARRAY(SELECT _x FROM unnest({0}) WITH ORDINALITY AS _t(_x, _o) "
3698
+ "WHERE _o > COALESCE((SELECT sum(_c) FROM unnest(({1})[1:({2}) - 1]) AS _s(_c)), 0) "
3699
+ "AND _o <= COALESCE((SELECT sum(_c) FROM unnest(({1})[1:({2})]) AS _s(_c)), 0) ORDER BY _o)")
3700
+
3701
+ # A collected list of paths (rule cypher-result-values): each path's arrays padded to one width (`CYPHER_PAD`) so
3702
+ # `array_agg` makes them one rectangular array, and the paths' elements read back out of it in order, a pad being
3703
+ # a null. `COLLECT_FLAT + <A|D per key>`: {0} the part padded, {1} the path's ids padded (a null id is a pad), {2}
3704
+ # the path's ids (a null one is no path), {3}... the keys. `COLLECT_FLAT_LABELS`: {0} the labels padded (a label is
3705
+ # never null), {1} the ids, {2}... the keys.
3706
+ COLLECT_FLAT = "CYPHER_COLLECT_FLAT_"
3707
+ COLLECT_FLAT_LABELS = "CYPHER_COLLECT_FLATL_"
3708
+ COLLECT_FLAT_NAMES = frozenset(prefix + d for prefix in (COLLECT_FLAT, COLLECT_FLAT_LABELS)
3709
+ for n in range(1, COLLECT_PART_KEYS + 1) for d in _directions(n))
3710
+ for _name in COLLECT_FLAT_NAMES:
3711
+ _labels = _name.startswith(COLLECT_FLAT_LABELS)
3712
+ _prefix = COLLECT_FLAT_LABELS if _labels else COLLECT_FLAT
3713
+ _first = 2 if _labels else 3
3714
+ _order = ", ".join(f"{{{i}}} {'ASC NULLS LAST' if d == 'A' else 'DESC NULLS FIRST'}"
3715
+ for i, d in enumerate(_name[len(_prefix):], _first))
3716
+ _where = f"FILTER (WHERE ({{{1 if _labels else 2}}}) IS NOT NULL)"
3717
+ if _labels:
3718
+ _sql = (f"ARRAY(SELECT _fl FROM unnest(array_agg({{0}} ORDER BY {_order}) {_where}) "
3719
+ "WITH ORDINALITY AS _fu(_fl, _fo) WHERE _fl IS NOT NULL ORDER BY _fo)")
3720
+ else:
3721
+ _sql = (f"ARRAY(SELECT _fv FROM unnest(array_agg({{1}} ORDER BY {_order}) {_where}, "
3722
+ f"array_agg({{0}} ORDER BY {_order}) {_where}) "
3723
+ "WITH ORDINALITY AS _fu(_fi, _fv, _fo) WHERE _fi IS NOT NULL ORDER BY _fo)")
3724
+ POSTGRESQL_DIALECT.function_map[_name] = _sql
3725
+ POSTGRESQL_DIALECT.function_map['CYPHER_PAD'] = "ARRAY(SELECT ({0})[_i] FROM generate_series(1, {1}) AS _g(_i))"
3726
+ POSTGRESQL_DIALECT.function_map['CYPHER_ARRAY_KEY'] = "COALESCE(array_to_string({0}, ','), '')"