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.
- prograph/__init__.py +36 -0
- prograph/__main__.py +5 -0
- prograph/_version.py +375 -0
- prograph/cli/__init__.py +1 -0
- prograph/cli/_iri_utils.py +124 -0
- prograph/cli/emit_genie_space.py +107 -0
- prograph/cli/generate_views.py +140 -0
- prograph/cli/main.py +420 -0
- prograph/cli/schema_inferrer.py +746 -0
- prograph/cli/ttl_loader.py +289 -0
- prograph/cli/upload_genie_space.py +84 -0
- prograph/config/__init__.py +27 -0
- prograph/config/yaml_config.py +848 -0
- prograph/demos/__init__.py +14 -0
- prograph/demos/turbofan/README.md +74 -0
- prograph/demos/turbofan/SCENARIO.md +209 -0
- prograph/demos/turbofan/__init__.py +75 -0
- prograph/demos/turbofan/create_tables.sql +206 -0
- prograph/demos/turbofan/data/engine-instances.ttl +1123 -0
- prograph/demos/turbofan/data/sensor-timeseries.csv +90 -0
- prograph/demos/turbofan/demo.py +162 -0
- prograph/demos/turbofan/genie_qa_notebook.py +434 -0
- prograph/demos/turbofan/graph.yaml +1098 -0
- prograph/demos/turbofan/instance_graph_demo.py +32 -0
- prograph/demos/turbofan/load.py +223 -0
- prograph/demos/turbofan/mapping.r2rml.ttl +991 -0
- prograph/demos/turbofan/ontology/engine-maintenance.ttl +606 -0
- prograph/demos/turbofan/payload-shapes.ttl +33 -0
- prograph/demos/turbofan/queries/example-queries.sparql +215 -0
- prograph/demos/turbofan/shapes.ttl +75 -0
- prograph/demos/turbofan/tables.py +106 -0
- prograph/demos/turbofan/turbofan_schema.py +393 -0
- prograph/demos/turbofan/validation_demo.py +153 -0
- prograph/describe.py +231 -0
- prograph/endpoint/__init__.py +12 -0
- prograph/endpoint/app.py +722 -0
- prograph/endpoint/cli.py +280 -0
- prograph/endpoint/credentials.py +240 -0
- prograph/endpoint/engines.py +418 -0
- prograph/endpoint/negotiate.py +101 -0
- prograph/endpoint/results.py +181 -0
- prograph/endpoint/service.py +31 -0
- prograph/engine.py +2718 -0
- prograph/execution.py +455 -0
- prograph/executors/__init__.py +6 -0
- prograph/executors/base.py +11 -0
- prograph/executors/databricks_sql_executor.py +51 -0
- prograph/executors/duckdb_executor.py +24 -0
- prograph/executors/mysql_executor.py +20 -0
- prograph/executors/oracle_executor.py +24 -0
- prograph/executors/pandas_executor.py +72 -0
- prograph/executors/postgresql_executor.py +35 -0
- prograph/executors/spark_executor.py +88 -0
- prograph/executors/sqlserver_executor.py +35 -0
- prograph/extras/__init__.py +1 -0
- prograph/extras/graphql/__init__.py +33 -0
- prograph/extras/graphql/execute.py +25 -0
- prograph/extras/graphql/schema.py +314 -0
- prograph/extras/instance_graph.py +559 -0
- prograph/genie/__init__.py +83 -0
- prograph/genie/databricks_uploader.py +306 -0
- prograph/genie/deploy.py +393 -0
- prograph/genie/formats.py +414 -0
- prograph/genie/instruction_generator.py +375 -0
- prograph/genie/sample_query_generator.py +256 -0
- prograph/genie/shacl_views.py +318 -0
- prograph/genie/space_emitter.py +329 -0
- prograph/genie/view_generator.py +628 -0
- prograph/genie/view_writer.py +172 -0
- prograph/graph/__init__.py +1 -0
- prograph/graph/ansi_session.py +348 -0
- prograph/graph/databricks_sql_graph.py +300 -0
- prograph/graph/drivers.py +143 -0
- prograph/graph/duckdb_graph.py +68 -0
- prograph/graph/mysql_graph.py +38 -0
- prograph/graph/oracle_graph.py +285 -0
- prograph/graph/pandas_graph.py +343 -0
- prograph/graph/postgresql_graph.py +644 -0
- prograph/graph/spark_graph.py +556 -0
- prograph/graph/sql_graph.py +83 -0
- prograph/graph/sqlserver_graph.py +72 -0
- prograph/hints.py +95 -0
- prograph/logging.py +550 -0
- prograph/mcp/__init__.py +29 -0
- prograph/mcp/__main__.py +180 -0
- prograph/mcp/bootstrap.py +98 -0
- prograph/mcp/server.py +601 -0
- prograph/mcp/sparql.py +397 -0
- prograph/mcp/tools.py +504 -0
- prograph/nlp/__init__.py +79 -0
- prograph/nlp/client.py +305 -0
- prograph/nlp/databricks_tools.py +369 -0
- prograph/nlp/graph_context.py +313 -0
- prograph/nlp/interface.py +652 -0
- prograph/nlp/prompts/__init__.py +19 -0
- prograph/nlp/prompts/examples.py +258 -0
- prograph/nlp/prompts/system.py +239 -0
- prograph/nlp/providers/__init__.py +14 -0
- prograph/nlp/providers/anthropic.py +274 -0
- prograph/nlp/providers/databricks.py +312 -0
- prograph/nlp/providers/openai.py +203 -0
- prograph/nlp/schema_context.py +78 -0
- prograph/ontology/__init__.py +53 -0
- prograph/ontology/alignment.py +542 -0
- prograph/ontology/hierarchy.py +310 -0
- prograph/ontology/imports.py +587 -0
- prograph/ontology/layer.py +991 -0
- prograph/ontology/skos.py +61 -0
- prograph/owl/__init__.py +27 -0
- prograph/owl/defined_classes.py +323 -0
- prograph/parameters.py +242 -0
- prograph/parser/__init__.py +1 -0
- prograph/parser/cypher/CypherLexer.py +836 -0
- prograph/parser/cypher/CypherListener.py +966 -0
- prograph/parser/cypher/CypherParser.py +10854 -0
- prograph/parser/cypher/CypherVisitor.py +543 -0
- prograph/parser/cypher/__init__.py +1 -0
- prograph/parser/sparql/SparqlLexer.py +801 -0
- prograph/parser/sparql/SparqlParser.py +10146 -0
- prograph/parser/sparql/SparqlParserListener.py +1056 -0
- prograph/parser/sparql/SparqlParserVisitor.py +593 -0
- prograph/parser/sparql/__init__.py +8 -0
- prograph/plan/__init__.py +3 -0
- prograph/plan/ir/__init__.py +1 -0
- prograph/plan/ir/casts.py +338 -0
- prograph/plan/ir/consistency.py +397 -0
- prograph/plan/ir/construct.py +90 -0
- prograph/plan/ir/cost.py +234 -0
- prograph/plan/ir/cypher_answer.py +1081 -0
- prograph/plan/ir/cypher_apoc.py +717 -0
- prograph/plan/ir/cypher_apoc_convert.py +505 -0
- prograph/plan/ir/cypher_apoc_date.py +845 -0
- prograph/plan/ir/cypher_apoc_map.py +496 -0
- prograph/plan/ir/cypher_apoc_meta.py +384 -0
- prograph/plan/ir/cypher_apoc_path.py +597 -0
- prograph/plan/ir/cypher_apoc_procedures.py +163 -0
- prograph/plan/ir/cypher_apoc_text.py +785 -0
- prograph/plan/ir/cypher_front.py +5474 -0
- prograph/plan/ir/cypher_functions.py +565 -0
- prograph/plan/ir/cypher_lists.py +935 -0
- prograph/plan/ir/cypher_maps.py +370 -0
- prograph/plan/ir/cypher_mixed.py +604 -0
- prograph/plan/ir/cypher_ontology.py +226 -0
- prograph/plan/ir/cypher_procedures.py +191 -0
- prograph/plan/ir/cypher_runtime_errors.py +107 -0
- prograph/plan/ir/cypher_temporal.py +1357 -0
- prograph/plan/ir/cypher_temporal_arith.py +339 -0
- prograph/plan/ir/cypher_values.py +546 -0
- prograph/plan/ir/cypher_write.py +612 -0
- prograph/plan/ir/dataset.py +643 -0
- prograph/plan/ir/dates.py +102 -0
- prograph/plan/ir/entry.py +992 -0
- prograph/plan/ir/expr.py +260 -0
- prograph/plan/ir/facts.py +1171 -0
- prograph/plan/ir/fresh.py +291 -0
- prograph/plan/ir/freshness.py +625 -0
- prograph/plan/ir/identity.py +797 -0
- prograph/plan/ir/java_datetime.py +265 -0
- prograph/plan/ir/java_regex.py +391 -0
- prograph/plan/ir/join_order.py +39 -0
- prograph/plan/ir/key_checks.py +433 -0
- prograph/plan/ir/keys.py +454 -0
- prograph/plan/ir/lower.py +5323 -0
- prograph/plan/ir/ops.py +619 -0
- prograph/plan/ir/owl_rl.py +1119 -0
- prograph/plan/ir/owl_rl_rules.py +725 -0
- prograph/plan/ir/profiles.py +694 -0
- prograph/plan/ir/ql.py +987 -0
- prograph/plan/ir/rdfs.py +342 -0
- prograph/plan/ir/rewrite.py +1782 -0
- prograph/plan/ir/saturate.py +339 -0
- prograph/plan/ir/semantics.py +103 -0
- prograph/plan/ir/slot.py +509 -0
- prograph/plan/ir/sparql_front.py +3418 -0
- prograph/plan/ir/strings.py +534 -0
- prograph/plan/ir/term_functions.py +274 -0
- prograph/plan/ir/terms.py +84 -0
- prograph/plan/ir/xpath_regex.py +264 -0
- prograph/plan/run_record.py +65 -0
- prograph/plan/sql/__init__.py +21 -0
- prograph/plan/sql/dialect.py +3726 -0
- prograph/plan/sql/mysql_serializer.py +103 -0
- prograph/plan/sql/names.py +65 -0
- prograph/plan/sql/oracle_serializer.py +306 -0
- prograph/plan/sql/pg_serializer.py +501 -0
- prograph/plan/sql/rel_algebra.py +781 -0
- prograph/plan/sql/sql_serializer.py +1419 -0
- prograph/plan/sql/sqlserver_serializer.py +603 -0
- prograph/provenance.py +222 -0
- prograph/r2rml/__init__.py +48 -0
- prograph/r2rml/catalog.py +259 -0
- prograph/r2rml/compile.py +372 -0
- prograph/r2rml/direct.py +140 -0
- prograph/r2rml/export.py +495 -0
- prograph/r2rml/model.py +180 -0
- prograph/r2rml/parser.py +546 -0
- prograph/r2rml/schema.py +120 -0
- prograph/read_only.py +122 -0
- prograph/results.py +157 -0
- prograph/rules/__init__.py +168 -0
- prograph/rules/__main__.py +32 -0
- prograph/schema/__init__.py +60 -0
- prograph/schema/change_set.py +50 -0
- prograph/schema/collations.py +198 -0
- prograph/schema/compile_schema.py +388 -0
- prograph/schema/context.py +845 -0
- prograph/schema/examples.py +234 -0
- prograph/schema/iri.py +817 -0
- prograph/schema/literals.py +94 -0
- prograph/schema/lpg_view.py +617 -0
- prograph/schema/mapping.py +1803 -0
- prograph/schema/maps_schema.py +40 -0
- prograph/schema/naming.py +77 -0
- prograph/schema/query_sources.py +59 -0
- prograph/schema/rows.py +215 -0
- prograph/schema/source_map.py +1103 -0
- prograph/schema/store_view.py +459 -0
- prograph/schema/types.py +569 -0
- prograph/schema/xsd.py +569 -0
- prograph/shacl/__init__.py +30 -0
- prograph/shacl/dqv.py +58 -0
- prograph/shacl/generate.py +255 -0
- prograph/shacl/report.py +138 -0
- prograph/shacl/validator.py +164 -0
- prograph/store.py +1737 -0
- prograph/utils/__init__.py +6 -0
- prograph/utils/exceptions.py +308 -0
- prograph/utils/helpers.py +126 -0
- prograph/warnings.py +46 -0
- prograph/writes.py +369 -0
- prograph-1.3.3.dist-info/METADATA +281 -0
- prograph-1.3.3.dist-info/RECORD +237 -0
- prograph-1.3.3.dist-info/WHEEL +5 -0
- prograph-1.3.3.dist-info/entry_points.txt +3 -0
- prograph-1.3.3.dist-info/licenses/LICENSE +201 -0
- prograph-1.3.3.dist-info/licenses/NOTICE +10 -0
- prograph-1.3.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,603 @@
|
|
|
1
|
+
"""SQL Server SQL Serializer.
|
|
2
|
+
|
|
3
|
+
Subclasses ``SparkSQLSerializer`` to emit SQL Server-compatible SQL.
|
|
4
|
+
About 80% of the serialization logic is shared; this module overrides
|
|
5
|
+
the methods that differ between Spark SQL and SQL Server.
|
|
6
|
+
|
|
7
|
+
Key differences:
|
|
8
|
+
|
|
9
|
+
- Identifier quoting: brackets ``[name]`` instead of backticks
|
|
10
|
+
- No ``LIMIT``: use ``TOP N`` (without ORDER BY) or
|
|
11
|
+
``OFFSET ... ROWS FETCH NEXT ... ROWS ONLY`` (with ORDER BY)
|
|
12
|
+
- No ``LEFT ANTI JOIN``: use ``LEFT JOIN ... WHERE x IS NULL``
|
|
13
|
+
- No ``LEFT SEMI JOIN``, use ``WHERE EXISTS (SELECT 1 ...)``
|
|
14
|
+
- No ``LATERAL VIEW EXPLODE``, use ``CROSS APPLY OPENJSON(...)``
|
|
15
|
+
- No ``/*+ BROADCAST(...) */`` hints
|
|
16
|
+
- Different function names and type keywords
|
|
17
|
+
|
|
18
|
+
Architecture:
|
|
19
|
+
|
|
20
|
+
RelNode tree -> Optimizer -> SQLServerSerializer -> SQL Server SQL string
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import re
|
|
26
|
+
from typing import List
|
|
27
|
+
|
|
28
|
+
from .rel_algebra import (
|
|
29
|
+
AntiJoin,
|
|
30
|
+
ColumnRef,
|
|
31
|
+
ColExpr,
|
|
32
|
+
CompareExpr,
|
|
33
|
+
Expr,
|
|
34
|
+
FuncExpr,
|
|
35
|
+
IsNullExpr,
|
|
36
|
+
LitExpr,
|
|
37
|
+
RelNode,
|
|
38
|
+
Scan,
|
|
39
|
+
SemiJoin,
|
|
40
|
+
SetOperation,
|
|
41
|
+
SortKey,
|
|
42
|
+
SortDirection,
|
|
43
|
+
NullsOrder,
|
|
44
|
+
WithClause,
|
|
45
|
+
)
|
|
46
|
+
from .sql_serializer import (
|
|
47
|
+
ExprSerializer,
|
|
48
|
+
QueryFrame,
|
|
49
|
+
SparkSQLSerializer,
|
|
50
|
+
_CAST_FUNCTIONS,
|
|
51
|
+
)
|
|
52
|
+
from .dialect import SQLSERVER_DIALECT, safe_alias
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# =========================================================================
|
|
56
|
+
# SQL Server CAST type mappings
|
|
57
|
+
# =========================================================================
|
|
58
|
+
|
|
59
|
+
_MSSQL_CAST_FUNCTIONS = {
|
|
60
|
+
'CAST_INT': 'INT',
|
|
61
|
+
'CAST_DOUBLE': 'FLOAT',
|
|
62
|
+
'CAST_STRING': 'NVARCHAR(MAX)',
|
|
63
|
+
'CAST_LONG': 'BIGINT',
|
|
64
|
+
'CAST_WHOLE_DECIMAL': 'DECIMAL(38,0)',
|
|
65
|
+
'CAST_PATH_STRING': 'NVARCHAR(MAX)',
|
|
66
|
+
'CAST_BOOLEAN': 'BIT',
|
|
67
|
+
'CAST_TIMESTAMP': 'DATETIME2',
|
|
68
|
+
'CAST_DATE': 'DATE',
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _is_predicate(expr) -> bool:
|
|
73
|
+
"""Whether a translated expression is a truth value, not a scalar."""
|
|
74
|
+
from .rel_algebra import (AndExpr, CompareExpr, ExistsExpr, IsNullExpr, NotExpr,
|
|
75
|
+
OrExpr)
|
|
76
|
+
if isinstance(expr, (CompareExpr, AndExpr, OrExpr, NotExpr, IsNullExpr, ExistsExpr)):
|
|
77
|
+
return True
|
|
78
|
+
if isinstance(expr, LitExpr) and isinstance(expr.value, bool):
|
|
79
|
+
return True
|
|
80
|
+
if isinstance(expr, FuncExpr) and expr.name == 'REGEX_FULL':
|
|
81
|
+
return True # Oracle's REGEXP_LIKE is a condition, not a value
|
|
82
|
+
return _is_truth_case(expr)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _case_results(expr) -> list:
|
|
86
|
+
"""The THEN and ELSE expressions of a CASE or CASE_SIMPLE FuncExpr."""
|
|
87
|
+
args = expr.args[1:] if expr.name == 'CASE_SIMPLE' else expr.args
|
|
88
|
+
results = [args[i] for i in range(1, len(args), 2)]
|
|
89
|
+
if len(args) % 2:
|
|
90
|
+
results.append(args[-1])
|
|
91
|
+
return results
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _is_truth_case(expr) -> bool:
|
|
95
|
+
"""Whether a CASE answers a truth value: a result that is a predicate
|
|
96
|
+
or TRUE or FALSE, the others NULL. `a = 'x'` between a number and a
|
|
97
|
+
string is written so (`CASE WHEN a IS NULL OR ... THEN NULL ELSE FALSE
|
|
98
|
+
END`)."""
|
|
99
|
+
if not (isinstance(expr, FuncExpr) and expr.name in ('CASE', 'CASE_SIMPLE')):
|
|
100
|
+
return False
|
|
101
|
+
results = [r for r in _case_results(expr)
|
|
102
|
+
if not (isinstance(r, LitExpr) and r.value is None)]
|
|
103
|
+
return bool(results) and all(_is_predicate(r) for r in results)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class TruthCaseWithoutBooleans:
|
|
107
|
+
"""For a dialect with no boolean type (SQL Server, Oracle): a CASE
|
|
108
|
+
that answers a truth value.
|
|
109
|
+
|
|
110
|
+
A CASE's results are values there, and TRUE, FALSE and a comparison
|
|
111
|
+
are conditions, so `CASE WHEN c THEN NULL ELSE (1=0) END` is not SQL
|
|
112
|
+
(ORA-00907). Each result is written as 1, 0 or NULL,
|
|
113
|
+
and the CASE compared with 1, which makes it the condition every
|
|
114
|
+
other truth value is in these dialects; where a value is wanted the
|
|
115
|
+
serializer's `_predicate_value` turns it back, as for any predicate."""
|
|
116
|
+
|
|
117
|
+
def _case_expr(self, args: tuple, simple: bool = False) -> str:
|
|
118
|
+
expr = FuncExpr(name='CASE_SIMPLE' if simple else 'CASE', args=tuple(args))
|
|
119
|
+
if not _is_truth_case(expr):
|
|
120
|
+
return super()._case_expr(args, simple)
|
|
121
|
+
parts, idx = ['CASE'], 0
|
|
122
|
+
if simple:
|
|
123
|
+
parts.append(self.serialize(args[0]))
|
|
124
|
+
idx = 1
|
|
125
|
+
while idx + 1 < len(args):
|
|
126
|
+
parts.append(f"WHEN {self.serialize(args[idx])} THEN {self._truth_value(args[idx + 1])}")
|
|
127
|
+
idx += 2
|
|
128
|
+
if idx < len(args):
|
|
129
|
+
parts.append(f"ELSE {self._truth_value(args[idx])}")
|
|
130
|
+
parts.append('END')
|
|
131
|
+
return f"({' '.join(parts)} = 1)"
|
|
132
|
+
|
|
133
|
+
def _truth_value(self, result) -> str:
|
|
134
|
+
if isinstance(result, LitExpr) and result.value is None:
|
|
135
|
+
return 'NULL'
|
|
136
|
+
if isinstance(result, LitExpr) and isinstance(result.value, bool):
|
|
137
|
+
return '1' if result.value else '0'
|
|
138
|
+
condition = self.serialize(result)
|
|
139
|
+
return f"CASE WHEN {condition} THEN 1 WHEN NOT ({condition}) THEN 0 END"
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class SQLServerExprSerializer(TruthCaseWithoutBooleans, ExprSerializer):
|
|
143
|
+
"""Serialize Expr nodes to SQL Server SQL strings.
|
|
144
|
+
|
|
145
|
+
Overrides ``_func_expr`` to translate Spark-centric IR function names
|
|
146
|
+
to their SQL Server equivalents.
|
|
147
|
+
"""
|
|
148
|
+
|
|
149
|
+
# No boolean type: a predicate is not a value to compare.
|
|
150
|
+
_predicates_are_values = False
|
|
151
|
+
|
|
152
|
+
def _func_expr(self, expr: FuncExpr) -> str:
|
|
153
|
+
name = expr.name
|
|
154
|
+
args = expr.args
|
|
155
|
+
|
|
156
|
+
# ---- SQL Server CAST types ----
|
|
157
|
+
if name in _MSSQL_CAST_FUNCTIONS:
|
|
158
|
+
target_type = _MSSQL_CAST_FUNCTIONS[name]
|
|
159
|
+
inner = self.serialize(args[0])
|
|
160
|
+
if name == 'CAST_PATH_STRING':
|
|
161
|
+
# A walk's path string, in both terms of its recursive CTE.
|
|
162
|
+
# A collation is part of a column's type here: text keys in
|
|
163
|
+
# a collation that is not the database's gave the step's
|
|
164
|
+
# path theirs and the seed the database's, "Types don't
|
|
165
|
+
# match between the anchor and the recursive part".
|
|
166
|
+
# Binary, so an edge is found in the path by its exact text.
|
|
167
|
+
return f"CAST({inner} AS {target_type}) COLLATE Latin1_General_100_BIN2"
|
|
168
|
+
return f"CAST({inner} AS {target_type})"
|
|
169
|
+
|
|
170
|
+
# COLLECT_LIST / COLLECT_SET / NAMED_STRUCT / SIZE / ARRAY have no SQL
|
|
171
|
+
# Server form (no list or map type); the dialect map refuses them
|
|
172
|
+
# before this is reached. They used to render as STRING_AGG (a
|
|
173
|
+
# string, not a list), a tuple, and NULL: wrong answers.
|
|
174
|
+
|
|
175
|
+
# ---- MONTHS_BETWEEN -> DATEDIFF(MONTH, start, end) ----
|
|
176
|
+
if name == 'MONTHS_BETWEEN':
|
|
177
|
+
end = self.serialize(args[0])
|
|
178
|
+
start = self.serialize(args[1])
|
|
179
|
+
return f"DATEDIFF(MONTH, {start}, {end})"
|
|
180
|
+
|
|
181
|
+
# ---- DATEDIFF -> DATEDIFF(DAY, start, end), reversed args ----
|
|
182
|
+
if name == 'DATEDIFF':
|
|
183
|
+
end = self.serialize(args[0])
|
|
184
|
+
start = self.serialize(args[1])
|
|
185
|
+
return f"DATEDIFF(DAY, {start}, {end})"
|
|
186
|
+
|
|
187
|
+
# ---- CURRENT_DATE() -> CAST(GETDATE() AS DATE) ----
|
|
188
|
+
if name == 'CURRENT_DATE' and len(args) == 0:
|
|
189
|
+
return 'CAST(GETDATE() AS DATE)'
|
|
190
|
+
if name == 'CURRENT_TIMESTAMP' and len(args) == 0:
|
|
191
|
+
return 'CURRENT_TIMESTAMP'
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
# ---- DISTINCT aggregates ----
|
|
195
|
+
if name.startswith('DISTINCT_'):
|
|
196
|
+
real_name = name[len('DISTINCT_'):]
|
|
197
|
+
inner = self.serialize(args[0])
|
|
198
|
+
return f"{real_name}(DISTINCT {inner})"
|
|
199
|
+
|
|
200
|
+
# ---- Arithmetic operators (inherited logic) ----
|
|
201
|
+
from .sql_serializer import _ARITHMETIC_OPS
|
|
202
|
+
if name in _ARITHMETIC_OPS and len(args) == 2:
|
|
203
|
+
left = self.serialize(args[0])
|
|
204
|
+
right = self.serialize(args[1])
|
|
205
|
+
return f"({left} {name} {right})"
|
|
206
|
+
|
|
207
|
+
# ---- CASE (inherited) ----
|
|
208
|
+
if name == 'CASE':
|
|
209
|
+
return self._case_expr(args)
|
|
210
|
+
if name == 'CASE_SIMPLE':
|
|
211
|
+
return self._case_expr(args, simple=True)
|
|
212
|
+
|
|
213
|
+
# ---- Default: standard function call ----
|
|
214
|
+
rendered_args = ', '.join(self.serialize(a) for a in args)
|
|
215
|
+
return f"{name}({rendered_args})"
|
|
216
|
+
|
|
217
|
+
def serialize(self, expr: Expr) -> str:
|
|
218
|
+
"""Override to convert backticks -> brackets and RLIKE -> LIKE."""
|
|
219
|
+
from .rel_algebra import ColExpr, CompareExpr
|
|
220
|
+
|
|
221
|
+
# No boolean type: TRUE / FALSE are predicates that always hold or
|
|
222
|
+
# never do.
|
|
223
|
+
if isinstance(expr, LitExpr) and isinstance(expr.value, bool):
|
|
224
|
+
return '(1=1)' if expr.value else '(1=0)'
|
|
225
|
+
|
|
226
|
+
# ColExpr: replace backticks with brackets
|
|
227
|
+
if isinstance(expr, ColExpr):
|
|
228
|
+
return _backtick_to_bracket(expr.ref.sql)
|
|
229
|
+
|
|
230
|
+
# RLIKE -> LIKE (best effort, SQL Server has no native regex)
|
|
231
|
+
if isinstance(expr, CompareExpr) and expr.op == 'RLIKE':
|
|
232
|
+
left = self.serialize(expr.left)
|
|
233
|
+
right = self.serialize(expr.right)
|
|
234
|
+
return f"{left} LIKE {right}"
|
|
235
|
+
|
|
236
|
+
return super().serialize(expr)
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
class SQLServerSerializer(SparkSQLSerializer):
|
|
240
|
+
"""Serialize a RelNode tree to SQL Server SQL.
|
|
241
|
+
|
|
242
|
+
Subclasses ``SparkSQLSerializer`` and overrides the methods that
|
|
243
|
+
differ between Spark SQL and SQL Server.
|
|
244
|
+
"""
|
|
245
|
+
|
|
246
|
+
_alias_dialect = 'sqlserver'
|
|
247
|
+
|
|
248
|
+
_recursive_with = "WITH"
|
|
249
|
+
_recursion_level_clause = ""
|
|
250
|
+
# SQL Server stops a statement's recursion at 100 levels unless the
|
|
251
|
+
# statement says otherwise; 0 is no limit. A walk ends by itself.
|
|
252
|
+
_recursive_statement_suffix = "\nOPTION (MAXRECURSION 0)"
|
|
253
|
+
|
|
254
|
+
def __init__(self):
|
|
255
|
+
from .dialect import get_dialect_instance
|
|
256
|
+
self._expr = SQLServerExprSerializer(get_dialect_instance('sqlserver'))
|
|
257
|
+
self._dialect = SQLSERVER_DIALECT
|
|
258
|
+
|
|
259
|
+
# -----------------------------------------------------------------
|
|
260
|
+
# Column rendering, bracket quoting
|
|
261
|
+
# -----------------------------------------------------------------
|
|
262
|
+
|
|
263
|
+
def _predicate_value(self, sql: str) -> str:
|
|
264
|
+
"""A predicate is not a value in SQL Server: 1, 0, or NULL where it
|
|
265
|
+
is unknown, as a BIT, which the driver returns as a
|
|
266
|
+
boolean; nothing converts it after."""
|
|
267
|
+
return f"CAST(CASE WHEN {sql} THEN 1 WHEN NOT ({sql}) THEN 0 END AS BIT)"
|
|
268
|
+
|
|
269
|
+
def _render_column(self, col: ColumnRef) -> str:
|
|
270
|
+
"""Render a ColumnRef with bracket quoting."""
|
|
271
|
+
expr_node = col.metadata.get('expr') if col.metadata else None
|
|
272
|
+
if expr_node is not None:
|
|
273
|
+
sql = self._value_sql(self._expr.as_value(expr_node))
|
|
274
|
+
else:
|
|
275
|
+
sql = _backtick_to_bracket(col.sql)
|
|
276
|
+
if col.alias:
|
|
277
|
+
return f'{sql} AS {self._quote_alias(col.alias)}'
|
|
278
|
+
return sql
|
|
279
|
+
|
|
280
|
+
def _resolve_null_order_aliases(self, frame) -> None:
|
|
281
|
+
"""SQL Server resolves an output alias in ORDER BY only bare, not
|
|
282
|
+
inside an expression, so the null-order key built from one
|
|
283
|
+
(`CASE WHEN [cnt] IS NULL ...`) takes the alias's expression."""
|
|
284
|
+
exprs = {}
|
|
285
|
+
for col in frame.select_columns:
|
|
286
|
+
head, sep, alias = col.rpartition(' AS [')
|
|
287
|
+
if sep and alias.endswith(']'):
|
|
288
|
+
exprs[f"[{alias}"] = head
|
|
289
|
+
fixed = []
|
|
290
|
+
for key in frame.order_by_columns:
|
|
291
|
+
if key.startswith('CASE WHEN ['):
|
|
292
|
+
ref = key[len('CASE WHEN '):key.index(' IS NULL')]
|
|
293
|
+
if ref in exprs:
|
|
294
|
+
key = f"CASE WHEN ({exprs[ref]}) IS NULL" + key[len('CASE WHEN ') + len(ref) + len(' IS NULL'):]
|
|
295
|
+
fixed.append(key)
|
|
296
|
+
frame.order_by_columns = fixed
|
|
297
|
+
|
|
298
|
+
def _collect_lateral_explode(self, node, frame):
|
|
299
|
+
# No array type to unnest (collect() refuses here too); the OPENJSON
|
|
300
|
+
# form this used named a column the query cannot see.
|
|
301
|
+
from prograph.utils.exceptions import SQLGenerationError
|
|
302
|
+
raise SQLGenerationError("UNWIND has no SQL Server rendering",
|
|
303
|
+
reason="unsupported_unwind:sqlserver")
|
|
304
|
+
|
|
305
|
+
def _render_sort_key(self, key: SortKey, beside=None) -> str:
|
|
306
|
+
"""Render a SortKey with bracket quoting.
|
|
307
|
+
|
|
308
|
+
`beside` is not read: the key is written twice, inside `CASE WHEN
|
|
309
|
+
... IS NULL` too, where a position would be a constant, so a key
|
|
310
|
+
named by one of two columns that differ by case alone is not
|
|
311
|
+
written by its position here as it is on DuckDB, Spark and
|
|
312
|
+
Databricks (the base class's `_render_sort_key`). A fault there is
|
|
313
|
+
logged and not fixed."""
|
|
314
|
+
col = key.column
|
|
315
|
+
expr_node = col.metadata.get('expr') if col.metadata else None
|
|
316
|
+
if expr_node is not None and not isinstance(expr_node, ColExpr):
|
|
317
|
+
ref = self._expr.serialize(expr_node)
|
|
318
|
+
elif col.sql and col.sql != '<sort_expr>':
|
|
319
|
+
ref = _backtick_to_bracket(col.sql)
|
|
320
|
+
elif col.alias:
|
|
321
|
+
ref = self._quote_alias(col.alias)
|
|
322
|
+
else:
|
|
323
|
+
ref = _backtick_to_bracket(col.sql)
|
|
324
|
+
ref = self._by_code_point(key, ref)
|
|
325
|
+
|
|
326
|
+
direction = 'ASC' if key.direction == SortDirection.ASC else 'DESC'
|
|
327
|
+
# SQL Server has no NULLS FIRST / LAST and sorts nulls lowest;
|
|
328
|
+
# Cypher's null is the largest value, last in ASC and first in DESC.
|
|
329
|
+
# A leading key puts them there.
|
|
330
|
+
nulls = key.nulls
|
|
331
|
+
if nulls is None:
|
|
332
|
+
nulls = NullsOrder.LAST if key.direction == SortDirection.ASC else NullsOrder.FIRST
|
|
333
|
+
null_dir = 'DESC' if nulls == NullsOrder.FIRST else 'ASC'
|
|
334
|
+
return f"CASE WHEN {ref} IS NULL THEN 1 ELSE 0 END {null_dir}, {ref} {direction}"
|
|
335
|
+
|
|
336
|
+
# -----------------------------------------------------------------
|
|
337
|
+
# Anti-join -> LEFT JOIN ... WHERE IS NULL
|
|
338
|
+
# -----------------------------------------------------------------
|
|
339
|
+
|
|
340
|
+
def _collect_anti_join(self, node: AntiJoin, frame: QueryFrame) -> None:
|
|
341
|
+
"""SQL Server doesn't support LEFT ANTI JOIN.
|
|
342
|
+
|
|
343
|
+
Emits LEFT JOIN + WHERE ... IS NULL instead.
|
|
344
|
+
"""
|
|
345
|
+
self._collect_from(node.child, frame)
|
|
346
|
+
|
|
347
|
+
right_clause = self._render_from_node(node.right)
|
|
348
|
+
conditions = ' AND '.join(
|
|
349
|
+
self._expr.serialize(c) for c in node.on
|
|
350
|
+
)
|
|
351
|
+
frame.joins.append(
|
|
352
|
+
f"LEFT JOIN {right_clause} ON {conditions}"
|
|
353
|
+
)
|
|
354
|
+
if node.null_check:
|
|
355
|
+
frame.where_conditions.append(
|
|
356
|
+
self._expr.serialize(node.null_check)
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
if node.second_right:
|
|
360
|
+
right2_clause = self._render_from_node(node.second_right)
|
|
361
|
+
conditions2 = ' AND '.join(
|
|
362
|
+
self._expr.serialize(c) for c in node.second_on
|
|
363
|
+
)
|
|
364
|
+
frame.joins.append(
|
|
365
|
+
f"LEFT JOIN {right2_clause} ON {conditions2}"
|
|
366
|
+
)
|
|
367
|
+
if node.second_null_check:
|
|
368
|
+
frame.where_conditions.append(
|
|
369
|
+
self._expr.serialize(node.second_null_check)
|
|
370
|
+
)
|
|
371
|
+
|
|
372
|
+
# -----------------------------------------------------------------
|
|
373
|
+
# Semi-join -> WHERE EXISTS (SELECT 1 ...)
|
|
374
|
+
# -----------------------------------------------------------------
|
|
375
|
+
|
|
376
|
+
def _collect_semi_join(self, node: SemiJoin, frame: QueryFrame) -> None:
|
|
377
|
+
"""SQL Server doesn't support LEFT SEMI JOIN.
|
|
378
|
+
|
|
379
|
+
Emits WHERE EXISTS (SELECT 1 FROM ... WHERE ...) instead.
|
|
380
|
+
"""
|
|
381
|
+
self._collect_from(node.child, frame)
|
|
382
|
+
|
|
383
|
+
right_clause = self._render_from_node(node.right)
|
|
384
|
+
conditions = ' AND '.join(
|
|
385
|
+
self._expr.serialize(c) for c in node.on
|
|
386
|
+
)
|
|
387
|
+
frame.where_conditions.append(
|
|
388
|
+
f"EXISTS (SELECT 1 FROM {right_clause} WHERE {conditions})"
|
|
389
|
+
)
|
|
390
|
+
|
|
391
|
+
# -----------------------------------------------------------------
|
|
392
|
+
# Lateral explode -> CROSS APPLY OPENJSON()
|
|
393
|
+
# -----------------------------------------------------------------
|
|
394
|
+
|
|
395
|
+
# -----------------------------------------------------------------
|
|
396
|
+
# GROUP BY quoting
|
|
397
|
+
# -----------------------------------------------------------------
|
|
398
|
+
|
|
399
|
+
def _group_by_column(self, sql: str) -> str:
|
|
400
|
+
return _backtick_to_bracket(sql)
|
|
401
|
+
|
|
402
|
+
# -----------------------------------------------------------------
|
|
403
|
+
# FROM node rendering, bracket quoting for subquery aliases
|
|
404
|
+
# -----------------------------------------------------------------
|
|
405
|
+
|
|
406
|
+
def _render_from_node(self, node: RelNode) -> str:
|
|
407
|
+
"""Render a node as a FROM/JOIN target with bracket quoting."""
|
|
408
|
+
if isinstance(node, Scan):
|
|
409
|
+
return self._scan_ref(node)
|
|
410
|
+
|
|
411
|
+
from .rel_algebra import CteRef
|
|
412
|
+
if isinstance(node, CteRef):
|
|
413
|
+
return node.cte_name
|
|
414
|
+
|
|
415
|
+
if isinstance(node, SetOperation):
|
|
416
|
+
sub_sql = self._serialize_set_op(node)
|
|
417
|
+
alias = node.alias or '_sub'
|
|
418
|
+
return f'({sub_sql}) AS [{safe_alias(alias)}]'
|
|
419
|
+
|
|
420
|
+
raise NotImplementedError(f"one-plan: {type(node).__name__} as a FROM target")
|
|
421
|
+
|
|
422
|
+
# -----------------------------------------------------------------
|
|
423
|
+
# SetOperation subquery wrapping, bracket alias
|
|
424
|
+
# -----------------------------------------------------------------
|
|
425
|
+
|
|
426
|
+
def _collect_from(
|
|
427
|
+
self, node: RelNode, frame: QueryFrame, *, top_level: bool = False,
|
|
428
|
+
) -> None:
|
|
429
|
+
"""Walk the remaining tree, override to use bracket aliases."""
|
|
430
|
+
if isinstance(node, SetOperation):
|
|
431
|
+
sub_sql = self._serialize_set_op(node)
|
|
432
|
+
alias = node.alias or '_sub'
|
|
433
|
+
frame.from_clause = f'({sub_sql}) AS [{safe_alias(alias)}]'
|
|
434
|
+
else:
|
|
435
|
+
super()._collect_from(node, frame, top_level=top_level)
|
|
436
|
+
|
|
437
|
+
# -----------------------------------------------------------------
|
|
438
|
+
# SQL assembly, LIMIT -> TOP / OFFSET FETCH
|
|
439
|
+
# -----------------------------------------------------------------
|
|
440
|
+
|
|
441
|
+
def _assemble_sql(self, frame: QueryFrame) -> str:
|
|
442
|
+
"""SQL Server: Convert LIMIT/OFFSET to TOP or OFFSET FETCH.
|
|
443
|
+
|
|
444
|
+
Strategy:
|
|
445
|
+
- LIMIT without ORDER BY -> SELECT TOP N
|
|
446
|
+
- LIMIT with ORDER BY -> OFFSET 0 ROWS FETCH NEXT N ROWS ONLY
|
|
447
|
+
- OFFSET + LIMIT with ORDER BY -> OFFSET M ROWS FETCH NEXT N ROWS ONLY
|
|
448
|
+
- OFFSET without ORDER BY -> inject ORDER BY (SELECT NULL)
|
|
449
|
+
"""
|
|
450
|
+
self._resolve_null_order_aliases(frame)
|
|
451
|
+
if frame.distinct and frame.order_by_columns:
|
|
452
|
+
wrapped = self._order_distinct_outside(frame)
|
|
453
|
+
if wrapped is not None:
|
|
454
|
+
return wrapped
|
|
455
|
+
has_limit = frame.limit is not None
|
|
456
|
+
has_offset = frame.offset is not None
|
|
457
|
+
has_order = bool(frame.order_by_columns)
|
|
458
|
+
|
|
459
|
+
if not has_limit and not has_offset:
|
|
460
|
+
# No LIMIT/OFFSET, standard assembly
|
|
461
|
+
return frame.to_sql()
|
|
462
|
+
|
|
463
|
+
if has_limit and not has_order and not has_offset:
|
|
464
|
+
# SELECT TOP N ... (no ORDER BY needed)
|
|
465
|
+
return self._assemble_with_top(frame)
|
|
466
|
+
|
|
467
|
+
# Has ORDER BY (or we need to inject one for OFFSET)
|
|
468
|
+
if not has_order:
|
|
469
|
+
# SQL Server requires ORDER BY for OFFSET FETCH
|
|
470
|
+
frame.order_by_columns.append('(SELECT NULL)')
|
|
471
|
+
|
|
472
|
+
return self._assemble_with_fetch(frame)
|
|
473
|
+
|
|
474
|
+
def _order_distinct_outside(self, frame: QueryFrame):
|
|
475
|
+
"""SELECT DISTINCT ... ORDER BY, with the ORDER BY outside.
|
|
476
|
+
|
|
477
|
+
SQL Server requires every ORDER BY item of a DISTINCT query to be in
|
|
478
|
+
its SELECT list, and the null-order key (`CASE WHEN x IS NULL ...`)
|
|
479
|
+
is not. The distinct rows go in a subquery and the outer query
|
|
480
|
+
orders them by the output aliases. None when a key names something
|
|
481
|
+
other than a projected expression.
|
|
482
|
+
"""
|
|
483
|
+
import re as _re
|
|
484
|
+
exprs = []
|
|
485
|
+
for col in frame.select_columns:
|
|
486
|
+
head, sep, alias = col.rpartition(' AS [')
|
|
487
|
+
if sep and alias.endswith(']'):
|
|
488
|
+
exprs.append((head, f"[{alias}"))
|
|
489
|
+
exprs.sort(key=lambda e: -len(e[0]))
|
|
490
|
+
keys = []
|
|
491
|
+
for key in frame.order_by_columns:
|
|
492
|
+
for expr, alias in exprs:
|
|
493
|
+
key = key.replace(f"({expr})", alias).replace(expr, alias)
|
|
494
|
+
if _re.search(r"\w\.\[", key):
|
|
495
|
+
return None
|
|
496
|
+
keys.append(key)
|
|
497
|
+
inner = QueryFrame()
|
|
498
|
+
for name, value in vars(frame).items():
|
|
499
|
+
setattr(inner, name, value)
|
|
500
|
+
inner.order_by_columns, inner.limit, inner.offset = [], None, None
|
|
501
|
+
outer = QueryFrame()
|
|
502
|
+
outer.select_columns = ['*']
|
|
503
|
+
outer.from_clause = f"({inner.to_sql()}) AS _d"
|
|
504
|
+
outer.order_by_columns = keys
|
|
505
|
+
outer.limit, outer.offset = frame.limit, frame.offset
|
|
506
|
+
outer.limit_style = frame.limit_style
|
|
507
|
+
return self._assemble_sql(outer)
|
|
508
|
+
|
|
509
|
+
def _assemble_with_top(self, frame: QueryFrame) -> str:
|
|
510
|
+
"""Assemble SQL with SELECT TOP N instead of LIMIT."""
|
|
511
|
+
parts: List[str] = []
|
|
512
|
+
|
|
513
|
+
distinct_kw = "DISTINCT " if frame.distinct else ""
|
|
514
|
+
top_n = frame.limit
|
|
515
|
+
|
|
516
|
+
if frame.select_columns:
|
|
517
|
+
cols = ', '.join(frame.select_columns)
|
|
518
|
+
parts.append(f"SELECT {distinct_kw}TOP {top_n} {cols}")
|
|
519
|
+
else:
|
|
520
|
+
parts.append(f"SELECT {distinct_kw}TOP {top_n} *")
|
|
521
|
+
|
|
522
|
+
# FROM + JOINs
|
|
523
|
+
if frame.from_clause:
|
|
524
|
+
parts.append(f"FROM {frame.from_clause}")
|
|
525
|
+
for join in frame.joins:
|
|
526
|
+
parts.append(join)
|
|
527
|
+
|
|
528
|
+
# WHERE
|
|
529
|
+
if frame.where_conditions:
|
|
530
|
+
parts.append(f"WHERE {' AND '.join(frame.where_conditions)}")
|
|
531
|
+
|
|
532
|
+
# GROUP BY
|
|
533
|
+
if frame.group_by_columns:
|
|
534
|
+
parts.append(f"GROUP BY {', '.join(frame.group_by_columns)}")
|
|
535
|
+
|
|
536
|
+
# HAVING
|
|
537
|
+
if frame.having_conditions:
|
|
538
|
+
parts.append(f"HAVING {' AND '.join(frame.having_conditions)}")
|
|
539
|
+
|
|
540
|
+
return '\n'.join(parts)
|
|
541
|
+
|
|
542
|
+
def _assemble_with_fetch(self, frame: QueryFrame) -> str:
|
|
543
|
+
"""Assemble SQL with OFFSET ... ROWS FETCH NEXT ... ROWS ONLY."""
|
|
544
|
+
parts: List[str] = []
|
|
545
|
+
|
|
546
|
+
# SELECT
|
|
547
|
+
hint = ""
|
|
548
|
+
distinct_kw = "DISTINCT " if frame.distinct else ""
|
|
549
|
+
if frame.select_columns:
|
|
550
|
+
cols = ', '.join(frame.select_columns)
|
|
551
|
+
parts.append(f"SELECT {hint}{distinct_kw}{cols}")
|
|
552
|
+
else:
|
|
553
|
+
parts.append(f"SELECT {hint}{distinct_kw}*")
|
|
554
|
+
|
|
555
|
+
# FROM + JOINs
|
|
556
|
+
if frame.from_clause:
|
|
557
|
+
parts.append(f"FROM {frame.from_clause}")
|
|
558
|
+
for join in frame.joins:
|
|
559
|
+
parts.append(join)
|
|
560
|
+
|
|
561
|
+
# WHERE
|
|
562
|
+
if frame.where_conditions:
|
|
563
|
+
parts.append(f"WHERE {' AND '.join(frame.where_conditions)}")
|
|
564
|
+
|
|
565
|
+
# GROUP BY
|
|
566
|
+
if frame.group_by_columns:
|
|
567
|
+
parts.append(f"GROUP BY {', '.join(frame.group_by_columns)}")
|
|
568
|
+
|
|
569
|
+
# HAVING
|
|
570
|
+
if frame.having_conditions:
|
|
571
|
+
parts.append(f"HAVING {' AND '.join(frame.having_conditions)}")
|
|
572
|
+
|
|
573
|
+
# ORDER BY
|
|
574
|
+
if frame.order_by_columns:
|
|
575
|
+
parts.append(f"ORDER BY {', '.join(frame.order_by_columns)}")
|
|
576
|
+
|
|
577
|
+
# OFFSET FETCH
|
|
578
|
+
offset_val = frame.offset or 0
|
|
579
|
+
if frame.limit is not None:
|
|
580
|
+
parts.append(
|
|
581
|
+
f"OFFSET {offset_val} ROWS FETCH NEXT {frame.limit} ROWS ONLY"
|
|
582
|
+
)
|
|
583
|
+
else:
|
|
584
|
+
# OFFSET only (no LIMIT)
|
|
585
|
+
parts.append(f"OFFSET {offset_val} ROWS")
|
|
586
|
+
|
|
587
|
+
return '\n'.join(parts)
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
# =========================================================================
|
|
591
|
+
# Utility
|
|
592
|
+
# =========================================================================
|
|
593
|
+
|
|
594
|
+
def _backtick_to_bracket(sql: str) -> str:
|
|
595
|
+
"""Replace backtick-quoted identifiers with bracket-quoted ones.
|
|
596
|
+
|
|
597
|
+
The IR tree bakes backtick quoting into ColumnRef.sql values from tree
|
|
598
|
+
construction. The SQL Server serializer swaps backticks for brackets
|
|
599
|
+
at render time.
|
|
600
|
+
|
|
601
|
+
Example: ``p.`name``` -> ``p.[name]``
|
|
602
|
+
"""
|
|
603
|
+
return re.sub(r'`([^`]*)`', r'[\1]', sql)
|