-
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.
Browse files
Browse the repository at this point in the history
# Backport This will backport the following commits from `main` to `8.x`: - [[NL-to-ESQL] autocorrect bad LIKE wildcards (#202464)](#202464) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Pierre Gayvallet","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-12-03T07:26:46Z","message":"[NL-to-ESQL] autocorrect bad LIKE wildcards (#202464)\n\n## Summary\r\n\r\nPart of https://github.com/elastic/kibana/issues/198942\r\n\r\nAdd autocorrect for wrong `LIKE` wildcard.\r\n\r\nThe LLM can make mistake and use SQL wildcards for LIKE operators (`_`\r\ninstead of `?` and `%` instead of `*`)\r\n\r\n\r\nExamples\r\n\r\n**generated**\r\n```\r\nFROM logs | WHERE message LIKE \"a%\" AND TO_UPPER(level) LIKE \"err%\" | WHERE foo LIKE \"ba_\"\r\n```\r\n**corrected**\r\n```\r\nFROM logs | WHERE message LIKE \"a*\" AND TO_UPPER(level) LIKE \"err*\" | WHERE foo LIKE \"ba?\"\r\n```\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"2ace6ffcedec826f7a6c3690fa4f99a5ea63c663","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:version","Team:AI Infra","v8.18.0"],"title":"[NL-to-ESQL] autocorrect bad LIKE wildcards","number":202464,"url":"https://github.com/elastic/kibana/pull/202464","mergeCommit":{"message":"[NL-to-ESQL] autocorrect bad LIKE wildcards (#202464)\n\n## Summary\r\n\r\nPart of https://github.com/elastic/kibana/issues/198942\r\n\r\nAdd autocorrect for wrong `LIKE` wildcard.\r\n\r\nThe LLM can make mistake and use SQL wildcards for LIKE operators (`_`\r\ninstead of `?` and `%` instead of `*`)\r\n\r\n\r\nExamples\r\n\r\n**generated**\r\n```\r\nFROM logs | WHERE message LIKE \"a%\" AND TO_UPPER(level) LIKE \"err%\" | WHERE foo LIKE \"ba_\"\r\n```\r\n**corrected**\r\n```\r\nFROM logs | WHERE message LIKE \"a*\" AND TO_UPPER(level) LIKE \"err*\" | WHERE foo LIKE \"ba?\"\r\n```\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"2ace6ffcedec826f7a6c3690fa4f99a5ea63c663"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/202464","number":202464,"mergeCommit":{"message":"[NL-to-ESQL] autocorrect bad LIKE wildcards (#202464)\n\n## Summary\r\n\r\nPart of https://github.com/elastic/kibana/issues/198942\r\n\r\nAdd autocorrect for wrong `LIKE` wildcard.\r\n\r\nThe LLM can make mistake and use SQL wildcards for LIKE operators (`_`\r\ninstead of `?` and `%` instead of `*`)\r\n\r\n\r\nExamples\r\n\r\n**generated**\r\n```\r\nFROM logs | WHERE message LIKE \"a%\" AND TO_UPPER(level) LIKE \"err%\" | WHERE foo LIKE \"ba_\"\r\n```\r\n**corrected**\r\n```\r\nFROM logs | WHERE message LIKE \"a*\" AND TO_UPPER(level) LIKE \"err*\" | WHERE foo LIKE \"ba?\"\r\n```\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"2ace6ffcedec826f7a6c3690fa4f99a5ea63c663"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Pierre Gayvallet <[email protected]>
- Loading branch information
1 parent
8ce1638
commit 06256a3
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