Skip to content

fix: Change count metric from signed to unsigned (int64_t -> uint64_t)#27276

Closed
peterenescu wants to merge 1 commit into
prestodb:masterfrom
peterenescu:export-D95451420
Closed

fix: Change count metric from signed to unsigned (int64_t -> uint64_t)#27276
peterenescu wants to merge 1 commit into
prestodb:masterfrom
peterenescu:export-D95451420

Conversation

@peterenescu

@peterenescu peterenescu commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

Summary:
Use an unsigned type for the count metric while keeping int64_t for value
metrics. When the unit is kNone, negative values can be valid such as for delta-
type metrics, so int64_t remains appropriate. In contrast, count should
always be non-negative. This PR also addresses potential overflow when
converting unsigned metrics to RuntimeMetrics.

X-link: facebookincubator/velox#15536

Reviewed By: Yuhta

Differential Revision: D95451420

Pulled By: peterenescu

Summary by Sourcery

Bug Fixes:

  • Guard the runtime metric count conversion with saturation to handle unsigned count values safely.

Summary:
Use an unsigned type for the count metric while keeping int64_t for value 
metrics. When the unit is kNone, negative values can be valid such as for delta-
type metrics, so int64_t remains appropriate. In contrast, `count` should 
always be non-negative. This PR also addresses potential overflow when 
converting unsigned metrics to `RuntimeMetrics`.

X-link: facebookincubator/velox#15536

Reviewed By: Yuhta

Differential Revision: D95451420

Pulled By: peterenescu
@peterenescu peterenescu requested review from a team as code owners March 6, 2026 05:43
@sourcery-ai

sourcery-ai Bot commented Mar 6, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Changes the Presto runtime metric conversion to treat the metric count as an unsigned value and safely cast it when building a RuntimeMetric, preventing overflow issues.

Class diagram for updated metric count handling

classDiagram
  class Metric {
    <<struct>>
    RuntimeUnit unit
    int64_t sum
    uint64_t count
    int64_t max
    int64_t min
  }

  class RuntimeMetric {
    <<struct>>
    RuntimeUnit unit
    int64_t sum
    int64_t count
    int64_t max
    int64_t min
  }

  class PrestoTask {
    +protocol_RuntimeMetric toRuntimeMetric(string name, Metric metric)
  }

  class RuntimeMetricsUtils {
    +int64_t saturateCast(uint64_t value)
    +protocol_RuntimeUnit toPrestoRuntimeUnit(RuntimeUnit unit)
  }

  PrestoTask --> Metric : uses
  PrestoTask --> RuntimeMetric : builds
  PrestoTask ..> RuntimeMetricsUtils : calls
  Metric --> RuntimeMetric : converted_via_toRuntimeMetric
  RuntimeMetricsUtils <.. RuntimeMetric : safe_cast_for_count
Loading

Flow diagram for safe casting of metric count to RuntimeMetric

flowchart LR
  A["Metric.count (uint64_t)"] --> B["saturateCast(count)"]
  B --> C["RuntimeMetric.count (int64_t)"]
Loading

File-Level Changes

Change Details Files
Safely cast unsigned metric counts when converting to RuntimeMetric to avoid overflow while keeping value metrics signed.
  • Include the RuntimeMetrics header to access casting utilities.
  • Wrap the metric count field in a saturating cast when constructing the RuntimeMetric.
  • Leave sum, max, and min fields unchanged as signed values.
presto-native-execution/presto_cpp/main/PrestoTask.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

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.

Hey - I've left some high level feedback:

  • If saturateCast is defined in a more specific header than RuntimeMetrics.h, consider including that directly instead to avoid an unnecessary dependency on the runtime metrics header from PrestoTask.cpp.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- If `saturateCast` is defined in a more specific header than `RuntimeMetrics.h`, consider including that directly instead to avoid an unnecessary dependency on the runtime metrics header from `PrestoTask.cpp`.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@rui-mo

rui-mo commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Hi @peterenescu, sorry if I misunderstood something earlier. Based on the #27295 (comment), I assumed that the correct fix is the one implemented in this PR. Would you like to reopen this PR so we can proceed with the fix in Presto, or would you prefer that I follow up with the changes? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants