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

JSON_EXTRACT produces weird "null" value

    XMLWordPrintable

Details

    Description

      Try this query:

      select 
        a, b, 
        a = b, 
        a = 'null', a = '"null"', 
        b = 'null', b = '"null"'
      from (
        select
          json_extract(j, '$.a') as a,
          json_extract(j, '$.b') as b
        from (
          select '{"a":null,"b":"null"}' as j
        ) t
      ) t
      union all
      select 
        json_extract(j, '$.a'), json_extract(j, '$.b'), 
        json_extract(j, '$.a') = json_extract(j, '$.b'), 
        json_extract(j, '$.a') = 'null', json_extract(j, '$.a') = '"null"', 
        json_extract(j, '$.b') = 'null', json_extract(j, '$.b') = '"null"'
      from (
        select '{"a":null,"b":"null"}' as j
      ) t;
      

      It produces this result:

      |a   |b     |a = b|a = 'null'|a = '"null"'|b = 'null'|b = '"null"'|
      |----|------|-----|----------|------------|----------|------------|
      |null|"null"|0    |1         |0           |0         |1           |
      |null|"null"|0    |1         |0           |1         |0           |
      

      Both UNION ALL subqueries should produce the same results, and they do in MySQL, but don't in MariaDB. The problem is that JSON_EXTRACT seems to produce a '"null"' JSON string value that is equal to a 'null' SQL string in some cases, but not in others. The query shows that a workaround is to wrap the JSON_EXTRACT calls in a subquery.

      Another workaround is to concatenate the JSON_EXTRACT result to '', to remove the weird JSON behaviour:

      select 
        a, b, 
        a = b, 
        a = 'null', a = '"null"', 
        b = 'null', b = '"null"'
      from (
        select
          json_extract(j, '$.a') as a,
          json_extract(j, '$.b') as b
        from (
          select '{"a":null,"b":"null"}' as j
        ) t
      ) t
      union all
      select 
        json_extract(j, '$.a'), json_extract(j, '$.b'), 
        json_extract(j, '$.a') = json_extract(j, '$.b'), 
        json_extract(j, '$.a') = 'null', json_extract(j, '$.a') = '"null"', 
        concat('', json_extract(j, '$.b')) = 'null', concat('', json_extract(j, '$.b')) = '"null"'
      from (
        select '{"a":null,"b":"null"}' as j
      ) t;
      

      In the long run, I really wish MariaDB just introduced a formal JSON type, like PostgreSQL did from the beginning, or Oracle / MySQL did after the fact.

      Attachments

        Activity

          People

            rucha174 Rucha Deodhar
            lukas.eder Lukas Eder
            Votes:
            0 Vote for this issue
            Watchers:
            3 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.