Skip to content

Commit

Permalink
Fix nested selects
Browse files Browse the repository at this point in the history
  • Loading branch information
stratoula committed Jul 25, 2022
1 parent 3a8abb3 commit 29afcbf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/kbn-es-query/src/es_query/es_query_sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ describe('sql query helpers', () => {
'SELECT woof, meow from logstash-1234! WHERE field > 100'
);
expect(idxPattern7).toBe('logstash-1234!');

const idxPattern8 = getIndexPatternFromSQLQuery(
'SELECT * FROM (SELECT woof, miaou FROM "logstash-1234!" GROUP BY woof)'
);
expect(idxPattern8).toBe('logstash-1234!');
});
});
});
7 changes: 6 additions & 1 deletion packages/kbn-es-query/src/es_query/es_query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ export function getAggregateQueryMode(query: AggregateQuery): Language {

// retrieves the index pattern from the aggregate query
export function getIndexPatternFromSQLQuery(sqlQuery?: string): string {
const sql = sqlQuery?.replaceAll('"', '').replaceAll("'", '');
let sql = sqlQuery?.replaceAll('"', '').replaceAll("'", '');
const splitFroms = sql?.split(new RegExp(/FROM\s/, 'ig'));
const fromsLength = splitFroms?.length ?? 0;
if (splitFroms && splitFroms?.length > 2) {
sql = `${splitFroms[fromsLength - 2]} FROM ${splitFroms[fromsLength - 1]}`;
}
// case insensitive match for the index pattern
const regex = new RegExp(/FROM\s+([\w*-.!@$^()~;]+)/, 'i');
const matches = sql?.match(regex);
Expand Down

0 comments on commit 29afcbf

Please sign in to comment.