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

Help text for four JSON mutators promises NULL for any NULL argument, but a NULL value stores a JSON null

    XMLWordPrintable

Details

    Description

      This is a documentation defect, not a code defect. The behaviour is
      the MySQL-compatible one and is what the test suite pins; four help rows
      describe it wrongly.

      SELECT JSON_SET('{}', '$.a', NULL)          AS s,
             JSON_REPLACE('{"a":1}', '$.a', NULL) AS r,
             JSON_ARRAY_APPEND('[1]', '$', NULL)  AS a,
             JSON_ARRAY_INSERT('[1]', '$[0]', NULL) AS i;
      s             r             a          i
      {"a": null}   {"a": null}   [1, null]  [null, 1]
      

      All four help rows say "or NULL if any of the arguments are NULL". A
      NULL in the value position does not make the result NULL; it stores a
      JSON null, which is the useful and expected behaviour.

      How to repeat

      Read the help text beside the behaviour:

      SELECT JSON_SET('{"a":1}','$.b',NULL,'$.c',2) AS v;
      v
      {"a": 1, "b": null, "c": 2}
      

      A second false clause, on the JSON_SET row only

      The JSON_SET row promises NULL for one further case the server does not
      give it: "or the optional path fails to find an object". A path that
      finds nothing leaves the document alone and hands it back; NULL comes
      only from a NULL json_doc or a NULL path.

      SELECT JSON_SET('{"a":1}', '$.b.c', 2) AS v;
      v
      {"a": 1}
      SELECT JSON_SET('[1]', '$[0].x', 2) AS v;
      v
      [1]
      

      For contrast, the two arguments that do produce NULL:

      SELECT JSON_SET(NULL, '$.a', 1) AS v;
      v
      NULL
      SELECT JSON_SET('{"a":1}', NULL, 1) AS v;
      v
      NULL
      

      The wording that is right

      JSON_INSERT's row (topic 368) is accurate and is the model the other
      four should follow: *"returning the resulting document or NULL if either
      of the json_doc or path arguments are null"*.

      The four rows to correct in scripts/fill_help_tables.sql are topics 356
      (JSON_ARRAY_APPEND), 357 (JSON_ARRAY_INSERT), 386 (JSON_REPLACE) and 389
      (JSON_SET). Topic 389 needs the second clause removed as well as the
      first corrected.

      Affected versions

      The four help rows have carried this wording since the functions
      were documented.

      Verified on 10.11 only (10.11.19-MariaDB-debug, commit 1dab253482d).
      Other branches were not tested.

      Note

      Found while auditing the JSON functions against their documented
      contracts. The code behaviour is pinned and correct -
      func_json_invalid.result:360-362 records

      JSON_SET('{"a":1}', '$.a', NULL) -> {"a": null}.
      

      Attachments

        Issue Links

          Activity

            People

              danblack Daniel Black
              arcivanov Arcadiy Ivanov
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

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