Repository navigation
[FR] Cache ES|QL parses and memoize the offline ES|QL schema helpers - #6926
Open
eric-forte-elastic wants to merge 6 commits into
Open
eric-forte-elastic wants to merge 6 commits into
eric-forte-elastic wants to merge 6 commits into
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block the offline ES|QL caching changes.
No open issues 🎉
What changed in this PR
Extends the earlier validation performance work to offline ES|QL, reducing repeated parsing and schema construction while ensuring schema caches are cleared after updates.
Changes:
- Adds shared, memoized ES|QL parsing with independent tree copies for callers.
- Memoizes schema helpers and registers the offline schema cache with
clear_caches(). - Adds cache-behavior tests, simplifies docstrings, and bumps the package version to
3.0.37.
| File | Description |
|---|---|
tests/test_utils.py |
Tests clearing registered dictionary caches. |
tests/test_esql.py |
Tests parse reuse, tree isolation, and uncached failures. |
tests/test_esql_multi_value.py |
Uses the shared parser and simplifies documentation. |
pyproject.toml |
Bumps the package version to 3.0.37. |
detection_rules/utils.py |
Adds dictionary-cache registration and centralized clearing. |
detection_rules/rule.py |
Routes validation and field extraction through the shared parser. |
detection_rules/rule_validators.py |
Memoizes schema helpers and integrates shared parsing and cache invalidation. |
detection_rules/integrations.py |
Exposes the memoized compatibility helper for reuse. |
detection_rules/esql.py |
Adds cached parsing with defensive copies and memoizes index-field schemas. |
Review effort: Balanced
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Pull Request
Issue link(s):
Related to https://github.com/elastic/ia-trade-team/issues/1056
Summary - What I changed
The earlier speedups (#6592, #6593, #6594) mostly skipped the offline ES|QL validation path. Profiling showed that most of the ES|QL time is spent in the ANTLR parser, and that the same queries are parsed over and over: loading one rule parsed its query about 6 times. Over the full unit test run that came to 4,797 parses, of which only 1,633 were distinct (grammar, query) pairs.
Also, this PR cleans up some doctrings which are not in the repo format.
1. One shared, memoized ES|QL parse
detection_rules/esql.pygets a newparse_esql_query(query, min_stack_version=None). It parses under a stack version's ES|QL config with an emptyallow_missingschema, which is what every one of these call sites already did by hand:ESQLRuleData.validates_esql_dataESQLValidator._parse_tree. This repeated the exact parsevalidates_esql_datahad just done.ESQLValidator.validate()get_unique_query_fields, used by the package buildtests/test_esql_multi_value.pyResults are cached per (query, normalized stack version) through
utils.cached, soclear_caches()still resets them. Each call returns acopy.deepcopyof the cached tree, becauseesql.analyze()editsJoinCommand.on_fields/on_lookup_fieldswhen a lookup schema is present. A deepcopy costs about 1/37 of a warm parse. Failed parses are not cached and raise again on every call.2. The ES|QL schema helpers use the #6592 memoization
integration_fields_for_indicesnow goes through a new cached_package_fields_for_indices. That helper uses the memoized_latest_compatible_version_from_etcinstead of callingfind_latest_compatible_versiondirectly._lookup_join_schemas_for_stackis cached. Before, it was rebuilt for every target on every validation attempt.collect_index_field_schemasis cached, and ECS now comes fromget_flat_ecs_schemainstead of re-flattening.packages_manifest/integrations_schemaspass-through arguments are removed from these private helpers. The_warm_esql_offline_cachesflag is removed too, since the loaders are already cached.3. Stale-cache fix
_ESQL_SCHEMA_DICT_CACHEwas a plain module dict thatclear_caches()never touched, so it could keep schemas built before a schema rebuild. A newutils.registered_cache()returns a dict thatclear_caches()empties. The manual clear inESQLValidator.auto_add_fieldis dropped becauseupdate_auto_generated_schemaalready callsclear_caches().How To Test
Run unit tests:
Old:
New:

Checklist
bug,enhancement,schema,maintenance,Rule: New,Rule: Deprecation,Rule: Tuning,Hunt: New, orHunt: Tuningso guidelines can be generatedmeta:rapid-mergelabel if planning to merge within 24 hoursContributor checklist