Skip to content

Commit 32c2b34

Browse files
mhallidaclaude
andcommitted
fix: sql_optimize now returns optimized SQL with rewrite suggestions
Integrated the TypeScript-level non-sargable predicate rewrites into sql_optimize so it produces actual optimized SQL instead of just listing anti-patterns with unchanged SQL. Fixes TICKET-004 in #422. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent ea98625 commit 32c2b34

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

‎packages/opencode/src/altimate/native/sql/register.ts‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,25 @@ register("sql.optimize", async (params) => {
142142
const rewrite = JSON.parse(JSON.stringify(rewriteRaw))
143143
const lint = JSON.parse(JSON.stringify(lintRaw))
144144

145-
const suggestions: SqlOptimizeSuggestion[] = (rewrite.suggestions ?? []).map((s: any) => ({
146-
type: "REWRITE",
147-
description: s.explanation ?? s.rule ?? "",
148-
before: params.sql,
149-
after: s.rewritten_sql,
150-
impact: s.confidence > 0.7 ? "high" : s.confidence > 0.4 ? "medium" : "low",
151-
}))
145+
// TypeScript-level non-sargable rewrites
146+
const tsRewrites = detectNonSargableRewrites(params.sql)
147+
148+
const suggestions: SqlOptimizeSuggestion[] = [
149+
...(rewrite.suggestions ?? []).map((s: any) => ({
150+
type: "REWRITE",
151+
description: s.explanation ?? s.rule ?? "",
152+
before: params.sql,
153+
after: s.rewritten_sql,
154+
impact: s.confidence > 0.7 ? "high" : s.confidence > 0.4 ? "medium" : "low",
155+
})),
156+
...tsRewrites.map((r) => ({
157+
type: "REWRITE",
158+
description: r.explanation,
159+
before: r.original_fragment,
160+
after: r.rewritten_fragment,
161+
impact: r.confidence > 0.7 ? "high" : r.confidence > 0.4 ? "medium" : "low",
162+
})),
163+
]
152164

153165
const antiPatterns = (lint.findings ?? []).map((f: any) => ({
154166
type: f.rule ?? "lint",
@@ -159,7 +171,7 @@ register("sql.optimize", async (params) => {
159171
confidence: "high",
160172
}))
161173

162-
const bestRewrite = rewrite.suggestions?.[0]?.rewritten_sql
174+
const bestRewrite = rewrite.suggestions?.[0]?.rewritten_sql ?? tsRewrites[0]?.rewritten_sql
163175

164176
return {
165177
success: true,

0 commit comments

Comments
 (0)