Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-39878

JSON_ARRAY_INTERSECT: nested call JSON_ARRAY_INTERSECT(JSON_ARRAY_INTERSECT(a,b), b) returns NULL instead of intersect result

    XMLWordPrintable

Details

    • Bug
    • Status: In Review (View Workflow)
    • Major
    • Resolution: Unresolved
    • 12.3, 12.3.1, 12.3.2
    • 12.3
    • JSON
    • ubuntu22.04

    Description

      Summary

      Single-level `JSON_ARRAY_INTERSECT(c1, c2)` returns correct JSON arrays.
      Nesting the function:

      SELECT JSON_ARRAY_INTERSECT(JSON_ARRAY_INTERSECT(c1, c2), c2) FROM t1;
      

      produces NULL for every row, although the inner intersect is non-empty.
      The outer intersect with `c2` should return the same result as the single-level call (`[2,3]` and `[4]` for sample data).

      Version

      MariaDB 12.3.1-MariaDB-asan
      

      Minimal reproducer

      DROP TABLE IF EXISTS t1;
      CREATE TABLE t1 (
      c1 longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
      DEFAULT NULL CHECK (json_valid(c1)),
      c2 longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
      DEFAULT NULL CHECK (json_valid(c2))
      ) ENGINE=InnoDB;
       
      INSERT INTO t1 VALUES
      ('[1,2,3]', '[2,3,4]'),
      ('[2,3,4]', '[4,5,6]');
       
      -- single-level
      SELECT JSON_ARRAY_INTERSECT(c1, c2) AS r FROM t1;
       
      -- nested (produces bug)
      SELECT JSON_ARRAY_INTERSECT(JSON_ARRAY_INTERSECT(c1, c2), c2) AS r FROM t1;
      

      Actual Result

      -- single-level
      r
      [2, 3]
      [4]
       
      -- nested
      r
      NULL
      NULL
      

      Expected Result

      Nested output should match single-level intersect:

      [2, 3]
      [4]
      

      Type / Priority / Component

      Field Value
      --------- ----------------------------------------------------------
      Type Bug
      Priority Major (wrong result)
      Component Server / SQL — JSON functions
      Labels json, json_array_intersect, wrong-result
      Link relates to MDEV-31411 (same PSI area, nested-call variant)

      Notes

      • No SQL error is produced; the wrong result is silent NULLs.
      • Only the nested expression triggers the bug; single-level usage works as expected.
      • Not related to filtering with `WHERE JSON_ARRAY_INTERSECT(...)`.

      Discovery

      • Tool: crdb_mutate n200 MariaDB batch (`--oracle --oracle-psi`)
      • PSI: MDEV-31411 (`JSON_ARRAY_INTERSECT(c1,c2)`)
      • Nested candidate confirmed manually.

      Environment

      • MariaDB tested: 12.3.1-MariaDB-asan
      • Reproduced: 2026-06-06

      Attachments

        Activity

          People

            holyfoot Alexey Botchkov
            mu mu
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0d
                0d
                Logged:
                Time Spent - 1d
                1d

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.