-
Notifications
You must be signed in to change notification settings - Fork 692
[FR] Support ES|QL sub queries #6665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eric-forte-elastic
wants to merge
6
commits into
main
Choose a base branch
from
esql_subquery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
07203d0
support ES|QL subqueries
eric-forte-elastic 4389dac
Merge branch 'main' into esql_subquery
eric-forte-elastic aeb97a0
Merge branch 'main' into esql_subquery
terrancedejesus 0bf8b49
add test for both index types
eric-forte-elastic 2eef626
Merge branch 'esql_subquery' of https://github.com/elastic/detection-…
eric-forte-elastic b331902
patch bump
eric-forte-elastic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,84 @@ | ||
| # 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. | ||
|
|
||
| """Test ES|QL query parsing.""" | ||
|
|
||
| import unittest | ||
|
|
||
| from detection_rules.esql import ( | ||
| get_esql_query_indices, | ||
| get_esql_query_source_groups, | ||
| replace_esql_query_sources, | ||
| ) | ||
|
|
||
|
|
||
| def replace_with_group_position(query: str) -> str: | ||
| """Replace each FROM clause source list with a marker for the group it belongs to.""" | ||
| groups = get_esql_query_source_groups(query) | ||
| replacements = {span: f"test-index-{position}" for position, group in enumerate(groups) for span in group.spans} | ||
| return replace_esql_query_sources(query, replacements) | ||
|
|
||
|
|
||
| class TestESQLQuerySources(unittest.TestCase): | ||
| """Test extraction and replacement of the sources of an ES|QL query.""" | ||
|
|
||
| def test_flat_sources(self): | ||
| """Test a query with a single FROM clause.""" | ||
| query = "FROM logs-a-*, logs-b-* METADATA _id\n| WHERE x == 1" | ||
| self.assertListEqual(get_esql_query_indices(query), ["logs-a-*", "logs-b-*"]) | ||
| self.assertEqual(replace_with_group_position(query), "FROM test-index-0 METADATA _id\n| WHERE x == 1") | ||
|
|
||
| def test_sources_without_metadata_or_pipe(self): | ||
| """Test that a source list terminated by the end of the query is extracted.""" | ||
| self.assertListEqual(get_esql_query_indices("FROM logs-a-*"), ["logs-a-*"]) | ||
| self.assertListEqual(get_esql_query_indices("FROM logs-a-*\n| WHERE x"), ["logs-a-*"]) | ||
|
|
||
| def test_cross_cluster_sources(self): | ||
| """Test that cross cluster sources are truncated to local indices.""" | ||
| query = "FROM cluster_one:logs-a-*, logs-b-* METADATA _id\n| WHERE x" | ||
| self.assertListEqual(get_esql_query_indices(query), ["logs-a-*", "logs-b-*"]) | ||
|
|
||
| def test_subqueries_are_grouped_by_their_own_sources(self): | ||
| """Test that subqueries reading different indices are grouped and replaced separately.""" | ||
| query = "FROM\n(\n FROM logs-a-* METADATA _id\n | WHERE x\n),\n(\n FROM logs-b-* METADATA _id\n)\n| WHERE y" | ||
| groups = get_esql_query_source_groups(query) | ||
| self.assertListEqual([group.indices for group in groups], [["logs-a-*"], ["logs-b-*"]]) | ||
| self.assertListEqual([len(group.spans) for group in groups], [1, 1]) | ||
| self.assertEqual( | ||
| replace_with_group_position(query), | ||
| "FROM\n(\n FROM test-index-0 METADATA _id\n | WHERE x\n),\n(\n FROM test-index-1 METADATA _id\n)\n" | ||
| "| WHERE y", | ||
| ) | ||
|
|
||
| def test_subqueries_reading_the_same_sources_share_a_group(self): | ||
| """Test that subqueries reading the same indices share one group, and so one set of indices.""" | ||
| query = "FROM (FROM logs-a-* METADATA _id | WHERE x), (FROM logs-a-* METADATA _id | WHERE y) | WHERE z" | ||
| groups = get_esql_query_source_groups(query) | ||
| self.assertListEqual([group.indices for group in groups], [["logs-a-*"]]) | ||
| self.assertEqual(len(groups[0].spans), 2) | ||
| self.assertListEqual(get_esql_query_indices(query), ["logs-a-*"]) | ||
| self.assertEqual( | ||
| replace_with_group_position(query), | ||
| "FROM (FROM test-index-0 METADATA _id | WHERE x), (FROM test-index-0 METADATA _id | WHERE y) | WHERE z", | ||
| ) | ||
|
|
||
| def test_sources_are_not_read_from_comments(self): | ||
| """Test that a FROM keyword or index pattern within a comment is ignored.""" | ||
| line_comment = "FROM logs-a-*\n// downloads from logs-evil-* are excluded\n| WHERE x" | ||
| block_comment = "/*\nSelects rows from logs-evil-* only\n*/\nFROM logs-a-* METADATA _id\n| WHERE x" | ||
| self.assertListEqual(get_esql_query_indices(line_comment), ["logs-a-*"]) | ||
| self.assertListEqual(get_esql_query_indices(block_comment), ["logs-a-*"]) | ||
|
|
||
| def test_sources_are_not_read_from_literals(self): | ||
| """Test that a FROM keyword or index pattern within a string literal is ignored.""" | ||
| literal = 'FROM logs-a-*\n| WHERE msg LIKE "*copied from logs-evil-**"\n| WHERE y' | ||
| raw_literal = 'FROM logs-a-*\n| EVAL x = REPLACE(y, """from logs-evil-*""", "")\n| WHERE z' | ||
| self.assertListEqual(get_esql_query_indices(literal), ["logs-a-*"]) | ||
| self.assertListEqual(get_esql_query_indices(raw_literal), ["logs-a-*"]) | ||
|
|
||
| def test_query_without_sources(self): | ||
| """Test that a query with no FROM clause yields no groups.""" | ||
| self.assertListEqual(get_esql_query_source_groups("| WHERE x == 1"), []) | ||
| self.assertListEqual(get_esql_query_indices("| WHERE x == 1"), []) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.