Skip to content

Replace PREWHERE in timeSeriesSelector - #104044

Merged
nikitamikhaylov merged 9 commits into
ClickHouse:masterfrom
vitlibar:replace-prewhere-in-ts-selector
May 19, 2026
Merged

nikitamikhaylov merged 9 commits into
ClickHouse:masterfrom
vitlibar:replace-prewhere-in-ts-selector

Conversation

@vitlibar

@vitlibar vitlibar commented May 4, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Replace PREWHERE in timeSeriesSelector()

Version info

  • Merged into: 26.5.1.811

@clickhouse-gh

clickhouse-gh Bot commented May 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b06e9bc]

Summary: ✅


AI Review

Summary

This PR replaces PREWHERE with WHERE in timeSeriesSelector, marks time-series tag-collector functions as stateful and not suitable for constant folding, and adds a regression test for the PromQL binary-operator path that previously could raise an Unknown identifier exception. I reviewed the current diff and all prior PR discussion threads; I did not find unresolved blocker/major issues in the current code.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 4, 2026
Comment thread tests/integration/helpers/cluster.py Resolved
@vitlibar
vitlibar force-pushed the replace-prewhere-in-ts-selector branch from 5470831 to 30327db Compare May 4, 2026 19:06
@vitlibar
vitlibar requested a review from Copilot May 4, 2026 19:07
select_query->setExpression(ASTSelectQuery::Expression::TABLES, tables);
}

/// PREWHERE (id IN (SELECT id FROM (select_query_from_tags_table))) AND (timestamp >= min_time) AND (timestamp <= max_time)

@vitlibar vitlibar May 4, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Originally the issue appeared in #98948 where we started joining expressions based on timeSeriesSelector() and came to the issue that inner filter WHERE id IN (SELECT timeSeriesStoreTags(...) FROM tags_table ...) inside table function timeSeriesSelector() could be merged with outer conditions using function timeSeriesIdToGroup(). However function timeSeriesIdToGroup(id) in outer conditions can work correctly only after this id is stored by timeSeriesStoreTags(id, tags).

In order to fix this issue #98948 replaced WHERE here with PREWHERE. It worked, however slowed down the query a lot.

So in this PR I'm reverting PREWHERE back to WHERE here, and adding isStateful() override to fix the original issue.

bool isDeterministicInScopeOfQuery() const override { return true; }

/// Stateful: result depends on the per-query tags collector populated by timeSeriesStoreTags().
bool isStateful() const override { return true; }

@vitlibar vitlibar May 4, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In fact this is enough to fix the original issue. But for consistency I added such overrides to other functions as well.

@vitlibar vitlibar added the comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... label May 4, 2026

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.

Pull request overview

This PR adjusts the SQL generated by timeSeriesSelector() to avoid relying on PREWHERE for correct execution ordering, and instead makes the involved time-series tag/group functions explicitly stateful so the optimizer won’t reorder them in a way that breaks PromQL binary-operator evaluation. It also adds a regression test covering the problematic JOIN path.

Changes:

  • Replace PREWHERE with WHERE in the data-table query generated by StorageTimeSeriesSelector.
  • Mark various TimeSeries tag/group functions as isStateful() == true to prevent unsafe optimizer reordering; additionally disable constant folding for timeSeriesStoreTags() to preserve its side effect at execution time.
  • Add a stateless regression test reproducing the PromQL binary-operator JOIN issue.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/queries/0_stateless/04201_time_series_selector_join_stateful.sql New regression test for PromQL binary-operator JOIN execution ordering.
