Skip to content

Commit

Permalink
[ES|QL] Add a helper to retrieve the metadata columns (#191814)
Browse files Browse the repository at this point in the history
## Summary

Create a helper to retrieve the metadata columns (will come in handy for
one discover project)

### Checklist

- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
stratoula authored Aug 30, 2024
1 parent 60b8c05 commit cf0f3f6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/kbn-esql-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ This package contains utilities for ES|QL.
- *removeDropCommandsFromESQLQuery*: Use this function to remove all the occurences of the `drop` command from the query.
- *appendToESQLQuery*: Use this function to append more pipes in an existing ES|QL query. It adds the additional commands in a new line.
- *appendWhereClauseToESQLQuery*: Use this function to append where clause in an existing query.

- *retieveMetadataColumns*: Use this function to get if there is a metadata option in the from command, and retrieve the columns if so
1 change: 1 addition & 0 deletions packages/kbn-esql-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export {
getTimeFieldFromESQLQuery,
getStartEndParams,
hasStartEndParams,
retieveMetadataColumns,
TextBasedLanguages,
} from './src';

Expand Down
1 change: 1 addition & 0 deletions packages/kbn-esql-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export {
removeDropCommandsFromESQLQuery,
hasTransformationalCommand,
getTimeFieldFromESQLQuery,
retieveMetadataColumns,
} from './utils/query_parsing_helpers';
export { appendToESQLQuery, appendWhereClauseToESQLQuery } from './utils/append_to_query';
export {
Expand Down
14 changes: 14 additions & 0 deletions packages/kbn-esql-utils/src/utils/query_parsing_helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
removeDropCommandsFromESQLQuery,
hasTransformationalCommand,
getTimeFieldFromESQLQuery,
retieveMetadataColumns,
} from './query_parsing_helpers';

describe('esql query helpers', () => {
Expand Down Expand Up @@ -175,4 +176,17 @@ describe('esql query helpers', () => {
).toBe('event.timefield');
});
});

describe('retieveMetadataColumns', () => {
it('should return metadata columns if they exist', () => {
expect(retieveMetadataColumns('from a metadata _id, _ignored | eval b = 1')).toStrictEqual([
'_id',
'_ignored',
]);
});

it('should return empty columns if metadata doesnt exist', () => {
expect(retieveMetadataColumns('from a | eval b = 1')).toStrictEqual([]);
});
});
});
19 changes: 18 additions & 1 deletion packages/kbn-esql-utils/src/utils/query_parsing_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import type { ESQLSource, ESQLFunction, ESQLColumn, ESQLSingleAstItem } from '@kbn/esql-ast';
import type {
ESQLSource,
ESQLFunction,
ESQLColumn,
ESQLSingleAstItem,
ESQLCommandOption,
} from '@kbn/esql-ast';
import { getAstAndSyntaxErrors, Walker, walk } from '@kbn/esql-ast';

const DEFAULT_ESQL_LIMIT = 1000;
Expand Down Expand Up @@ -105,3 +111,14 @@ export const getTimeFieldFromESQLQuery = (esql: string) => {

return column?.name;
};

export const retieveMetadataColumns = (esql: string): string[] => {
const { ast } = getAstAndSyntaxErrors(esql);
const options: ESQLCommandOption[] = [];

walk(ast, {
visitCommandOption: (node) => options.push(node),
});
const metadataOptions = options.find(({ name }) => name === 'metadata');
return metadataOptions?.args.map((column) => (column as ESQLColumn).name) ?? [];
};

0 comments on commit cf0f3f6

Please sign in to comment.