Skip to content

[FR] Cache ES|QL parses and memoize the offline ES|QL schema helpers - #6926

Open
eric-forte-elastic wants to merge 6 commits into
mainfrom
esql-validation-speedups
Open

eric-forte-elastic wants to merge 6 commits into
mainfrom
esql-validation-speedups

Conversation

@eric-forte-elastic

@eric-forte-elastic eric-forte-elastic commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

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.py gets a new parse_esql_query(query, min_stack_version=None). It parses under a stack version's ES|QL config with an empty allow_missing schema, which is what every one of these call sites already did by hand:

  • ESQLRuleData.validates_esql_data
  • ESQLValidator._parse_tree. This repeated the exact parse validates_esql_data had just done.
  • The per-grammar parses in ESQLValidator.validate()
  • get_unique_query_fields, used by the package build
  • The helper in tests/test_esql_multi_value.py

Results are cached per (query, normalized stack version) through utils.cached, so clear_caches() still resets them. Each call returns a copy.deepcopy of the cached tree, because esql.analyze() edits JoinCommand.on_fields / on_lookup_fields when 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_indices now goes through a new cached _package_fields_for_indices. That helper uses the memoized _latest_compatible_version_from_etc instead of calling find_latest_compatible_version directly.
  • _lookup_join_schemas_for_stack is cached. Before, it was rebuilt for every target on every validation attempt.
  • collect_index_field_schemas is cached, and ECS now comes from get_flat_ecs_schema instead of re-flattening.
  • The packages_manifest / integrations_schemas pass-through arguments are removed from these private helpers. The _warm_esql_offline_caches flag is removed too, since the loaders are already cached.

3. Stale-cache fix

_ESQL_SCHEMA_DICT_CACHE was a plain module dict that clear_caches() never touched, so it could keep schemas built before a schema rebuild. A new utils.registered_cache() returns a dict that clear_caches() empties. The manual clear in ESQLValidator.auto_add_field is dropped because update_auto_generated_schema already calls clear_caches().

How To Test

Run unit tests:

Old:

image

New:
image

Checklist

  • Added a label for the type of pr: bug, enhancement, schema, maintenance, Rule: New, Rule: Deprecation, Rule: Tuning, Hunt: New, or Hunt: Tuning so guidelines can be generated
  • Added the meta:rapid-merge label if planning to merge within 24 hours
  • Secret and sensitive material has been managed correctly
  • Automated testing was updated or added to match the most common scenarios
  • Documentation and comments were added for features that require explanation

Contributor checklist

@eric-forte-elastic eric-forte-elastic self-assigned this Sep 28, 2026
@eric-forte-elastic eric-forte-elastic added python Internal python for the repository esql ES|QL patch labels Sep 28, 2026
@eric-forte-elastic

Copy link
Copy Markdown
Contributor Author

@eric-forte-elastic
eric-forte-elastic marked this pull request as ready for review November 25, 2026 21:31
Copilot AI balanced review requested due to automatic review settings November 25, 2026 21:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport: auto esql ES|QL patch python Internal python for the repository

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants