← Back to Pull requests

MDEV-40254 Preserve large ROUND results in internal temporary tables #5775

Open
DerZc opened this pull request with 30 lines diff across 4 files
View on GitHub
DerZc DerZc commented on Sep 28, 2026 06:42 UTC
Author

ROUND() has already applied the requested scale to its DOUBLE result. Its internal temporary field nevertheless treats the display width and scale as storage limits, so materialization can clip large rounded values and change grouped query results.

Mark the internal DOUBLE field's scale as unfixed after ROUND() has computed its result, allowing the full floating-point range to be stored.

Regression coverage

Add one grouped query to main.func_math over squared BIGINT values. Two rows are necessary to exercise temporary-table storage; a one-row probe already passes on the unmodified server. It checks that the exact value 100000000000000000000 and its row count survive temporary-table storage. The five-line regression needs no self-join, extra SET, database creation, engine setting, manual prepared statements or duplicate queries.

Validation

On current 11.4 at f49e838d367f2154b3edb43effa99ff9eb0d8602:

  • The unmodified server fails main.func_math only on the new query, returning 100000000000000000 instead of 100000000000000000000.

  • The patched Release build succeeds.

  • Normal protocol: main.func_math, main.func_group, main.group_by, main.select, main.type_decimal, main.type_float and main.view pass.

  • Prepared-statement protocol: main.func_math passes.

  • View protocol: main.func_math passes.

  • Expected values are independently derived with integer arithmetic and checked to be exactly representable as DOUBLE.

  • The full database regression suite was not run.
  • Bug report: https://jira.mariadb.org/browse/MDEV-40254

    Diff: 30 lines (+30 / -0) across 4 files Reviewer: gkodinov Review duration: 25613m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.28) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.28 · 30 lines diff across 4 files
    0.28
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.91 · 2 substantive comment(s)
    0.91
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.205
    Real PR Conversation Timeline
    5 items
    gkodinov gkodinov commented on code on Sep 28, 2026 10:18 UTC
    Code Review
    mysql-test/main/func_math.result:1
    @@ -4100,3 +4100,69 @@ SELECT ROUND(NAME_CONST('x', 0x0123456789ABCDEF0123456789ABCDEF0123456789AB)) as
     a
     14839080548208970155
     # End of 10.11 tests
    +#
    +# Start of 11.4 tests
    +#
    +# MDEV-40254 Wrong result with POWER(BIGINT, 2) through VIEW/CTE and ROUND/GROUP BY
    +#
    +SET @save_optimizer_switch=@@optimizer_switch;

    Do you need all of these SET statements? Ditto the CREATE DATABASE? Please always strive towards the minimal and the simplest test case.

    gkodinov gkodinov commented on code on Sep 28, 2026 10:20 UTC
    Code Review
    mysql-test/main/func_math.test:1
    @@ -2225,3 +2225,59 @@ DROP TABLE t1;
     SELECT ROUND(NAME_CONST('x', 0x0123456789ABCDEF0123456789ABCDEF0123456789AB)) as a;
     
     --echo # End of 10.11 tests
    +
    +--echo #
    +--echo # Start of 11.4 tests
    +--echo #
    +--echo # MDEV-40254 Wrong result with POWER(BIGINT, 2) through VIEW/CTE and ROUND/GROUP BY
    +--echo #
    +SET @save_optimizer_switch=@@optimizer_switch;
    +SET @save_sql_mode=@@sql_mode;
    +SET @save_default_storage_engine=@@default_storage_engine;
    +SET @save_character_set_client=@@character_set_client;
    +SET @save_character_set_results=@@character_set_results;
    +SET @save_character_set_connection=@@character_set_connection;
    +SET @save_collation_connection=@@collation_connection;
    +CREATE DATABASE bug_40254 CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;
    +USE bug_40254;
    +SET sql_mode=DEFAULT;
    +SET optimizer_switch=DEFAULT;
    +SET default_storage_engine=InnoDB;
    +SET NAMES utf8mb4 COLLATE utf8mb4_general_ci;
    +CREATE TABLE t0 (c0 BIGINT);
    +INSERT INTO t0 VALUES (-5745763371133338700);
    +--sorted_result
    +SELECT ROUND(POWER(b.c0,2),0),COUNT(DISTINCT 1) FROM t0 d JOIN t0 b GROUP BY 1;
    +PREPARE regression_stmt FROM 'SELECT ROUND(POWER(b.c0,2),0),COUNT(DISTINCT 1) FROM t0 d JOIN t0 b GROUP BY 1;';
    +--sorted_result
    +EXECUTE regression_stmt;
    +--sorted_result
    +EXECUTE regression_stmt;
    +DEALLOCATE PREPARE regression_stmt;
    +--sorted_result
    +SELECT ROUND(POWER(c0,2),0),1 FROM t0;
    +--sorted_result
    +SELECT ROUND(POWER(b.c0,2),0),COUNT(DISTINCT 1) FROM t0 b JOIN t0 d GROUP BY 1;

    Do you really need all of these test cases? does any of them provide extra coverage? Please remove the ones that do not.

    gkodinov gkodinov requested changes on Sep 28, 2026 10:21 UTC
    Changes Requested Reviewer

    Thank you for your contribution! This is a preliminary review.

    The test case obviously needs simplification. Please attend to that.

    gkodinov gkodinov approved these changes on Sep 29, 2026 08:00 UTC
    Approved Reviewer

    LGTM. Thanks!

    Please stand by for the final review.

    Reviewers
    Assignees
    DerZc DerZc
    Labels
    risk: low confidence: 89.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 27.9% (LOW) · Higher = larger blast radius
    27.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.9% · Lower = rushed or shallow review
    89.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 2.8% · Safe to merge
    2.8%
    Repository