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,103 @@
1
+ """MySQL SQL Serializer.
2
+
3
+ MySQL is close to the Spark base dialect, backtick identifier quoting and
4
+ ``LIMIT`` paging, so this subclasses ``SparkSQLSerializer`` and overrides only
5
+ what differs.
6
+
7
+ The load-bearing difference is **null ordering**: MySQL has no
8
+ ``NULLS FIRST`` / ``NULLS LAST`` syntax (the base serializer always emits it to
9
+ pin Cypher semantics, which MySQL rejects outright). Cypher treats null as the
10
+ largest value, nulls sort last in ASC, first in DESC, so we emulate that with
11
+ a leading ``(<expr>) IS NULL`` sort key.
12
+
13
+ Other Spark-isms the base serializer can emit (``COLLECT_LIST``,
14
+ ``NAMED_STRUCT``, ``ELEMENT_AT``, …) have no MySQL equivalent and are not yet
15
+ translated here; queries needing them fall back to the SQL-only error rather
16
+ than producing wrong results. The cross-backend query battery
17
+ (scan / filter / join / aggregate / order / with / mutations) does not use them.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ from .dialect import scope_name
23
+ from .rel_algebra import ColExpr, NullsOrder, SortDirection, SortKey
24
+ from .sql_serializer import SparkSQLSerializer
25
+
26
+
27
+ class MySQLSerializer(SparkSQLSerializer):
28
+ """Serialize a RelNode tree to MySQL SQL.
29
+
30
+ Inherits Spark-style backtick quoting and ``LIMIT`` handling; overrides
31
+ sort-key rendering because MySQL lacks ``NULLS FIRST`` / ``NULLS LAST``.
32
+ """
33
+
34
+ _alias_dialect = 'mysql'
35
+
36
+ _limit_style = 'limit_required'
37
+ # MySQL's recursion limit (1,000 levels) is the session variable
38
+ # cte_max_recursion_depth, which ProGraph does not set: past
39
+ # it MySQL's error 3636 stands, and names the variable.
40
+ _recursion_level_clause = ""
41
+
42
+ def __init__(self):
43
+ super().__init__(dialect='mysql')
44
+
45
+ def _table_ref(self, table: str, alias: str) -> str:
46
+ """Quote each part of the table name: MySQL reserves words such as
47
+ `lines`, and an unquoted one is a syntax error. Backtick-quoting
48
+ leaves its case rules as they are."""
49
+ quoted = '.'.join(p if p.startswith('`') else f"`{p}`" for p in table.split('.'))
50
+ return f"{quoted} AS {scope_name(alias)}"
51
+
52
+ def _collect_lateral_explode(self, node, frame):
53
+ # MySQL has no array type to unnest (collect() refuses here too).
54
+ from prograph.utils.exceptions import SQLGenerationError
55
+ raise SQLGenerationError("UNWIND has no MySQL rendering",
56
+ reason="unsupported_unwind:mysql")
57
+
58
+ # MySQL has no LEFT SEMI / LEFT ANTI JOIN, the Spark spelling the base
59
+ # serializer emits: every NOT pattern and pattern predicate failed with a
60
+ # syntax error. PostgreSQL's forms (EXISTS, and LEFT JOIN ... IS NULL)
61
+ # are MySQL 8 SQL too.
62
+ def _collect_anti_join(self, node, frame):
63
+ from .pg_serializer import PostgreSQLSerializer
64
+ return PostgreSQLSerializer._collect_anti_join(self, node, frame)
65
+
66
+ def _collect_semi_join(self, node, frame):
67
+ from .pg_serializer import PostgreSQLSerializer
68
+ return PostgreSQLSerializer._collect_semi_join(self, node, frame)
69
+
70
+ def _render_sort_key(self, key: SortKey, beside=None) -> str:
71
+ """Render a SortKey, emulating Cypher null ordering for MySQL.
72
+
73
+ `beside` is not read: the key is written twice, inside `(...) IS
74
+ NULL` too, where a position would be a constant, so a key named
75
+ by one of two columns that differ by case alone is not written
76
+ by its position here as it is on DuckDB, Spark and Databricks
77
+ (the base class's `_render_sort_key`). A fault there is logged and
78
+ not fixed."""
79
+ col = key.column
80
+ # Mirror the base serializer's reference resolution (prefer a complex
81
+ # expression, then the column SQL, then the alias).
82
+ expr_node = col.metadata.get('expr') if col.metadata else None
83
+ if expr_node is not None and not isinstance(expr_node, ColExpr):
84
+ ref = self._expr.serialize(expr_node)
85
+ elif col.sql and col.sql != '<sort_expr>':
86
+ ref = col.sql
87
+ elif col.alias:
88
+ ref = self._quote_alias(col.alias)
89
+ else:
90
+ ref = col.sql
91
+ ref = self._by_code_point(key, ref)
92
+
93
+ direction = 'ASC' if key.direction == SortDirection.ASC else 'DESC'
94
+
95
+ # `(<expr>) IS NULL` is 1 for null, 0 otherwise. Cypher: nulls last in
96
+ # ASC, first in DESC. So nulls-last -> IS NULL ASC (0 before 1),
97
+ # nulls-first -> IS NULL DESC (1 before 0). Then the value sort.
98
+ nulls = key.nulls
99
+ if nulls is None:
100
+ nulls = (NullsOrder.LAST if key.direction == SortDirection.ASC
101
+ else NullsOrder.FIRST)
102
+ null_dir = 'DESC' if nulls == NullsOrder.FIRST else 'ASC'
103
+ return f"({ref}) IS NULL {null_dir}, {ref} {direction}"
@@ -0,0 +1,65 @@
1
+ """Helpers the serializers share: names as a database compares them, an
2
+ expression with one column replaced, an aggregate's carried sort keys."""
3
+
4
+ from __future__ import annotations
5
+
6
+ from .rel_algebra import (
7
+ AndExpr, ColExpr, ColumnId, CompareExpr, ExistsExpr, Expr, FuncExpr,
8
+ IsNullExpr, LitExpr, NotExpr, OrExpr,
9
+ )
10
+
11
+
12
+ def name_to_a_database(name: str) -> str:
13
+ """`name` as DuckDB, Spark and Databricks compare it: without regard to
14
+ case. The key of a set or a count of names as a database reads them."""
15
+ return name.lower()
16
+
17
+
18
+ def one_name_to_a_database(a: str, b: str) -> bool:
19
+ """Whether `a` and `b`, the same or differing by case alone, are one
20
+ name to DuckDB, Spark and Databricks (`differ_by_case_alone`)."""
21
+ return name_to_a_database(a) == name_to_a_database(b)
22
+
23
+
24
+ def differ_by_case_alone(a: str, b: str) -> bool:
25
+ """Whether `a` and `b` are two names of the query and one name to a
26
+ database: DuckDB, Spark and Databricks compare names without regard to
27
+ case, so `N` and `n` are one column and `p` and `P` one table
28
+ (690; docs/architecture/rules.md, "A name
29
+ of the query is its own column")."""
30
+ return a != b and one_name_to_a_database(a, b)
31
+
32
+
33
+ def substitute_expr(expr: Expr, target: ColumnId, repl) -> Expr:
34
+ """Rebuild *expr*, replacing every column reference whose identity equals
35
+ *target* with *repl*: a reference to it where it is a ColumnRef, itself
36
+ where it is an expression (a select alias read inside a sort key, by
37
+ the value it names: `SparkSQLSerializer._read_by_its_own_select_alias`).
38
+ Frozen dataclasses → reconstructed.
39
+ """
40
+ if isinstance(expr, ColExpr):
41
+ if expr.ref.identity() != target:
42
+ return expr
43
+ return repl if isinstance(repl, Expr) else ColExpr(ref=repl)
44
+ if isinstance(expr, CompareExpr):
45
+ return CompareExpr(
46
+ left=substitute_expr(expr.left, target, repl), op=expr.op,
47
+ right=substitute_expr(expr.right, target, repl))
48
+ if isinstance(expr, AndExpr):
49
+ return AndExpr(children=tuple(substitute_expr(c, target, repl) for c in expr.children))
50
+ if isinstance(expr, OrExpr):
51
+ return OrExpr(children=tuple(substitute_expr(c, target, repl) for c in expr.children))
52
+ if isinstance(expr, NotExpr):
53
+ return NotExpr(child=substitute_expr(expr.child, target, repl) if expr.child else None)
54
+ if isinstance(expr, IsNullExpr):
55
+ return IsNullExpr(
56
+ child=substitute_expr(expr.child, target, repl) if expr.child else None,
57
+ negated=expr.negated)
58
+ if isinstance(expr, FuncExpr):
59
+ return FuncExpr(name=expr.name,
60
+ args=tuple(substitute_expr(a, target, repl) for a in expr.args))
61
+ if isinstance(expr, ExistsExpr) and expr.condition is not None:
62
+ return ExistsExpr(cte_name=expr.cte_name, alias=expr.alias,
63
+ condition=substitute_expr(expr.condition, target, repl))
64
+ # LitExpr and anything unknown: opaque / no column refs.
65
+ return expr
@@ -0,0 +1,306 @@
1
+ """Oracle SQL serializer.
2
+
3
+ Subclasses PostgreSQLSerializer and overrides Oracle-specific syntax:
4
+ - Table aliases without ``AS``
5
+ - FETCH FIRST instead of LIMIT
6
+ - Unquoted simple identifiers (Oracle stores them uppercase)
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import re
12
+
13
+ from prograph.plan.sql.dialect import scope_name
14
+ from prograph.rules import home
15
+ from prograph.plan.sql.pg_serializer import PostgreSQLSerializer
16
+ from prograph.plan.sql.rel_algebra import RelNode
17
+
18
+
19
+ from prograph.plan.sql.pg_serializer import PostgreSQLExprSerializer as _PGExpr
20
+
21
+
22
+ from prograph.plan.sql.sqlserver_serializer import TruthCaseWithoutBooleans
23
+
24
+
25
+ @home("oracle-sql-forms", "What Oracle 21c has no SQL form for, and how it is written instead",
26
+ terms=("Oracle", "DUAL", "boolean", "condition", "empty string", "NUMBER(1)", "reserved word", "recursive WITH"))
27
+ class _OracleExprSerializer(TruthCaseWithoutBooleans, _PGExpr):
28
+ """No boolean type in Oracle SQL: TRUE / FALSE are predicates that
29
+ always hold or never do."""
30
+
31
+ # No boolean type: a predicate is not a value to compare.
32
+ _predicates_are_values = False
33
+
34
+ def _operand(self, expr):
35
+ """An operand of a comparison. A predicate is no value here, so it
36
+ is the 1, 0 or NULL a CASE makes of it, which is what a boolean
37
+ column holds (NUMBER(1)): `b = true` is `b = 1`."""
38
+ from .rel_algebra import LitExpr
39
+ from .sqlserver_serializer import _is_predicate
40
+ if isinstance(expr, LitExpr) and isinstance(expr.value, bool):
41
+ return '1' if expr.value else '0'
42
+ if _is_predicate(expr):
43
+ sql = self.serialize(expr)
44
+ return f"CASE WHEN {sql} THEN 1 WHEN NOT ({sql}) THEN 0 END"
45
+ return super()._operand(expr)
46
+
47
+ def _condition(self, expr) -> str:
48
+ """`expr` where a condition stands (under NOT, AND, OR, after WHEN).
49
+ A boolean column, or a CASE that picks boolean values, holds 1 or 0
50
+ there and is no condition: it is compared with 1 (ORA-00920). A
51
+ NULL is the condition that is unknown."""
52
+ from .rel_algebra import ColExpr, FuncExpr, LitExpr
53
+ from .sqlserver_serializer import _is_truth_case
54
+ sql = self.serialize(expr)
55
+ if isinstance(expr, LitExpr) and expr.value is None:
56
+ return '(NULL = 1)'
57
+ if isinstance(expr, ColExpr) or (isinstance(expr, FuncExpr)
58
+ and expr.name in ('CASE', 'CASE_SIMPLE')
59
+ and not _is_truth_case(expr)):
60
+ return f"({sql} = 1)"
61
+ return sql
62
+
63
+ def _case_expr(self, args: tuple, simple: bool = False) -> str:
64
+ from .rel_algebra import FuncExpr
65
+ from .sqlserver_serializer import _is_truth_case
66
+ if not simple and args and not _is_truth_case(FuncExpr(name='CASE', args=tuple(args))):
67
+ parts, idx = ['CASE'], 0
68
+ while idx + 1 < len(args):
69
+ parts.append(f"WHEN {self._condition(args[idx])} THEN {self.serialize(args[idx + 1])}")
70
+ idx += 2
71
+ if idx < len(args):
72
+ parts.append(f"ELSE {self.serialize(args[idx])}")
73
+ parts.append('END')
74
+ return ' '.join(parts)
75
+ return super()._case_expr(args, simple)
76
+
77
+ def serialize(self, expr):
78
+ from .rel_algebra import AndExpr, CompareExpr, FuncExpr, LitExpr, NotExpr, OrExpr
79
+ if isinstance(expr, LitExpr) and isinstance(expr.value, bool):
80
+ return '(1=1)' if expr.value else '(1=0)'
81
+ if isinstance(expr, AndExpr) and expr.children:
82
+ return ' AND '.join(self._condition(c) for c in expr.children)
83
+ if isinstance(expr, OrExpr) and expr.children:
84
+ return f"({' OR '.join(self._condition(c) for c in expr.children)})"
85
+ if isinstance(expr, NotExpr):
86
+ return f"NOT ({self._condition(expr.child)})"
87
+ if isinstance(expr, CompareExpr) and expr.op == 'RLIKE' and isinstance(expr.right, LitExpr) \
88
+ and isinstance(expr.right.value, str) and re.search(r'\\x[0-9A-Fa-f]{2}', expr.right.value):
89
+ # Oracle's regular expressions read no \x escape: `\x00-\x20` would be the letter x.
90
+ from prograph.utils.exceptions import SQLGenerationError
91
+ raise SQLGenerationError("a regular expression with a \\x escape (the check that a text is a valid IRI) "
92
+ "has no equivalent in oracle", reason="unsupported_function:REGEX_ESCAPE_X")
93
+ if isinstance(expr, CompareExpr) and expr.op == '=':
94
+ absent = [_absent_part(side) for side in (expr.left, expr.right)]
95
+ if all(a is not None for a in absent):
96
+ # `COALESCE(a, '') = COALESCE(b, '')` is "the same, or both absent" in the other
97
+ # databases; Oracle's '' is NULL, so it would read NULL = NULL (unknown) and drop
98
+ # the row. It holds no '' to confuse with an absent part.
99
+ a, b = (self.serialize(x) for x in absent)
100
+ return f"({a} = {b} OR {a} IS NULL AND {b} IS NULL)"
101
+ # Oracle's CONCAT takes exactly two arguments; `||` takes any number.
102
+ if isinstance(expr, FuncExpr) and expr.name == 'CONCAT' and len(expr.args) != 2:
103
+ return "(" + " || ".join(self.serialize(a) for a in expr.args) + ")"
104
+ return super().serialize(expr)
105
+
106
+
107
+ def _absent_part(expr):
108
+ """The `x` of `COALESCE(x, '')`, the text of a part that may be absent, else None."""
109
+ from .rel_algebra import FuncExpr, LitExpr
110
+ if isinstance(expr, FuncExpr) and expr.name == 'COALESCE' and len(expr.args) == 2 \
111
+ and isinstance(expr.args[1], LitExpr) and expr.args[1].value == '':
112
+ return expr.args[0]
113
+ return None
114
+
115
+
116
+ class OracleSerializer(PostgreSQLSerializer):
117
+ """Serialize a RelNode tree to Oracle SQL."""
118
+
119
+ _alias_dialect = 'oracle'
120
+
121
+ _recursive_with = "WITH"
122
+
123
+ _limit_style = 'fetch'
124
+
125
+ _one_row_source = "(SELECT 1 AS _dummy FROM DUAL) AS _base"
126
+
127
+ def __init__(self):
128
+ from .dialect import get_dialect_instance
129
+ super().__init__()
130
+ self._expr = _OracleExprSerializer(dialect=get_dialect_instance('oracle'))
131
+
132
+ def _table_ref(self, table: str, alias: str) -> str:
133
+ """Oracle: no AS, and quote aliases starting with underscore."""
134
+ alias = scope_name(alias)
135
+ if alias.startswith('_'):
136
+ return f'{table} "{alias}"'
137
+ return f"{table} {alias}"
138
+
139
+ def _predicate_value(self, sql: str) -> str:
140
+ """A predicate is not a value in Oracle (no SQL boolean before 23ai):
141
+ 1, 0, or NULL where it is unknown, declared NUMBER(1)
142
+ so the driver reads it as an integer, not a double."""
143
+ return f"CAST(CASE WHEN {sql} THEN 1 WHEN NOT ({sql}) THEN 0 END AS NUMBER(1))"
144
+
145
+ def _cte_column_list(self, cte) -> str:
146
+ """Oracle's recursive WITH names its columns (ORA-00905 without):
147
+ the anchor branch's output aliases, in order."""
148
+ if not getattr(cte, 'recursive', False):
149
+ return ""
150
+ from .rel_algebra import Project, SetOperation
151
+ node = cte.definition
152
+ while isinstance(node, SetOperation) and node.branches:
153
+ node = node.branches[0]
154
+ while node is not None and not isinstance(node, Project):
155
+ node = getattr(node, 'child', None)
156
+ if node is None or not all(c.alias for c in node.columns):
157
+ return ""
158
+ names = [c.alias if c.alias[:1].isalpha() and c.alias.replace('_', 'a').isalnum()
159
+ else f'"{c.alias}"' for c in node.columns]
160
+ return f"({', '.join(names)})"
161
+
162
+ def _cte_suffix(self, cte) -> str:
163
+ """Oracle raises ORA-32044 on a recursive WITH that revisits a row
164
+ unless a CYCLE clause names what identifies one. The walk's path
165
+ string never repeats (a path is a distinct edge sequence, and the
166
+ walk already refuses a reused edge), so CYCLE on it keeps every row
167
+ and satisfies Oracle; without a path, every column."""
168
+ if not getattr(cte, 'recursive', False):
169
+ return ""
170
+ from .rel_algebra import SetOp, SetOperation
171
+ if isinstance(cte.definition, SetOperation) and cte.definition.op != SetOp.UNION_ALL:
172
+ # A recursive step that drops the rows it has seen (UNION) ends a walk on a cycle; Oracle's
173
+ # recursive WITH takes UNION ALL alone (ORA-32040), and CYCLE would keep a row reached twice.
174
+ from prograph.utils.exceptions import SQLGenerationError
175
+ raise SQLGenerationError("a recursive WITH over UNION (rows seen before are dropped) has no Oracle rendering: an unbounded property path (p+, p*) is not built on Oracle; write a bounded path (p{1,4}) or run the query on another backend",
176
+ reason="unsupported_recursive_union:oracle")
177
+ columns = self._cte_column_list(cte)
178
+ if not columns:
179
+ return ""
180
+ names = columns[1:-1]
181
+ mark = '"__path"' if '"__path"' in names.split(', ') else names
182
+ return f""" CYCLE {mark} SET "_is_cycle" TO '1' DEFAULT '0'"""
183
+
184
+ def _collect_lateral_explode(self, node, frame):
185
+ # No array type to unnest (collect() refuses here too).
186
+ from prograph.utils.exceptions import SQLGenerationError
187
+ raise SQLGenerationError("UNWIND has no Oracle rendering",
188
+ reason="unsupported_unwind:oracle")
189
+
190
+ def _assemble_sql(self, frame) -> str:
191
+ # A SELECT names a table in Oracle; `RETURN 1` reads DUAL.
192
+ if not frame.from_clause:
193
+ frame.from_clause = "DUAL"
194
+ return super()._assemble_sql(frame)
195
+
196
+ def serialize(self, tree: RelNode) -> str:
197
+ """Serialize tree to SQL, then strip unnecessary double-quotes.
198
+
199
+ Oracle stores unquoted identifiers as uppercase. The IR tree
200
+ bakes double-quote quoting into column references (e.g.
201
+ ``p."name"``). For columns created without quoting (the normal
202
+ case), we strip the quotes so Oracle's uppercase matching works.
203
+
204
+ Names starting with underscore (``"_inferred"``, ``"_r1"``)
205
+ are preserved since Oracle requires quoting for those.
206
+ """
207
+ # The serializer recurses into CTEs and bodies through serialize;
208
+ # only the outermost call makes the result, so only it is renamed.
209
+ outermost = not getattr(self, '_serializing', False)
210
+ self._serializing = True
211
+ try:
212
+ sql = super().serialize(tree)
213
+ finally:
214
+ if outermost:
215
+ self._serializing = False
216
+ if not outermost:
217
+ return sql
218
+ sql = _outside_literals(sql, _oracle_identifiers)
219
+ return _named_as_returned(sql, _result_aliases(tree))
220
+
221
+
222
+ def _result_aliases(tree: RelNode):
223
+ """The names the query's result columns are meant to have, in order:
224
+ the aliases of the projection that makes the result, or None if a
225
+ column has no alias."""
226
+ from .rel_algebra import Project, SetOperation, WithClause
227
+ node = tree
228
+ while node is not None and not isinstance(node, Project):
229
+ if isinstance(node, WithClause):
230
+ node = node.body
231
+ elif isinstance(node, SetOperation):
232
+ node = node.branches[0] if node.branches else None
233
+ else:
234
+ node = getattr(node, 'child', None)
235
+ if node is None or not node.columns or not all(c.alias for c in node.columns):
236
+ return None
237
+ return [c.alias for c in node.columns]
238
+
239
+
240
+ def _named_as_returned(sql: str, aliases) -> str:
241
+ """``sql`` with its result columns named exactly as the query names them
242
+ . Oracle folds an unquoted alias to upper case, and
243
+ the SQL leaves simple names unquoted so they match the catalog, so the
244
+ result is selected again from an inline view: each column by the name
245
+ Oracle gave it, as the name the query gave it. The view keeps the inner
246
+ query's row order through a plain projection, as Oracle does; SQL does
247
+ not promise it. The name Oracle gave each column comes from the rule
248
+ that wrote it (``OracleDialect.returned_name``); a query's result names
249
+ are distinct (Cypher refuses a repeated one)."""
250
+ from .dialect import get_dialect_instance
251
+ oracle = get_dialect_instance('oracle')
252
+ if not aliases or all(oracle.returned_name(a) == a for a in aliases):
253
+ return sql
254
+ quoted = oracle.quote_exact
255
+ select = ", ".join(f'"_result".{quoted(oracle.returned_name(a))} AS {quoted(a)}' for a in aliases)
256
+ return f'SELECT {select} FROM (\n{sql.rstrip().rstrip(";").rstrip()}\n) "_result"'
257
+
258
+
259
+ # A string literal, with '' escapes. The rewrites below run between them:
260
+ # over the whole text they rewrote literals too ('a _b' became 'a "_b"').
261
+ _LITERAL = re.compile(r"'(?:[^']|'')*'")
262
+
263
+ # Types a CAST may name after a parenthesised operand: that AS stays.
264
+ _CAST_TYPES = r"(?:NUMBER|VARCHAR2|NVARCHAR2|CHAR|BINARY_DOUBLE|BINARY_FLOAT|DATE|TIMESTAMP|INTEGER|FLOAT|CLOB)\b"
265
+
266
+
267
+ # A quoted name, with "" escapes.
268
+ _QUOTED_NAME = re.compile(r'"(?:[^"]|"")*"')
269
+
270
+
271
+ def _outside_literals(sql: str, rewrite) -> str:
272
+ out, last = [], 0
273
+ for m in _LITERAL.finditer(sql):
274
+ out.append(rewrite(sql[last:m.start()]))
275
+ out.append(m.group(0))
276
+ last = m.end()
277
+ out.append(rewrite(sql[last:]))
278
+ return ''.join(out)
279
+
280
+
281
+ def _oracle_identifiers(text: str) -> str:
282
+ from .dialect import get_dialect_instance
283
+ quote = get_dialect_instance('oracle').quote
284
+ # 1. Remove double-quotes around simple identifiers, which Oracle stores
285
+ # upper case, as OracleDialect.quote decides: a reserved word (LEVEL,
286
+ # DATE) keeps its quotes, and so does an underscore-prefixed name.
287
+ # 2. Quote bare underscore-prefixed identifiers (_r1 -> "_r1"), outside
288
+ # the quoted names (`"?_v_t"` is one name).
289
+ def rewrite(m):
290
+ token = m.group(0)
291
+ simple = re.fullmatch(r'"([A-Za-z][A-Za-z0-9_]*)"', token)
292
+ return quote(simple.group(1)) if simple else token
293
+ out, last = [], 0
294
+ for m in _QUOTED_NAME.finditer(text):
295
+ out.append(re.sub(r'(?<![\w"])(_[A-Za-z0-9_]+)', r'"\1"', text[last:m.start()]))
296
+ out.append(rewrite(m))
297
+ last = m.end()
298
+ out.append(re.sub(r'(?<![\w"])(_[A-Za-z0-9_]+)', r'"\1"', text[last:]))
299
+ text = ''.join(out)
300
+ # 3. No AS before a table or subquery alias (Oracle rejects it; the
301
+ # fragments built before the dialect is known write it). A column
302
+ # alias may drop it too; a CAST's type keeps it.
303
+ text = re.sub(r'\b(FROM|JOIN)\s+([A-Za-z_][\w.$]*|"[^"]+")\s+AS\s+', r'\1 \2 ', text)
304
+ # `name(cols) AS (...)` introduces a CTE and keeps its AS.
305
+ text = re.sub(r'\)\s+AS\s+(?!' + _CAST_TYPES + r'|\()', ') ', text)
306
+ return text