← Back to Pull requests

MDEV-40779 Evaluate HAVING on completed groups before DISTINCT #5776

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

HAVING can read stale aggregate and field values when grouped rows are
copied into a second temporary table for DISTINCT. Rechecking the condition
during duplicate removal can discard qualifying rows.

Bind HAVING to the stored values of the completed groups. Evaluate it while
writing the DISTINCT table, and avoid attaching or evaluating it again
against that table.

Bug report: https://jira.mariadb.org/browse/MDEV-40779

Regression coverage

Reduce the addition to main.func_group from 67 lines to five: an issue
header, table creation, three input rows, one query and cleanup. Two groups
satisfy HAVING and produce the same output; a third group fails HAVING. The
expected DISTINCT result is one row, (2, 2).

Remove the extra InnoDB include, session settings, separate database,
sorting directives, equivalent query variants and manual prepared statements.
The original source fix and existing test contents are preserved.

Validation

On current 11.4 at f49e838d367f2154b3edb43effa99ff9eb0d8602:

  • The unmodified server builds, then fails main.func_group only because the

  • new query returns no rows instead of (2, 2).
  • The patched Release build succeeds.

  • Normal MTR passes: main.func_group, main.group_by, main.having,

  • main.select, main.distinct and main.view.
  • main.func_group passes with the view protocol.

  • The exact five-line regression passes as a temporary standalone MTR test

  • with the prepared-statement protocol, including its automatic repeated
    SELECT execution. The existing full main.func_group test disables that
    protocol at its start.
  • The full server regression suite was not run.
  • Diff: 50 lines (+47 / -3) across 6 files Reviewer: gkodinov Review duration: 24320m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.34) × (1 − Review Confidence (0.92)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.34 · 50 lines diff across 6 files
    0.34
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.96 · 1 substantive comment(s)
    0.96
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    91%
    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.235
    Real PR Conversation Timeline
    5 items
    gkodinov gkodinov commented on code on Sep 28, 2026 10:13 UTC
    Code Review
    mysql-test/main/func_group.test:1
    @@ -1815,3 +1816,69 @@ enable_view_protocol;
     disable_metadata;
     
     --echo # End of 10.11 tests
    +
    +--echo #
    +--echo # Start of 11.4 tests
    +--echo #
    +--echo # MDEV-40779 DISTINCT + char_length(SPACE(AVG(…))) + HAVING returns empty for one insertion order of the same rows
    +--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_40779 CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;

    Is all of the above relevant to the test case? The original bug report had no charset or SQL modes etc.
    Also, do you really need to create a database?

    I'd suggest sticking to the minimal test case.

    gkodinov gkodinov commented on Sep 28, 2026 10:15 UTC
    Reviewer

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

    Very close, but I'd like to see a clearer test case before I approve it.

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

    Requested changes.

    gkodinov gkodinov approved these changes on Sep 29, 2026 10:04 UTC
    Approved Reviewer

    LGTM. Thanks. Please stand by for the final review.

    Reviewers
    Assignees
    DerZc DerZc
    Labels
    risk: low confidence: 91.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: 33.8% (LOW) · Higher = larger blast radius
    33.8%
    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: 91.9% · Lower = rushed or shallow review
    91.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.7% · Safe to merge
    2.7%
    Repository