-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathregister.ts
More file actions
454 lines (415 loc) · 14.7 KB
/
register.ts
File metadata and controls
454 lines (415 loc) · 14.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
/**
* Register composite SQL dispatcher methods that combine
* altimate-core analysis calls with result formatting.
*
* These 10 methods were previously handled by the Python bridge
* as composite operations (calling multiple guard_* functions).
*/
import * as core from "@altimateai/altimate-core"
import { register } from "../dispatcher"
import { schemaOrEmpty, resolveSchema } from "../schema-resolver"
import { preprocessIff, postprocessQualify } from "../altimate-core"
import type {
SqlAnalyzeResult,
SqlAnalyzeIssue,
SqlTranslateResult,
SqlOptimizeResult,
SqlOptimizeSuggestion,
LineageCheckResult,
SchemaDiffResult,
} from "../types"
/** Register all composite SQL handlers with the Dispatcher.
* Exported so tests can re-register after Dispatcher.reset(). */
export function registerAllSql(): void {
// ---------------------------------------------------------------------------
// sql.analyze — lint + semantics + safety
// ---------------------------------------------------------------------------
register("sql.analyze", async (params) => {
try {
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
const [lintRaw, semanticsRaw, safetyRaw] = await Promise.all([
core.lint(params.sql, schema),
core.checkSemantics(params.sql, schema),
core.scanSql(params.sql),
])
const lint = JSON.parse(JSON.stringify(lintRaw))
const semantics = JSON.parse(JSON.stringify(semanticsRaw))
const safety = JSON.parse(JSON.stringify(safetyRaw))
const issues: SqlAnalyzeIssue[] = []
for (const f of lint.findings ?? []) {
issues.push({
type: "lint",
rule: f.rule,
severity: f.severity ?? "warning",
message: f.message ?? f.rule ?? "",
recommendation: f.suggestion ?? "",
location: f.line ? `line ${f.line}` : undefined,
confidence: "high",
})
}
for (const f of semantics.findings ?? []) {
issues.push({
type: "semantic",
severity: f.severity ?? "warning",
message: f.message ?? "",
recommendation: f.suggestion ?? f.explanation ?? "",
confidence: String(f.confidence ?? "medium"),
})
}
for (const t of safety.threats ?? []) {
issues.push({
type: "safety",
severity: t.severity ?? "high",
message: t.message ?? "",
recommendation: t.detail ?? "",
location: t.location ? `chars ${t.location[0]}-${t.location[1]}` : undefined,
confidence: "high",
})
}
return {
success: true,
issues,
issue_count: issues.length,
confidence: "high",
confidence_factors: ["lint", "semantics", "safety"],
} satisfies SqlAnalyzeResult
} catch (e) {
return {
success: false,
issues: [],
issue_count: 0,
confidence: "low",
confidence_factors: [],
error: String(e),
} satisfies SqlAnalyzeResult
}
})
// ---------------------------------------------------------------------------
// sql.translate — transpile with IFF/QUALIFY transforms
// ---------------------------------------------------------------------------
register("sql.translate", async (params) => {
try {
const processed = preprocessIff(params.sql)
const raw = core.transpile(processed, params.source_dialect, params.target_dialect)
const result = JSON.parse(JSON.stringify(raw))
let translatedSql = result.transpiled_sql?.[0] ?? ""
const target = params.target_dialect.toLowerCase()
if (["bigquery", "databricks", "spark", "trino"].includes(target)) {
if (translatedSql.toUpperCase().includes("QUALIFY")) {
translatedSql = postprocessQualify(translatedSql)
}
}
return {
success: result.success ?? true,
translated_sql: translatedSql,
source_dialect: params.source_dialect,
target_dialect: params.target_dialect,
warnings: result.error ? [result.error] : [],
} satisfies SqlTranslateResult
} catch (e) {
return {
success: false,
source_dialect: params.source_dialect,
target_dialect: params.target_dialect,
warnings: [],
error: String(e),
} satisfies SqlTranslateResult
}
})
// ---------------------------------------------------------------------------
// sql.optimize — rewrite + lint
// ---------------------------------------------------------------------------
register("sql.optimize", async (params) => {
try {
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
const [rewriteRaw, lintRaw] = await Promise.all([
core.rewrite(params.sql, schema),
core.lint(params.sql, schema),
])
const rewrite = JSON.parse(JSON.stringify(rewriteRaw))
const lint = JSON.parse(JSON.stringify(lintRaw))
const suggestions: SqlOptimizeSuggestion[] = (rewrite.suggestions ?? []).map((s: any) => ({
type: "REWRITE",
description: s.explanation ?? s.rule ?? "",
before: params.sql,
after: s.rewritten_sql,
impact: s.confidence > 0.7 ? "high" : s.confidence > 0.4 ? "medium" : "low",
}))
const antiPatterns = (lint.findings ?? []).map((f: any) => ({
type: f.rule ?? "lint",
severity: f.severity ?? "warning",
message: f.message ?? "",
recommendation: f.suggestion ?? "",
location: f.line ? `line ${f.line}` : undefined,
confidence: "high",
}))
const bestRewrite = rewrite.suggestions?.[0]?.rewritten_sql
return {
success: true,
original_sql: params.sql,
optimized_sql: bestRewrite ?? params.sql,
suggestions,
anti_patterns: antiPatterns,
confidence: suggestions.length > 0 ? "high" : "medium",
} satisfies SqlOptimizeResult
} catch (e) {
return {
success: false,
original_sql: params.sql,
suggestions: [],
anti_patterns: [],
confidence: "low",
error: String(e),
} satisfies SqlOptimizeResult
}
})
// ---------------------------------------------------------------------------
// sql.format
// ---------------------------------------------------------------------------
register("sql.format", async (params) => {
try {
const raw = core.formatSql(params.sql, params.dialect)
const result = JSON.parse(JSON.stringify(raw))
return {
success: result.success ?? true,
formatted_sql: result.formatted_sql ?? params.sql,
dialect: params.dialect ?? "generic",
error: result.error,
}
} catch (e) {
return { success: false, formatted_sql: params.sql, dialect: params.dialect ?? "generic", error: String(e) }
}
})
// ---------------------------------------------------------------------------
// sql.fix
// ---------------------------------------------------------------------------
register("sql.fix", async (params) => {
try {
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
const raw = await core.fix(params.sql, schema)
const result = JSON.parse(JSON.stringify(raw))
const suggestions = (result.fixes_applied ?? []).map((f: any) => ({
type: f.type ?? f.rule ?? "fix",
message: f.message ?? f.description ?? "",
confidence: f.confidence ?? "medium",
fixed_sql: f.fixed_sql ?? f.rewritten_sql,
}))
const unfixableMessages = Array.isArray(result.unfixable_errors)
? result.unfixable_errors
.map((e: any) => e.error?.message ?? e.message ?? e.reason ?? String(e))
.filter((msg: any) => typeof msg === "string" ? msg.trim().length > 0 : Boolean(msg))
: []
const unfixableError = !result.fixed && unfixableMessages.length > 0
? unfixableMessages.join("; ")
: undefined
return {
success: result.fixed ?? true,
original_sql: result.original_sql ?? params.sql,
fixed_sql: result.fixed_sql ?? params.sql,
error_message: params.error_message ?? "",
suggestions,
suggestion_count: suggestions.length,
...(unfixableError && { error: unfixableError }),
}
} catch (e) {
return {
success: false,
original_sql: params.sql,
fixed_sql: params.sql,
error_message: params.error_message ?? "",
suggestions: [],
suggestion_count: 0,
error: String(e),
}
}
})
// ---------------------------------------------------------------------------
// sql.autocomplete — uses altimate-core complete() + schema cache search
// ---------------------------------------------------------------------------
register("sql.autocomplete", async (params) => {
try {
const suggestions: Array<{
name: string
type: string
detail?: string
fqn?: string
table?: string
warehouse?: string
in_context: boolean
}> = []
// Try altimate-core completion if we have a schema context
if (params.table_context?.length) {
try {
const ddl = params.table_context
.map((t: string) => `CREATE TABLE ${t} (id INT);`)
.join("\n")
const schema = core.Schema.fromDdl(ddl)
const raw = core.complete(params.prefix, params.prefix.length, schema)
const result = JSON.parse(JSON.stringify(raw))
for (const item of result.items ?? []) {
suggestions.push({
name: item.label,
type: item.kind ?? "keyword",
detail: item.detail,
in_context: true,
})
}
} catch {
// Fallback to simple keyword suggestions below
}
}
// SQL keyword suggestions as fallback
if (suggestions.length === 0 && params.prefix) {
const prefix = params.prefix.toUpperCase()
const keywords = [
"SELECT", "FROM", "WHERE", "JOIN", "LEFT JOIN", "RIGHT JOIN",
"INNER JOIN", "GROUP BY", "ORDER BY", "HAVING", "LIMIT",
"INSERT", "UPDATE", "DELETE", "CREATE", "ALTER", "DROP",
"UNION", "UNION ALL", "DISTINCT", "AS", "ON", "AND", "OR",
"NOT", "IN", "BETWEEN", "LIKE", "IS NULL", "IS NOT NULL",
"COUNT", "SUM", "AVG", "MIN", "MAX", "CASE", "WHEN", "THEN",
"ELSE", "END", "EXISTS", "WITH", "OVER", "PARTITION BY",
]
for (const kw of keywords) {
if (kw.startsWith(prefix)) {
suggestions.push({ name: kw, type: "keyword", in_context: false })
}
}
}
const limit = params.limit ?? 50
return {
suggestions: suggestions.slice(0, limit),
prefix: params.prefix,
position: params.position ?? "",
suggestion_count: Math.min(suggestions.length, limit),
}
} catch (e) {
return {
suggestions: [],
prefix: params.prefix ?? "",
position: params.position ?? "",
suggestion_count: 0,
}
}
})
// ---------------------------------------------------------------------------
// sql.diff — text diff + equivalence check
// ---------------------------------------------------------------------------
register("sql.diff", async (params) => {
try {
const schema = params.schema_context
? resolveSchema(undefined, params.schema_context) ?? undefined
: undefined
const sqlA = params.original ?? params.sql_a
const sqlB = params.modified ?? params.sql_b
const compareRaw = schema
? await core.checkEquivalence(sqlA, sqlB, schema)
: null
const compare = compareRaw ? JSON.parse(JSON.stringify(compareRaw)) : null
// Simple line-based diff
const linesA = sqlA.split("\n")
const linesB = sqlB.split("\n")
const diffLines: string[] = []
const maxLen = Math.max(linesA.length, linesB.length)
for (let i = 0; i < maxLen; i++) {
const a = linesA[i] ?? ""
const b = linesB[i] ?? ""
if (a !== b) {
if (a) diffLines.push(`- ${a}`)
if (b) diffLines.push(`+ ${b}`)
}
}
return {
success: true,
diff: diffLines.join("\n"),
equivalent: compare?.equivalent ?? false,
equivalence_confidence: compare?.confidence ?? 0,
differences: compare?.differences ?? [],
}
} catch (e) {
return { success: false, diff: "", equivalent: false, equivalence_confidence: 0, differences: [], error: String(e) }
}
})
// ---------------------------------------------------------------------------
// sql.rewrite
// ---------------------------------------------------------------------------
register("sql.rewrite", async (params) => {
try {
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
const raw = core.rewrite(params.sql, schema)
const result = JSON.parse(JSON.stringify(raw))
return {
success: true,
original_sql: params.sql,
rewritten_sql: result.suggestions?.[0]?.rewritten_sql ?? null,
rewrites_applied: result.suggestions?.map((s: any) => ({
rule: s.rule,
original_fragment: params.sql,
rewritten_fragment: s.rewritten_sql ?? params.sql,
explanation: s.explanation ?? s.improvement ?? "",
can_auto_apply: (s.confidence ?? 0) >= 0.7,
})) ?? [],
}
} catch (e) {
return { success: false, original_sql: params.sql, rewritten_sql: null, rewrites_applied: [], error: String(e) }
}
})
// ---------------------------------------------------------------------------
// sql.schema_diff
// ---------------------------------------------------------------------------
register("sql.schema_diff", async (params) => {
try {
const oldDdl = params.old_sql
const newDdl = params.new_sql
const oldSchema = core.Schema.fromDdl(oldDdl, params.dialect || undefined)
const newSchema = core.Schema.fromDdl(newDdl, params.dialect || undefined)
const raw = core.diffSchemas(oldSchema, newSchema)
const result = JSON.parse(JSON.stringify(raw))
const changes = result.changes ?? []
const hasBreaking = changes.some((c: any) => c.severity === "breaking")
return {
success: true,
changes,
has_breaking_changes: hasBreaking,
summary: result.summary ?? {},
error: undefined,
} satisfies SchemaDiffResult
} catch (e) {
return {
success: false,
changes: [],
has_breaking_changes: false,
summary: {},
error: String(e),
} satisfies SchemaDiffResult
}
})
// ---------------------------------------------------------------------------
// lineage.check
// ---------------------------------------------------------------------------
register("lineage.check", async (params) => {
try {
const schema = params.schema_context
? resolveSchema(undefined, params.schema_context) ?? undefined
: undefined
const raw = core.columnLineage(
params.sql,
params.dialect ?? undefined,
schema ?? undefined,
)
const result = JSON.parse(JSON.stringify(raw))
return {
success: true,
data: result,
} satisfies LineageCheckResult
} catch (e) {
return {
success: false,
data: {},
error: String(e),
} satisfies LineageCheckResult
}
})
} // end registerAllSql
// Auto-register on module load
registerAllSql()