Skip to content

Fix stack overflow in quantileDD merge - #86560

Merged
shankar-iyer merged 5 commits into
ClickHouse:masterfrom
raphael-theriault-swi:ddsketch-merge
Oct 8, 2025
Merged

shankar-iyer merged 5 commits into
ClickHouse:masterfrom
raphael-theriault-swi:ddsketch-merge

Conversation

@raphael-theriault-swi

Copy link
Copy Markdown
Contributor

The current quantileDD merge implementation can recurse infinitely and cause a stack overflow when the computed gammas are not perfectly equal. This can be triggered by manually submitting data with varying relative accuracy as the AggregateFunction state. It could also feasibly be triggered without doing so if a database were migrated to different hardware where the computed gamma for the same relative accuracy wasn't bitwise identical to the one on the previous hardware.

This happens because when merging two states where the gammas are not bitwise identical, the implementation changes the gamma of one of the states to match the other, then recurses. However, it's possible that at the end of the process (due to floating point imprecision) the two states still don't have bitwise identical gammas, which causes the same path to be taken and the function to recurse until it overflows the stack.

This logic was changed to accept a margin of error (where the margin is larger for lower precision gammas) instead of checking for bitwise equality, so that two states with gammas that are not bitwise identical but are the same in practice will get merged as is. A test was added which passes with the new fixes but segfaults without.

I also had format-on-save enabled which caused a bit of noise, I can revert the formatting only changes if requested, I just figured I'd leave them in given they only affect a few files and the diff is still readable.

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC) or LOGICAL_ERROR

Changelog entry (a [user-readable short description]

Fixes a crash where some valid user-submitted data to an AggregateFunction(quantileDD) column could cause merges to recurse infinitely.

@CLAassistant

CLAassistant commented Sep 2, 2025 •

Copy link
Copy Markdown

CLA signed.

@bharatnc bharatnc added the can be tested Allows running workflows for external contributors label Sep 2, 2025
@clickhouse-gh

clickhouse-gh Bot commented Sep 2, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a9e70da]

Summary: ❌

job_name test_name status info comment
Bugfix validation (functional tests) failure

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-cloud labels Sep 2, 2025
@shankar-iyer shankar-iyer self-assigned this Sep 4, 2025
@shankar-iyer

Copy link
Copy Markdown
Member

@raphael-theriault-swi Thanks for the PR!

This can be triggered by manually submitting data with varying relative accuracy

Is it possible to write a SQL testcase for this?

@raphael-theriault-swi

Copy link
Copy Markdown
Contributor Author

@shankar-iyer Yeah will do, sorry I was waiting for the CCLA to be signed on our side :)

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@raphael-theriault-swi

raphael-theriault-swi commented Oct 2, 2025 •

Copy link
Copy Markdown
Contributor Author

@shankar-iyer Added SQL test as requested

Edit: It appears I'm not able to reproduce the bug with an SQL test. For reference this was extremely hard to track down as the bug was triggered by an L2 merge and corrupted our database, which we only realized when L3 merges started failing, which forced us to clear the affected column. So I don't have any real data for an SQL reproduction, but I was still able to trigger a segfault with synthetic data in the gtest. I hope this can be sufficient given the circumstances and just how hard this bug is to trigger to begin with.

@shankar-iyer

Copy link
Copy Markdown
Member

@shankar-iyer Added SQL test as requested

Edit: It appears I'm not able to reproduce the bug with an SQL test. For reference this was extremely hard to track down as the bug was triggered by an L2 merge and corrupted our database, which we only realized when L3 merges started failing, which forced us to clear the affected column. So I don't have any real data for an SQL reproduction, but I was still able to trigger a segfault with synthetic data in the gtest. I hope this can be sufficient given the circumstances and just how hard this bug is to trigger to begin with.

Thanks! I will review in the next 2 days.

@shankar-iyer
shankar-iyer added this pull request to the merge queue Oct 8, 2025
Merged via the queue into ClickHouse:master with commit eea0282 Oct 8, 2025
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 8, 2025
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added pr-backports-created-cloud deprecated label, NOOP pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Oct 8, 2025
@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 Oct 8, 2025
clickhouse-gh Bot added a commit that referenced this pull request Oct 8, 2025
Backport #86560 to 25.9: Fix stack overflow in quantileDD merge
clickhouse-gh Bot added a commit that referenced this pull request Oct 8, 2025
Backport #86560 to 25.7: Fix stack overflow in quantileDD merge
clickhouse-gh Bot added a commit that referenced this pull request Oct 8, 2025
Backport #86560 to 25.8: Fix stack overflow in quantileDD merge
alexey-milovidov added a commit that referenced this pull request Oct 16, 2025
Backport #86560 to 25.3: Fix stack overflow in quantileDD merge
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 pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-backports-created-cloud deprecated label, NOOP pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

7 participants