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

Wrong result when using JSON_TABLE in correlated subquery

    XMLWordPrintable

Details

    Description

      Run this query:

      select a, b, (
        select json_arrayagg(i)
        from json_table(json_array(a, b), '$[*]' columns (i int path '$')) t
      ) 
      from (select 1 a, 2 b union all select 3 a, 4 b) as t
      order by a;
      

      MariaDB returns this:

      |a  |b  |json_arrayagg|
      |---|---|-------------|
      |1  |2  |[1, 2]       |
      |3  |4  |[1, 2]       |
      

      But this is expected

      |a  |b  |json_arrayagg|
      |---|---|-------------|
      |1  |2  |[1, 2]       |
      |3  |4  |[3, 4]       |
      

      It looks as though the correlated subquery is cached. A workaround is to disable the cache with a non-deterministic function, e.g.:

      select a, b, (
        select json_arrayagg(i)
        from json_table(json_array(a, b), '$[*]' columns (i int path '$')) t
        where rand() is not null
      ) 
      from (select 1 a, 2 b union all select 3 a, 4 b) as t
      order by a;
      

      Now, the result is correct

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              lukas.eder Lukas Eder
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

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