Skip to content

Commit

Permalink
Addresses multiple escaped indices
Browse files Browse the repository at this point in the history
  • Loading branch information
stratoula committed Jul 4, 2024
1 parent 39068df commit ffe4d57
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ describe('correctCommonEsqlMistakes', () => {
| STATS avg_duration = AVG(transaction.duration.us), total_successes = SUM(success), total_requests = COUNT(*) BY service.name`
);
});

it("replaces ` or ' escaping in FROM statements with double quotes", () => {
expectQuery(`FROM "logs-*" | LIMIT 10`, 'FROM "logs-*"\n| LIMIT 10');
expectQuery(`FROM 'logs-*' | LIMIT 10`, 'FROM "logs-*"\n| LIMIT 10');
expectQuery(
`FROM 'logs-2024-07-01','logs-2024-07-02' | LIMIT 10`,
'FROM "logs-2024-07-01","logs-2024-07-02"\n| LIMIT 10'
);
expectQuery(`FROM logs-* | LIMIT 10`, 'FROM logs-*\n| LIMIT 10');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { getIndexPatternFromESQLQuery } from '@kbn/esql-utils';

const STRING_DELIMITER_TOKENS = ['`', "'", '"'];
const ESCAPE_TOKEN = '\\\\';
Expand Down Expand Up @@ -234,14 +235,14 @@ export function correctCommonEsqlMistakes(query: string): {

const formattedCommands: string[] = commands.map(({ name, command }, index) => {
let formattedCommand = command;

switch (name) {
case 'FROM':
formattedCommand = formattedCommand
.replaceAll(/FROM '(.*)'/g, 'FROM "$1"')
.replaceAll(/FROM `(.*)`/g, 'FROM "$1"');
case 'FROM': {
// gets the index pattern from the FROM command using AST parsing
const indexPattern = getIndexPatternFromESQLQuery(formattedCommand);
const indexPatternWithFixedQuotes = replaceSingleQuotesWithDoubleQuotes(indexPattern);
formattedCommand = formattedCommand.replace(indexPattern, indexPatternWithFixedQuotes);
break;

}
case 'WHERE':
formattedCommand = replaceSingleQuotesWithDoubleQuotes(formattedCommand);
formattedCommand = ensureEqualityOperators(formattedCommand);
Expand Down

0 comments on commit ffe4d57

Please sign in to comment.