@@ -123,6 +123,87 @@ export function postprocessQualify(sql: string): string {
123123// BigQuery supports QUALIFY natively — only wrap for dialects that don't
124124const QUALIFY_TARGETS = new Set ( [ "databricks" , "spark" , "trino" ] )
125125
126+ // ---------------------------------------------------------------------------
127+ // SQL keyword fuzzy correction
128+ // ---------------------------------------------------------------------------
129+
130+ const SQL_KEYWORDS = [
131+ "SELECT" , "FROM" , "WHERE" , "JOIN" , "LEFT" , "RIGHT" , "INNER" , "OUTER" ,
132+ "FULL" , "CROSS" , "ON" , "AND" , "OR" , "NOT" , "IN" , "BETWEEN" , "LIKE" ,
133+ "IS" , "NULL" , "AS" , "ORDER" , "BY" , "GROUP" , "HAVING" , "LIMIT" ,
134+ "OFFSET" , "UNION" , "ALL" , "DISTINCT" , "INSERT" , "INTO" , "VALUES" ,
135+ "UPDATE" , "SET" , "DELETE" , "CREATE" , "TABLE" , "ALTER" , "DROP" ,
136+ "INDEX" , "VIEW" , "CASE" , "WHEN" , "THEN" , "ELSE" , "END" , "EXISTS" ,
137+ "WITH" , "OVER" , "PARTITION" , "WINDOW" , "ROWS" , "RANGE" , "UNBOUNDED" ,
138+ "PRECEDING" , "FOLLOWING" , "CURRENT" , "ROW" , "CAST" , "COALESCE" ,
139+ "NULLIF" , "COUNT" , "SUM" , "AVG" , "MIN" , "MAX" , "HAVING" , "ASC" ,
140+ "DESC" , "TRUE" , "FALSE" , "PRIMARY" , "KEY" , "FOREIGN" , "REFERENCES" ,
141+ "CONSTRAINT" , "DEFAULT" , "CHECK" , "UNIQUE" , "TRIGGER" , "PROCEDURE" ,
142+ "FUNCTION" , "RETURN" , "RETURNS" , "BEGIN" , "DECLARE" , "IF" , "WHILE" ,
143+ "FOR" , "EACH" , "AFTER" , "BEFORE" , "INSTEAD" , "OF" , "EXECUTE" ,
144+ "GRANT" , "REVOKE" , "COMMIT" , "ROLLBACK" , "SAVEPOINT" , "TRUNCATE" ,
145+ "MERGE" , "USING" , "MATCHED" , "QUALIFY" , "EXCEPT" , "INTERSECT" ,
146+ ]
147+
148+ /**
149+ * Levenshtein distance between two strings.
150+ */
151+ function levenshtein ( a : string , b : string ) : number {
152+ const m = a . length
153+ const n = b . length
154+ const dp : number [ ] [ ] = Array . from ( { length : m + 1 } , ( ) => Array ( n + 1 ) . fill ( 0 ) )
155+ for ( let i = 0 ; i <= m ; i ++ ) dp [ i ] [ 0 ] = i
156+ for ( let j = 0 ; j <= n ; j ++ ) dp [ 0 ] [ j ] = j
157+ for ( let i = 1 ; i <= m ; i ++ ) {
158+ for ( let j = 1 ; j <= n ; j ++ ) {
159+ dp [ i ] [ j ] = a [ i - 1 ] === b [ j - 1 ]
160+ ? dp [ i - 1 ] [ j - 1 ]
161+ : 1 + Math . min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j - 1 ] )
162+ }
163+ }
164+ return dp [ m ] [ n ]
165+ }
166+
167+ /**
168+ * Pre-process SQL to fix obvious keyword typos before passing to the Rust fix engine.
169+ * Returns the corrected SQL and a list of corrections made.
170+ */
171+ export function fixKeywordTypos ( sql : string ) : { sql : string ; corrections : string [ ] } {
172+ const corrections : string [ ] = [ ]
173+ // Tokenize by splitting on whitespace and punctuation boundaries
174+ const result = sql . replace ( / \b ( [ A - Z a - z _ ] \w * ) \b / g, ( match ) => {
175+ const upper = match . toUpperCase ( )
176+ // Skip if it's already a valid keyword
177+ if ( SQL_KEYWORDS . includes ( upper ) ) return match
178+ // Skip if it looks like an identifier (lowercase, mixed case with underscores)
179+ if ( match . includes ( "_" ) || ( match [ 0 ] === match [ 0 ] . toLowerCase ( ) && match . length > 3 ) ) return match
180+
181+ // Only try to fix short all-caps or Title-case tokens that look like keyword typos
182+ if ( match . length < 3 || match . length > 12 ) return match
183+
184+ // Find closest keyword by Levenshtein distance
185+ let bestKeyword = ""
186+ let bestDist = Infinity
187+ for ( const kw of SQL_KEYWORDS ) {
188+ if ( Math . abs ( kw . length - upper . length ) > 2 ) continue
189+ const dist = levenshtein ( upper , kw )
190+ if ( dist < bestDist ) {
191+ bestDist = dist
192+ bestKeyword = kw
193+ }
194+ }
195+
196+ // Accept if edit distance is 1-2 and the token is short enough relative to keyword
197+ const maxDist = upper . length <= 4 ? 1 : 2
198+ if ( bestDist > 0 && bestDist <= maxDist && bestKeyword ) {
199+ corrections . push ( `${ match } → ${ bestKeyword } ` )
200+ return bestKeyword
201+ }
202+ return match
203+ } )
204+ return { sql : result , corrections }
205+ }
206+
126207// ---------------------------------------------------------------------------
127208// Handler registrations
128209// ---------------------------------------------------------------------------
@@ -237,16 +318,56 @@ register("altimate_core.check", async (params) => {
237318 }
238319} )
239320
240- // 7. altimate_core.fix
321+ // 7. altimate_core.fix — with keyword typo correction
241322register ( "altimate_core.fix" , async ( params ) => {
242323 try {
324+ // Pre-fix keyword typos before passing to Rust
325+ const { sql : preprocessed , corrections } = fixKeywordTypos ( params . sql )
326+
243327 const schema = schemaOrEmpty ( params . schema_path , params . schema_context )
244328 const raw = await core . fix (
245- params . sql ,
329+ preprocessed ,
246330 schema ,
247331 params . max_iterations ?? undefined ,
248332 )
249333 const data = toData ( raw )
334+
335+ // If we fixed keyword typos and Rust didn't change anything further,
336+ // report the keyword fixes as the result
337+ if ( corrections . length > 0 && ! data . fixed && preprocessed !== params . sql ) {
338+ return ok ( true , {
339+ ...data ,
340+ fixed : true ,
341+ original_sql : params . sql ,
342+ fixed_sql : data . fixed_sql ?? preprocessed ,
343+ fixes_applied : [
344+ ...( data . fixes_applied as any [ ] ?? [ ] ) ,
345+ ...corrections . map ( ( c ) => ( {
346+ action : "replace_keyword" ,
347+ original : c . split ( " → " ) [ 0 ] ,
348+ replacement : c . split ( " → " ) [ 1 ] ,
349+ confidence : 0.95 ,
350+ explanation : `Fixed keyword typo: ${ c } ` ,
351+ } ) ) ,
352+ ] ,
353+ } )
354+ }
355+
356+ // If Rust fixed it, ensure we preserve the keyword corrections too
357+ if ( corrections . length > 0 && data . fixed ) {
358+ data . original_sql = params . sql
359+ data . fixes_applied = [
360+ ...corrections . map ( ( c ) => ( {
361+ action : "replace_keyword" ,
362+ original : c . split ( " → " ) [ 0 ] ,
363+ replacement : c . split ( " → " ) [ 1 ] ,
364+ confidence : 0.95 ,
365+ explanation : `Fixed keyword typo: ${ c } ` ,
366+ } ) ) ,
367+ ...( data . fixes_applied as any [ ] ?? [ ] ) ,
368+ ]
369+ }
370+
250371 return ok ( data . fixed !== false , data )
251372 } catch ( e ) {
252373 return fail ( e )
0 commit comments