-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NL-to-ESQL] autocorrect bad LIKE wildcards (#202464)
## Summary Part of #198942 Add autocorrect for wrong `LIKE` wildcard. The LLM can make mistake and use SQL wildcards for LIKE operators (`_` instead of `?` and `%` instead of `*`) Examples **generated** ``` FROM logs | WHERE message LIKE "a%" AND TO_UPPER(level) LIKE "err%" | WHERE foo LIKE "ba_" ``` **corrected** ``` FROM logs | WHERE message LIKE "a*" AND TO_UPPER(level) LIKE "err*" | WHERE foo LIKE "ba?" ``` --------- Co-authored-by: kibanamachine <[email protected]> (cherry picked from commit 2ace6ff)
- Loading branch information
1 parent
8ce1638
commit 550c2f4
Showing
7 changed files
with
160 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
x-pack/plugins/inference/common/tasks/nl_to_esql/ast/corrections/like.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { parse, BasicPrettyPrinter } from '@kbn/esql-ast'; | ||
import { correctLikeWildcards } from './like'; | ||
|
||
describe('correctLikeWildcards', () => { | ||
it('replaces badly used "_" wildcard', () => { | ||
const query = 'FROM logs | WHERE message LIKE "ba_"'; | ||
const { root } = parse(query); | ||
correctLikeWildcards(root); | ||
|
||
const output = BasicPrettyPrinter.print(root); | ||
expect(output).toEqual('FROM logs | WHERE message LIKE "ba?"'); | ||
}); | ||
|
||
it('replaces badly used "%" wildcard', () => { | ||
const query = 'FROM logs | WHERE message LIKE "b%"'; | ||
const { root } = parse(query); | ||
correctLikeWildcards(root); | ||
|
||
const output = BasicPrettyPrinter.print(root); | ||
expect(output).toEqual('FROM logs | WHERE message LIKE "b*"'); | ||
}); | ||
|
||
it('replaces multiple bad wildcards', () => { | ||
const query = 'FROM logs | WHERE message LIKE "a__t%"'; | ||
const { root } = parse(query); | ||
correctLikeWildcards(root); | ||
|
||
const output = BasicPrettyPrinter.print(root); | ||
expect(output).toEqual('FROM logs | WHERE message LIKE "a??t*"'); | ||
}); | ||
|
||
it('replaces bad wildcards in multiple commands and functions', () => { | ||
const query = | ||
'FROM logs | WHERE message LIKE "a%" AND TO_UPPER(level) LIKE "err%" | WHERE foo LIKE "ba_"'; | ||
const { root } = parse(query); | ||
correctLikeWildcards(root); | ||
|
||
const output = BasicPrettyPrinter.print(root); | ||
expect(output).toEqual( | ||
'FROM logs | WHERE message LIKE "a*" AND TO_UPPER(level) LIKE "err*" | WHERE foo LIKE "ba?"' | ||
); | ||
}); | ||
|
||
it('does not replace escaped characters', () => { | ||
const query = 'FROM logs | WHERE message LIKE "ba\\\\_"'; | ||
const { root } = parse(query); | ||
correctLikeWildcards(root); | ||
|
||
const output = BasicPrettyPrinter.print(root); | ||
expect(output).toEqual('FROM logs | WHERE message LIKE "ba\\\\_"'); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
x-pack/plugins/inference/common/tasks/nl_to_esql/ast/corrections/like.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { Walker, type ESQLAstQueryExpression } from '@kbn/esql-ast'; | ||
import { isLikeOperatorNode, isStringLiteralNode } from '../typeguards'; | ||
import type { ESQLLikeOperator, ESQLStringLiteral } from '../types'; | ||
import type { QueryCorrection } from './types'; | ||
|
||
/** | ||
* Correct wrong LIKE wildcard mistakes. | ||
* The LLM can make mistake and use SQL wildcards for LIKE operators. | ||
* | ||
* E.g. | ||
* `column LIKE "ba_"` => `column LIKE "ba?"` | ||
* `column LIKE "ba%"` => `column LIKE "ba*"` | ||
*/ | ||
export const correctLikeWildcards = (query: ESQLAstQueryExpression): QueryCorrection[] => { | ||
const corrections: QueryCorrection[] = []; | ||
|
||
Walker.walk(query, { | ||
visitFunction: (node) => { | ||
if (isLikeOperatorNode(node)) { | ||
corrections.push(...checkLikeNode(node)); | ||
} | ||
}, | ||
}); | ||
|
||
return corrections; | ||
}; | ||
|
||
function checkLikeNode(node: ESQLLikeOperator): QueryCorrection[] { | ||
if (node.args.length !== 2 || !isStringLiteralNode(node.args[1])) { | ||
return []; | ||
} | ||
const likeExpression = node.args[1] as ESQLStringLiteral; | ||
|
||
const initialValue = likeExpression.value; | ||
|
||
likeExpression.value = likeExpression.value | ||
.replaceAll(/(?<!\\)%/g, '*') | ||
.replaceAll(/(?<!\\)_/g, '?'); | ||
|
||
if (likeExpression.value !== initialValue) { | ||
likeExpression.name = likeExpression.value; | ||
|
||
const correction: QueryCorrection = { | ||
type: 'wrong_like_wildcard', | ||
node, | ||
description: `Replaced wrong like wildcard in LIKE operator at position ${node.location.min}`, | ||
}; | ||
return [correction]; | ||
} | ||
|
||
return []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters