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

Incorrect result of $[last] of JSON

    XMLWordPrintable

Details

    • Q4/2026 Server Development

    Description

      Hi,

      I found that $[last] might generate an incorrect index for JSON data. This is an example:

      CREATE TABLE t(
        id INT PRIMARY KEY,
        j JSON
      );
       
      INSERT INTO t VALUES
        (1, '["x","target"]'),
        (2, '["x","y","target"]');
       
      SELECT id,
             JSON_EXTRACT(j, '$[last]') AS extracted_last,
             JSON_SEARCH(j, 'one', 'target', NULL, '$[last]') AS searched_last
      FROM t
      ORDER BY id;
      -- 1	"target"	"$[1]"
      -- 2	"y"	NULL
      

      For the first row, the $[last] index is correct, but for the second row, the $[last] index is 1, which is incorrect.

      Attachments

        Issue Links

          Activity

            People

              rucha174 Rucha Deodhar
              ChiZhang Chi Zhang
              Rucha Deodhar Rucha Deodhar
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

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

                  Git Integration

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