Skip to content

*: add statement summary for RUv2 and use float64 for calculations#67223

Merged
ti-chi-bot[bot] merged 9 commits into
pingcap:masterfrom
oh-my-tidb:ruv2-statement
Mar 23, 2026
Merged

*: add statement summary for RUv2 and use float64 for calculations#67223
ti-chi-bot[bot] merged 9 commits into
pingcap:masterfrom
oh-my-tidb:ruv2-statement

Conversation

@disksing
Copy link
Copy Markdown
Contributor

@disksing disksing commented Mar 23, 2026

What problem does this PR solve?

Issue Number: ref #67199

Problem Summary:
Add RUv2 totals to statement summaries and use float64 for RUv2 metrics calculations to improve precision.

What changed and how does it work?

  1. Add RUV2 totals (RUv2TotalCPUTimeMs and RUv2TotalReadRequestUnit) to statement summaries for better resource usage visibility.
  2. Change RUv2 metrics calculations from int64 to float64 to avoid precision loss in aggregation.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

Summary by CodeRabbit

  • New Features

    • Added Total Request Unit V2 tracking and AVG_REQUEST_UNIT_V2 / MAX_REQUEST_UNIT_V2 statement summary columns.
  • Improvements

    • RU v2 calculations and reporting now use floating-point values for higher precision; formatting consistently shows two decimal places across logs and runtime displays.
    • Statement summaries and result reporting include RU v2 totals for aggregation and max/avg tracking.
  • Tests

    • Expanded tests to cover RU v2 aggregation, formatting, summary columns, and reporting.

Record and aggregate Request-Unit V2 (RUV2) metrics across statement summaries. Introduces StmtExecInfo.TotalRUV2 and a helper calculateStatementTotalRUV2 to compute total RUV2 from metrics and RUDetails. StmtRUSummary now tracks SumRUV2 and MaxRUV2 (with Add and Merge updated), and summary readers/columns (v1 and v2) expose AVG_REQUEST_UNIT_V2 and MAX_REQUEST_UNIT_V2. Info schema columns for avg/max RUV2 were added, and related tests and record generation were updated to include TotalRUV2.

Signed-off-by: disksing <i@disksing.com>
Convert RU v2 related values from int64 to float64 across the codebase to preserve fractional RU values and avoid truncation. Key changes: RUV2 metrics CalculateRUValues/TotalRU now return float64; FormatRUV2Metrics prints floats with two decimals; runtime stats, slow log, resultset tracker, and statement summary fields updated to float64; info schema columns for RequestUnitV2 changed to DOUBLE; tests and summary reader/column factories adjusted accordingly. This ensures consistent handling and formatting of RUv2 totals (TiDB/TiKV/TiFlash) throughout the system.

Signed-off-by: disksing <i@disksing.com>
@ti-chi-bot ti-chi-bot Bot added the release-note-none Denotes a PR that doesn't merit a release note. label Mar 23, 2026
@pantheon-ai
Copy link
Copy Markdown

pantheon-ai Bot commented Mar 23, 2026

Review failed due to infrastructure/execution failure after retries. Please re-trigger review.

ℹ️ Learn more details on Pantheon AI.

@ti-chi-bot ti-chi-bot Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Mar 23, 2026
@tiprow
Copy link
Copy Markdown

tiprow Bot commented Mar 23, 2026

Hi @disksing. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 23, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

This PR converts RU v2 accounting from integer types to float64 across metrics, reporting, formatting, and statement-summary plumbing; adds a statement-level TotalRUV2 field; and exposes AVG_REQUEST_UNIT_V2 / MAX_REQUEST_UNIT_V2 columns and factories.

Changes

Cohort / File(s) Summary
RU V2 Core APIs
pkg/util/execdetails/ruv2_metrics.go
Switch RUv2 APIs and internal calculations to float64 (CalculateRUValues, TotalRU, FormatRUV2Metrics); use two-decimal float formatting and update function signatures.
Statement Exec & Adapter
pkg/executor/adapter.go, pkg/util/stmtsummary/statement_summary.go
Add helper to compute statement TotalRUV2; add StmtExecInfo.TotalRUV2 float64; propagate total into StmtRUSummary.Add and update RU aggregation (SumRUV2, MaxRUV2).
Info Schema & Column Factories
pkg/infoschema/tables.go, pkg/util/stmtsummary/reader.go, pkg/util/stmtsummary/v2/column.go
Add AVG_REQUEST_UNIT_V2 / MAX_REQUEST_UNIT_V2 constants, column entries, and factories; extend INFORMATION_SCHEMA.STATEMENTS_SUMMARY schema.
Record & Summary V2
pkg/util/stmtsummary/v2/record.go, pkg/util/stmtsummary/v2/record_test.go
Propagate TotalRUV2 into StmtRecord.Add; update test fixtures and assertions for SumRUV2/MaxRUV2.
Cursor RU Tracker & Resultset
pkg/server/internal/resultset/resultset.go
Change CursorRUV2Tracker.reportedTiDBRU from int64float64; compute and report TiDB RU deltas as floats.
Runtime Display & Slow Log
pkg/util/execdetails/runtime_stats.go, pkg/sessionctx/variable/slow_log.go
Change temporary TiKV/TiFlash RU vars to float64, remove integer casts, and update formatted outputs to show two decimal places.
Tests & Small Fixes
pkg/util/execdetails/execdetails_test.go, pkg/executor/explain_unit_test.go, pkg/util/stmtsummary/statement_summary_test.go, pkg/sessionctx/variable/tests/session_test.go
Adjust tests and expected strings to account for float RU values and decimal formatting; replace exact integer assertions with epsilon checks where appropriate.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

