-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathfeldera_client.py
More file actions
63 lines (48 loc) · 2.04 KB
/
Copy pathfeldera_client.py
File metadata and controls
63 lines (48 loc) · 2.04 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
from __future__ import annotations
import subprocess
import tempfile
from pathlib import Path
from felderize.constants import COMPILER_TIMEOUT
def _compiler_cmd(compiler: Path) -> list[str]:
"""Return the command prefix for invoking the compiler."""
if compiler.suffix == ".jar":
return ["java", "-jar", str(compiler)]
return [str(compiler)]
def validate_sql(sql: str, compiler_path: str | Path | None = None) -> list[str]:
"""Validate SQL using the local sql-to-dbsp compiler. Returns compiler errors."""
if not compiler_path:
return ["Compiler not found: set FELDERA_COMPILER in .env or use --compiler"]
compiler = Path(compiler_path)
if not compiler.is_file():
return [f"Compiler not found at {compiler}"]
cmd_prefix = _compiler_cmd(compiler)
with tempfile.NamedTemporaryFile(mode="w", suffix=".sql") as f:
f.write(sql)
f.flush()
try:
result = subprocess.run(
cmd_prefix + ["-i", "--ignoreOrder", "--alltables", "--noRust", f.name],
capture_output=True,
text=True,
timeout=COMPILER_TIMEOUT,
)
except subprocess.TimeoutExpired:
return [f"Compilation timed out after {COMPILER_TIMEOUT}s"]
except FileNotFoundError:
if compiler.suffix == ".jar":
return ["java not found — install Java 19+ to use the compiler JAR"]
return [f"Compiler not found at {compiler}"]
if result.returncode == 0:
return []
# Parse errors from stderr, stripping temp file paths for readability
stderr = result.stderr.replace(f.name, "<input>")
errors: list[str] = []
for line in stderr.strip().split("\n"):
line = line.strip()
if line.startswith("<input>:") or "error:" in line.lower():
errors.append(line)
if not errors and stderr.strip():
errors.append(stderr.strip())
return (
errors if errors else [f"Compilation failed with exit code {result.returncode}"]
)