← Back to Pull requests

MDEV-39972 Preserve negative indexes when reusing constant JSON paths #5770

Open
DerZc opened this pull request with 25 lines diff across 3 files
View on GitHub
DerZc DerZc commented on Sep 28, 2026 06:09 UTC
Author

JSON_EXTRACT() and JSON_SEARCH() detect negative indexes only when a path is first parsed, so later rows using a cached constant path do not recompute array lengths. JSON_SEARCH() also clears flags from cached paths.

Read the negative-index flag for every path, including cached constants, and reset JSON_SEARCH() path flags only when parsing.

Regression coverage

Add one query to main.func_json that calls both functions on two arrays of different lengths. It checks the last value and its path on both rows. The seven-line test reuses the existing test setup and MTR protocol support, with no extra SET, database creation, manual prepared statements or duplicate queries.

Validation

On current 11.4 at f49e838d367f2154b3edb43effa99ff9eb0d8602:

  • The unmodified server fails main.func_json only on the new second-row results: 0 and NULL instead of "x" and "$[1]".

  • The patched Release build succeeds.

  • Normal protocol: main.func_json and main.select pass.

  • Prepared-statement protocol: main.func_json passes.

  • View protocol: main.func_json passes.

  • Expected values and indexes are independently derived from the two JSON arrays.

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

    Diff: 25 lines (+22 / -3) across 3 files Reviewer: gkodinov Review duration: 23593m
    All Vouch review confidence checks passed
    Residual risk 0.02 = Change Risk (0.25) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.25 · 25 lines diff across 3 files
    0.25
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.93 · 4 substantive comment(s)
    0.93
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    90%
    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.195
    Real PR Conversation Timeline
    7 items
    gkodinov gkodinov commented on code on Sep 28, 2026 10:55 UTC
    Code Review
    mysql-test/main/func_json.test:1
    @@ -4451,3 +4451,95 @@ SELECT JSON_SCHEMA_VALID(@schema, @value);
     SELECT JSON_SCHEMA_VALID( '{"enum": ["x"]}', CONVERT(CONCAT('"', REPEAT('e', 100000), '"') USING ucs2)) AS r;
     
     --echo # End of 11.4 tests
    +
    +--echo #
    +--echo # Start of 11.4 tests
    +--echo #
    +--echo # MDEV-39972 Incorrect result of \$[last] of JSON
    +--echo #
    +SET @save_optimizer_switch=@@optimizer_switch;

    please avoid extra SET and CREATE DATABASE unless they contribute to coverage.

    gkodinov gkodinov commented on code on Sep 28, 2026 10:56 UTC
    Code Review
    mysql-test/main/func_json.test:1
    @@ -4451,3 +4451,95 @@ SELECT JSON_SCHEMA_VALID(@schema, @value);
     SELECT JSON_SCHEMA_VALID( '{"enum": ["x"]}', CONVERT(CONCAT('"', REPEAT('e', 100000), '"') USING ucs2)) AS r;
     
     --echo # End of 11.4 tests
    +
    +--echo #
    +--echo # Start of 11.4 tests
    +--echo #
    +--echo # MDEV-39972 Incorrect result of \$[last] of JSON
    +--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_39972 CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;
    +USE bug_39972;
    +SET sql_mode=DEFAULT;
    +SET optimizer_switch=DEFAULT;
    +SET default_storage_engine=InnoDB;
    +SET NAMES utf8mb4 COLLATE utf8mb4_general_ci;
    +CREATE TABLE t (id INT PRIMARY KEY,j JSON);
    +INSERT INTO t VALUES (1,'["x","target"]'),(2,'["x","y","target"]');
    +--sorted_result
    +SELECT id,JSON_EXTRACT(j,'$[last]') AS extracted_last,JSON_SEARCH(j,'one','target',NULL,'$[last]') AS searched_last FROM t ORDER BY id;
    +PREPARE regression_stmt FROM 'SELECT id,JSON_EXTRACT(j,''$[last]'') AS extracted_last,JSON_SEARCH(j,''one'',''target'',NULL,''$[last]'') AS searched_last FROM t ORDER BY id;';

    avoid anything that doesn't contribute to coverage. Note that mtr already runs tests with view and prepared statement protocols.

    gkodinov gkodinov commented on code on Sep 28, 2026 10:56 UTC
    Code Review
    mysql-test/main/func_json.test:1
    @@ -4451,3 +4451,95 @@ SELECT JSON_SCHEMA_VALID(@schema, @value);
     SELECT JSON_SCHEMA_VALID( '{"enum": ["x"]}', CONVERT(CONCAT('"', REPEAT('e', 100000), '"') USING ucs2)) AS r;
     
     --echo # End of 11.4 tests
    +
    +--echo #
    +--echo # Start of 11.4 tests
    +--echo #
    +--echo # MDEV-39972 Incorrect result of \$[last] of JSON
    +--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_39972 CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;
    +USE bug_39972;
    +SET sql_mode=DEFAULT;
    +SET optimizer_switch=DEFAULT;
    +SET default_storage_engine=InnoDB;
    +SET NAMES utf8mb4 COLLATE utf8mb4_general_ci;
    +CREATE TABLE t (id INT PRIMARY KEY,j JSON);
    +INSERT INTO t VALUES (1,'["x","target"]'),(2,'["x","y","target"]');
    +--sorted_result
    +SELECT id,JSON_EXTRACT(j,'$[last]') AS extracted_last,JSON_SEARCH(j,'one','target',NULL,'$[last]') AS searched_last FROM t ORDER BY id;
    +PREPARE regression_stmt FROM 'SELECT id,JSON_EXTRACT(j,''$[last]'') AS extracted_last,JSON_SEARCH(j,''one'',''target'',NULL,''$[last]'') AS searched_last FROM t ORDER BY id;';
    +--sorted_result
    +EXECUTE regression_stmt;
    +--sorted_result
    +EXECUTE regression_stmt;
    +DEALLOCATE PREPARE regression_stmt;
    +--sorted_result
    +SELECT id,JSON_EXTRACT(j,CONCAT('$[',JSON_LENGTH(j)-1,']')) AS extracted_last,JSON_SEARCH(j,'one','target',NULL,CONCAT('$[',JSON_LENGTH(j)-1,']')) AS searched_last FROM t ORDER BY id;
    +--sorted_result
    +SELECT id, JSON_EXTRACT(j, '$[last]') AS extracted_last, JSON_SEARCH(j, 'one', 'target', NULL, '$[last]') AS searched_last FROM t FORCE INDEX (PRIMARY) ORDER BY id;
    +--sorted_result
    +SELECT id, JSON_EXTRACT(j, '$[last]') AS extracted_last, JSON_SEARCH(j, 'one', 'target', NULL, '$[last]') AS searched_last FROM (SELECT id, j FROM t) AS base ORDER BY id;
    +--sorted_result
    +WITH base AS (SELECT id, j FROM t) SELECT id, JSON_EXTRACT(j, '$[last]') AS extracted_last, JSON_SEARCH(j, 'one', 'target', NULL, '$[last]') AS searched_last FROM base ORDER BY id;
    +--sorted_result
    +SELECT id, JSON_EXTRACT(j,'$[last]') AS extracted_last, JSON_SEARCH(j,'one','target',NULL,'$[last]') AS searched_last FROM t FORCE INDEX (PRIMARY) ORDER BY id;

    avoid redundant test case that do not contribute to test coverage.

    gkodinov gkodinov commented on code on Sep 28, 2026 10:58 UTC
    Code Review
    sql/item_jsonfunc.cc:1
    @@ -1195,12 +1195,13 @@ String *Item_func_json_extract::read_json(String *str,
              goto return_null;
            }
            c_path->parsed= c_path->constant;
    -       has_negative_path|= c_path->p.types_used & JSON_PATH_NEGATIVE_INDEX;

    You're changing JSON_EXTRACT but there's not a single word about this in the commit message.
    Commit messages usually follow this pattern:

    1. what was wrong
    2. how it was fixed
    3. how is the fix tested.

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

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

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

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

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