Skip to content

Prometheus: execute query_range via parsed EsqlStatement plan#144416

Merged
felixbarny merged 11 commits intoelastic:mainfrom
felixbarny:promql-query-range-pr4
Mar 26, 2026
Merged

Prometheus: execute query_range via parsed EsqlStatement plan#144416
felixbarny merged 11 commits intoelastic:mainfrom
felixbarny:promql-query-range-pr4

Conversation

@felixbarny
Copy link
Copy Markdown
Member

@felixbarny felixbarny commented Mar 17, 2026

Summary

This PR updates the query_range endpoint to build and execute a pre-built ES|QL EsqlStatement instead of constructing/parsing an ES|QL query string.

Key changes

  • build a PromqlCommand-based logical plan directly in PrometheusQueryRangeRestAction
  • convert step with TO_LONG(step) in-plan and add a projection to preserve original output order
  • adjust PrometheusQueryRangeResponseListener and related tests for the updated output contract

Stacking order

Move the logical plan construction logic into a dedicated
PromqlQueryPlanBuilder class, and add a matching test class with
assertions on index pattern and query.
@felixbarny felixbarny force-pushed the promql-query-range-pr4 branch from 4e0de4e to b108e1a Compare March 23, 2026 09:30
Keep PreparedEsqlQueryRequest approach from HEAD; pass the `index` path
parameter to PromqlQueryPlanBuilder instead of the hardcoded "*".
- Pass query description to PreparedEsqlQueryRequest.sync (new required arg added in elastic#144415)
- Pass index path param instead of hardcoded "*" to PromqlQueryPlanBuilder
- Add INDEX_PARAM constant; replace unused EsqlQueryRequest import with PreparedEsqlQueryRequest
The Project node captured _timeseries attribute IDs at plan-build time.
After analysis, the promqlPlan inside PromqlCommand is transformed
(label matchers get resolved), creating a new InstantSelector instance
whose output() produces a fresh TimeSeriesMetadataAttribute with a new
NameId. The pre-built Project then referenced a stale ID, causing
PlanConsistencyChecker to report "optimized incorrectly due to missing
references [_timeseries{f}#N]".

The Project was also selecting the wrong step column: it kept
step(datetime) and discarded step_alias(long), so the response listener
never received step as a long at the last column position.

The fix: remove the Project entirely. Eval.mergeOutputAttributes already
produces the correct output — it drops step(datetime) because the
step_alias shadows it by name, then appends step_alias(long) at the end,
giving [value, ...dimensions, step(long)] as the listener expects.
@felixbarny felixbarny added >non-issue :StorageEngine/PromQL PromQL support for Elastic labels Mar 25, 2026
@felixbarny felixbarny force-pushed the promql-query-range-pr4 branch from a8ee806 to d853bfa Compare March 25, 2026 11:40
@felixbarny felixbarny marked this pull request as ready for review March 25, 2026 11:41
@felixbarny felixbarny self-assigned this Mar 25, 2026
@felixbarny felixbarny requested review from kkrik-es March 25, 2026 11:41
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-storage-engine (Team:StorageEngine)

Add an OrderBy(step ASC) node in PromqlQueryPlanBuilder so that
values within each series are returned in chronological order.
The AddImplicitLimit analyzer rule supplies the required bounding
Limit(1_000_000) that ES|QL's plan verifier expects.

Add ascending-timestamp assertions to PrometheusQueryRangeRestIT.
@felixbarny felixbarny requested a review from sidosera March 25, 2026 12:42
@felixbarny felixbarny enabled auto-merge (squash) March 25, 2026 13:35
@felixbarny felixbarny merged commit 33ec91d into elastic:main Mar 26, 2026
40 checks passed
@felixbarny felixbarny deleted the promql-query-range-pr4 branch March 26, 2026 10:48
szybia added a commit to szybia/elasticsearch that referenced this pull request Mar 26, 2026
* upstream/main: (146 commits)
  Revert "[Native] Gradle-related tweaks to improve handling of the simdvec native library (elastic#144539)"
  Fix ArrayIndexOutOfBoundsException in fetch phase with partial results (elastic#144385)
  ESQL: Correctly manage NULL data type for SUM (elastic#144942)
  [ESQL] Fixes GroupedTopNBenchmark not executing (elastic#144944)
  Fix reader context leak when query response serialization fails (elastic#144708)
  Validate individual offset values in BULK_OFFSETS bounds checks (elastic#144643)
  Merge main21 source set into main in simdvec (elastic#144921)
  [TEST] Unmute TsidExtractingIdFieldMapperTests (elastic#144848)
  [Native] Gradle-related tweaks to improve handling of the simdvec native library (elastic#144539)
  Fix `ThreadedActionListenerTests#testRejectionHandling` (elastic#144795)
  Add new DLM Frozen Tier Transition execution plugin and service (elastic#144595)
  Prometheus: execute query_range via parsed EsqlStatement plan (elastic#144416)
  Investigate `testBulkIndexingRequestSplitting` failure (elastic#144766)
  Add test utility for wrapping directories in FilterDirectory layer (elastic#143563)
  Fix ES|QL decay tests with negative scale (elastic#144657)
  Fix circuit breaker leak in percolator query construction (elastic#144827)
  Use XPerFieldDocValuesFormat in AbstractTSDBSyntheticIdCodec (elastic#144744)
  [DOCS] Document how reindex work in CPS (elastic#144016)
  Fix Int4 vector library tests failing on Java 21 (elastic#144830)
  [DiskBBQ] Fix index sorting on flush (elastic#144938)
  ...
seanzatzdev pushed a commit to seanzatzdev/elasticsearch that referenced this pull request Mar 26, 2026
…c#144416)

Move the logical plan construction logic into a dedicated
PromqlQueryPlanBuilder class, and add a matching test class with
assertions on index pattern and query.
seanzatzdev pushed a commit to seanzatzdev/elasticsearch that referenced this pull request Mar 27, 2026
…c#144416)

Move the logical plan construction logic into a dedicated
PromqlQueryPlanBuilder class, and add a matching test class with
assertions on index pattern and query.
mamazzol pushed a commit to mamazzol/elasticsearch that referenced this pull request Mar 30, 2026
…c#144416)

Move the logical plan construction logic into a dedicated
PromqlQueryPlanBuilder class, and add a matching test class with
assertions on index pattern and query.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external-contributor Pull request authored by a developer outside the Elasticsearch team >non-issue :StorageEngine/PromQL PromQL support for Elastic Team:StorageEngine v9.4.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants