-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathaltimate-core-equivalence.ts
More file actions
63 lines (60 loc) · 2.93 KB
/
altimate-core-equivalence.ts
File metadata and controls
63 lines (60 loc) · 2.93 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
import z from "zod"
import { Tool } from "../../tool/tool"
import { Dispatcher } from "../native"
export const AltimateCoreEquivalenceTool = Tool.define("altimate_core_equivalence", {
description:
"Check semantic equivalence of two SQL queries. Determines if two queries produce the same result set regardless of syntactic differences. Provide schema_context or schema_path for accurate table/column resolution.",
parameters: z.object({
sql1: z.string().describe("First SQL query"),
sql2: z.string().describe("Second SQL query"),
schema_path: z.string().optional().describe("Path to YAML/JSON schema file"),
schema_context: z.record(z.string(), z.any()).optional().describe("Inline schema definition"),
}),
async execute(args, ctx) {
if (!args.schema_path && (!args.schema_context || Object.keys(args.schema_context).length === 0)) {
const error = "No schema provided. Provide schema_context or schema_path so table/column references can be resolved."
return { title: "Equivalence: NO SCHEMA", metadata: { success: false, equivalent: false, error }, output: `Error: ${error}` }
}
try {
const result = await Dispatcher.call("altimate_core.equivalence", {
sql1: args.sql1,
sql2: args.sql2,
schema_path: args.schema_path ?? "",
schema_context: args.schema_context,
})
const data = (result.data ?? {}) as Record<string, any>
const error = result.error ?? data.error ?? extractEquivalenceErrors(data)
// "Not equivalent" is a valid analysis result, not a failure.
// Only treat it as failure when there's an actual error.
const isRealFailure = !!error
return {
title: isRealFailure ? "Equivalence: ERROR" : `Equivalence: ${data.equivalent ? "EQUIVALENT" : "DIFFERENT"}`,
metadata: { success: !isRealFailure, equivalent: data.equivalent, ...(error && { error }) },
output: formatEquivalence(data),
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return { title: "Equivalence: ERROR", metadata: { success: false, equivalent: false, error: msg }, output: `Failed: ${msg}` }
}
},
})
function extractEquivalenceErrors(data: Record<string, any>): string | undefined {
if (Array.isArray(data.validation_errors) && data.validation_errors.length > 0) {
const msgs = data.validation_errors.filter(Boolean)
return msgs.length > 0 ? msgs.join("; ") : undefined
}
return undefined
}
function formatEquivalence(data: Record<string, any>): string {
if (data.error) return `Error: ${data.error}`
const lines: string[] = []
lines.push(data.equivalent ? "Queries are semantically equivalent." : "Queries produce different results.")
if (data.differences?.length) {
lines.push("\nDifferences:")
for (const d of data.differences) {
lines.push(` - ${d.description ?? d}`)
}
}
if (data.confidence) lines.push(`\nConfidence: ${data.confidence}`)
return lines.join("\n")
}