Skip to content

Do not claim that dictGetHierarchy is injective - #117357

Merged
alexey-milovidov merged 5 commits into
masterfrom
fix-dict-get-hierarchy-not-injective-117225
Sep 7, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
fix-dict-get-hierarchy-not-injective-117225

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed GROUP BY dictGetHierarchy(...) returning duplicate groups for keys that are absent from the dictionary. dictGetHierarchy is no longer declared injective.

Documentation entry for user-facing changes

dictGetHierarchy returns an empty array for every key the dictionary does not have, so two distinct absent keys collide on [] and the function is not injective. The default-on optimize_injective_functions_in_group_by trusted the claim, dropped the function from GROUP BY and grouped by the raw key instead, so a query returned one [] row per absent key where the correct answer is a single group:

SELECT dictGetHierarchy('d', k) AS h, count()
FROM (SELECT arrayJoin([toUInt64(100), toUInt64(200)]) AS k)
GROUP BY h;
-- two rows ([], 1) and ([], 1); the truth is one row ([], 2)

The claim would only hold for keys the dictionary has, where the returned hierarchy starts with the key itself. dictGetChildren and dictGetDescendants never claimed injectivity, and the dictGet family forwards the user-declared INJECTIVE attribute flag, which is by design.

Closes: #117225


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

Version info

  • Merged into: 26.9.1.879 (included in 26.9 and later)

`dictGetHierarchy` returns an empty array for every key the dictionary does
not have, so two distinct absent keys collide on `[]` and the function is not
injective. The default-on `optimize_injective_functions_in_group_by` trusted
the claim, dropped the function from `GROUP BY` and grouped by the raw key
instead, so a query returned one `[]` row per absent key where the correct
answer is a single group:

    SELECT dictGetHierarchy('d', k) AS h, count()
    FROM (SELECT arrayJoin([toUInt64(100), toUInt64(200)]) AS k)
    GROUP BY h;
    -- two rows ([], 1) and ([], 1); the truth is one row ([], 2)

The claim would only hold for keys the dictionary has, where the returned
hierarchy starts with the key itself. `dictGetChildren` and
`dictGetDescendants` never claimed injectivity, and the `dictGet` family
forwards the user-declared `INJECTIVE` attribute flag, which is by design.

Closes: #117225
@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fc0f2aa]

Summary: ✅


AI Review

Summary

This PR fixes the actual bug in the implementation by stopping dictGetHierarchy from advertising injectivity, which is the right change for the wrong-results case in GROUP BY on absent dictionary keys. The remaining concern is the new regression coverage: the added uniqExact assertions do not execute any injective-elimination path affected by this change, so one of the real sibling consumers is still unpinned.

Findings

⚠️ Majors

  • [tests/queries/0_stateless/05099_dict_get_hierarchy_not_injective.sql:24] The new uniqExact coverage is ineffective because both analyzer and legacy uniq* injective-elimination paths only unwrap one-argument functions. dictGetHierarchy('d_hier', k) always keeps the constant dictionary-name argument, so these assertions would stay green even without this PR. The affected sibling surface here is LIMIT BY, whose injective rewrite uses the same multi-argument helper as GROUP BY and would also return too many rows for absent keys. Suggested fix: replace or supplement the uniqExact case with LIMIT 1 BY dictGetHierarchy('d_hier', k) and an optimize_injective_functions_in_limit_by = 0 control query.
Final Verdict

⚠️ Changes requested: the code fix looks correct, but the remaining regression test should cover an optimization path that this isInjective change actually affects.

LLVM Coverage Report

Measured on commit fc0f2aa.

Metric Baseline Current Δ
Lines 88.80% 88.80% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.10% 81.10% +0.00%

