-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathapi_usage.py
More file actions
69 lines (56 loc) · 2.07 KB
/
Copy pathapi_usage.py
File metadata and controls
69 lines (56 loc) · 2.07 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
#!/usr/bin/env python3
"""Example: translate Spark SQL to Feldera SQL with the felderize Python API.
Run from `python/felderize/` with the project venv (so `felderize` is importable
and ANTHROPIC_API_KEY is available, e.g. from a .env file):
.venv/bin/python examples/api_usage.py
"""
from felderize import Config, Status, translate_spark_to_feldera
SCHEMA = """
CREATE TABLE orders (
id BIGINT,
region STRING,
status STRING,
amount DECIMAL(10, 2),
created_at TIMESTAMP
) USING parquet;
"""
QUERY = """
CREATE OR REPLACE TEMP VIEW revenue_by_region AS
SELECT
region,
date_trunc('MONTH', created_at) AS month,
SUM(amount) AS total
FROM orders
WHERE status = 'PAID'
GROUP BY region, date_trunc('MONTH', created_at);
"""
def main() -> None:
# Reads ANTHROPIC_API_KEY, FELDERIZE_MODEL, and (for validate=True)
# FELDERA_COMPILER from the environment / a .env file.
cfg = Config.from_env()
# validate=True compiles the output against the Feldera compiler and repairs
# it using the compiler's feedback. Set validate=False to skip the compiler
# (faster, but the output SQL is not verified).
result = translate_spark_to_feldera(SCHEMA, QUERY, cfg, validate=False)
print(f"status: {result.status.value}\n")
print("-- Schema --")
print(result.feldera_schema.strip())
print("\n-- Query --")
print(result.feldera_query.strip())
if result.unsupported:
print("\nunsupported:")
for item in result.unsupported:
print(f" - {item}")
if result.warnings:
print("\nwarnings:")
for warning in result.warnings:
print(f" - {warning}")
# Branch on status the way a real integration would.
if result.status is Status.SUCCESS:
print("\nOK — the translated SQL is ready to deploy to a Feldera pipeline.")
else:
# Status.UNSUPPORTED -> some constructs became CAST(NULL ...) placeholders;
# Status.ERROR -> best-effort SQL that did not compile.
print("\nReview needed — see the unsupported/warnings above.")
if __name__ == "__main__":
main()