Skip to content

Do not let a derived indexHint keep HAVING above GROUPING SETS - #128077

Merged
nickitat merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/grouping-sets-hint-gate-128072
Nov 13, 2026
Merged

nickitat merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/grouping-sets-hint-gate-128072

Conversation

@groeneai

@groeneai groeneai commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #128072
Related: #126219

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):

...

Description

Since #126219 (master, and the 26.10 branch through its backport #128130; in no release yet), a HAVING over GROUPING SETS whose compare chain or OR-LIKE chain derives an indexHint on a key expression is no longer pushed below the aggregation, so its other conjuncts stop pruning. With the table from #128072 (ORDER BY v), this reads 78/78 granules instead of 3/78 on 26.10.2; results are the same:

SELECT intDiv(k, 10) AS g, v, count() FROM t GROUP BY GROUPING SETS ((g, v), (g, v, w)) HAVING g < v AND v < 3;

#126219 made the GROUPING SETS check in tryPushDownFilter also count the columns a derived hint reads. The hint's own actions read the table column k, not the key intDiv(k, 10), so k is in no grouping set and the check rejected the whole filter.

Now the check still rejects a filter whose own columns are missing from a set. When only a derived hint reads such a column, the filter is pushed with PushDownIndexHints::None: the other conjuncts go below the aggregation as on 26.10.2 and the hints stay above it, so a hint never prunes rows that a set without its column aggregates. This covers MergingAggregated too.

Validation: 05401_index_hint_derived_below_group_by gains the issue's query and a remote('127.0.0.{1,1}') variant (79/79 granules without the fix, 3/79 with it). Its existing GROUPING SETS ((a, b), (b)) case still returns 0, and returns 1 if the hints are pushed. 50/50 randomized, 10/10 with a Replicated database; the grouping sets, filter push-down and index hint tests pass.


Workflow [PR]
Sync PR [sync-upstream/pr/128077]

Version info

  • Merged into: 26.11.1.2202-master (included in 26.11 and later)
  • Backported to: 26.10.7.24

Since ClickHouse#126219 the GROUPING SETS check in tryPushDownFilter also counts the
columns that an implied indexHint reads, so that a filter consisting only of
such a hint cannot prune rows below a set that lacks its key. But the hint's
own actions are built from the query tree and read the table column (`k` of
key `intDiv(k, 10)`, `ts` of `toDate(ts)`), which is in no grouping set. The
check then rejected the whole filter, and its other conjuncts (`v < 3`),
which 26.10 pushed to the read, stopped pruning: the query from ClickHouse#128072 read
78/78 granules instead of 3/78.

Collect the hint inputs separately. The filter's own identifiers still gate
it as before. When a hint input is missing from some set, push the filter
with PushDownIndexHints::None, so the other conjuncts go below the
aggregation as on 26.10 and every hint stays above it; otherwise keep
PushDownIndexHints::Implied as on master. Applies to MergingAggregated too.

The existing GROUPING SETS ((a, b), (b)) case of 05401 still returns 0; with
the hints pushed (Implied instead of None) it returns 1.