component/statistics, size/XXL

Suggested reviewers

  • yibin87
  • AilinKid
  • XuHuaiyu
  • JmPotato

Poem

🐰 I swapped my ints for floats today,
totals now shimmer with two decimals' sway,
summaries gather each fractional clue,
AVG and MAX bloom in the view —
hop, nibble, tests pass — hooray! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 35.71% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main changes: adding statement summary for RUv2 and converting RUv2 metrics calculations to float64 for improved precision.
Description check ✅ Passed The description includes the required sections with an issue reference (ref #67199), a clear problem statement, detailed explanation of changes, completed test checklist, and release notes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@hawkingrei
Copy link
Copy Markdown
Member

/ok-to-test

@ti-chi-bot ti-chi-bot Bot added the ok-to-test Indicates a PR is ready to be tested. label Mar 23, 2026
@disksing disksing changed the title *: add statement-level RUv2 totals and use float64 for calculations *: add statement summary for RUv2 and use float64 for calculations Mar 23, 2026
@disksing disksing requested review from XuHuaiyu and nolouch and removed request for XuHuaiyu March 23, 2026 08:34
Signed-off-by: disksing <i@disksing.com>
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
pkg/util/stmtsummary/v2/record.go (1)

713-713: Use a fractional TotalRUV2 fixture value to better protect float64 behavior.

Line 713 currently uses an integer-like value, which doesn’t exercise fractional RUv2 handling after the int64→float64 transition.

♻️ Suggested fixture tweak
-		TotalRUV2:         12345,
+		TotalRUV2:         12345.67,
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/util/stmtsummary/v2/record.go` at line 713, The TotalRUV2 fixture value
is currently an integer-like literal which won't exercise fractional float64
behavior; update the struct literal field TotalRUV2 in
pkg/util/stmtsummary/v2/record.go (the record/fixture where TotalRUV2 is set) to
use a fractional value (for example 12345.67) so the float64 path is exercised
after the int64→float64 transition.
pkg/util/stmtsummary/v2/record_test.go (1)

66-67: Harden RUv2 float assertions and cover merged max value.

For float64 aggregation checks, tolerant assertions reduce brittleness; also add a MaxRUV2 merge assertion to cover both aggregate dimensions.

🧪 Suggested test refinement
-	require.Equal(t, info.TotalRUV2, record1.MaxRUV2)
-	require.Equal(t, info.TotalRUV2, record1.SumRUV2)
+	require.InDelta(t, info.TotalRUV2, record1.MaxRUV2, 1e-9)
+	require.InDelta(t, info.TotalRUV2, record1.SumRUV2, 1e-9)
...
-	require.Equal(t, info.TotalRUV2*2, record2.SumRUV2)
+	require.InDelta(t, info.TotalRUV2*2, record2.SumRUV2, 1e-9)
+	require.InDelta(t, info.TotalRUV2, record2.MaxRUV2, 1e-9)

Also applies to: 83-83

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/util/stmtsummary/v2/record_test.go` around lines 66 - 67, Replace brittle
require.Equal float assertions in pkg/util/stmtsummary/v2/record_test.go with
tolerant assertions (e.g., require.InDelta or require.InEpsilon) when comparing
info.TotalRUV2 to record1.SumRUV2 and record1.MaxRUV2; specifically, change the
comparisons that reference info.TotalRUV2 vs record1.SumRUV2 and the one against
record1.MaxRUV2 to use a small delta/epsilon, and add an assertion that
record1.MaxRUV2 matches the expected merged max (compare record1.MaxRUV2 to
info.MaxRUV2) using the same tolerant matcher so the merged max dimension is
covered.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@pkg/util/execdetails/execdetails_test.go`:
- Around line 363-370: The test uses type-strict require.Equal on RU floats and
mismatched string formatting; update the assertions to compare floats (convert
expected ints to float64 or use require.InEpsilon/require.EqualValues) for
tidbRU (from metrics.CalculateRUValues) and totalRU (from metrics.TotalRU), and
adjust TestFormatRUV2MetricsIncludesRUValuesFirst expected output strings to
match FormatRUV2Metrics' "%.2f" formatting (i.e., include ".00" decimal
suffixes) so the string comparisons align with the float formatting.

In `@pkg/util/execdetails/ruv2_metrics.go`:
- Around line 437-439: Update the CalculateRUValues doc comment on RUV2Metrics
to accurately describe its behavior and return type: state that
CalculateRUValues(weights RUV2Weights) computes and returns the current TiDB RU
as a float64 (not a scaled integer), mention that the argument is RUV2Weights
used to weight components, and keep the comment as an exported-symbol doc
comment directly above the function declaration for clarity and tooling.

---

Nitpick comments:
In `@pkg/util/stmtsummary/v2/record_test.go`:
- Around line 66-67: Replace brittle require.Equal float assertions in
pkg/util/stmtsummary/v2/record_test.go with tolerant assertions (e.g.,
require.InDelta or require.InEpsilon) when comparing info.TotalRUV2 to
record1.SumRUV2 and record1.MaxRUV2; specifically, change the comparisons that
reference info.TotalRUV2 vs record1.SumRUV2 and the one against record1.MaxRUV2
to use a small delta/epsilon, and add an assertion that record1.MaxRUV2 matches
the expected merged max (compare record1.MaxRUV2 to info.MaxRUV2) using the same
tolerant matcher so the merged max dimension is covered.

In `@pkg/util/stmtsummary/v2/record.go`:
- Line 713: The TotalRUV2 fixture value is currently an integer-like literal
which won't exercise fractional float64 behavior; update the struct literal
field TotalRUV2 in pkg/util/stmtsummary/v2/record.go (the record/fixture where
TotalRUV2 is set) to use a fractional value (for example 12345.67) so the
float64 path is exercised after the int64→float64 transition.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: d7c91117-35c8-4d5c-af74-cb513e0cc61b

📥 Commits

Reviewing files that changed from the base of the PR and between 362aeba and 59e5a07.

📒 Files selected for processing (14)
  • pkg/executor/adapter.go
  • pkg/executor/explain_unit_test.go
  • pkg/infoschema/tables.go
  • pkg/server/internal/resultset/resultset.go
  • pkg/sessionctx/variable/slow_log.go
  • pkg/util/execdetails/execdetails_test.go
  • pkg/util/execdetails/runtime_stats.go
  • pkg/util/execdetails/ruv2_metrics.go
  • pkg/util/stmtsummary/reader.go
  • pkg/util/stmtsummary/statement_summary.go
  • pkg/util/stmtsummary/statement_summary_test.go
  • pkg/util/stmtsummary/v2/column.go
  • pkg/util/stmtsummary/v2/record.go
  • pkg/util/stmtsummary/v2/record_test.go

Comment thread pkg/util/execdetails/execdetails_test.go Outdated
Comment thread pkg/util/execdetails/ruv2_metrics.go Outdated
Comment thread pkg/executor/adapter.go
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 23, 2026

Codecov Report

❌ Patch coverage is 94.64286% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.7652%. Comparing base (28ea978) to head (3d763f0).
⚠️ Report is 7 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #67223        +/-   ##
================================================
+ Coverage   77.7535%   79.7652%   +2.0116%     
================================================
  Files          2022       1968        -54     
  Lines        554209     545476      -8733     
================================================
+ Hits         430917     435100      +4183     
+ Misses       121550     108913     -12637     
+ Partials       1742       1463       -279     
Flag Coverage Δ
integration 46.9919% <75.0000%> (-1.1346%) ⬇️
unit 77.0165% <94.6428%> (+0.7246%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 61.5065% <ø> (ø)
parser ∅ <ø> (∅)
br 66.3893% <ø> (+5.5336%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread pkg/util/execdetails/ruv2_metrics.go
Comment thread pkg/util/execdetails/execdetails_test.go Outdated
- Fix float comparisons in TestRUV2MetricsSnapshotCalculateRUValues by
  using require.InEpsilon instead of require.Equal for float64 values
- Fix expected string values in TestFormatRUV2MetricsIncludesRUValuesFirst
  to match FormatRUV2Metrics' "%.2f" formatting (include decimal suffixes)
- Fix CalculateRUValues doc comment to accurately describe return type
  as float64 instead of incorrect "scaled integer"

Signed-off-by: disksing <i@disksing.com>
Update expected string in TestSlowLogFormatIncludesTiFlashRUInRUV2Metrics
to match FormatRUV2Metrics' "%.2f" formatting (include ".00" decimal
suffixes) for total_ru, tidb_ru, tikv_ru, and tiflash_ru values.

Signed-off-by: disksing <i@disksing.com>
Signed-off-by: disksing <i@disksing.com>
Copy link
Copy Markdown
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the latest head again. The follow-up commits fixed the test/assertion issues and updated the doc comment. I reran the targeted tests for the updated cases locally and they passed.\n\nThe remaining cursor stmt-summary RUv2 undercount is acknowledged in-thread and intentionally kept consistent with current RU v1 behavior for this PR. No other issues from my side.

@ti-chi-bot ti-chi-bot Bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Mar 23, 2026
Copy link
Copy Markdown
Member

@nolouch nolouch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ti-chi-bot ti-chi-bot Bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Mar 23, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented Mar 23, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-03-23 10:58:54.173224653 +0000 UTC m=+179530.209294903: ☑️ agreed by XuHuaiyu.
  • 2026-03-23 11:10:21.293576391 +0000 UTC m=+180217.329646651: ☑️ agreed by nolouch.

Fix linter warning: unnecessary conversion (unconvert) at line 1732

Signed-off-by: disksing <i@disksing.com>
Fix linter warning: unnecessary conversion (unconvert) at line 497

Signed-off-by: disksing <i@disksing.com>
Fix linter warning: unnecessary conversion (unconvert) at line 203

Signed-off-by: disksing <i@disksing.com>
@disksing
Copy link
Copy Markdown
Contributor Author

/test unit-test

@tiprow
Copy link
Copy Markdown

tiprow Bot commented Mar 23, 2026

@disksing: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

/test fast_test_tiprow
/test tidb_parser_test

Use /test all to run all jobs.

Details

In response to this:

/test unit-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented Mar 23, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: nolouch, wjhuang2016, XuHuaiyu

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added the approved label Mar 23, 2026
@disksing
Copy link
Copy Markdown
Contributor Author

/test check_dev

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented Mar 23, 2026

@disksing: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

/test build
/test check-dev
/test check-dev2
/test mysql-test
/test pull-br-integration-test
/test pull-build-next-gen
/test pull-integration-e2e-test
/test pull-integration-realcluster-test-next-gen
/test pull-lightning-integration-test
/test pull-mysql-client-test
/test pull-mysql-client-test-next-gen
/test pull-unit-test-ddlv1
/test pull-unit-test-next-gen
/test unit-test

The following commands are available to trigger optional jobs:

/test pingcap/tidb/canary_ghpr_unit_test
/test pull-br-integration-test-next-gen
/test pull-check-deps
/test pull-common-test
/test pull-e2e-test
/test pull-error-log-review
/test pull-integration-common-test
/test pull-integration-copr-test
/test pull-integration-ddl-test
/test pull-integration-ddl-test-next-gen
/test pull-integration-e2e-test-next-gen
/test pull-integration-jdbc-test
/test pull-integration-mysql-test
/test pull-integration-nodejs-test
/test pull-integration-python-orm-test
/test pull-mysql-test-next-gen
/test pull-sqllogic-test
/test pull-tiflash-integration-test

Use /test all to run the following jobs that were automatically triggered:

pingcap/tidb/ghpr_build
pingcap/tidb/ghpr_check
pingcap/tidb/ghpr_check2
pingcap/tidb/ghpr_mysql_test
pingcap/tidb/ghpr_unit_test
pingcap/tidb/pull_br_integration_test
pingcap/tidb/pull_build_next_gen
pingcap/tidb/pull_integration_e2e_test
pingcap/tidb/pull_integration_realcluster_test_next_gen
pingcap/tidb/pull_lightning_integration_test
pingcap/tidb/pull_mysql_client_test
pingcap/tidb/pull_mysql_client_test_next_gen
pingcap/tidb/pull_unit_test_next_gen
pull-check-deps
pull-error-log-review
Details

In response to this:

/test check_dev

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@tiprow
Copy link
Copy Markdown

tiprow Bot commented Mar 23, 2026

@disksing: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

/test fast_test_tiprow
/test tidb_parser_test

Use /test all to run all jobs.

Details

In response to this:

/test check_dev

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@disksing
Copy link
Copy Markdown
Contributor Author

/test check-dev

@tiprow
Copy link
Copy Markdown

tiprow Bot commented Mar 23, 2026

@disksing: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

/test fast_test_tiprow
/test tidb_parser_test

Use /test all to run all jobs.

Details

In response to this:

/test check-dev

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

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

Labels

approved lgtm ok-to-test Indicates a PR is ready to be tested. release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants