-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathtranslator.py
More file actions
464 lines (393 loc) · 17 KB
/
Copy pathtranslator.py
File metadata and controls
464 lines (393 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
from __future__ import annotations
import json
import re
import sys
from pathlib import Path
import sqlparse
from felderize.config import Config
from felderize.constants import DEFAULT_MAX_RETRIES
from felderize.feldera_client import validate_sql
from felderize.llm import LLMClient, PromptTooLargeError, create_client
from felderize.models import Status, TranslationResult
from felderize.prompts import (
build_force_query_prompt,
build_repair_prompt,
build_user_prompt,
)
from felderize.skills import build_system_prompt
_CREATE_TABLE_RE = re.compile(r"\bCREATE\s+TABLE\b", re.IGNORECASE)
_CREATE_VIEW_RE = re.compile(
r"\bCREATE\s+(OR\s+REPLACE\s+)?(TEMP(ORARY)?\s+)?VIEW\b", re.IGNORECASE
)
_SELECT_RE = re.compile(r"\bSELECT\b", re.IGNORECASE)
def trim_schema_to_referenced(schema_sql: str, query_sql: str) -> str:
"""Return schema_sql with unreferenced CREATE TABLE statements removed.
Builds a reference set from the query SQL and the SELECT bodies of any
CREATE VIEW statements in the schema. CREATE TABLE definitions themselves
are excluded from the scan so a table's own name doesn't count as a
self-reference. Non-table statements (views) always pass through.
This reduces LLM prompt size when a schema has many shared tables that the
specific query doesn't use.
"""
stmts = [s.strip() for s in sqlparse.split(schema_sql) if s.strip()]
scan_parts: list[str] = [query_sql]
for stmt in stmts:
s = stmt.rstrip(";")
vm = re.match(
r"CREATE\s+(?:OR\s+REPLACE\s+)?(?:TEMP(?:ORARY)?\s+)?VIEW\s+\S+"
r"(?:\s*\([^)]*\))?" # optional column list
r"(?:\s|--[^\n]*\n)*" # optional whitespace and line comments
r"AS\s+(.*)",
s,
re.IGNORECASE | re.DOTALL,
)
if vm:
scan_parts.append(vm.group(1))
ref_tokens: set[str] = set(
re.findall(r"\b[a-z_]\w*\b", "\n".join(scan_parts).lower())
)
kept: list[str] = []
for stmt in stmts:
s = stmt.rstrip(";")
m = re.match(
r"CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?(\w+)", s, re.IGNORECASE
)
if m:
if m.group(1).lower() in ref_tokens:
kept.append(stmt if stmt.endswith(";") else stmt + ";")
else:
kept.append(stmt if stmt.endswith(";") else stmt + ";")
return "\n\n".join(kept)
def validate_schema(schema_sql: str) -> list[str]:
"""Return validation errors for the schema SQL, empty if valid."""
if not schema_sql.strip():
return ["Schema file is empty."]
if not _CREATE_TABLE_RE.search(schema_sql):
return [
"Schema file does not contain any CREATE TABLE statement. "
"Expected a SQL schema, not arbitrary text."
]
return []
def validate_query(query_sql: str) -> list[str]:
"""Return validation errors for the query SQL, empty if valid."""
if not query_sql.strip():
return ["Query file is empty."]
if not _CREATE_VIEW_RE.search(query_sql) and not _SELECT_RE.search(query_sql):
return [
"Query file does not contain any CREATE VIEW or SELECT statement. "
"Expected a SQL query, not arbitrary text."
]
return []
def validate_inputs(schema_sql: str, query_sql: str) -> list[str]:
"""Return validation errors for both schema and query SQL."""
return validate_schema(schema_sql) + validate_query(query_sql)
def split_combined_sql(sql: str) -> tuple[str, str]:
"""Split a combined SQL file into (schema_sql, query_sql).
Schema: CREATE TABLE statements. Query: CREATE [OR REPLACE] [TEMP] VIEW
statements. Unrecognised statements are placed in schema_sql.
"""
raw_stmts = [s.strip().rstrip(";") for s in sqlparse.split(sql) if s.strip()]
schema_parts: list[str] = []
query_parts: list[str] = []
for stripped in raw_stmts:
if not stripped:
continue
first_kw = next(
(
ln.strip()
for ln in stripped.splitlines()
if ln.strip() and not ln.strip().startswith("--")
),
"",
).upper()
if not first_kw:
continue
if _CREATE_VIEW_RE.match(first_kw):
query_parts.append(stripped + ";")
else:
schema_parts.append(stripped + ";")
return "\n\n".join(schema_parts), "\n\n".join(query_parts)
# ---------------------------------------------------------------------------
# LLM response parsing
# ---------------------------------------------------------------------------
def _parse_response(raw: str) -> dict:
"""Extract JSON from an LLM response, handling markdown fences."""
text = raw.strip()
match = re.search(r"```(?:json)?\s*\n(.*?)```", text, re.DOTALL)
if match:
text = match.group(1).strip()
return json.loads(text)
_SQL_FENCE_RE = re.compile(r"```(?:sql)?\s*\n(.*?)```", re.DOTALL | re.IGNORECASE)
# A salvageable statement must be a *structured* CREATE — a VIEW followed by AS,
# or a TABLE followed by its column list — not merely the word "CREATE". The
# strict shape stops the model's reasoning prose (e.g. "CREATE VIEW statement
# from the previous attempt's reasoning...") from being scraped in as SQL.
_CREATE_STMT_RE = re.compile(
r"""\bCREATE\s+(?:OR\s+REPLACE\s+)?
(?:(?:LOCAL|MATERIALIZED|TEMP(?:ORARY)?)\s+)?
(?:VIEW\s+[\w`".-]+\s+AS\b | TABLE\s+[\w`".-]+\s*\()
.*?;""",
re.DOTALL | re.IGNORECASE | re.VERBOSE,
)
def _salvage_sql(raw: str) -> tuple[str, str]:
"""Best-effort extraction of (schema_sql, query_sql) from a non-JSON response.
Some responses come back as prose or bare SQL instead of the requested JSON
(or as truncated JSON). Rather than give up and return nothing, pull whatever
SQL we can: prefer ```sql fences, else scan for well-formed CREATE statements,
then split them into schema (CREATE TABLE) vs query (CREATE VIEW). Returns
("", "") when no SQL-looking statements are present.
"""
fences = _SQL_FENCE_RE.findall(raw)
text = "\n".join(f.strip() for f in fences) if fences else raw
statements = _CREATE_STMT_RE.findall(text)
if not statements:
return "", ""
return split_combined_sql("\n".join(statements))
def _parse_or_salvage(raw: str) -> tuple[dict, list[str]]:
"""Parse the JSON response, or salvage best-effort SQL when that fails.
Returns (data, warnings). On a clean parse, warnings is empty. On failure,
salvaged SQL (if any) is returned as a result dict so the caller always has
something to validate/repair rather than an empty translation.
"""
try:
return _parse_response(raw), []
except (json.JSONDecodeError, KeyError):
schema_sql, query_sql = _salvage_sql(raw)
if schema_sql.strip() or query_sql.strip():
return (
{
"feldera_schema": schema_sql,
"feldera_query": query_sql,
"unsupported": [],
},
["LLM response was not valid JSON — salvaged SQL from the raw text"],
)
return {}, ["Failed to parse LLM response and no SQL could be salvaged"]
def _as_str(val: object) -> str:
"""Normalize a value to string — handles lists from LLM responses."""
if isinstance(val, list):
return "\n".join(str(v) for v in val)
return str(val) if val else ""
def _as_list(val: object) -> list[str]:
if isinstance(val, list):
return [str(v) for v in val]
if isinstance(val, str) and val:
return [val]
return []
def _normalize_escapes(sql: str) -> str:
"""Undo a doubly-escaped LLM response.
Some responses arrive escaped a second time, so the SQL body holds the
literal two-character sequences ``\\n`` and ``\\"`` instead of real newlines
and quotes; the compiler then fails on the stray backslash. When the text
carries no real newline yet contains a literal ``\\n``, the whole statement
has collapsed onto one line — treat it as doubly escaped and restore the
intended characters. Genuinely multi-line SQL is left untouched.
"""
if "\n" in sql or "\\n" not in sql:
return sql
return (
sql.replace("\\n", "\n")
.replace("\\t", "\t")
.replace("\\r", "\r")
.replace('\\"', '"')
)
def _build_result(data: dict) -> TranslationResult:
unsupported = _as_list(data.get("unsupported", []))
return TranslationResult(
feldera_schema=_normalize_escapes(_as_str(data.get("feldera_schema", ""))),
feldera_query=_normalize_escapes(_as_str(data.get("feldera_query", ""))),
unsupported=unsupported,
warnings=_as_list(data.get("warnings", [])),
explanations=_as_list(data.get("explanations", [])),
status=Status.UNSUPPORTED if unsupported else Status.SUCCESS,
)
def _too_large_message(detail: str) -> str:
"""User-facing guidance for a program that overflows the model's context window."""
return (
"The program is too large to fit the model's context window "
f"({detail}). Shorten the input — translate fewer views at a time, drop "
"unused tables, or split it into smaller files — or set a model with a "
"larger context window via FELDERIZE_MODEL / --model."
)
def _log_verbose_sql(full_sql: str, attempt: int) -> None:
print(f"\n--- SQL submitted to validator (attempt {attempt}) ---", file=sys.stderr)
print(full_sql, file=sys.stderr)
print("---", file=sys.stderr)
# ---------------------------------------------------------------------------
# Translation: one LLM call on the whole program, with optional compiler repair
# ---------------------------------------------------------------------------
def _translate_with_repair(
schema_sql: str,
query_sql: str,
config: Config,
client: LLMClient,
system_prompt: str,
validate: bool,
max_retries: int,
verbose: bool = False,
) -> TranslationResult:
"""Translate the whole program with one LLM call, then optionally validate + repair."""
raw = client.translate(system_prompt, build_user_prompt(schema_sql, query_sql))
data, parse_warnings = _parse_or_salvage(raw)
result = _build_result(data)
result.warnings.extend(parse_warnings)
# If the query came back empty, force one retry with NULL placeholders.
if not result.feldera_query.strip():
raw2 = client.translate(
system_prompt,
build_force_query_prompt(schema_sql, query_sql, result.unsupported),
)
data2, warn2 = _parse_or_salvage(raw2)
result2 = _build_result(data2)
if result2.feldera_query.strip():
result = result2
result.warnings.extend(warn2)
result.warnings.append(
"Query produced on force-retry after empty first response"
)
# A response we could not parse or salvage is a genuine failure, not a clean
# translation — mark it so callers don't treat an empty query as success.
if parse_warnings and not result.feldera_query.strip():
result.status = Status.ERROR
if not validate:
return result
full_sql = result.feldera_schema + "\n\n" + result.feldera_query
# Schema-only or empty SQL always compiles, which is not a meaningful success.
if not full_sql.strip() or not result.feldera_query.strip():
if not result.unsupported:
result.status = Status.UNSUPPORTED
result.unsupported = ["No query generated — translation incomplete"]
return result
for attempt in range(max_retries):
if verbose:
_log_verbose_sql(full_sql, attempt + 1)
errors = validate_sql(full_sql, config.compiler_path)
if errors and any("Compiler not found" in e for e in errors):
print(
"Warning: Feldera compiler not found — skipping validation.",
file=sys.stderr,
)
result.warnings.append("Compiler not found — output SQL is not validated")
result.status = Status.UNSUPPORTED if result.unsupported else Status.SUCCESS
return result
if not errors:
result.warnings.append(f"Validated successfully (attempt {attempt + 1})")
result.status = Status.UNSUPPORTED if result.unsupported else Status.SUCCESS
return result
print(
f"Validation attempt {attempt + 1}/{max_retries} failed: "
f"{len(errors)} error(s)",
file=sys.stderr,
)
for err in errors:
print(f" {err}", file=sys.stderr)
repair = build_repair_prompt(schema_sql, query_sql, full_sql, errors)
raw = client.translate(system_prompt, repair)
data, repair_warnings = _parse_or_salvage(raw)
if repair_warnings:
result.warnings.append(
f"Repair attempt {attempt + 1}: {repair_warnings[0]}"
)
# When parsing/salvage yields nothing, data is empty and these .get calls
# keep the previous best-effort SQL rather than blanking it out.
result.feldera_schema = _as_str(
data.get("feldera_schema", result.feldera_schema)
)
result.feldera_query = _as_str(data.get("feldera_query", result.feldera_query))
result.unsupported = _as_list(data.get("unsupported", result.unsupported))
result.explanations = _as_list(data.get("explanations", result.explanations))
full_sql = result.feldera_schema + "\n\n" + result.feldera_query
if verbose:
_log_verbose_sql(full_sql, max_retries + 1)
errors = validate_sql(full_sql, config.compiler_path)
if not errors:
result.warnings.append(f"Validated successfully (attempt {max_retries + 1})")
result.status = Status.UNSUPPORTED if result.unsupported else Status.SUCCESS
else:
result.status = Status.ERROR
result.warnings.extend(
f"Still failing after {max_retries} repairs: {e}" for e in errors
)
return result
def translate_spark_to_feldera(
schema_sql: str,
query_sql: str,
config: Config,
validate: bool = False,
max_retries: int = DEFAULT_MAX_RETRIES,
include_docs: bool = True,
verbose: bool = False,
extra_rules: list[Path] | None = None,
extra_examples_dirs: list[Path] | None = None,
extra_examples_files: list[Path] | None = None,
) -> TranslationResult:
"""Translate a Spark SQL schema + query (the whole program) to Feldera SQL.
A single LLM call translates the entire program. When ``validate`` is set the
output is checked against the Feldera compiler and repaired with error
feedback for up to ``max_retries`` attempts. On a non-success first pass the
translation is retried once more with Feldera documentation added to the
prompt (unless ``include_docs`` is false).
"""
schema_sql = trim_schema_to_referenced(schema_sql, query_sql)
combined_sql = schema_sql + "\n" + query_sql
client = create_client(config, verbose=verbose)
# First pass: rules + validated examples (no docs).
system_prompt = build_system_prompt(
spark_sql=combined_sql,
with_docs=False,
with_examples=True,
extra_rules=extra_rules,
extra_examples_dirs=extra_examples_dirs,
extra_examples_files=extra_examples_files,
)
try:
result = _translate_with_repair(
schema_sql,
query_sql,
config,
client,
system_prompt,
validate,
max_retries,
verbose,
)
except PromptTooLargeError as e:
message = _too_large_message(str(e))
print(f"Error: {message}", file=sys.stderr)
return TranslationResult(status=Status.ERROR, warnings=[message])
# Retry with docs when the first pass was not fully successful.
if not include_docs or result.status == Status.SUCCESS:
return result
print("Retrying with skills + docs prompt...", file=sys.stderr)
system_prompt_docs = build_system_prompt(
spark_sql=combined_sql,
with_docs=True,
with_examples=True,
with_skills=True,
retry_unsupported=result.unsupported or None,
extra_rules=extra_rules,
extra_examples_dirs=extra_examples_dirs,
extra_examples_files=extra_examples_files,
)
try:
docs_result = _translate_with_repair(
schema_sql,
query_sql,
config,
client,
system_prompt_docs,
validate,
max_retries,
verbose,
)
except PromptTooLargeError as e:
# The docs prompt is the largest; keep the first-pass result and explain
# why the docs retry was skipped.
message = _too_large_message(str(e))
print(f"Warning: docs retry skipped — {message}", file=sys.stderr)
result.warnings.append(f"Docs retry skipped — {message}")
return result
result = docs_result
if result.status != Status.ERROR:
result.warnings.append("Resolved with skills + docs retry")
return result