Repository navigation
Replace PREWHERE in timeSeriesSelector - #104044
Conversation
|
Workflow [PR], commit [b06e9bc] Summary: ✅ AI ReviewSummaryThis PR replaces Final Verdict
|
5470831 to
30327db
Compare
| 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) |
There was a problem hiding this comment.
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; } |
There was a problem hiding this comment.
In fact this is enough to fix the original issue. But for consistency I added such overrides to other functions as well.
There was a problem hiding this comment.
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
PREWHEREwithWHEREin the data-table query generated byStorageTimeSeriesSelector. - Mark various TimeSeries tag/group functions as
isStateful() == trueto prevent unsafe optimizer reordering; additionally disable constant folding fortimeSeriesStoreTags()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). |
30327db to
dc8fa1f
Compare
5b44469 to
858e509
Compare
858e509 to
5880562
Compare
5880562 to
93f99ea
Compare
|
@groeneai Investigate CI failures |
|
@vitlibar — investigated CI failures on 1.
|
…eSeriesSelector()
77ab5de to
06c4d65
Compare
LLVM Coverage Report
Changed lines: 58.82% (50/85) | lost baseline coverage: 3 line(s) · Uncovered code |
6bc012d
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Replace PREWHERE in
timeSeriesSelector()Version info
26.5.1.811