Closes ClickHouse#128072

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review (2 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ The remote() arm does not show that a hint stays above MergingAggregated with GROUPING SETS; asks for a remote() copy of the GROUPING SETS ((a, b), (b)) case DISAGREE Every MergingAggregated with grouping sets sits above a partial AggregatingStep with the same sets (Planner.cpp, makeDistributed.cpp, applyParallelReplicas.cpp), and that step runs the same lines of tryPushDownFilter, so a remote() copy returns 0 even if only the merging step pushed the hint. Both halves of the fix fail their arm when reverted: whole fix 79/79, the None downgrade alone 0 -> 1.
2 💡 Extends 05401 instead of adding a new test noted 05401 is the test of the feature that regressed; additions only, no existing line changed.
3 💡 One hint input missing from a set keeps every derived hint of that filter above noted Never worse than 26.10 or the current master: neither pushes any of them there.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The SQL from #128072 on clickhouse local: unfixed master Granules: 78/78, master before #126219 and this PR 3/78, every run, same rows.
b Root cause explained? The derived hint's actions read the table column k of key intDiv(k, 10); #126219 added those columns to the GROUPING SETS check, k is in no set, so tryPushDownFilter returned 0 for the whole filter and v < 3 never reached the read.
c Fix matches root cause? Yes. The check is split at that site: a filter's own columns still gate it; a hint reading a column missing from a set only downgrades the push to PushDownIndexHints::None (existing mechanism, used for totals).
d Test intent preserved / new tests added? Yes. 05401 gains the issue's query, its result and a remote() variant; no existing line changed. The existing GROUPING SETS ((a, b), (b)) case is the negative control.
e Both directions demonstrated? Yes. Unfixed: the test fails only on the 3 new lines (79/79). Fixed: passes. With the downgrade removed, the existing case returns 1 instead of 0.
f Fix is general across code paths? Yes. AggregatingStep and MergingAggregatedStep share the branch (the remote() arm measures the latter). Plain GROUP BY, LIMIT BY, totals, group_by_use_nulls and ROLLUP/CUBE are unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. toDate(ts) range chain 79/79 to 5/79, equality chain to 2/79, OR-LIKE chain to 3/79, LowCardinality key to 3/79, identical rows; a key column missing from a set and group_by_use_nulls = 1 still stay above.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or protocol change; the regressed case behaves as on 26.10, every other case as on master.
i Invariants and contracts preserved? Yes. Below GROUPING SETS a pushed condition, and a pushed hint's pruning, still depend only on columns of every set; a hint-only filter under None pushes nothing. 50/50 randomized, 10/10 Replicated database.

Session id: cron:clickhouse-impl-slot-4:20261028-162700

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @nickitat @novikd, could you review this and add the can be tested label so CI runs? Since #126219 the GROUPING SETS check in tryPushDownFilter also counted the columns a derived indexHint reads (the table column k of a key intDiv(k, 10), which is in no set), so it rejected the whole HAVING filter; now such hint columns only downgrade the push to PushDownIndexHints::None, so the other conjuncts prune again and the hints stay above the aggregation.

@clickhouse-gh

clickhouse-gh Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b69d8bb]

Summary: ✅


AI Review

Summary

Reviewed PR head b69d8bb4799078c947ff6e3cb0f7b3735e2cbb6a. The change restores ordinary predicate pushdown below GROUPING SETS when a derived indexHint reads columns absent from some grouping sets, while retaining those hints above aggregation. No actionable correctness, safety, or performance findings.

Missing context / blind spots
  • ⚠️ Current-head CI is still running, and no local ClickHouse build was available to execute the regression test. Completion of functional CI for 05401_index_hint_derived_below_group_by would close this execution gap; the PR includes focused pruning and result assertions and reports prior repeated validation.
Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Oct 28, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing b69d8bb47 with master 7f7d60b3e (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.75, programs/clickhouse-keeper ×0.81 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 6.2 s 12.7 s +8.1 s (+175%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.4 s 7.8 s +5.2 s (+206%)
Compile time of recompiled translation units

7 translation units recompiled, 14 s compile time in total, 7 of them have a recent master baseline.

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - ec46cd5

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (arm_binary, parallel) / 05055_format_schema_source_query_background_access flaky, not from this PR (its diff does not touch 05055): the randomized session_timezone Africa/Khartoum puts the session date ahead of the server date, so the text_log filter misses the row; it passed 21 of 21 times without randomized settings. The tested merge tree predates the fix #127797 (mine, merged)
Mergeable Check, PR praktika rollup of the row above #127797 (the owner of the row above)
Sync - CH Inc sync (private, not actionable by me)

The flaky check passed the extended test 05401_index_hint_derived_below_group_by 50 of 50 times on all five builds. The earlier Config Workflow failures were the attempts before the can be tested label. CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261029-013003

groeneai and others added 2 commits November 12, 2026 23:04
…ets-hint-gate-128072

Resolve the conflict in filterPushDown.cpp: master rewrote findIdentifiersOfNode on top of visitReachableNodes; keep that form and route the implied indexHint inputs to the separate set.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The plain conjunct `v < 3` prunes to `3/79` on its own, so without a derived
hint the granule check passes whether or not the fix is present (measured with
`optimize_and_compare_chain = 0`). Assert the hint is there first, as the other
cases of this test already do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nickitat nickitat self-assigned this Nov 13, 2026
@nickitat
nickitat added this pull request to the merge queue Nov 13, 2026
Merged via the queue into ClickHouse:master with commit 8b65eed Nov 13, 2026
204 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 Nov 13, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 13, 2026
robot-ch-test-poll added a commit that referenced this pull request Nov 19, 2026
Cherry pick #128077 to 26.10: Do not let a derived `indexHint` keep `HAVING` above `GROUPING SETS`
robot-clickhouse added a commit that referenced this pull request Nov 19, 2026
robot-clickhouse added a commit that referenced this pull request Nov 19, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 19, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 19, 2026
Backport #128077 to 26.10: Do not let a derived `indexHint` keep `HAVING` above `GROUPING SETS`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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 v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GROUPING SETS HAVING loses primary-key pruning after #126219

6 participants