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

JSON_CONTAINS and JSON_OVERLAPS report unequal strings when arguments use different character sets

    XMLWordPrintable

Details

    Description

      JSON_CONTAINS and JSON_OVERLAPS return 0 when comparing valid JSON strings that represent the same Unicode character but are supplied as independently encoded latin1 and utf8mb4 SQL arguments. SQL comparison and JSON_EQUALS recognize the decoded values as equal, but both other functions report inequality in either argument direction. This was reproduced with MariaDB 12.3.3-MariaDB-ubu2404 on Linux/amd64.

      Cross-character-set comparison

      Run the following SQL in one session:

      SET @latin1_doc =
        CONVERT(0x22E922 USING latin1);
      SET @utf8_doc =
        CONVERT(0x22C3A922 USING utf8mb4);
       
      SELECT JSON_VALID(@latin1_doc) AS latin1_valid,
             JSON_VALID(@utf8_doc) AS utf8_valid,
             CONVERT(JSON_UNQUOTE(@latin1_doc) USING utf8mb4) =
               JSON_UNQUOTE(@utf8_doc) AS decoded_equal,
             JSON_EQUALS(@latin1_doc,
                         @utf8_doc) AS json_equals,
             JSON_CONTAINS(@latin1_doc,
                           @utf8_doc) AS contains_latin1_utf8,
             JSON_CONTAINS(@utf8_doc,
                           @latin1_doc) AS contains_utf8_latin1,
             JSON_OVERLAPS(@latin1_doc,
                           @utf8_doc) AS overlaps_latin1_utf8,
             JSON_OVERLAPS(@utf8_doc,
                           @latin1_doc) AS overlaps_utf8_latin1;
       
      SET @latin1_doc = NULL;
      SET @utf8_doc = NULL;

      The query returns:

      latin1_valid	utf8_valid	decoded_equal	json_equals	contains_latin1_utf8	contains_utf8_latin1	overlaps_latin1_utf8	overlaps_utf8_latin1
      1	1	1	1	0	0	0	0

      Both inputs are valid, their decoded SQL values compare equal, and JSON_EQUALS returns 1. All result columns should return 1 because the two JSON strings represent U+00E9 and differ only in their SQL argument encodings. Semantic JSON comparison should compare the decoded characters independently of those encodings and produce the same result in either argument direction.

      This occurs when JSON_CONTAINS or JSON_OVERLAPS compares equivalent JSON string values supplied through SQL arguments with different character sets.

      Attachments

        Issue Links

          Activity

            People

              holyfoot Alexey Botchkov
              imchifan 刘启帆
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - 0d
                  0d
                  Remaining:
                  Remaining Estimate - 1d
                  1d
                  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.