Changed lines: Changed C/C++ lines covered: 3/4 (75.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 31, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing fc0f2aa92 with master 5a66fff99 (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.

Compile time of recompiled translation units

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

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-dictionary Dictionaries (in-memory key-value, periodically refreshed from sources). label Sep 4, 2026
alexey-milovidov and others added 2 commits September 6, 2026 09:33
…it 05087 -> 05099

The old-analyzer stateless job failed with `EXPLAIN QUERY TREE is only supported with the analyzer`
(NOT_IMPLEMENTED) because the test used `EXPLAIN QUERY TREE` to assert that `dictGetHierarchy` is no
longer eliminated from `GROUP BY`. Set `enable_analyzer = 1` right before that query; the result rows
above it are still checked under both analyzers.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117357&sha=6fddd35aa8b23f9da036a5195964f7bd8aba312e&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20old%20analyzer%2C%20s3%20storage%2C%20DBReplicated%2C%20parallel%2C%202%2F3%29
PR: #117357

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…limination in the test

The old analyzer removes injective functions from `GROUP BY` in `TreeOptimizer`, independently of
the query-tree pass, so the test now has absent-key `GROUP BY` and `uniqExact` cases with
`SETTINGS enable_analyzer = 0`. On a binary without the fix they return one `[]` row per absent key.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The two stateless reds on 0b21535 are unrelated to this one-line isInjective change (both are distributed-plan tests, neither touches dictionaries):

@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117357&sha=0b215350b2cf3ee035eb4d9e4a8207c2283ee326&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20azure%2C%20sequential%29 (04340_distributed_plan_status_reenqueue_fault) and provide a fix in a separate PR. If the fix is already in progress, link it here.

The integration shard amd_asan_ubsan, db disk, old analyzer, 1/6 errored with session-timeout occurred during test execution before running any test, which is the runner, not this change.

SETTINGS enable_analyzer = 0;
SELECT dictGetHierarchy('d_hier', k) AS h, count() FROM (SELECT arrayJoin([toUInt64(100), toUInt64(200)]) AS k) GROUP BY h
SETTINGS enable_analyzer = 0;
SELECT uniqExact(dictGetHierarchy('d_hier', k)) FROM (SELECT arrayJoin([toUInt64(100), toUInt64(200), toUInt64(300)]) AS k)

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.

The new uniqExact checks do not actually exercise the contract changed by this PR. Both uniq* injective-elimination paths only unwrap single-argument functions, so dictGetHierarchy('d_hier', k) is never simplified there because the constant dictionary name is still a second argument. These rows would stay green even on the pre-fix code.

The affected sibling surface here is LIMIT BY: OptimizeLimitByInjectiveFunctionsPass uses the same multi-argument unwrapInjectiveFunctionsInKeys helper as GROUP BY, so LIMIT 1 BY dictGetHierarchy('d_hier', k) would also return too many rows for absent keys before this fix. Please replace or supplement the uniqExact coverage with that case, plus an optimize_injective_functions_in_limit_by = 0 control query if you want the same ground-truth pattern.

@groeneai

groeneai commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

The 04340 fix is in the PR you already linked. Besides the rethrow reorder, #118417 also changes src/Common/FailPoint.cpp: distributed_plan_record_failure_while_starting_tasks goes from REGULAR to ONCE, i.e. FIU_ONETIME, self-disarming after the first injection, which is the only one 04648 needs. That is exactly the leak behind reenqueue fault handled: 0, and that PR's own description walks the cascade. So there is nothing separate for me to open.

Two things to add.

The class-level fix is #105942, open since 2026-05-27 and approved by @ hanfei1991 on 2026-06-01: after each no-parallel/sequential test it disables whatever is still enabled in system.fail_points. 04648 is not the only test with that shape. On master, 9 other stateless .sql tests arm a REGULAR failpoint before a statement carrying a serverError hint, so an unexpected code there aborts the file before the DISABLE and leaks the same way: 02910_prefetch_unexpceted_exception, 02919_insert_meet_eternal_hardware_error, 04077_wide_part_writer_cancel_on_exception, 04113_compact_part_writer_cancel_on_exception, 04209_statistics_retry_load, 04653_atomic_populate_materialized_view_rollback_on_error, 04826_parallel_insert_sinks_too_many_parts_self_race, 04871_text_index_lazy_always_false_on_cancel, 05076_merge_refused_before_pending_rename_column.

"Passed 3/3 reruns" is not evidence against the leak: the rerun is a separate clickhouse-test invocation that replays only the failed tests from the saved artifact (tests/clickhouse-test:6732-6739), so 04648 does not precede 04340 there, and 04648's own reruns reach its DISABLE.

On #118390: it removes the same trigger by a different route, giving the final result reader the query's cancellation so readFromSocket consults the recorded cause before rethrowing the socket error. #118417 is the smaller change and carries the failpoint flip as well, so I am happy to close mine in favour of it. Your call.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 7, 2026
Merged via the queue into master with commit 3d6a0d1 Sep 7, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-dict-get-hierarchy-not-injective-117225 branch September 7, 2026 00:51
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-dictionary Dictionaries (in-memory key-value, periodically refreshed from sources). pr-bugfix Pull request with bugfix, not backported by default 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.

False isInjective claim of dictGetHierarchy makes GROUP BY return duplicate groups for keys absent from the dictionary

4 participants