Refactor midpoint function and fix many logical errors, overflow and functional bugs#92102
Merged
nihalzp merged 17 commits intoClickHouse:masterfrom Dec 17, 2025
Merged
Refactor midpoint function and fix many logical errors, overflow and functional bugs#92102nihalzp merged 17 commits intoClickHouse:masterfrom
midpoint function and fix many logical errors, overflow and functional bugs#92102nihalzp merged 17 commits intoClickHouse:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the midpoint function to fix multiple critical bugs including incorrect handling of nullable types, mixed numeric type conversions, inconsistent rounding behavior, and integer overflow issues. The fix implements proper nullable-aware semantics that ignore NULL values per row and uses wider integer types during summation to prevent overflow.
Key changes:
- Reimplements nullable handling to correctly ignore NULL arguments per row instead of treating all rows as NULL
- Fixes integer overflow by using wider types (Int128/UInt128/Int256/UInt256) during summation before division
- Corrects rounding behavior for signed integers to consistently truncate toward zero
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Functions/midpoint.h | Complete refactor of midpoint logic: adds nullable-aware calculation paths, overflow-safe summation using wider types, and consistent truncation-toward-zero rounding for integers |
| src/Functions/avg2.cpp | Simplifies avg2 (2-arg alias) by removing duplicate type resolution logic now handled by base class |
| tests/queries/0_stateless/03710_midpoint.sql | Adds comprehensive test coverage for nullable types, overflow cases, mixed types, and edge cases |
| tests/queries/0_stateless/03710_midpoint.reference | Expected results for new test cases |
| tests/queries/0_stateless/03710_midpoint_jit.sql | Adds JIT compilation test coverage for both positive and negative number cases |
| tests/queries/0_stateless/03710_midpoint_jit.reference | Expected results for JIT tests |
Contributor
|
Workflow [PR], commit [ac5b006] Summary: ⏳
|
antonio2368
reviewed
Dec 15, 2025
antonio2368
approved these changes
Dec 17, 2025
Member
Author
Merged
via the queue into
ClickHouse:master
with commit Dec 17, 2025
0564323
125 of 130 checks passed
robot-ch-test-poll4
added a commit
that referenced
this pull request
Dec 17, 2025
Cherry pick #92102 to 25.11: Refactor `midpoint` function and fix many logical errors, overflow and functional bugs
robot-clickhouse
added a commit
that referenced
this pull request
Dec 17, 2025
…ogical errors, overflow and functional bugs
clickhouse-gh bot
added a commit
that referenced
this pull request
Dec 17, 2025
Backport #92102 to 25.11: Refactor `midpoint` function and fix many logical errors, overflow and functional bugs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Logical error:
Wrong result for mixed numeric types:
Outputs
100.05but should be150.Inconsistent rounding behavior:
Overflow:
Returns
3074457345618258601, but should be9223372036854775807.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix numerous logical errors, overflow and functional bugs in
midpointfunction. Closes #91816.Documentation entry for user-facing changes