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

JSON_CONTAINS with a path answers 1 from a document that never formed a value, with no diagnostic

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • 10.11, 11.4, 11.8, 12.3, 13.0
    • None
    • JSON

    Description

      JSON_CONTAINS is documented to raise an error when the document is not
      valid. Given a path, it stops the moment the path is found and never
      reads the rest, so a document that breaks after the match is answered
      from as though it were whole.

      SELECT JSON_CONTAINS('{"a":1 "b":2}', '1', '$.a') AS v;
      v
      1

      No warning is raised.

      {"a":1 "b":2}

      has no comma between its members
      and is not a JSON document.

      How to repeat

      SELECT JSON_CONTAINS('{"a":1 "b":2}', '1', '$.a') AS v;   -- 1, silent
      SELECT JSON_CONTAINS('[1 2]', '1', '$[0]')        AS v;   -- 1, silent
      SELECT JSON_CONTAINS('{"a":1, "b"}', '1', '$.a')  AS v;   -- 1, silent

      Every other reader refuses the same bytes:

      SELECT JSON_EXTRACT('{"a":1 "b":2}', '$.a') AS v;
      v
      NULL
      Warnings:
      Warning  4038  Syntax error in JSON text in argument 1
                    to function 'json_extract' at position 8
       
      SELECT JSON_VALID('{"a":1 "b":2}') AS v;
      v
      0

      And so does the same function, on the same bytes, with the path left
      off:

      SELECT JSON_CONTAINS('{"a":1, "b":2 "c":3}', '{"a":1}') AS v;
      v
      NULL
      Warnings:
      Warning  4038  Syntax error in JSON text in argument 1
                    to function 'json_contains' at position 15

      Root cause

      The search stops at the first success and the remainder of the document
      is never scanned, so the parse error that lies past the match is never
      raised. Without a path the whole document is read, which is why the
      last case above is diagnosed.

      Contract, `scripts/fill_help_tables.sql:435`: *"Returns 1 if it does, 0
      if not and NULL if any of the arguments are null. An error occurs if
      the document or path is not valid, or contains the or * wildcards."*

      Affected versions

      JSON_CONTAINS was added in 10.2.3 and has stopped at the match
      throughout.

      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 suite pins the defect rather than the
      contract: func_json_invalid.result:255 records
      JSON_CONTAINS('{"a":1,', '1', '$.a') = 1. The missing-comma shapes
      are absent.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              arcivanov Arcadiy Ivanov
              Votes:
              0 Vote for this issue
              Watchers:
              1 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.