← Back to Pull requests

MDEV-40245 Match equivalent NULLIF expressions during view analysis #5774

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

During view analysis, NULLIF can have two arguments while its fixed form has a third argument for the return value. Generic function comparison rejects these equivalent expressions because their argument counts differ. A view using DISTINCT and ORDER BY can consequently lose distinct rows.

Recognize the two- and three-argument NULLIF forms as equal when both original operands match and the extra argument repeats the first operand.

Regression coverage

Add a seven-line regression to main.view: create one table with two rows, create a view using DISTINCT, ORDER BY and division by NULLIF(1,0), query it once, and clean up. Both distinct values must survive. No extra SET, database creation, manual prepared statements or duplicate queries are needed.

Validation

On current 11.4 at f49e838d367f2154b3edb43effa99ff9eb0d8602:

  • The unmodified server fails main.view only on the new query, omitting (NULL, 4.0000).

  • The patched Release build succeeds.

  • Normal protocol: main.view, main.func_group, main.group_by, main.select and main.func_if pass.

  • Prepared-statement protocol: main.view passes.

  • View protocol: the same seven-line regression passes as a temporary standalone MTR test. Full main.view is skipped in this mode because its existing count_sessions.inc requires the view protocol to be disabled.

  • Expected rows are independently derived from NULLIF(1,0) = 1 and exact decimal division of the distinct input values.

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

    Diff: 42 lines (+42 / -0) across 4 files Reviewer: gkodinov Review duration: 24091m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.30) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.30 · 42 lines diff across 4 files
    0.30
    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.225
    Real PR Conversation Timeline
    5 items
    gkodinov gkodinov commented on code on Sep 28, 2026 10:26 UTC
    Code Review
    mysql-test/main/view.result:1
    @@ -7079,3 +7079,65 @@ ERROR HY000: Partition management on a not partitioned table is not possible
     drop procedure p;
     drop view v;
     # End of 10.11 test
    +#
    +# Start of 11.4 tests
    +#
    +# MDEV-40245 Wrong result with DISTINCT and ORDER BY in VIEW using NULLIF expression
    +#
    +SET @save_optimizer_switch=@@optimizer_switch;

    Simplify the test case please. I do not think neither CREATE DATABASE nor SET are needed.

    gkodinov gkodinov commented on code on Sep 28, 2026 10:27 UTC
    Code Review
    mysql-test/main/view.result:1
    @@ -7079,3 +7079,65 @@ ERROR HY000: Partition management on a not partitioned table is not possible
     drop procedure p;
     drop view v;
     # End of 10.11 test
    +#
    +# Start of 11.4 tests
    +#
    +# MDEV-40245 Wrong result with DISTINCT and ORDER BY in VIEW using NULLIF expression
    +#
    +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_40245 CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;
    +USE bug_40245;
    +SET sql_mode=DEFAULT;
    +SET optimizer_switch=DEFAULT;
    +SET default_storage_engine=InnoDB;
    +SET NAMES utf8mb4 COLLATE utf8mb4_general_ci;
    +CREATE TABLE t0 (c0 INT,c1 INT);
    +INSERT INTO t0 VALUES (2,NULL),(4,NULL);
    +CREATE VIEW v0 AS SELECT DISTINCT c1 AS vc_0,c0/NULLIF(1,0) AS vc_1 FROM t0 ORDER BY vc_0,vc_1;
    +SELECT * FROM v0;
    +vc_0	vc_1
    +NULL	2.0000
    +NULL	4.0000
    +PREPARE regression_stmt FROM 'SELECT * FROM v0;';
    +EXECUTE regression_stmt;
    +vc_0	vc_1
    +NULL	2.0000
    +NULL	4.0000
    +EXECUTE regression_stmt;
    +vc_0	vc_1
    +NULL	2.0000
    +NULL	4.0000
    +DEALLOCATE PREPARE regression_stmt;
    +SELECT DISTINCT c1 AS vc_0, c0/1 AS vc_1 FROM t0 ORDER BY vc_0, vc_1;

    How many of these contribute to extra coverage? Remove the extra. Note that the test suite itself runs with prepared statements and with view protocols!

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

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

    Please simplify the test case.

    gkodinov gkodinov approved these changes on Sep 29, 2026 07:57 UTC
    Approved Reviewer

    Thanks. LGTM. 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: 29.9% (LOW) · Higher = larger blast radius
    29.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: 3% · Safe to merge
    3%
    Repository