Skip to content

Commit 4da972c

Browse files
mhallidaclaude
andcommitted
fix: sql_analyze detects WINDOW_WITHOUT_PARTITION and LARGE_IN_LIST
Added TypeScript-level detection for two documented rules that the Rust lint engine doesn't fire: - WINDOW_WITHOUT_PARTITION: Flags window functions with OVER() or OVER(ORDER BY ...) but no PARTITION BY clause. - LARGE_IN_LIST: Flags IN clauses with 20+ values, suggesting a CTE with VALUES for better readability and performance. Fixes TICKET-017 in #422. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 5a0dd2b commit 4da972c

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,46 @@ register("sql.analyze", async (params) => {
7474
})
7575
}
7676

77+
// TypeScript-level rule: WINDOW_WITHOUT_PARTITION
78+
// Detect window functions with empty OVER() or OVER () (no PARTITION BY)
79+
const windowNoPartition = /\bOVER\s*\(\s*(?:ORDER\s+BY\b[^)]*)?(?<!\bPARTITION\s+BY\b[^)]*)\)/gi
80+
const hasWindowNoPartition = windowNoPartition.test(params.sql)
81+
if (hasWindowNoPartition) {
82+
const hasRule = issues.some((i) =>
83+
i.message.includes("WINDOW_WITHOUT_PARTITION") || i.message.includes("window") && i.message.includes("PARTITION"),
84+
)
85+
if (!hasRule) {
86+
issues.push({
87+
type: "lint",
88+
severity: "warning",
89+
message: "WINDOW_WITHOUT_PARTITION: Window function without PARTITION BY operates on the entire result set",
90+
recommendation: "Add a PARTITION BY clause to limit the window scope, or confirm whole-table windowing is intended.",
91+
confidence: "high",
92+
})
93+
}
94+
}
95+
96+
// TypeScript-level rule: LARGE_IN_LIST
97+
// Detect IN (...) with 20+ values
98+
const inListPattern = /\bIN\s*\(([^)]+)\)/gi
99+
let inMatch: RegExpExecArray | null
100+
while ((inMatch = inListPattern.exec(params.sql)) !== null) {
101+
const values = inMatch[1].split(",")
102+
if (values.length >= 20) {
103+
const hasRule = issues.some((i) => i.message.includes("LARGE_IN_LIST"))
104+
if (!hasRule) {
105+
issues.push({
106+
type: "lint",
107+
severity: "info",
108+
message: `LARGE_IN_LIST: IN clause contains ${values.length} values. Consider using a CTE with VALUES or a temporary table for better readability and performance.`,
109+
recommendation: "Replace large IN lists with a CTE: WITH vals AS (SELECT * FROM VALUES (...)) SELECT ... WHERE col IN (SELECT * FROM vals)",
110+
confidence: "high",
111+
})
112+
break // Only report once
113+
}
114+
}
115+
}
116+
77117
return {
78118
success: issues.length === 0,
79119
issues,

0 commit comments

Comments
 (0)