← Back to Pull requests

MDEV-39193 Preserve matches when string range bounds are truncated #5773

Open
DerZc opened this pull request with 39 lines diff across 5 files
View on GitHub
DerZc DerZc commented on Sep 28, 2026 06:19 UTC
Author

Storing a range bound in a short string field can silently truncate it.
Under utf8mb4_0900_ai_ci, ß compares equal to ss, but storing ss
in VARCHAR(1) leaves s. Using that shorter upper key can exclude
matching rows.

Reject an upper range bound when the stored value compares below the
original value. The focused regression uses one indexed InnoDB row and
two queries: BETWEEN covers the inclusive upper bound, and < covers
the exclusive upper bound. It needs no database creation or session
settings. Existing unordered BETWEEN outputs in the UTF-8 tests are
sorted because the safe fallback can change scan order.

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

Validation

On 11.4 at 322658cd442333a13b46521d1acca67d1e8366cb:

  • The unmodified server fails main.ctype_utf8mb4 only at the two new

  • queries: each returns 0 instead of the expected 1. main.ctype_utf8
    passes with the sorted existing output.
  • A fresh Release build succeeds.

  • Normal protocol: main.ctype_utf8mb4, main.ctype_utf8 and

  • main.select pass.
  • Prepared-statement protocol: main.ctype_utf8mb4 and main.ctype_utf8

  • pass.
  • The full server regression suite was not run.
  • Diff: 39 lines (+35 / -4) across 5 files Reviewer: gkodinov Review duration: 29740m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.31) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.31 · 39 lines diff across 5 files
    0.31
    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.221
    Real PR Conversation Timeline
    5 items
    gkodinov gkodinov commented on code on Sep 28, 2026 10:31 UTC
    Code Review
    mysql-test/main/ctype_utf8mb4.test:1
    @@ -2246,3 +2247,62 @@ DROP TABLE t1;
     --echo #
     --echo # End of 10.11 tests
     --echo #
    +
    +--echo #
    +--echo # Start of 11.4 tests
    +--echo #
    +--echo # MDEV-39193 Wrong results: index range vs full scan on utf8mb4_0900_ai_ci VARCHAR(1) when bound string is longer than column (ß / 'ss')
    +--echo #
    +SET @save_optimizer_switch=@@optimizer_switch;

    simplify the test case please: no CREATE DATABASE, no SET etc.

    gkodinov gkodinov commented on code on Sep 28, 2026 10:32 UTC
    Code Review
    mysql-test/main/ctype_utf8mb4.test:1
    @@ -2246,3 +2247,62 @@ DROP TABLE t1;
     --echo #
     --echo # End of 10.11 tests
     --echo #
    +
    +--echo #
    +--echo # Start of 11.4 tests
    +--echo #
    +--echo # MDEV-39193 Wrong results: index range vs full scan on utf8mb4_0900_ai_ci VARCHAR(1) when bound string is longer than column (ß / 'ss')
    +--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_39193 CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;
    +USE bug_39193;
    +SET sql_mode=DEFAULT;
    +SET optimizer_switch=DEFAULT;
    +SET default_storage_engine=InnoDB;
    +SET NAMES utf8mb4 COLLATE utf8mb4_general_ci;
    +SET NAMES utf8mb4;
    +CREATE TABLE t (x VARCHAR(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,KEY(x));
    +INSERT INTO t VALUES ('ß');
    +--sorted_result
    +SELECT COUNT(*) FROM t WHERE x BETWEEN 'r' AND 'ss';
    +PREPARE regression_stmt FROM 'SELECT COUNT(*) FROM t WHERE x BETWEEN ''r'' AND ''ss'';';
    +--sorted_result
    +EXECUTE regression_stmt;
    +--sorted_result
    +EXECUTE regression_stmt;
    +DEALLOCATE PREPARE regression_stmt;
    +--sorted_result
    +SELECT COUNT(*) FROM t WHERE NOT (NOT (x BETWEEN 'r' AND 'ss'));
    +--sorted_result
    +SELECT COUNT(*) FROM t FORCE INDEX (x) WHERE x BETWEEN 'r' AND 'ss';
    +--sorted_result
    +SELECT COUNT(CASE WHEN x BETWEEN 'r' AND 'ss' THEN 1 ELSE NULL END) FROM t;

    find out what provides extra coverage. trim the ones that don't.

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

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

    gkodinov gkodinov approved these changes on Sep 29, 2026 08:06 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: 30.9% (LOW) · Higher = larger blast radius
    30.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.1% · Safe to merge
    3.1%
    Repository