Skip to content

Conversation

charleskorn
Copy link
Contributor

What this PR does

This PR fixes an issue in common subexpression elimination where an expression like timestamp(foo_time) - foo_time would produce incorrect results.

This happens because the instant vector selector is incorrectly deduplicated in this case: at present, the instant vector selector operator behaves differently if it is wrapped in timestamp(), but the query planner did not take this into consideration when identifying duplicate expressions.

A future improvement would be to allow the instant vector selector operator to return both the sample value and sample timestamp to different consumers, but given queries like this are rare, I've opted for the simpler approach.

Which issue(s) this PR fixes or relates to

#11189

Checklist

  • Tests updated.
  • [n/a] Documentation added.
  • [covered by Mimir Query Engine #10067] CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]. If changelog entry is not needed, please add the changelog-not-needed label to the PR.
  • [n/a] about-versioning.md updated with experimental features.

@charleskorn charleskorn added the changelog-not-needed PRs that don't need a CHANGELOG.md entry label Jun 24, 2025
@charleskorn charleskorn force-pushed the charleskorn/mqe-cse-timestamp branch from 9aa5f2b to 0a75c75 Compare June 24, 2025 03:14
@charleskorn charleskorn marked this pull request as ready for review June 24, 2025 03:34
@charleskorn charleskorn requested a review from a team as a code owner June 24, 2025 03:34
@aknuds1 aknuds1 requested a review from Copilot June 24, 2025 07:32
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for propagating timestamp() through vector selectors to avoid incorrect deduplication, ensuring sample timestamps are returned when requested.

  • Introduce a ReturnSampleTimestamps flag on VectorSelector nodes and wire it through planning, execution, and serialization.
  • Update Describe, EquivalentTo, and OperatorFactory methods to account for the new flag.
  • Extend tests for vector selectors and common subexpression elimination to cover timestamp() scenarios and regenerate protobuf definitions.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pkg/streamingpromql/planning/core/vector_selector.go Support ReturnSampleTimestamps in Describe, EquivalentTo, and factory
pkg/streamingpromql/planning/core/vector_selector_test.go Add test cases for Describe and Equivalence with sample timestamps flag
pkg/streamingpromql/planning/core/core.proto Add returnSampleTimestamps field to VectorSelectorDetails
pkg/streamingpromql/planning/core/core.pb.go Regenerate protobuf code to handle the new field
pkg/streamingpromql/planning.go Mark ReturnSampleTimestamps on VectorSelector when planning timestamp()
pkg/streamingpromql/optimize/plan/commonsubexpressionelimination/optimization_pass_test.go Add CSE test scenarios involving timestamp()
pkg/streamingpromql/operators/functions/factories.go Remove redundant setter in TimestampFunctionOperatorFactory
Comments suppressed due to low confidence (1)

pkg/streamingpromql/planning.go:324

  • Add a unit test for nodeFromExpr to verify that parsing timestamp(foo) correctly sets ReturnSampleTimestamps on the resulting VectorSelector.
		case "timestamp":


if isSelector {
selector.ReturnSampleTimestamps = true
// We'll have already set ReturnSampleTimestamps on the InstantVectorSelector during the planning process, so we don't need to do that here.
Copy link
Preview

Copilot AI Jun 24, 2025

Choose a reason for hiding this comment

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

[nitpick] This comment references the now-removed selector variable and may confuse readers; consider updating or removing the outdated comment.

Suggested change
// We'll have already set ReturnSampleTimestamps on the InstantVectorSelector during the planning process, so we don't need to do that here.
// The ReturnSampleTimestamps property of InstantVectorSelector is set during the planning process, so no additional action is needed here.

Copilot uses AI. Check for mistakes.

@charleskorn charleskorn merged commit dd17406 into main Jun 24, 2025
31 checks passed
@charleskorn charleskorn deleted the charleskorn/mqe-cse-timestamp branch June 24, 2025 08:48
mimir-github-bot bot pushed a commit that referenced this pull request Jun 24, 2025
…imestamp()` produce incorrect results (#11830)

#### What this PR does

This PR fixes an issue in common subexpression elimination where an
expression like `timestamp(foo_time) - foo_time` would produce incorrect
results.

This happens because the instant vector selector is incorrectly
deduplicated in this case: at present, the instant vector selector
operator behaves differently if it is wrapped in `timestamp()`, but the
query planner did not take this into consideration when identifying
duplicate expressions.

A future improvement would be to allow the instant vector selector
operator to return both the sample value and sample timestamp to
different consumers, but given queries like this are rare, I've opted
for the simpler approach.

#### Which issue(s) this PR fixes or relates to

#11189

#### Checklist

- [x] Tests updated.
- [n/a] Documentation added.
- [covered by #10067] `CHANGELOG.md` updated - the order of entries
should be `[CHANGE]`, `[FEATURE]`, `[ENHANCEMENT]`, `[BUGFIX]`. If
changelog entry is not needed, please add the `changelog-not-needed`
label to the PR.
- [n/a]
[`about-versioning.md`](https://github.com/grafana/mimir/blob/main/docs/sources/mimir/configure/about-versioning.md)
updated with experimental features.

(cherry picked from commit dd17406)
charleskorn added a commit that referenced this pull request Jun 24, 2025
… and `timestamp()` produce incorrect results (#11832)

Backport dd17406 from #11830

Co-authored-by: Charles Korn <charleskorn@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport r348 changelog-not-needed PRs that don't need a CHANGELOG.md entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants