-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathserver.py
More file actions
1121 lines (1066 loc) · 45.7 KB
/
server.py
File metadata and controls
1121 lines (1066 loc) · 45.7 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""JSON-RPC server over stdio for DataPilot Engine.
Reads JSON-RPC requests from stdin (one per line), dispatches to handlers,
and writes JSON-RPC responses to stdout.
Usage:
echo '{"jsonrpc":"2.0","method":"sql.validate","params":{"sql":"SELECT 1"},"id":1}' | python -m altimate_engine.server
"""
from __future__ import annotations
import json
import os
import sys
import traceback
from altimate_engine.models import (
WarehouseAddParams,
WarehouseAddResult,
WarehouseRemoveParams,
WarehouseRemoveResult,
DockerContainer,
WarehouseDiscoverResult,
DbtLineageParams,
DbtManifestParams,
DbtRunParams,
DbtProfilesParams,
DbtProfileConnection,
DbtProfilesResult,
JsonRpcError,
JsonRpcRequest,
JsonRpcResponse,
LineageCheckParams,
LocalSchemaSyncParams,
LocalSchemaSyncResult,
LocalTestParams,
LocalTestResult,
SchemaCacheStatusResult,
SchemaCacheWarehouseStatus,
SchemaIndexParams,
SchemaIndexResult,
SchemaInspectParams,
SchemaSearchColumnResult,
SchemaSearchParams,
SchemaSearchResult,
SchemaSearchTableResult,
SqlAnalyzeIssue,
SqlAnalyzeParams,
SqlAnalyzeResult,
SqlAutocompleteParams,
SqlAutocompleteResult,
SqlAutocompleteSuggestion,
SqlExecuteParams,
SqlExplainParams,
SqlFixParams,
SqlFixResult,
SqlFixSuggestion,
SqlFormatParams,
SqlFormatResult,
SqlOptimizeParams,
SqlOptimizeResult,
SqlOptimizeSuggestion,
SqlRewriteParams,
SqlRewriteResult,
SqlRewriteRule,
SqlTranslateParams,
SqlTranslateResult,
WarehouseInfo,
WarehouseListResult,
WarehouseTestParams,
WarehouseTestResult,
QueryHistoryParams,
QueryHistoryResult,
CreditAnalysisParams,
CreditAnalysisResult,
ExpensiveQueriesParams,
ExpensiveQueriesResult,
WarehouseAdvisorParams,
WarehouseAdvisorResult,
UnusedResourcesParams,
UnusedResourcesResult,
RoleGrantsParams,
RoleGrantsResult,
RoleHierarchyParams,
RoleHierarchyResult,
UserRolesParams,
UserRolesResult,
PiiDetectParams,
PiiDetectResult,
PiiFinding,
TagsGetParams,
TagsGetResult,
TagsListParams,
TagsListResult,
SqlDiffParams,
SqlDiffResult,
AltimateCoreValidateParams,
AltimateCoreLintParams,
AltimateCoreSafetyParams,
AltimateCoreTranspileParams,
AltimateCoreExplainParams,
AltimateCoreCheckParams,
AltimateCoreResult,
)
from altimate_engine.sql.executor import execute_sql
from altimate_engine.sql.explainer import explain_sql
from altimate_engine.sql.autocomplete import autocomplete_sql
from altimate_engine.sql.diff import diff_sql
from altimate_engine.schema.inspector import inspect_schema
from altimate_engine.schema.pii_detector import detect_pii
from altimate_engine.schema.tags import get_tags, list_tags
from altimate_engine.dbt.runner import run_dbt
from altimate_engine.dbt.manifest import parse_manifest
from altimate_engine.dbt.lineage import dbt_lineage
from altimate_engine.connections import ConnectionRegistry
# lineage.check delegates to guard_column_lineage
from altimate_engine.schema.cache import SchemaCache
from altimate_engine.finops.query_history import get_query_history
from altimate_engine.finops.credit_analyzer import (
analyze_credits,
get_expensive_queries,
)
from altimate_engine.finops.warehouse_advisor import advise_warehouse_sizing
from altimate_engine.finops.unused_resources import find_unused_resources
from altimate_engine.finops.role_access import (
query_grants,
query_role_hierarchy,
query_user_roles,
)
from altimate_engine.sql.guard import (
guard_validate,
guard_lint,
guard_scan_safety,
guard_transpile,
guard_explain,
guard_check,
# Phase 1 (P0)
guard_fix as guard_fix_sql,
guard_check_policy,
guard_check_semantics,
guard_generate_tests,
# Phase 2 (P1)
guard_check_equivalence,
guard_analyze_migration,
guard_diff_schemas,
guard_rewrite as guard_rewrite_sql,
guard_correct,
guard_evaluate,
# Phase 3 (P2)
guard_classify_pii,
guard_check_query_pii,
guard_resolve_term,
guard_column_lineage,
guard_track_lineage,
guard_format_sql,
guard_extract_metadata,
guard_compare_queries,
guard_complete,
guard_optimize_context,
guard_optimize_for_query,
guard_prune_schema,
guard_import_ddl,
guard_export_ddl,
guard_schema_fingerprint,
guard_introspection_sql,
guard_parse_dbt_project,
guard_is_safe,
)
from altimate_engine.dbt.profiles import discover_dbt_connections
from altimate_engine.local.schema_sync import sync_schema
from altimate_engine.local.test_local import test_sql_local
from altimate_engine.sql.jinja_preprocessor import (
contains_jinja,
preprocess_jinja,
)
from altimate_engine.models import (
SqlPreprocessJinjaParams,
SqlPreprocessJinjaResult,
AltimateCoreFixParams,
AltimateCorePolicyParams,
AltimateCoreSemanticsParams,
AltimateCoreTestgenParams,
# Phase 2 (P1)
AltimateCoreEquivalenceParams,
AltimateCoreMigrationParams,
AltimateCoreSchemaDiffParams,
AltimateCoreGuardRewriteParams,
AltimateCoreCorrectParams,
AltimateCoreGradeParams,
# Phase 3 (P2)
AltimateCoreClassifyPiiParams,
AltimateCoreQueryPiiParams,
AltimateCoreResolveTermParams,
AltimateCoreColumnLineageParams,
AltimateCoreTrackLineageParams,
AltimateCoreFormatSqlParams,
AltimateCoreExtractMetadataParams,
AltimateCoreCompareQueriesParams,
AltimateCoreCompleteParams,
AltimateCoreOptimizeContextParams,
AltimateCoreOptimizeForQueryParams,
AltimateCorePruneSchemaParams,
AltimateCoreImportDdlParams,
AltimateCoreExportDdlParams,
AltimateCoreSchemaFingerprintParams,
AltimateCoreIntrospectionSqlParams,
AltimateCoreParseDbtProjectParams,
AltimateCoreIsSafeParams,
)
# JSON-RPC error codes
PARSE_ERROR = -32700
INVALID_REQUEST = -32600
METHOD_NOT_FOUND = -32601
INVALID_PARAMS = -32602
INTERNAL_ERROR = -32603
# Lazily-initialized singletons
def _schema_context_to_dict(
schema_context: dict[str, list] | None,
) -> dict | None:
"""Convert LineageCheckParams schema_context to guard.py format.
Input: {"table_name": [ModelColumn(name=..., data_type=...), ...]}
Output: {"tables": {"table_name": {"columns": [{"name": ..., "type": ...}]}}, "version": "1"}
"""
if not schema_context:
return None
tables = {}
for table_name, columns in schema_context.items():
cols = []
for col in columns:
if hasattr(col, "name"):
cols.append({"name": col.name, "type": getattr(col, "data_type", "")})
elif isinstance(col, dict):
cols.append(
{"name": col.get("name", ""), "type": col.get("data_type", "")}
)
tables[table_name] = {"columns": cols}
return {"tables": tables, "version": "1"}
_schema_cache: SchemaCache | None = None
def _get_schema_cache() -> SchemaCache:
"""Return the singleton SchemaCache, creating it on first use."""
global _schema_cache
if _schema_cache is None:
_schema_cache = SchemaCache()
return _schema_cache
def _compute_overall_confidence(issues: list) -> str:
"""Compute overall confidence from individual issue confidences."""
if not issues:
return "high"
confidences = [getattr(i, "confidence", "high") for i in issues]
if "low" in confidences:
return "low"
if "medium" in confidences:
return "medium"
return "high"
def _get_confidence_factors(raw_result: dict) -> list[str]:
"""Extract confidence factors from analysis result."""
factors = []
if not raw_result.get("success", True):
factors.append("SQL parse failed — results may be incomplete")
return factors
def _split_sql_statements(sql: str) -> list[str]:
"""Split SQL by semicolons, ignoring those inside string literals."""
stmts, current, in_str, str_char = [], [], False, None
for ch in sql:
if not in_str and ch in ("'", '"'):
in_str, str_char = True, ch
elif in_str and ch == str_char:
in_str = False
elif ch == ";" and not in_str:
s = "".join(current).strip()
if s and not all(
line.strip().startswith("--") or not line.strip()
for line in s.splitlines()
):
stmts.append(s)
current = []
continue
current.append(ch)
s = "".join(current).strip()
if s:
stmts.append(s)
return stmts or [sql]
def dispatch(request: JsonRpcRequest) -> JsonRpcResponse:
"""Dispatch a JSON-RPC request to the appropriate handler."""
method = request.method
params = request.params or {}
try:
if method == "sql.preprocess_jinja":
pp_params = SqlPreprocessJinjaParams(**params)
pp_result = preprocess_jinja(pp_params.sql)
result = SqlPreprocessJinjaResult(
success=True,
preprocessed_sql=pp_result.preprocessed_sql,
original_sql=pp_result.original_sql,
was_preprocessed=pp_result.was_preprocessed,
refs_found=pp_result.refs_found,
sources_found=pp_result.sources_found,
variables_found=pp_result.variables_found,
macros_removed=pp_result.macros_removed,
warnings=pp_result.warnings,
)
elif method == "sql.execute":
result = execute_sql(SqlExecuteParams(**params))
elif method == "schema.inspect":
result = inspect_schema(SchemaInspectParams(**params))
elif method == "sql.analyze":
params_obj = SqlAnalyzeParams(**params)
# Auto-preprocess Jinja if present
jinja_note = ""
sql_to_analyze = params_obj.sql
if contains_jinja(sql_to_analyze):
pp = preprocess_jinja(sql_to_analyze)
if pp.was_preprocessed:
sql_to_analyze = pp.preprocessed_sql
parts = []
if pp.refs_found:
parts.append(f"refs: {', '.join(pp.refs_found)}")
if pp.sources_found:
parts.append(f"sources: {', '.join(pp.sources_found)}")
if pp.variables_found:
parts.append(f"vars: {', '.join(pp.variables_found)}")
detail = f" ({'; '.join(parts)})" if parts else ""
jinja_note = (
f"Jinja templates were preprocessed before analysis{detail}. "
"Results are based on the rendered SQL and may be approximate."
)
statements = _split_sql_statements(sql_to_analyze)
issues = []
any_error = None
for stmt_idx, stmt in enumerate(statements):
label = f"[Query {stmt_idx + 1}] " if len(statements) > 1 else ""
lint_result = guard_lint(stmt, schema_context=params_obj.schema_context)
if lint_result.get("error"):
any_error = lint_result["error"]
continue
for issue in lint_result.get("findings", lint_result.get("issues", [])):
issues.append(
SqlAnalyzeIssue(
type=issue.get("rule", issue.get("type", "LINT")),
severity=issue.get("severity", "warning"),
message=label + issue.get("message", ""),
recommendation=issue.get(
"suggestion", issue.get("recommendation", "")
),
location=issue.get("location"),
confidence=issue.get("confidence", "high"),
)
)
sem_result = guard_check_semantics(
stmt, schema_context=params_obj.schema_context
)
for si in sem_result.get("issues", []):
issues.append(
SqlAnalyzeIssue(
type=f"SEMANTIC_{si.get('rule', si.get('type', 'UNKNOWN'))}",
severity=si.get("severity", "warning"),
message=label + si.get("message", ""),
recommendation=si.get(
"suggestion", si.get("recommendation", "")
),
location=si.get("location"),
confidence=si.get("confidence", "medium"),
)
)
safety_result = guard_scan_safety(stmt)
for threat in safety_result.get("threats", []):
issues.append(
SqlAnalyzeIssue(
type=f"SAFETY_{threat.get('type', 'THREAT')}",
severity=threat.get("severity", "error"),
message=label
+ threat.get("description", threat.get("message", "")),
recommendation="Review this SQL for potential security risks.",
location=threat.get("location"),
confidence="high",
)
)
confidence_factors = []
if any_error is not None:
confidence_factors.append(f"Parse failed on one statement: {any_error}")
if jinja_note:
confidence_factors.append(jinja_note)
result = SqlAnalyzeResult(
success=any_error is None,
issues=issues,
issue_count=len(issues),
confidence=_compute_overall_confidence(issues),
confidence_factors=confidence_factors,
error=any_error,
)
elif method == "sql.translate":
params_obj = SqlTranslateParams(**params)
# Auto-preprocess Jinja if present
sql_to_translate = params_obj.sql
jinja_warnings: list[str] = []
if contains_jinja(sql_to_translate):
pp = preprocess_jinja(sql_to_translate)
if pp.was_preprocessed:
sql_to_translate = pp.preprocessed_sql
jinja_warnings.append(
"Jinja templates were preprocessed before translation. "
"Review the translated SQL and re-apply Jinja syntax as needed."
)
raw = guard_transpile(
sql_to_translate, params_obj.source_dialect, params_obj.target_dialect
)
all_warnings = jinja_warnings + raw.get("warnings", [])
result = SqlTranslateResult(
success=raw.get("success", True),
translated_sql=raw.get("sql", raw.get("translated_sql")),
source_dialect=params_obj.source_dialect,
target_dialect=params_obj.target_dialect,
warnings=all_warnings,
error=raw.get("error"),
)
elif method == "sql.optimize":
params_obj = SqlOptimizeParams(**params)
# Auto-preprocess Jinja if present
sql_to_optimize = params_obj.sql
jinja_preprocessed = False
if contains_jinja(sql_to_optimize):
pp = preprocess_jinja(sql_to_optimize)
if pp.was_preprocessed:
sql_to_optimize = pp.preprocessed_sql
jinja_preprocessed = True
# Rewrite for optimization
rw = guard_rewrite_sql(
sql_to_optimize, schema_context=params_obj.schema_context
)
# Lint for remaining issues
lint = guard_lint(sql_to_optimize, schema_context=params_obj.schema_context)
suggestions = []
for r in rw.get("rewrites", []):
suggestions.append(
SqlOptimizeSuggestion(
type="REWRITE",
description=r.get("explanation", "Optimization rewrite"),
before=r.get("original_fragment", ""),
after=r.get("rewritten_fragment", ""),
)
)
optimized_sql = rw.get("rewritten_sql", params_obj.sql)
if not suggestions and optimized_sql.strip() != params_obj.sql.strip():
suggestions.append(
SqlOptimizeSuggestion(
type="REWRITE",
description="Query rewritten for performance",
before=params_obj.sql,
after=optimized_sql,
)
)
anti_patterns = []
for issue in lint.get("findings", lint.get("issues", [])):
anti_patterns.append(
{
"type": issue.get("rule", issue.get("type", "LINT")),
"message": issue.get("message", ""),
"suggestion": issue.get("suggestion", ""),
}
)
opt_confidence = "high"
if jinja_preprocessed:
opt_confidence = "medium"
result = SqlOptimizeResult(
success=True,
original_sql=params_obj.sql,
optimized_sql=rw.get("rewritten_sql", sql_to_optimize),
suggestions=suggestions,
anti_patterns=anti_patterns,
confidence=opt_confidence,
error=rw.get("error"),
)
elif method == "lineage.check":
p = LineageCheckParams(**params)
raw = guard_column_lineage(
p.sql,
dialect=p.dialect or "",
schema_context=_schema_context_to_dict(p.schema_context)
if p.schema_context
else None,
)
_err = raw.get("error")
result = AltimateCoreResult(
success=_err is None,
data=raw if _err is None else None,
error=_err,
)
elif method == "dbt.run":
result = run_dbt(DbtRunParams(**params))
elif method == "dbt.manifest":
result = parse_manifest(DbtManifestParams(**params))
elif method == "dbt.lineage":
result = dbt_lineage(DbtLineageParams(**params))
elif method == "warehouse.list":
warehouses = [WarehouseInfo(**w) for w in ConnectionRegistry.list()]
result = WarehouseListResult(warehouses=warehouses)
elif method == "warehouse.test":
test_params = WarehouseTestParams(**params)
test_result = ConnectionRegistry.test(test_params.name)
result = WarehouseTestResult(**test_result)
elif method == "warehouse.add":
p = WarehouseAddParams(**params)
try:
ConnectionRegistry.add(p.name, p.config)
result = WarehouseAddResult(
success=True, name=p.name, type=p.config.get("type", "unknown")
)
except Exception as e:
result = WarehouseAddResult(
success=False, name=p.name, type="", error=str(e)
)
elif method == "warehouse.remove":
p = WarehouseRemoveParams(**params)
try:
removed = ConnectionRegistry.remove(p.name)
result = WarehouseRemoveResult(success=removed)
except Exception as e:
result = WarehouseRemoveResult(success=False, error=str(e))
elif method == "warehouse.discover":
from altimate_engine.docker_discovery import discover_containers
try:
containers = discover_containers()
result = WarehouseDiscoverResult(
containers=[DockerContainer(**c) for c in containers],
container_count=len(containers),
)
except Exception as e:
result = WarehouseDiscoverResult(error=str(e))
elif method == "sql.format":
fmt_params = SqlFormatParams(**params)
# Auto-preprocess Jinja if present
sql_to_format = fmt_params.sql
jinja_fmt_note = None
if contains_jinja(sql_to_format):
pp = preprocess_jinja(sql_to_format)
if pp.was_preprocessed:
sql_to_format = pp.preprocessed_sql
jinja_fmt_note = (
"Note: Jinja templates were removed before formatting. "
"The formatted output contains plain SQL only."
)
raw = guard_format_sql(sql_to_format, fmt_params.dialect)
fmt_error = raw.get("error")
if jinja_fmt_note and not fmt_error:
fmt_error = jinja_fmt_note
result = SqlFormatResult(
success=raw.get("success", True),
formatted_sql=raw.get("formatted_sql", raw.get("sql")),
statement_count=raw.get("statement_count", 1),
error=fmt_error,
)
elif method == "sql.explain":
result = explain_sql(SqlExplainParams(**params))
elif method == "sql.fix":
fix_params = SqlFixParams(**params)
guard_result = guard_fix_sql(fix_params.sql)
fixed = guard_result.get("fixed", guard_result.get("success", False))
fixed_sql = guard_result.get("fixed_sql")
if fixed and fixed_sql:
result = SqlFixResult(
success=True,
original_sql=fix_params.sql,
fixed_sql=fixed_sql,
error_message=fix_params.error_message,
suggestions=[
SqlFixSuggestion(
type="ALTIMATE_CORE_FIX",
message="Auto-fixed by altimate_core engine",
confidence="high",
fixed_sql=fixed_sql,
)
],
suggestion_count=1,
)
else:
result = SqlFixResult(
success=False,
original_sql=fix_params.sql,
fixed_sql=fixed_sql,
error_message=fix_params.error_message,
suggestions=[],
suggestion_count=0,
error=guard_result.get("error", "Unable to auto-fix"),
)
elif method == "sql.autocomplete":
ac_params = SqlAutocompleteParams(**params)
cache = _get_schema_cache()
ac_result = autocomplete_sql(
prefix=ac_params.prefix,
position=ac_params.position,
warehouse=ac_params.warehouse,
table_context=ac_params.table_context,
limit=ac_params.limit,
cache=cache,
)
result = SqlAutocompleteResult(
suggestions=[
SqlAutocompleteSuggestion(**s) for s in ac_result["suggestions"]
],
prefix=ac_result["prefix"],
position=ac_result["position"],
suggestion_count=ac_result["suggestion_count"],
)
elif method == "schema.index":
idx_params = SchemaIndexParams(**params)
connector = ConnectionRegistry.get(idx_params.warehouse)
connector.connect()
try:
# Look up warehouse type from registry
wh_list = ConnectionRegistry.list()
wh_type = "unknown"
for wh in wh_list:
if wh["name"] == idx_params.warehouse:
wh_type = wh.get("type", "unknown")
break
cache = _get_schema_cache()
idx_result = cache.index_warehouse(
idx_params.warehouse, wh_type, connector
)
result = SchemaIndexResult(**idx_result)
finally:
connector.close()
elif method == "schema.search":
search_params = SchemaSearchParams(**params)
cache = _get_schema_cache()
raw = cache.search(
query=search_params.query,
warehouse=search_params.warehouse,
limit=search_params.limit,
)
result = SchemaSearchResult(
tables=[SchemaSearchTableResult(**t) for t in raw["tables"]],
columns=[SchemaSearchColumnResult(**c) for c in raw["columns"]],
query=raw["query"],
match_count=raw["match_count"],
)
elif method == "schema.cache_status":
cache = _get_schema_cache()
raw = cache.cache_status()
result = SchemaCacheStatusResult(
warehouses=[SchemaCacheWarehouseStatus(**w) for w in raw["warehouses"]],
total_tables=raw["total_tables"],
total_columns=raw["total_columns"],
cache_path=raw["cache_path"],
)
# --- FinOps methods ---
elif method == "finops.query_history":
p = QueryHistoryParams(**params)
try:
raw = get_query_history(
p.warehouse, p.days, p.limit, p.user, p.warehouse_filter
)
result = QueryHistoryResult(**raw)
except Exception as e:
raise RuntimeError(
f"finops.query_history failed: {e}. "
f"For non-Snowflake warehouses, query directly: "
f"SELECT user_name, query_text, total_elapsed_time, bytes_scanned, credits_used "
f"FROM query_history ORDER BY start_time DESC LIMIT {p.limit}"
)
elif method == "finops.analyze_credits":
p = CreditAnalysisParams(**params)
try:
raw = analyze_credits(p.warehouse, p.days, p.limit, p.warehouse_filter)
result = CreditAnalysisResult(**raw)
except Exception as e:
raise RuntimeError(
f"finops.analyze_credits failed: {e}. "
f"For non-Snowflake warehouses, query directly: "
f"SELECT warehouse_name, SUM(credits_used) AS credits, COUNT(*) AS queries "
f"FROM query_history WHERE start_time >= CURRENT_DATE - INTERVAL '{p.days} days' "
f"GROUP BY 1 ORDER BY 2 DESC LIMIT {p.limit}"
)
elif method == "finops.expensive_queries":
p = ExpensiveQueriesParams(**params)
try:
raw = get_expensive_queries(p.warehouse, p.days, p.limit)
result = ExpensiveQueriesResult(**raw)
except Exception as e:
raise RuntimeError(
f"finops.expensive_queries failed: {e}. "
f"For non-Snowflake warehouses, query directly: "
f"SELECT query_text, bytes_scanned, total_elapsed_time, credits_used "
f"FROM query_history ORDER BY bytes_scanned DESC LIMIT {p.limit}"
)
elif method == "finops.warehouse_advice":
p = WarehouseAdvisorParams(**params)
try:
raw = advise_warehouse_sizing(p.warehouse, p.days)
result = WarehouseAdvisorResult(**raw)
except Exception as e:
raise RuntimeError(
f"finops.warehouse_advice failed: {e}. "
f"For non-Snowflake warehouses, analyze warehouse usage directly: "
f"SELECT warehouse_name, COUNT(*) AS queries, AVG(total_elapsed_time) AS avg_ms, "
f"SUM(credits_used) AS credits FROM query_history GROUP BY 1 ORDER BY 4 DESC"
)
elif method == "finops.unused_resources":
p = UnusedResourcesParams(**params)
try:
raw = find_unused_resources(p.warehouse, p.days, p.limit)
result = UnusedResourcesResult(**raw)
except Exception as e:
raise RuntimeError(
f"finops.unused_resources failed: {e}. "
f"For non-Snowflake warehouses, find idle resources directly: "
f"SELECT table_name, last_altered FROM information_schema.tables "
f"WHERE last_altered < CURRENT_DATE - INTERVAL '{p.days} days' LIMIT {p.limit}"
)
elif method == "finops.role_grants":
p = RoleGrantsParams(**params)
try:
raw = query_grants(p.warehouse, p.role, p.object_name, p.limit)
result = RoleGrantsResult(**raw)
except Exception as e:
raise RuntimeError(f"finops.role_grants failed: {e}. This tool requires Snowflake.")
elif method == "finops.role_hierarchy":
p = RoleHierarchyParams(**params)
try:
raw = query_role_hierarchy(p.warehouse)
result = RoleHierarchyResult(**raw)
except Exception as e:
raise RuntimeError(f"finops.role_hierarchy failed: {e}. This tool requires Snowflake.")
elif method == "finops.user_roles":
p = UserRolesParams(**params)
try:
raw = query_user_roles(p.warehouse, p.user, p.limit)
result = UserRolesResult(**raw)
except Exception as e:
raise RuntimeError(f"finops.user_roles failed: {e}. This tool requires Snowflake.")
# --- Schema discovery methods ---
elif method == "schema.detect_pii":
p = PiiDetectParams(**params)
cache = _get_schema_cache()
raw = detect_pii(p.warehouse, p.schema_name, p.table, cache)
result = PiiDetectResult(
success=raw["success"],
findings=[PiiFinding(**f) for f in raw["findings"]],
finding_count=raw["finding_count"],
columns_scanned=raw["columns_scanned"],
by_category=raw["by_category"],
tables_with_pii=raw["tables_with_pii"],
)
elif method == "schema.tags":
p = TagsGetParams(**params)
raw = get_tags(p.warehouse, p.object_name, p.tag_name, p.limit)
result = TagsGetResult(**raw)
elif method == "schema.tags_list":
p = TagsListParams(**params)
raw = list_tags(p.warehouse, p.limit)
result = TagsListResult(**raw)
# --- SQL diff ---
elif method == "sql.diff":
p = SqlDiffParams(**params)
raw = diff_sql(p.original, p.modified, p.context_lines)
# Add semantic equivalence check via altimate_core
equiv = guard_check_equivalence(p.original, p.modified)
if equiv.get("equivalent") is not None:
raw["semantic_equivalent"] = equiv["equivalent"]
result = SqlDiffResult(**raw)
# --- SQL rewrite ---
elif method == "sql.rewrite":
p = SqlRewriteParams(**params)
guard_rw = guard_rewrite_sql(p.sql, schema_context=p.schema_context)
if guard_rw.get("success") and guard_rw.get("rewritten_sql"):
rewrites = []
for r in guard_rw.get("rewrites", []):
rewrites.append(
SqlRewriteRule(
rule=r.get("rule", "ALTIMATE_CORE_REWRITE"),
original_fragment=r.get("original_fragment", ""),
rewritten_fragment=r.get("rewritten_fragment", ""),
explanation=r.get("explanation", "Rewritten by altimate_core"),
can_auto_apply=True,
)
)
result = SqlRewriteResult(
success=True,
original_sql=p.sql,
rewritten_sql=guard_rw["rewritten_sql"],
rewrites_applied=rewrites,
)
else:
result = SqlRewriteResult(
success=False,
original_sql=p.sql,
rewritten_sql=None,
rewrites_applied=[],
error=guard_rw.get("error", "No rewrites applicable"),
)
# --- altimate_core ---
elif method == "altimate_core.validate":
p = AltimateCoreValidateParams(**params)
raw = guard_validate(p.sql, p.schema_path, p.schema_context)
result = AltimateCoreResult(
success=raw.get("valid", True), data=raw, error=raw.get("error")
)
elif method == "altimate_core.lint":
p = AltimateCoreLintParams(**params)
raw = guard_lint(p.sql, p.schema_path, p.schema_context)
result = AltimateCoreResult(
success=raw.get("clean", True), data=raw, error=raw.get("error")
)
elif method == "altimate_core.safety":
p = AltimateCoreSafetyParams(**params)
raw = guard_scan_safety(p.sql)
result = AltimateCoreResult(
success=raw.get("safe", True), data=raw, error=raw.get("error")
)
elif method == "altimate_core.transpile":
p = AltimateCoreTranspileParams(**params)
raw = guard_transpile(p.sql, p.from_dialect, p.to_dialect)
result = AltimateCoreResult(
success=raw.get("success", True), data=raw, error=raw.get("error")
)
elif method == "altimate_core.explain":
p = AltimateCoreExplainParams(**params)
raw = guard_explain(p.sql, p.schema_path, p.schema_context)
result = AltimateCoreResult(
success=raw.get("valid", True), data=raw, error=raw.get("error")
)
elif method == "altimate_core.check":
p = AltimateCoreCheckParams(**params)
raw = guard_check(p.sql, p.schema_path, p.schema_context)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
# --- altimate_core Phase 1 (P0) ---
elif method == "altimate_core.fix":
p = AltimateCoreFixParams(**params)
raw = guard_fix_sql(
p.sql, p.schema_path, p.schema_context, p.max_iterations
)
result = AltimateCoreResult(
success=raw.get("success", True), data=raw, error=raw.get("error")
)
elif method == "altimate_core.policy":
p = AltimateCorePolicyParams(**params)
raw = guard_check_policy(
p.sql, p.policy_json, p.schema_path, p.schema_context
)
result = AltimateCoreResult(
success=raw.get("pass", True), data=raw, error=raw.get("error")
)
elif method == "altimate_core.semantics":
p = AltimateCoreSemanticsParams(**params)
raw = guard_check_semantics(p.sql, p.schema_path, p.schema_context)
result = AltimateCoreResult(
success=raw.get("valid", True), data=raw, error=raw.get("error")
)
elif method == "altimate_core.testgen":
p = AltimateCoreTestgenParams(**params)
raw = guard_generate_tests(p.sql, p.schema_path, p.schema_context)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
# --- altimate_core Phase 2 (P1) ---
elif method == "altimate_core.equivalence":
p = AltimateCoreEquivalenceParams(**params)
raw = guard_check_equivalence(
p.sql1, p.sql2, p.schema_path, p.schema_context
)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.migration":
p = AltimateCoreMigrationParams(**params)
raw = guard_analyze_migration(p.old_ddl, p.new_ddl, p.dialect)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.schema_diff":
p = AltimateCoreSchemaDiffParams(**params)
raw = guard_diff_schemas(
p.schema1_path,
p.schema2_path,
p.schema1_context,
p.schema2_context,
)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.rewrite":
p = AltimateCoreGuardRewriteParams(**params)
raw = guard_rewrite_sql(p.sql, p.schema_path, p.schema_context)
result = AltimateCoreResult(
success=raw.get("success", True), data=raw, error=raw.get("error")
)
elif method == "altimate_core.correct":
p = AltimateCoreCorrectParams(**params)
raw = guard_correct(p.sql, p.schema_path, p.schema_context)
result = AltimateCoreResult(
success=raw.get("success", True), data=raw, error=raw.get("error")
)
elif method == "altimate_core.grade":
p = AltimateCoreGradeParams(**params)
raw = guard_evaluate(p.sql, p.schema_path, p.schema_context)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
# --- altimate_core Phase 3 (P2) ---
elif method == "altimate_core.classify_pii":
p = AltimateCoreClassifyPiiParams(**params)
raw = guard_classify_pii(p.schema_path, p.schema_context)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.query_pii":
p = AltimateCoreQueryPiiParams(**params)
raw = guard_check_query_pii(p.sql, p.schema_path, p.schema_context)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.resolve_term":
p = AltimateCoreResolveTermParams(**params)
raw = guard_resolve_term(p.term, p.schema_path, p.schema_context)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.column_lineage":
p = AltimateCoreColumnLineageParams(**params)
raw = guard_column_lineage(
p.sql, p.dialect, p.schema_path, p.schema_context
)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.track_lineage":
p = AltimateCoreTrackLineageParams(**params)
raw = guard_track_lineage(p.queries, p.schema_path, p.schema_context)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.format":
p = AltimateCoreFormatSqlParams(**params)
raw = guard_format_sql(p.sql, p.dialect)
result = AltimateCoreResult(
success=raw.get("success", True), data=raw, error=raw.get("error")
)
elif method == "altimate_core.metadata":
p = AltimateCoreExtractMetadataParams(**params)
raw = guard_extract_metadata(p.sql, p.dialect)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.compare":
p = AltimateCoreCompareQueriesParams(**params)
raw = guard_compare_queries(p.left_sql, p.right_sql, p.dialect)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.complete":
p = AltimateCoreCompleteParams(**params)
raw = guard_complete(p.sql, p.cursor_pos, p.schema_path, p.schema_context)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.optimize_context":
p = AltimateCoreOptimizeContextParams(**params)
raw = guard_optimize_context(p.schema_path, p.schema_context)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.optimize_for_query":
p = AltimateCoreOptimizeForQueryParams(**params)
raw = guard_optimize_for_query(p.sql, p.schema_path, p.schema_context)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.prune_schema":
p = AltimateCorePruneSchemaParams(**params)
raw = guard_prune_schema(p.sql, p.schema_path, p.schema_context)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.import_ddl":
p = AltimateCoreImportDdlParams(**params)
raw = guard_import_ddl(p.ddl, p.dialect)
result = AltimateCoreResult(success=True, data=raw, error=raw.get("error"))
elif method == "altimate_core.export_ddl":
p = AltimateCoreExportDdlParams(**params)
raw = guard_export_ddl(p.schema_path, p.schema_context)
result = AltimateCoreResult(
success=raw.get("success", True), data=raw, error=raw.get("error")
)
elif method == "altimate_core.fingerprint":
p = AltimateCoreSchemaFingerprintParams(**params)
raw = guard_schema_fingerprint(p.schema_path, p.schema_context)
result = AltimateCoreResult(
success=raw.get("success", True), data=raw, error=raw.get("error")
)
elif method == "altimate_core.introspection_sql":
p = AltimateCoreIntrospectionSqlParams(**params)
raw = guard_introspection_sql(p.db_type, p.database, p.schema_name)