tests/queries/0_stateless/04201_time_series_selector_join_stateful.reference Expected output for the new regression test.
src/Storages/StorageTimeSeriesSelector.cpp Switch selector filter from PREWHERE to WHERE.
src/Functions/TimeSeries/timeSeriesThrowDuplicateSeriesIf.cpp Mark function stateful (uses per-query tags collector).
src/Functions/TimeSeries/timeSeriesTagsToGroup.cpp Mark function stateful (uses per-query tags collector).
src/Functions/TimeSeries/timeSeriesStoreTags.cpp Disable constant folding to preserve side effects; keep tags collector behavior correct.
src/Functions/TimeSeries/timeSeriesReplaceTag.cpp Mark function stateful (uses per-query tags collector).
src/Functions/TimeSeries/timeSeriesRemoveTags.cpp Mark function stateful (uses per-query tags collector).
src/Functions/TimeSeries/timeSeriesRemoveTag.cpp Mark function stateful (uses per-query tags collector).
src/Functions/TimeSeries/timeSeriesRemoveAllTagsExcept.cpp Mark function stateful (uses per-query tags collector).
src/Functions/TimeSeries/timeSeriesJoinTags.cpp Mark function stateful (uses per-query tags collector).
src/Functions/TimeSeries/timeSeriesIdToTags.cpp Mark function stateful (uses per-query tags collector).
src/Functions/TimeSeries/timeSeriesIdToGroup.cpp Mark function stateful (uses per-query tags collector).
src/Functions/TimeSeries/timeSeriesGroupToTags.cpp Mark function stateful (uses per-query tags collector).
src/Functions/TimeSeries/timeSeriesGroupToSamplingKey.cpp Mark function stateful (uses per-query tags collector).
src/Functions/TimeSeries/timeSeriesExtractTag.cpp Mark function stateful (uses per-query tags collector).
src/Functions/TimeSeries/timeSeriesCopyTags.cpp Mark function stateful (uses per-query tags collector).
src/Functions/TimeSeries/timeSeriesCopyTag.cpp Mark function stateful (uses per-query tags collector).

@nikitamikhaylov nikitamikhaylov self-assigned this May 5, 2026
@vitlibar
vitlibar force-pushed the replace-prewhere-in-ts-selector branch from 30327db to dc8fa1f Compare May 5, 2026 18:41
@vitlibar
vitlibar requested a review from Copilot May 5, 2026 18:42

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.

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 4 comments.

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.

Pull request overview

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

@vitlibar
vitlibar force-pushed the replace-prewhere-in-ts-selector branch from 5b44469 to 858e509 Compare May 6, 2026 08:33
@vitlibar
vitlibar requested a review from Copilot May 6, 2026 08:33

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.

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated no new comments.

@vitlibar
vitlibar force-pushed the replace-prewhere-in-ts-selector branch from 858e509 to 5880562 Compare May 6, 2026 08:54
@vitlibar
vitlibar requested a review from Copilot May 6, 2026 08:54

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.

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 15 comments.

@vitlibar
vitlibar force-pushed the replace-prewhere-in-ts-selector branch from 5880562 to 93f99ea Compare May 6, 2026 09:34

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.

Pull request overview

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

@vitlibar

Copy link
Copy Markdown
Member Author

@groeneai Investigate CI failures

@groeneai

Copy link
Copy Markdown
Collaborator

@vitlibar — investigated CI failures on 77ab5de (latest run 2026-05-06). Both chronic, neither PR-caused — detail below.

1. AST fuzzer (amd_debug, targeted, old_compatibility) — STID 4870-4f21 (AggregateFunctionArray) — CHRONIC, FIX MERGED

30-day CIDB: failures on 14+ distinct days, multiple unrelated PRs, 5–10 daily hits. This is the chronic AggregateFunctionArray: parameters mismatch between Array wrapper and nested function family covered by your own PR #103428 (merged 2026-05-11 13:06 UTC). Zero master hits since the merge.

2. Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) — 01710_projection_additional_filters — CHRONIC

30-day CIDB: 60+ hits / 50+ distinct PRs / consistent master hits since 2026-05-05. Tracked in Issue #104219, fix PR #104296 (@azat) — "Fix applying additional_table_filters with parallel replicas and analyzer" — currently in review.

Recommendation

Rebase against current master (post #103428) and re-run CI; both failures should clear without any change to your diff.

@vitlibar
vitlibar force-pushed the replace-prewhere-in-ts-selector branch from 77ab5de to 06c4d65 Compare May 15, 2026 11:13
@clickhouse-gh

clickhouse-gh Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 90.80% 90.80% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 58.82% (50/85) | lost baseline coverage: 3 line(s) · Uncovered code

Full report · Diff report

@nikitamikhaylov
nikitamikhaylov added this pull request to the merge queue May 19, 2026
Merged via the queue into ClickHouse:master with commit 6bc012d May 19, 2026
488 of 493 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants